1 |
efrain |
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 |
|
|
|
27 |
namespace core_badges\form;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
32 |
require_once($CFG->libdir . '/badgeslib.php');
|
|
|
33 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
34 |
|
|
|
35 |
use moodleform;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Form to edit badge details.
|
|
|
39 |
*
|
|
|
40 |
*/
|
|
|
41 |
class badge extends moodleform {
|
|
|
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'));
|
|
|
55 |
// When downloading badge, it will be necessary to clean the name as PARAM_FILE.
|
|
|
56 |
$mform->setType('name', PARAM_TEXT);
|
|
|
57 |
$mform->addRule('name', null, 'required');
|
|
|
58 |
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
|
|
|
59 |
|
|
|
60 |
$mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70'));
|
|
|
61 |
$mform->setType('version', PARAM_TEXT);
|
|
|
62 |
$mform->addHelpButton('version', 'version', 'badges');
|
|
|
63 |
|
|
|
64 |
$languages = get_string_manager()->get_list_of_languages();
|
|
|
65 |
$mform->addElement('select', 'language', get_string('language'), $languages);
|
|
|
66 |
$mform->addHelpButton('language', 'language', 'badges');
|
|
|
67 |
|
|
|
68 |
$mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
|
|
|
69 |
$mform->setType('description', PARAM_NOTAGS);
|
|
|
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('optimised_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');
|
|
|
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');
|
|
|
95 |
$mform->addElement('tags', 'tags', get_string('tags', 'badges'), ['itemtype' => 'badge', 'component' => 'core_badges']);
|
|
|
96 |
|
|
|
97 |
if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
|
|
|
98 |
$mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
|
|
|
99 |
|
|
|
100 |
$mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
|
|
|
101 |
$mform->setType('issuername', PARAM_NOTAGS);
|
|
|
102 |
$mform->addRule('issuername', null, 'required');
|
|
|
103 |
if (isset($CFG->badges_defaultissuername)) {
|
|
|
104 |
$mform->setDefault('issuername', $CFG->badges_defaultissuername);
|
|
|
105 |
}
|
|
|
106 |
$mform->addHelpButton('issuername', 'issuername', 'badges');
|
|
|
107 |
|
|
|
108 |
$mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
|
|
|
109 |
if (isset($CFG->badges_defaultissuercontact)) {
|
|
|
110 |
$mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
|
|
|
111 |
}
|
|
|
112 |
$mform->setType('issuercontact', PARAM_RAW);
|
|
|
113 |
$mform->addHelpButton('issuercontact', 'contact', 'badges');
|
|
|
114 |
// Set issuer URL.
|
|
|
115 |
// Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
|
|
|
116 |
$url = parse_url($CFG->wwwroot);
|
|
|
117 |
$mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
|
|
|
118 |
$mform->setType('issuerurl', PARAM_URL);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
|
|
|
122 |
|
|
|
123 |
$issuancedetails = array();
|
|
|
124 |
$issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
|
|
|
125 |
$issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
|
|
|
126 |
$issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
|
|
|
127 |
$issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
|
|
|
128 |
$issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
|
|
|
129 |
$issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
|
|
|
130 |
$issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
|
|
|
131 |
$issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
|
|
|
132 |
|
|
|
133 |
$mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
|
|
|
134 |
$mform->addHelpButton('expirydategr', 'expirydate', 'badges');
|
|
|
135 |
$mform->setDefault('expiry', 0);
|
|
|
136 |
$mform->setDefault('expiredate', strtotime('+1 year'));
|
|
|
137 |
$mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
|
|
|
138 |
$mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
|
|
|
139 |
$mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
|
|
|
140 |
$mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
|
|
|
141 |
$mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
|
|
|
142 |
|
|
|
143 |
$mform->addElement('hidden', 'action', $action);
|
|
|
144 |
$mform->setType('action', PARAM_TEXT);
|
|
|
145 |
|
|
|
146 |
if ($action == 'new') {
|
|
|
147 |
// Try to set default badge language to that of current language, or it's parent.
|
|
|
148 |
$language = current_language();
|
|
|
149 |
if (isset($languages[$language])) {
|
|
|
150 |
$defaultlanguage = $language;
|
|
|
151 |
} else {
|
|
|
152 |
// Calling get_parent_language returns an empty string instead of 'en'.
|
|
|
153 |
$defaultlanguage = get_parent_language($language) ?: 'en';
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
$mform->setDefault('language', $defaultlanguage);
|
|
|
157 |
$this->add_action_buttons(true, get_string('createbutton', 'badges'));
|
|
|
158 |
} else {
|
|
|
159 |
// Add hidden fields.
|
|
|
160 |
$mform->addElement('hidden', 'id', $badge->id);
|
|
|
161 |
$mform->setType('id', PARAM_INT);
|
|
|
162 |
|
|
|
163 |
$this->add_action_buttons();
|
|
|
164 |
$this->set_data($badge);
|
|
|
165 |
|
|
|
166 |
// Freeze all elements if badge is active or locked.
|
|
|
167 |
if ($badge->is_active() || $badge->is_locked()) {
|
|
|
168 |
$mform->hardFreezeAllVisibleExcept(array());
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Load in existing data as form defaults
|
|
|
175 |
*
|
|
|
176 |
* @param stdClass|array $badge object or array of default values
|
|
|
177 |
*/
|
|
|
178 |
public function set_data($badge) {
|
|
|
179 |
$defaultvalues = [];
|
|
|
180 |
parent::set_data($badge);
|
|
|
181 |
|
|
|
182 |
if (!empty($badge->expiredate)) {
|
|
|
183 |
$defaultvalues['expiry'] = 1;
|
|
|
184 |
$defaultvalues['expiredate'] = $badge->expiredate;
|
|
|
185 |
} else if (!empty($badge->expireperiod)) {
|
|
|
186 |
$defaultvalues['expiry'] = 2;
|
|
|
187 |
$defaultvalues['expireperiod'] = $badge->expireperiod;
|
|
|
188 |
}
|
|
|
189 |
$defaultvalues['tags'] = \core_tag_tag::get_item_tags_array('core_badges', 'badge', $badge->id);
|
|
|
190 |
$defaultvalues['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
|
|
|
191 |
|
|
|
192 |
parent::set_data($defaultvalues);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Validates form data
|
|
|
197 |
*/
|
|
|
198 |
public function validation($data, $files) {
|
|
|
199 |
global $DB;
|
|
|
200 |
$errors = parent::validation($data, $files);
|
|
|
201 |
|
|
|
202 |
if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
|
|
|
203 |
if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
|
|
|
204 |
$errors['issuercontact'] = get_string('invalidemail');
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
|
|
|
209 |
$errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
|
|
|
213 |
$errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
|
|
|
217 |
$errors['imageauthoremail'] = get_string('invalidemail');
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
// Check for duplicate badge names.
|
|
|
221 |
if ($data['action'] == 'new') {
|
|
|
222 |
$duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
|
|
|
223 |
array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
|
|
|
224 |
} else {
|
|
|
225 |
$duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
|
|
|
226 |
array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
if ($duplicate) {
|
|
|
230 |
$errors['name'] = get_string('error:duplicatename', 'badges');
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
if ($data['imageauthorurl'] && !preg_match('@^https?://.+@', $data['imageauthorurl'])) {
|
|
|
234 |
$errors['imageauthorurl'] = get_string('invalidurl', 'badges');
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
return $errors;
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
|