on-demand release 3.6dev+
[moodle.git] / badges / edit_form.php
CommitLineData
27806552
YB
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/**
18 * Form classes for editing badges
19 *
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>
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->libdir . '/formslib.php');
30require_once($CFG->libdir . '/badgeslib.php');
31require_once($CFG->libdir . '/filelib.php');
32
33/**
34 * Form to edit badge details.
35 *
36 */
37class edit_details_form extends moodleform {
38
39 /**
40 * Defines the form
41 */
42 public function definition() {
43 global $CFG;
44
45 $mform = $this->_form;
46 $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
47 $action = $this->_customdata['action'];
d363a5c2 48 $languages = get_string_manager()->get_list_of_languages();
27806552
YB
49
50 $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
51 $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
d77a6026
SA
52 // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
53 $mform->setType('name', PARAM_TEXT);
27806552
YB
54 $mform->addRule('name', null, 'required');
55 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
56
d363a5c2
TT
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');
62
27806552 63 $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
17f5e50f 64 $mform->setType('description', PARAM_NOTAGS);
27806552
YB
65 $mform->addRule('description', null, 'required');
66
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);
70
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');
76 }
77 $mform->addHelpButton('image', 'badgeimage', 'badges');
d363a5c2
TT
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');
27806552
YB
90
91 $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
92
93 $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
94 $mform->setType('issuername', PARAM_NOTAGS);
95 $mform->addRule('issuername', null, 'required');
60d72efb
SC
96 if (isset($CFG->badges_defaultissuername)) {
97 $mform->setDefault('issuername', $CFG->badges_defaultissuername);
98 }
27806552
YB
99 $mform->addHelpButton('issuername', 'issuername', 'badges');
100
101 $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
60d72efb
SC
102 if (isset($CFG->badges_defaultissuercontact)) {
103 $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
104 }
e2805314 105 $mform->setType('issuercontact', PARAM_RAW);
27806552
YB
106 $mform->addHelpButton('issuercontact', 'contact', 'badges');
107
108 $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
109
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'));
119
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);
129
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);
135
136 $mform->addElement('hidden', 'action', $action);
137 $mform->setType('action', PARAM_TEXT);
138
139 if ($action == 'new') {
d363a5c2 140 $mform->setDefault('language', $CFG->lang);
27806552
YB
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);
146
147 $this->add_action_buttons();
148 $this->set_data($badge);
149
150 // Freeze all elements if badge is active or locked.
151 if ($badge->is_active() || $badge->is_locked()) {
152 $mform->hardFreezeAllVisibleExcept(array());
153 }
154 }
155 }
156
157 /**
158 * Load in existing data as form defaults
159 *
160 * @param stdClass|array $default_values object or array of default values
161 */
162 public function set_data($badge) {
163 $default_values = array();
164 parent::set_data($badge);
165
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;
172 }
173 $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
174
175 parent::set_data($default_values);
176 }
177
178 /**
179 * Validates form data
180 */
181 public function validation($data, $files) {
182 global $DB;
183 $errors = parent::validation($data, $files);
184
e2805314
YB
185 if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
186 $errors['issuercontact'] = get_string('invalidemail');
187 }
188
27806552
YB
189 if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
190 $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
191 }
192
193 if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
194 $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
195 }
d363a5c2
TT
196
197 if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
198 $errors['imageauthoremail'] = get_string('invalidemail');
199 }
27806552
YB
200
201 // Check for duplicate badge names.
202 if ($data['action'] == 'new') {
e2805314
YB
203 $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
204 array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
27806552 205 } else {
e2805314
YB
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));
27806552
YB
208 }
209
210 if ($duplicate) {
211 $errors['name'] = get_string('error:duplicatename', 'badges');
212 }
213
214 return $errors;
215 }
216}
217
218/**
219 * Form to edit badge message.
220 *
221 */
222class edit_message_form extends moodleform {
223 public function definition() {
224 global $CFG, $OUTPUT;
225
226 $mform = $this->_form;
227 $badge = $this->_customdata['badge'];
228 $action = $this->_customdata['action'];
229 $editoroptions = $this->_customdata['editoroptions'];
230
231 // Add hidden fields.
232 $mform->addElement('hidden', 'id', $badge->id);
233 $mform->setType('id', PARAM_INT);
234
235 $mform->addElement('hidden', 'action', $action);
236 $mform->setType('action', PARAM_TEXT);
237
238 $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
239 $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
240
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);
245
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');
249
250 $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
251 $mform->addHelpButton('attachment', 'attachment', 'badges');
f5d17c14
YB
252 if (empty($CFG->allowattachments)) {
253 $mform->freeze('attachment');
254 }
27806552
YB
255
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'),
262 );
263 $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
264 $mform->addHelpButton('notification', 'notification', 'badges');
265
266 $this->add_action_buttons();
267 $this->set_data($badge);
268 }
269
270 public function validation($data, $files) {
271 $errors = parent::validation($data, $files);
272
273 return $errors;
274 }
60d72efb 275}