MDL-35073 badges: Open badges integration
[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'];
48
49 $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
50 $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
51 // Using PARAM_FILE to avoid problems later when downloading badge files.
52 $mform->setType('name', PARAM_FILE);
53 $mform->addRule('name', null, 'required');
54 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
55
56 $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
57 $mform->setType('description', PARAM_CLEANHTML);
58 $mform->addRule('description', null, 'required');
59
60 $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
61 $imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('web_image'));
62 $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
63
64 if ($action == 'new') {
65 $mform->addRule('image', null, 'required');
66 } else {
67 $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
68 $mform->insertElementBefore($currentimage, 'image');
69 }
70 $mform->addHelpButton('image', 'badgeimage', 'badges');
71
72 $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
73
74 $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
75 $mform->setType('issuername', PARAM_NOTAGS);
76 $mform->addRule('issuername', null, 'required');
77 $mform->setDefault('issuername', $CFG->badges_defaultissuername);
78 $mform->addHelpButton('issuername', 'issuername', 'badges');
79
80 $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
81 $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
82 $mform->setType('issuercontact', PARAM_EMAIL);
83 $mform->addRule('issuercontact', get_string('invalidemail', 'moodle'), 'email', null, 'client', true);
84 $mform->addHelpButton('issuercontact', 'contact', 'badges');
85
86 $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
87
88 $issuancedetails = array();
89 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
90 $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
91 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
92 $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
93 $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
94 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
95 $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
96 $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
97
98 $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
99 $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
100 $mform->setDefault('expiry', 0);
101 $mform->setDefault('expiredate', strtotime('+1 year'));
102 $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
103 $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
104 $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
105 $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
106 $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
107
108 // Set issuer URL.
109 // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
110 $url = parse_url($CFG->wwwroot);
111 $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
112 $mform->setType('issuerurl', PARAM_URL);
113
114 $mform->addElement('hidden', 'action', $action);
115 $mform->setType('action', PARAM_TEXT);
116
117 if ($action == 'new') {
118 $this->add_action_buttons(true, get_string('createbutton', 'badges'));
119 } else {
120 // Add hidden fields.
121 $mform->addElement('hidden', 'id', $badge->id);
122 $mform->setType('id', PARAM_INT);
123
124 $this->add_action_buttons();
125 $this->set_data($badge);
126
127 // Freeze all elements if badge is active or locked.
128 if ($badge->is_active() || $badge->is_locked()) {
129 $mform->hardFreezeAllVisibleExcept(array());
130 }
131 }
132 }
133
134 /**
135 * Load in existing data as form defaults
136 *
137 * @param stdClass|array $default_values object or array of default values
138 */
139 public function set_data($badge) {
140 $default_values = array();
141 parent::set_data($badge);
142
143 if (!empty($badge->expiredate)) {
144 $default_values['expiry'] = 1;
145 $default_values['expiredate'] = $badge->expiredate;
146 } else if (!empty($badge->expireperiod)) {
147 $default_values['expiry'] = 2;
148 $default_values['expireperiod'] = $badge->expireperiod;
149 }
150 $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
151
152 parent::set_data($default_values);
153 }
154
155 /**
156 * Validates form data
157 */
158 public function validation($data, $files) {
159 global $DB;
160 $errors = parent::validation($data, $files);
161
162 if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
163 $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
164 }
165
166 if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
167 $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
168 }
169
170 // Check for duplicate badge names.
171 if ($data['action'] == 'new') {
172 $duplicate = $DB->record_exists_select('badge', 'name = :name',
173 array('name' => $data['name']));
174 } else {
175 $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid',
176 array('name' => $data['name'], 'badgeid' => $data['id']));
177 }
178
179 if ($duplicate) {
180 $errors['name'] = get_string('error:duplicatename', 'badges');
181 }
182
183 return $errors;
184 }
185}
186
187/**
188 * Form to edit badge message.
189 *
190 */
191class edit_message_form extends moodleform {
192 public function definition() {
193 global $CFG, $OUTPUT;
194
195 $mform = $this->_form;
196 $badge = $this->_customdata['badge'];
197 $action = $this->_customdata['action'];
198 $editoroptions = $this->_customdata['editoroptions'];
199
200 // Add hidden fields.
201 $mform->addElement('hidden', 'id', $badge->id);
202 $mform->setType('id', PARAM_INT);
203
204 $mform->addElement('hidden', 'action', $action);
205 $mform->setType('action', PARAM_TEXT);
206
207 $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
208 $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
209
210 $mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
211 $mform->setType('messagesubject', PARAM_TEXT);
212 $mform->addRule('messagesubject', null, 'required');
213 $mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
214
215 $mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
216 $mform->setType('message_editor', PARAM_RAW);
217 $mform->addRule('message_editor', null, 'required');
218
219 $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
220 $mform->addHelpButton('attachment', 'attachment', 'badges');
221
222 $options = array(
223 BADGE_MESSAGE_NEVER => get_string('never'),
224 BADGE_MESSAGE_ALWAYS => get_string('notifyevery', 'badges'),
225 BADGE_MESSAGE_DAILY => get_string('notifydaily', 'badges'),
226 BADGE_MESSAGE_WEEKLY => get_string('notifyweekly', 'badges'),
227 BADGE_MESSAGE_MONTHLY => get_string('notifymonthly', 'badges'),
228 );
229 $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
230 $mform->addHelpButton('notification', 'notification', 'badges');
231
232 $this->add_action_buttons();
233 $this->set_data($badge);
234 }
235
236 public function validation($data, $files) {
237 $errors = parent::validation($data, $files);
238
239 return $errors;
240 }
241}