2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Form classes for editing 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');
34 * Form to edit badge details.
37 class edit_details_form
extends moodleform
{
42 public function definition() {
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');
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);
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'));
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) {
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
));
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
));
211 $errors['name'] = get_string('error:duplicatename', 'badges');
214 if ($data['imageauthorurl'] && !preg_match('@^https?://.+@', $data['imageauthorurl'])) {
215 $errors['imageauthorurl'] = get_string('invalidurl', 'badges');
223 * Form to edit badge message.
226 class edit_message_form
extends moodleform
{
227 public function definition() {
228 global $CFG, $OUTPUT;
230 $mform = $this->_form
;
231 $badge = $this->_customdata
['badge'];
232 $action = $this->_customdata
['action'];
233 $editoroptions = $this->_customdata
['editoroptions'];
235 // Add hidden fields.
236 $mform->addElement('hidden', 'id', $badge->id
);
237 $mform->setType('id', PARAM_INT
);
239 $mform->addElement('hidden', 'action', $action);
240 $mform->setType('action', PARAM_TEXT
);
242 $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
243 $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
245 $mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
246 $mform->setType('messagesubject', PARAM_TEXT
);
247 $mform->addRule('messagesubject', null, 'required');
248 $mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
250 $mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
251 $mform->setType('message_editor', PARAM_RAW
);
252 $mform->addRule('message_editor', null, 'required');
254 $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
255 $mform->addHelpButton('attachment', 'attachment', 'badges');
256 if (empty($CFG->allowattachments
)) {
257 $mform->freeze('attachment');
261 BADGE_MESSAGE_NEVER
=> get_string('never'),
262 BADGE_MESSAGE_ALWAYS
=> get_string('notifyevery', 'badges'),
263 BADGE_MESSAGE_DAILY
=> get_string('notifydaily', 'badges'),
264 BADGE_MESSAGE_WEEKLY
=> get_string('notifyweekly', 'badges'),
265 BADGE_MESSAGE_MONTHLY
=> get_string('notifymonthly', 'badges'),
267 $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
268 $mform->addHelpButton('notification', 'notification', 'badges');
270 $this->add_action_buttons();
271 $this->set_data($badge);
274 public function validation($data, $files) {
275 $errors = parent
::validation($data, $files);