| 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 |
namespace core_badges\form;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
22 |
require_once($CFG->libdir . '/badgeslib.php');
|
|
|
23 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
24 |
|
|
|
25 |
use moodleform;
|
|
|
26 |
|
|
|
27 |
/**
|
| 1441 |
ariadna |
28 |
* Form classes for editing badges
|
| 1 |
efrain |
29 |
*
|
| 1441 |
ariadna |
30 |
* @package core_badges
|
|
|
31 |
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
|
| 1 |
efrain |
34 |
*/
|
|
|
35 |
class badge extends moodleform {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Defines the form
|
|
|
39 |
*/
|
|
|
40 |
public function definition() {
|
| 1441 |
ariadna |
41 |
global $CFG, $SITE;
|
| 1 |
efrain |
42 |
|
|
|
43 |
$mform = $this->_form;
|
|
|
44 |
$badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
|
|
|
45 |
$action = $this->_customdata['action'];
|
| 1441 |
ariadna |
46 |
if (array_key_exists('courseid', $this->_customdata)) {
|
|
|
47 |
$courseid = $this->_customdata['courseid'];
|
|
|
48 |
} else if (array_key_exists('badge', $this->_customdata)) {
|
|
|
49 |
$courseid = $this->_customdata['badge']->courseid;
|
|
|
50 |
}
|
|
|
51 |
if (!empty($courseid)) {
|
|
|
52 |
$mform->addElement('hidden', 'courseid', $courseid);
|
|
|
53 |
$mform->setType('courseid', PARAM_INT);
|
|
|
54 |
}
|
| 1 |
efrain |
55 |
|
|
|
56 |
$mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
|
| 1441 |
ariadna |
57 |
$mform->addElement('text', 'name', get_string('name'), ['size' => '70']);
|
| 1 |
efrain |
58 |
// When downloading badge, it will be necessary to clean the name as PARAM_FILE.
|
|
|
59 |
$mform->setType('name', PARAM_TEXT);
|
|
|
60 |
$mform->addRule('name', null, 'required');
|
|
|
61 |
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
|
|
|
62 |
|
| 1441 |
ariadna |
63 |
$mform->addElement('text', 'version', get_string('version', 'badges'), ['size' => '70']);
|
| 1 |
efrain |
64 |
$mform->setType('version', PARAM_TEXT);
|
|
|
65 |
|
|
|
66 |
$languages = get_string_manager()->get_list_of_languages();
|
|
|
67 |
$mform->addElement('select', 'language', get_string('language'), $languages);
|
|
|
68 |
|
| 1441 |
ariadna |
69 |
$mform->addElement(
|
|
|
70 |
'textarea',
|
|
|
71 |
'description',
|
|
|
72 |
get_string('description', 'badges'),
|
|
|
73 |
'wrap="virtual" rows="8" cols="70" placeholder="' . s(get_string('descriptioninfo', 'badges')) . '"',
|
|
|
74 |
);
|
| 1 |
efrain |
75 |
$mform->setType('description', PARAM_NOTAGS);
|
|
|
76 |
$mform->addRule('description', null, 'required');
|
|
|
77 |
|
|
|
78 |
$str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
|
| 1441 |
ariadna |
79 |
$imageoptions = ['maxbytes' => 262144, 'accepted_types' => ['optimised_image']];
|
| 1 |
efrain |
80 |
$mform->addElement('filepicker', 'image', $str, null, $imageoptions);
|
|
|
81 |
|
|
|
82 |
if ($action == 'new') {
|
|
|
83 |
$mform->addRule('image', null, 'required');
|
|
|
84 |
} else {
|
|
|
85 |
$currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
|
|
|
86 |
$mform->insertElementBefore($currentimage, 'image');
|
|
|
87 |
}
|
| 1441 |
ariadna |
88 |
$mform->addElement('static', 'imageinfo', null, get_string('badgeimageinfo', 'badges'));
|
|
|
89 |
|
|
|
90 |
$mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), ['size' => '70']);
|
| 1 |
efrain |
91 |
$mform->setType('imagecaption', PARAM_TEXT);
|
| 1441 |
ariadna |
92 |
|
| 1 |
efrain |
93 |
$mform->addElement('tags', 'tags', get_string('tags', 'badges'), ['itemtype' => 'badge', 'component' => 'core_badges']);
|
|
|
94 |
|
| 1441 |
ariadna |
95 |
$mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
|
| 1 |
efrain |
96 |
|
| 1441 |
ariadna |
97 |
$mform->addElement('text', 'issuername', get_string('issuername', 'badges'), ['size' => '70']);
|
|
|
98 |
$mform->setType('issuername', PARAM_NOTAGS);
|
|
|
99 |
$mform->addRule('issuername', null, 'required');
|
|
|
100 |
$site = get_site();
|
|
|
101 |
$issuername = $CFG->badges_defaultissuername ?: $site->fullname;
|
|
|
102 |
$mform->setDefault('issuername', $issuername);
|
| 1 |
efrain |
103 |
|
| 1441 |
ariadna |
104 |
$mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), ['size' => '70']);
|
|
|
105 |
if (isset($CFG->badges_defaultissuercontact)) {
|
|
|
106 |
$mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
|
| 1 |
efrain |
107 |
}
|
| 1441 |
ariadna |
108 |
$mform->setType('issuercontact', PARAM_RAW);
|
|
|
109 |
$mform->addRule('issuercontact', null, 'email');
|
| 1 |
efrain |
110 |
|
| 1441 |
ariadna |
111 |
// Set issuer URL.
|
|
|
112 |
// Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
|
|
|
113 |
$url = parse_url($CFG->wwwroot);
|
|
|
114 |
$mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
|
|
|
115 |
$mform->setType('issuerurl', PARAM_URL);
|
|
|
116 |
|
| 1 |
efrain |
117 |
$mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
|
|
|
118 |
|
| 1441 |
ariadna |
119 |
$issuancedetails = [];
|
|
|
120 |
$issuancedetails[] = $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
|
|
|
121 |
$issuancedetails[] = $mform->createElement('static', 'none_break', null, '<br/>');
|
|
|
122 |
$issuancedetails[] = $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
|
|
|
123 |
$issuancedetails[] = $mform->createElement('date_selector', 'expiredate', '');
|
|
|
124 |
$issuancedetails[] = $mform->createElement('static', 'expirydate_break', null, '<br/>');
|
|
|
125 |
$issuancedetails[] = $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
|
|
|
126 |
$issuancedetails[] = $mform->createElement('duration', 'expireperiod', '', ['defaultunit' => 86400, 'optional' => false]);
|
|
|
127 |
$issuancedetails[] = $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
|
| 1 |
efrain |
128 |
|
| 1441 |
ariadna |
129 |
$mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), [' '], false);
|
| 1 |
efrain |
130 |
$mform->setDefault('expiry', 0);
|
|
|
131 |
$mform->setDefault('expiredate', strtotime('+1 year'));
|
|
|
132 |
$mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
|
|
|
133 |
$mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
|
|
|
134 |
$mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
|
|
|
135 |
$mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
|
|
|
136 |
$mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
|
|
|
137 |
|
|
|
138 |
$mform->addElement('hidden', 'action', $action);
|
|
|
139 |
$mform->setType('action', PARAM_TEXT);
|
|
|
140 |
|
|
|
141 |
if ($action == 'new') {
|
|
|
142 |
// Try to set default badge language to that of current language, or it's parent.
|
|
|
143 |
$language = current_language();
|
|
|
144 |
if (isset($languages[$language])) {
|
|
|
145 |
$defaultlanguage = $language;
|
|
|
146 |
} else {
|
|
|
147 |
// Calling get_parent_language returns an empty string instead of 'en'.
|
|
|
148 |
$defaultlanguage = get_parent_language($language) ?: 'en';
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
$mform->setDefault('language', $defaultlanguage);
|
|
|
152 |
$this->add_action_buttons(true, get_string('createbutton', 'badges'));
|
|
|
153 |
} else {
|
|
|
154 |
// Add hidden fields.
|
|
|
155 |
$mform->addElement('hidden', 'id', $badge->id);
|
|
|
156 |
$mform->setType('id', PARAM_INT);
|
|
|
157 |
|
|
|
158 |
$this->add_action_buttons();
|
|
|
159 |
$this->set_data($badge);
|
|
|
160 |
|
|
|
161 |
// Freeze all elements if badge is active or locked.
|
|
|
162 |
if ($badge->is_active() || $badge->is_locked()) {
|
| 1441 |
ariadna |
163 |
$mform->hardFreezeAllVisibleExcept([]);
|
| 1 |
efrain |
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Load in existing data as form defaults
|
|
|
170 |
*
|
| 1441 |
ariadna |
171 |
* @param \core_badges\badge $badge object or array of default values
|
| 1 |
efrain |
172 |
*/
|
|
|
173 |
public function set_data($badge) {
|
|
|
174 |
$defaultvalues = [];
|
| 1441 |
ariadna |
175 |
parent::set_data((object) $badge);
|
| 1 |
efrain |
176 |
|
|
|
177 |
if (!empty($badge->expiredate)) {
|
|
|
178 |
$defaultvalues['expiry'] = 1;
|
|
|
179 |
$defaultvalues['expiredate'] = $badge->expiredate;
|
|
|
180 |
} else if (!empty($badge->expireperiod)) {
|
|
|
181 |
$defaultvalues['expiry'] = 2;
|
|
|
182 |
$defaultvalues['expireperiod'] = $badge->expireperiod;
|
|
|
183 |
}
|
| 1441 |
ariadna |
184 |
|
|
|
185 |
if (!empty($badge->name)) {
|
|
|
186 |
$defaultvalues['name'] = trim($badge->name);
|
|
|
187 |
}
|
|
|
188 |
|
| 1 |
efrain |
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) {
|
| 1441 |
ariadna |
199 |
global $DB, $SITE;
|
|
|
200 |
|
|
|
201 |
// Trim badge name (to guarantee no badges are created with the same name but some extra spaces).
|
|
|
202 |
$data['name'] = trim($data['name']);
|
|
|
203 |
|
| 1 |
efrain |
204 |
$errors = parent::validation($data, $files);
|
|
|
205 |
|
|
|
206 |
if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
|
|
|
207 |
$errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
|
|
|
211 |
$errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
return $errors;
|
|
|
215 |
}
|
|
|
216 |
}
|