1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of the customcert module for 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 |
/**
|
|
|
18 |
* This file contains the instance add/edit form.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use mod_customcert\certificate;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
|
|
28 |
|
|
|
29 |
require_once($CFG->dirroot.'/course/moodleform_mod.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Instance add/edit form.
|
|
|
33 |
*
|
|
|
34 |
* @package mod_customcert
|
|
|
35 |
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class mod_customcert_mod_form extends moodleform_mod {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Form definition.
|
|
|
42 |
*/
|
|
|
43 |
public function definition() {
|
|
|
44 |
global $CFG;
|
|
|
45 |
|
|
|
46 |
$mform =& $this->_form;
|
|
|
47 |
|
|
|
48 |
$mform->addElement('header', 'general', get_string('general', 'form'));
|
|
|
49 |
|
|
|
50 |
$mform->addElement('text', 'name', get_string('name', 'customcert'), ['size' => '64']);
|
|
|
51 |
if (!empty($CFG->formatstringstriptags)) {
|
|
|
52 |
$mform->setType('name', PARAM_TEXT);
|
|
|
53 |
} else {
|
|
|
54 |
$mform->setType('name', PARAM_CLEANHTML);
|
|
|
55 |
}
|
|
|
56 |
$mform->addRule('name', null, 'required', null, 'client');
|
|
|
57 |
|
|
|
58 |
$this->standard_intro_elements(get_string('description', 'customcert'));
|
|
|
59 |
|
|
|
60 |
$mform->addElement('header', 'options', get_string('options', 'customcert'));
|
|
|
61 |
|
|
|
62 |
$deliveryoptions = [
|
|
|
63 |
certificate::DELIVERY_OPTION_INLINE => get_string('deliveryoptioninline', 'customcert'),
|
|
|
64 |
certificate::DELIVERY_OPTION_DOWNLOAD => get_string('deliveryoptiondownload', 'customcert'),
|
|
|
65 |
];
|
|
|
66 |
$mform->addElement('select', 'deliveryoption', get_string('deliveryoptions', 'customcert'), $deliveryoptions);
|
|
|
67 |
$mform->setDefault('deliveryoption', certificate::DELIVERY_OPTION_INLINE);
|
|
|
68 |
|
|
|
69 |
if (has_capability('mod/customcert:manageemailstudents', $this->get_context())) {
|
|
|
70 |
$mform->addElement('selectyesno', 'emailstudents', get_string('emailstudents', 'customcert'));
|
|
|
71 |
$mform->setDefault('emailstudents', get_config('customcert', 'emailstudents'));
|
|
|
72 |
$mform->addHelpButton('emailstudents', 'emailstudents', 'customcert');
|
|
|
73 |
$mform->setType('emailstudents', PARAM_INT);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if (has_capability('mod/customcert:manageemailteachers', $this->get_context())) {
|
|
|
77 |
$mform->addElement('selectyesno', 'emailteachers', get_string('emailteachers', 'customcert'));
|
|
|
78 |
$mform->setDefault('emailteachers', get_config('customcert', 'emailteachers'));
|
|
|
79 |
$mform->addHelpButton('emailteachers', 'emailteachers', 'customcert');
|
|
|
80 |
$mform->setType('emailteachers', PARAM_INT);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
if (has_capability('mod/customcert:manageemailothers', $this->get_context())) {
|
|
|
84 |
$mform->addElement('text', 'emailothers', get_string('emailothers', 'customcert'), ['size' => '40']);
|
|
|
85 |
$mform->addHelpButton('emailothers', 'emailothers', 'customcert');
|
|
|
86 |
$mform->setDefault('emailothers', get_config('customcert', 'emailothers'));
|
|
|
87 |
$mform->setType('emailothers', PARAM_TEXT);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if (has_capability('mod/customcert:manageverifyany', $this->get_context())) {
|
|
|
91 |
$mform->addElement('selectyesno', 'verifyany', get_string('verifycertificateanyone', 'customcert'));
|
|
|
92 |
$mform->addHelpButton('verifyany', 'verifycertificateanyone', 'customcert');
|
|
|
93 |
$mform->setDefault('verifyany', get_config('customcert', 'verifyany'));
|
|
|
94 |
$mform->setType('verifyany', PARAM_INT);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (has_capability('mod/customcert:managerequiredtime', $this->get_context())) {
|
|
|
98 |
$mform->addElement('text', 'requiredtime', get_string('coursetimereq', 'customcert'), ['size' => '3']);
|
|
|
99 |
$mform->addHelpButton('requiredtime', 'coursetimereq', 'customcert');
|
|
|
100 |
$mform->setDefault('requiredtime', get_config('customcert', 'requiredtime'));
|
|
|
101 |
$mform->setType('requiredtime', PARAM_INT);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
if (has_capability('mod/customcert:manageprotection', $this->get_context())) {
|
|
|
105 |
$mform->addElement('checkbox', 'protection_print', get_string('setprotection', 'customcert'),
|
|
|
106 |
get_string('print', 'customcert'));
|
|
|
107 |
$mform->addElement('checkbox', 'protection_modify', '', get_string('modify', 'customcert'));
|
|
|
108 |
$mform->addElement('checkbox', 'protection_copy', '', get_string('copy', 'customcert'));
|
|
|
109 |
$mform->addHelpButton('protection_print', 'setprotection', 'customcert');
|
|
|
110 |
$mform->setType('protection_print', PARAM_BOOL);
|
|
|
111 |
$mform->setType('protection_modify', PARAM_BOOL);
|
|
|
112 |
$mform->setType('protection_copy', PARAM_BOOL);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// Create an element for language selector.
|
|
|
116 |
if (has_capability('mod/customcert:managelanguages', $this->get_context())) {
|
|
|
117 |
$languages = get_string_manager()->get_list_of_translations();
|
|
|
118 |
$languages = ['' => get_string('userlanguage', 'customcert')] + $languages;
|
|
|
119 |
$mform->addElement('select', 'language', get_string('languageoptions', 'customcert'), $languages);
|
|
|
120 |
$mform->addHelpButton('language', 'userlanguage', 'customcert');
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
$this->standard_coursemodule_elements();
|
|
|
124 |
|
|
|
125 |
$this->add_action_buttons();
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Any data processing needed before the form is displayed.
|
|
|
130 |
*
|
|
|
131 |
* @param array $defaultvalues
|
|
|
132 |
*/
|
|
|
133 |
public function data_preprocessing(&$defaultvalues) {
|
|
|
134 |
// Set the values in the form to what has been set in database if updating
|
|
|
135 |
// or set default configured values if creating.
|
|
|
136 |
if (!empty($defaultvalues['update'])) {
|
|
|
137 |
if (!empty($defaultvalues['protection'])) {
|
|
|
138 |
$protection = $this->build_protection_data($defaultvalues['protection']);
|
|
|
139 |
|
|
|
140 |
$defaultvalues['protection_print'] = $protection->protection_print;
|
|
|
141 |
$defaultvalues['protection_modify'] = $protection->protection_modify;
|
|
|
142 |
$defaultvalues['protection_copy'] = $protection->protection_copy;
|
|
|
143 |
}
|
|
|
144 |
} else {
|
|
|
145 |
$defaultvalues['protection_print'] = get_config('customcert', 'protection_print');
|
|
|
146 |
$defaultvalues['protection_modify'] = get_config('customcert', 'protection_modify');
|
|
|
147 |
$defaultvalues['protection_copy'] = get_config('customcert', 'protection_copy');
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Post process form data.
|
|
|
153 |
*
|
|
|
154 |
* @param \stdClass $data
|
|
|
155 |
*
|
|
|
156 |
* @throws \dml_exception
|
|
|
157 |
*/
|
|
|
158 |
public function data_postprocessing($data) {
|
|
|
159 |
global $DB;
|
|
|
160 |
|
|
|
161 |
parent::data_postprocessing($data);
|
|
|
162 |
|
|
|
163 |
// If creating a new activity.
|
|
|
164 |
if (!empty($data->add)) {
|
|
|
165 |
foreach ($this->get_options_elements_with_required_caps() as $name => $capability) {
|
|
|
166 |
if (!isset($data->$name) && !has_capability($capability, $this->get_context())) {
|
|
|
167 |
$data->$name = get_config('customcert', $name);
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
} else {
|
|
|
171 |
// If updating, but a user can't manage protection, then get data from database.
|
|
|
172 |
if (!has_capability('mod/customcert:manageprotection', $this->get_context())) {
|
|
|
173 |
$customcert = $DB->get_record('customcert', ['id' => $data->instance]);
|
|
|
174 |
|
|
|
175 |
$protection = $this->build_protection_data($customcert->protection);
|
|
|
176 |
$data->protection_print = $protection->protection_print;
|
|
|
177 |
$data->protection_modify = $protection->protection_modify;
|
|
|
178 |
$data->protection_copy = $protection->protection_copy;
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Some basic validation.
|
|
|
185 |
*
|
|
|
186 |
* @param array $data
|
|
|
187 |
* @param array $files
|
|
|
188 |
* @return array the errors that were found
|
|
|
189 |
*/
|
|
|
190 |
public function validation($data, $files) {
|
|
|
191 |
$errors = parent::validation($data, $files);
|
|
|
192 |
|
|
|
193 |
// Check that the required time entered is valid if it was entered at all.
|
|
|
194 |
if (!empty($data['requiredtime'])) {
|
|
|
195 |
if ((!is_number($data['requiredtime']) || $data['requiredtime'] < 0)) {
|
|
|
196 |
$errors['requiredtime'] = get_string('requiredtimenotvalid', 'customcert');
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
return $errors;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Get a list of all options form elements with required capabilities for managing each element.
|
|
|
205 |
*
|
|
|
206 |
* @return array
|
|
|
207 |
*/
|
|
|
208 |
protected function get_options_elements_with_required_caps() {
|
|
|
209 |
return [
|
|
|
210 |
'emailstudents' => 'mod/customcert:manageemailstudents',
|
|
|
211 |
'emailteachers' => 'mod/customcert:manageemailteachers',
|
|
|
212 |
'emailothers' => 'mod/customcert:manageemailothers',
|
|
|
213 |
'verifyany' => 'mod/customcert:manageverifyany',
|
|
|
214 |
'requiredtime' => 'mod/customcert:managerequiredtime',
|
|
|
215 |
'protection_print' => 'mod/customcert:manageprotection',
|
|
|
216 |
'protection_modify' => 'mod/customcert:manageprotection',
|
|
|
217 |
'protection_copy' => 'mod/customcert:manageprotection',
|
|
|
218 |
];
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Build a protection data to be able to set to the form.
|
|
|
223 |
*
|
|
|
224 |
* @param string $protection Protection sting from database.
|
|
|
225 |
*
|
|
|
226 |
* @return \stdClass
|
|
|
227 |
*/
|
|
|
228 |
protected function build_protection_data($protection) {
|
|
|
229 |
$data = new stdClass();
|
|
|
230 |
|
|
|
231 |
$data->protection_print = 0;
|
|
|
232 |
$data->protection_modify = 0;
|
|
|
233 |
$data->protection_copy = 0;
|
|
|
234 |
|
|
|
235 |
$protection = explode(', ', $protection);
|
|
|
236 |
|
|
|
237 |
if (in_array(certificate::PROTECTION_PRINT, $protection)) {
|
|
|
238 |
$data->protection_print = 1;
|
|
|
239 |
}
|
|
|
240 |
if (in_array(certificate::PROTECTION_MODIFY, $protection)) {
|
|
|
241 |
$data->protection_modify = 1;
|
|
|
242 |
}
|
|
|
243 |
if (in_array(certificate::PROTECTION_COPY, $protection)) {
|
|
|
244 |
$data->protection_copy = 1;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
return $data;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
}
|