Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace mod_feedback\form;
18
 
19
use core_form\dynamic_form;
20
use moodle_url;
21
use context;
22
use context_module;
23
use context_system;
24
 
25
/**
26
 * Prints the create new template form
27
 *
28
 * @copyright 2021 Peter Dias
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30
 * @package mod_feedback
31
 */
32
class create_template_form extends dynamic_form {
33
    /**
34
     * Define the form
35
     */
36
    public function definition() {
37
        $mform =& $this->_form;
38
 
39
        $mform->addElement('hidden', 'id');
40
        $mform->setType('id', PARAM_INT);
41
 
42
        $mform->addElement('text',
43
            'templatename',
44
            get_string('name', 'feedback'),
45
            ['maxlength' => '200', 'size' => '50']);
46
        $mform->setType('templatename', PARAM_TEXT);
1441 ariadna 47
        $mform->addRule('templatename', null, 'required', null, 'client');
1 efrain 48
 
49
        if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
50
            $mform->addElement('checkbox',
51
                'ispublic', '',
1441 ariadna 52
                get_string('availableforallcourses', 'feedback'));
1 efrain 53
        }
54
    }
55
 
56
    /**
57
     * Returns context where this form is used
58
     *
59
     * @return context
60
     */
61
    protected function get_context_for_dynamic_submission(): context {
62
        $id = $this->optional_param('id', null, PARAM_INT);
63
        list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
64
        return context_module::instance($cm->id);
65
    }
66
 
67
    /**
68
     * Checks if current user has access to this form, otherwise throws exception
69
     *
70
     * @throws \moodle_exception User does not have capability to access the form
71
     */
72
    protected function check_access_for_dynamic_submission(): void {
73
        $context = $this->get_context_for_dynamic_submission();
74
        if (!has_capability('mod/feedback:edititems', $context) ||
75
            !(has_capability('mod/feedback:createprivatetemplate', $context) ||
76
            has_capability('mod/feedback:createpublictemplate', $context))) {
77
            throw new \moodle_exception('nocapabilitytousethisservice');
78
        }
79
    }
80
 
81
    /**
82
     * Process the form submission, used if form was submitted via AJAX
83
     *
84
     * @return array Returns whether a new template was created.
85
     */
86
    public function process_dynamic_submission(): array {
87
        global $PAGE;
88
        $formdata = $this->get_data();
89
        $ispublic = !empty($formdata->ispublic) ? 1 : 0;
90
        $result = feedback_save_as_template($PAGE->activityrecord, $formdata->templatename, $ispublic);
91
        return [
92
            'result' => $result,
93
        ];
94
    }
95
 
96
    /**
97
     * Load in existing data as form defaults
98
     */
99
    public function set_data_for_dynamic_submission(): void {
100
        $this->set_data((object)[
101
            'id' => $this->optional_param('id', null, PARAM_INT),
102
        ]);
103
    }
104
 
105
    /**
106
     * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
107
     *
108
     * @return moodle_url
109
     */
110
    protected function get_page_url_for_dynamic_submission(): moodle_url {
111
        $params = [
112
            'id' => $this->optional_param('id', null, PARAM_INT),
113
        ];
114
        return new moodle_url('/mod/feedback/edit.php', $params);
115
    }
116
}