weekly release 3.9dev+
[moodle.git] / badges / classes / form / badge.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
aae219ac
DW
27namespace core_badges\form;
28
27806552
YB
29defined('MOODLE_INTERNAL') || die();
30
31require_once($CFG->libdir . '/formslib.php');
32require_once($CFG->libdir . '/badgeslib.php');
33require_once($CFG->libdir . '/filelib.php');
34
aae219ac
DW
35use moodleform;
36
27806552
YB
37/**
38 * Form to edit badge details.
39 *
40 */
aae219ac 41class badge extends moodleform {
27806552
YB
42
43 /**
44 * Defines the form
45 */
46 public function definition() {
47 global $CFG;
48
49 $mform = $this->_form;
50 $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
51 $action = $this->_customdata['action'];
52
53 $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
54 $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
d77a6026
SA
55 // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
56 $mform->setType('name', PARAM_TEXT);
27806552
YB
57 $mform->addRule('name', null, 'required');
58 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
59
d363a5c2
TT
60 $mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70'));
61 $mform->setType('version', PARAM_TEXT);
62 $mform->addHelpButton('version', 'version', 'badges');
5253834d
PH
63
64 $languages = get_string_manager()->get_list_of_languages();
d363a5c2
TT
65 $mform->addElement('select', 'language', get_string('language'), $languages);
66 $mform->addHelpButton('language', 'language', 'badges');
67
27806552 68 $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
17f5e50f 69 $mform->setType('description', PARAM_NOTAGS);
27806552
YB
70 $mform->addRule('description', null, 'required');
71
72 $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
73 $imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('web_image'));
74 $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
75
76 if ($action == 'new') {
77 $mform->addRule('image', null, 'required');
78 } else {
79 $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
80 $mform->insertElementBefore($currentimage, 'image');
81 }
82 $mform->addHelpButton('image', 'badgeimage', 'badges');
d363a5c2
TT
83 $mform->addElement('text', 'imageauthorname', get_string('imageauthorname', 'badges'), array('size' => '70'));
84 $mform->setType('imageauthorname', PARAM_TEXT);
85 $mform->addHelpButton('imageauthorname', 'imageauthorname', 'badges');
86 $mform->addElement('text', 'imageauthoremail', get_string('imageauthoremail', 'badges'), array('size' => '70'));
87 $mform->setType('imageauthoremail', PARAM_TEXT);
88 $mform->addHelpButton('imageauthoremail', 'imageauthoremail', 'badges');
89 $mform->addElement('text', 'imageauthorurl', get_string('imageauthorurl', 'badges'), array('size' => '70'));
90 $mform->setType('imageauthorurl', PARAM_URL);
91 $mform->addHelpButton('imageauthorurl', 'imageauthorurl', 'badges');
92 $mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), array('size' => '70'));
93 $mform->setType('imagecaption', PARAM_TEXT);
94 $mform->addHelpButton('imagecaption', 'imagecaption', 'badges');
27806552
YB
95
96 $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
97
aae219ac
DW
98 if (badges_open_badges_backpack_api() != OPEN_BADGES_V2) {
99 $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
100 $mform->setType('issuername', PARAM_NOTAGS);
101 $mform->addRule('issuername', null, 'required');
102 if (isset($CFG->badges_defaultissuername)) {
103 $mform->setDefault('issuername', $CFG->badges_defaultissuername);
104 }
105 $mform->addHelpButton('issuername', 'issuername', 'badges');
27806552 106
aae219ac
DW
107 $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
108 if (isset($CFG->badges_defaultissuercontact)) {
109 $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
110 }
111 $mform->setType('issuercontact', PARAM_RAW);
112 $mform->addHelpButton('issuercontact', 'contact', 'badges');
113 // Set issuer URL.
114 // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
115 $url = parse_url($CFG->wwwroot);
116 $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
117 $mform->setType('issuerurl', PARAM_URL);
118
119 } else {
120 $name = $CFG->badges_defaultissuername;
121 $mform->addElement('static', 'issuernamelabel', get_string('name'), $name);
122 $mform->addElement('hidden', 'issuername', $name);
123 $mform->setType('issuername', PARAM_NOTAGS);
124
125 $contact = $CFG->badges_defaultissuercontact;
126 $mform->addElement('static', 'issuercontactlabel', get_string('contact', 'badges'), $contact);
127 $mform->addElement('hidden', 'issuercontact', $contact);
128 $mform->setType('issuercontact', PARAM_RAW);
129
130 $url = parse_url($CFG->wwwroot);
131 $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
132 $mform->setType('issuerurl', PARAM_URL);
60d72efb 133 }
27806552
YB
134
135 $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
136
137 $issuancedetails = array();
138 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
139 $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
140 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
141 $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
142 $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
143 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
144 $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
145 $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
146
147 $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
148 $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
149 $mform->setDefault('expiry', 0);
150 $mform->setDefault('expiredate', strtotime('+1 year'));
151 $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
152 $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
153 $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
154 $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
155 $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
156
27806552
YB
157 $mform->addElement('hidden', 'action', $action);
158 $mform->setType('action', PARAM_TEXT);
159
160 if ($action == 'new') {
5253834d
PH
161 // Try to set default badge language to that of current language, or it's parent.
162 $language = current_language();
163 if (isset($languages[$language])) {
164 $defaultlanguage = $language;
165 } else {
166 // Calling get_parent_language returns an empty string instead of 'en'.
167 $defaultlanguage = get_parent_language($language) ?: 'en';
168 }
169
170 $mform->setDefault('language', $defaultlanguage);
27806552
YB
171 $this->add_action_buttons(true, get_string('createbutton', 'badges'));
172 } else {
173 // Add hidden fields.
174 $mform->addElement('hidden', 'id', $badge->id);
175 $mform->setType('id', PARAM_INT);
176
177 $this->add_action_buttons();
178 $this->set_data($badge);
179
180 // Freeze all elements if badge is active or locked.
181 if ($badge->is_active() || $badge->is_locked()) {
182 $mform->hardFreezeAllVisibleExcept(array());
183 }
184 }
185 }
186
187 /**
188 * Load in existing data as form defaults
189 *
190 * @param stdClass|array $default_values object or array of default values
191 */
192 public function set_data($badge) {
193 $default_values = array();
194 parent::set_data($badge);
195
196 if (!empty($badge->expiredate)) {
197 $default_values['expiry'] = 1;
198 $default_values['expiredate'] = $badge->expiredate;
199 } else if (!empty($badge->expireperiod)) {
200 $default_values['expiry'] = 2;
201 $default_values['expireperiod'] = $badge->expireperiod;
202 }
203 $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
204
205 parent::set_data($default_values);
206 }
207
208 /**
209 * Validates form data
210 */
211 public function validation($data, $files) {
212 global $DB;
213 $errors = parent::validation($data, $files);
214
aae219ac
DW
215 if (badges_open_badges_backpack_api() != OPEN_BADGES_V2) {
216 if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
217 $errors['issuercontact'] = get_string('invalidemail');
218 }
e2805314
YB
219 }
220
27806552
YB
221 if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
222 $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
223 }
224
225 if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
226 $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
227 }
d363a5c2
TT
228
229 if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
230 $errors['imageauthoremail'] = get_string('invalidemail');
231 }
27806552
YB
232
233 // Check for duplicate badge names.
234 if ($data['action'] == 'new') {
e2805314
YB
235 $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
236 array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
27806552 237 } else {
e2805314
YB
238 $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
239 array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
27806552
YB
240 }
241
242 if ($duplicate) {
243 $errors['name'] = get_string('error:duplicatename', 'badges');
244 }
245
347b771a
AG
246 if ($data['imageauthorurl'] && !preg_match('@^https?://.+@', $data['imageauthorurl'])) {
247 $errors['imageauthorurl'] = get_string('invalidurl', 'badges');
248 }
249
27806552
YB
250 return $errors;
251 }
252}
253