MDL-63724 messaging: Prefix field with the table alias
[moodle.git] / badges / edit_form.php
blobc21bf5a9890f2088e75e5de15ebff915baea95a9
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.
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/>.
17 /**
18 * Form classes for editing badges
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/formslib.php');
30 require_once($CFG->libdir . '/badgeslib.php');
31 require_once($CFG->libdir . '/filelib.php');
33 /**
34 * Form to edit badge details.
37 class edit_details_form extends moodleform {
39 /**
40 * Defines the form
42 public function definition() {
43 global $CFG;
45 $mform = $this->_form;
46 $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
47 $action = $this->_customdata['action'];
48 $languages = get_string_manager()->get_list_of_languages();
50 $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
51 $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
52 // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
53 $mform->setType('name', PARAM_TEXT);
54 $mform->addRule('name', null, 'required');
55 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
57 $mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70'));
58 $mform->setType('version', PARAM_TEXT);
59 $mform->addHelpButton('version', 'version', 'badges');
60 $mform->addElement('select', 'language', get_string('language'), $languages);
61 $mform->addHelpButton('language', 'language', 'badges');
63 $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
64 $mform->setType('description', PARAM_NOTAGS);
65 $mform->addRule('description', null, 'required');
67 $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
68 $imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('web_image'));
69 $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
71 if ($action == 'new') {
72 $mform->addRule('image', null, 'required');
73 } else {
74 $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
75 $mform->insertElementBefore($currentimage, 'image');
77 $mform->addHelpButton('image', 'badgeimage', 'badges');
78 $mform->addElement('text', 'imageauthorname', get_string('imageauthorname', 'badges'), array('size' => '70'));
79 $mform->setType('imageauthorname', PARAM_TEXT);
80 $mform->addHelpButton('imageauthorname', 'imageauthorname', 'badges');
81 $mform->addElement('text', 'imageauthoremail', get_string('imageauthoremail', 'badges'), array('size' => '70'));
82 $mform->setType('imageauthoremail', PARAM_TEXT);
83 $mform->addHelpButton('imageauthoremail', 'imageauthoremail', 'badges');
84 $mform->addElement('text', 'imageauthorurl', get_string('imageauthorurl', 'badges'), array('size' => '70'));
85 $mform->setType('imageauthorurl', PARAM_URL);
86 $mform->addHelpButton('imageauthorurl', 'imageauthorurl', 'badges');
87 $mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), array('size' => '70'));
88 $mform->setType('imagecaption', PARAM_TEXT);
89 $mform->addHelpButton('imagecaption', 'imagecaption', 'badges');
91 $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
93 $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
94 $mform->setType('issuername', PARAM_NOTAGS);
95 $mform->addRule('issuername', null, 'required');
96 if (isset($CFG->badges_defaultissuername)) {
97 $mform->setDefault('issuername', $CFG->badges_defaultissuername);
99 $mform->addHelpButton('issuername', 'issuername', 'badges');
101 $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
102 if (isset($CFG->badges_defaultissuercontact)) {
103 $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
105 $mform->setType('issuercontact', PARAM_RAW);
106 $mform->addHelpButton('issuercontact', 'contact', 'badges');
108 $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
110 $issuancedetails = array();
111 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
112 $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
113 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
114 $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
115 $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
116 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
117 $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
118 $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
120 $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
121 $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
122 $mform->setDefault('expiry', 0);
123 $mform->setDefault('expiredate', strtotime('+1 year'));
124 $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
125 $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
126 $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
127 $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
128 $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
130 // Set issuer URL.
131 // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
132 $url = parse_url($CFG->wwwroot);
133 $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
134 $mform->setType('issuerurl', PARAM_URL);
136 $mform->addElement('hidden', 'action', $action);
137 $mform->setType('action', PARAM_TEXT);
139 if ($action == 'new') {
140 $mform->setDefault('language', $CFG->lang);
141 $this->add_action_buttons(true, get_string('createbutton', 'badges'));
142 } else {
143 // Add hidden fields.
144 $mform->addElement('hidden', 'id', $badge->id);
145 $mform->setType('id', PARAM_INT);
147 $this->add_action_buttons();
148 $this->set_data($badge);
150 // Freeze all elements if badge is active or locked.
151 if ($badge->is_active() || $badge->is_locked()) {
152 $mform->hardFreezeAllVisibleExcept(array());
158 * Load in existing data as form defaults
160 * @param stdClass|array $default_values object or array of default values
162 public function set_data($badge) {
163 $default_values = array();
164 parent::set_data($badge);
166 if (!empty($badge->expiredate)) {
167 $default_values['expiry'] = 1;
168 $default_values['expiredate'] = $badge->expiredate;
169 } else if (!empty($badge->expireperiod)) {
170 $default_values['expiry'] = 2;
171 $default_values['expireperiod'] = $badge->expireperiod;
173 $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
175 parent::set_data($default_values);
179 * Validates form data
181 public function validation($data, $files) {
182 global $DB;
183 $errors = parent::validation($data, $files);
185 if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
186 $errors['issuercontact'] = get_string('invalidemail');
189 if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
190 $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
193 if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
194 $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
197 if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
198 $errors['imageauthoremail'] = get_string('invalidemail');
201 // Check for duplicate badge names.
202 if ($data['action'] == 'new') {
203 $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
204 array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
205 } else {
206 $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
207 array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
210 if ($duplicate) {
211 $errors['name'] = get_string('error:duplicatename', 'badges');
214 return $errors;
219 * Form to edit badge message.
222 class edit_message_form extends moodleform {
223 public function definition() {
224 global $CFG, $OUTPUT;
226 $mform = $this->_form;
227 $badge = $this->_customdata['badge'];
228 $action = $this->_customdata['action'];
229 $editoroptions = $this->_customdata['editoroptions'];
231 // Add hidden fields.
232 $mform->addElement('hidden', 'id', $badge->id);
233 $mform->setType('id', PARAM_INT);
235 $mform->addElement('hidden', 'action', $action);
236 $mform->setType('action', PARAM_TEXT);
238 $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
239 $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
241 $mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
242 $mform->setType('messagesubject', PARAM_TEXT);
243 $mform->addRule('messagesubject', null, 'required');
244 $mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
246 $mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
247 $mform->setType('message_editor', PARAM_RAW);
248 $mform->addRule('message_editor', null, 'required');
250 $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
251 $mform->addHelpButton('attachment', 'attachment', 'badges');
252 if (empty($CFG->allowattachments)) {
253 $mform->freeze('attachment');
256 $options = array(
257 BADGE_MESSAGE_NEVER => get_string('never'),
258 BADGE_MESSAGE_ALWAYS => get_string('notifyevery', 'badges'),
259 BADGE_MESSAGE_DAILY => get_string('notifydaily', 'badges'),
260 BADGE_MESSAGE_WEEKLY => get_string('notifyweekly', 'badges'),
261 BADGE_MESSAGE_MONTHLY => get_string('notifymonthly', 'badges'),
263 $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
264 $mform->addHelpButton('notification', 'notification', 'badges');
266 $this->add_action_buttons();
267 $this->set_data($badge);
270 public function validation($data, $files) {
271 $errors = parent::validation($data, $files);
273 return $errors;