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);
|
|
|
47 |
|
|
|
48 |
if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
|
|
|
49 |
$mform->addElement('checkbox',
|
|
|
50 |
'ispublic', '',
|
|
|
51 |
get_string('public', 'feedback'));
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Returns context where this form is used
|
|
|
57 |
*
|
|
|
58 |
* @return context
|
|
|
59 |
*/
|
|
|
60 |
protected function get_context_for_dynamic_submission(): context {
|
|
|
61 |
$id = $this->optional_param('id', null, PARAM_INT);
|
|
|
62 |
list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
|
|
|
63 |
return context_module::instance($cm->id);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Checks if current user has access to this form, otherwise throws exception
|
|
|
68 |
*
|
|
|
69 |
* @throws \moodle_exception User does not have capability to access the form
|
|
|
70 |
*/
|
|
|
71 |
protected function check_access_for_dynamic_submission(): void {
|
|
|
72 |
$context = $this->get_context_for_dynamic_submission();
|
|
|
73 |
if (!has_capability('mod/feedback:edititems', $context) ||
|
|
|
74 |
!(has_capability('mod/feedback:createprivatetemplate', $context) ||
|
|
|
75 |
has_capability('mod/feedback:createpublictemplate', $context))) {
|
|
|
76 |
throw new \moodle_exception('nocapabilitytousethisservice');
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Process the form submission, used if form was submitted via AJAX
|
|
|
82 |
*
|
|
|
83 |
* @return array Returns whether a new template was created.
|
|
|
84 |
*/
|
|
|
85 |
public function process_dynamic_submission(): array {
|
|
|
86 |
global $PAGE;
|
|
|
87 |
$formdata = $this->get_data();
|
|
|
88 |
$ispublic = !empty($formdata->ispublic) ? 1 : 0;
|
|
|
89 |
$result = feedback_save_as_template($PAGE->activityrecord, $formdata->templatename, $ispublic);
|
|
|
90 |
return [
|
|
|
91 |
'result' => $result,
|
|
|
92 |
];
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Load in existing data as form defaults
|
|
|
97 |
*/
|
|
|
98 |
public function set_data_for_dynamic_submission(): void {
|
|
|
99 |
$this->set_data((object)[
|
|
|
100 |
'id' => $this->optional_param('id', null, PARAM_INT),
|
|
|
101 |
]);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
|
|
|
106 |
*
|
|
|
107 |
* @return moodle_url
|
|
|
108 |
*/
|
|
|
109 |
protected function get_page_url_for_dynamic_submission(): moodle_url {
|
|
|
110 |
$params = [
|
|
|
111 |
'id' => $this->optional_param('id', null, PARAM_INT),
|
|
|
112 |
];
|
|
|
113 |
return new moodle_url('/mod/feedback/edit.php', $params);
|
|
|
114 |
}
|
|
|
115 |
}
|