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