weekly release 3.3.7+
[moodle.git] / badges / edit_form.php
blob1c2a40aed31682ad7a4326a9599b966fefc5dc7e
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.
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/>.
17 /**
18 * Form classes for editing badges
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>
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/formslib.php');
30 require_once($CFG->libdir . '/badgeslib.php');
31 require_once($CFG->libdir . '/filelib.php');
33 /**
34 * Form to edit badge details.
37 class edit_details_form extends moodleform {
39 /**
40 * Defines the form
42 public function definition() {
43 global $CFG;
45 $mform = $this->_form;
46 $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
47 $action = $this->_customdata['action'];
49 $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
50 $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
51 // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
52 $mform->setType('name', PARAM_TEXT);
53 $mform->addRule('name', null, 'required');
54 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
56 $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
57 $mform->setType('description', PARAM_NOTAGS);
58 $mform->addRule('description', null, 'required');
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);
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');
70 $mform->addHelpButton('image', 'badgeimage', 'badges');
72 $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
74 $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
75 $mform->setType('issuername', PARAM_NOTAGS);
76 $mform->addRule('issuername', null, 'required');
77 if (isset($CFG->badges_defaultissuername)) {
78 $mform->setDefault('issuername', $CFG->badges_defaultissuername);
80 $mform->addHelpButton('issuername', 'issuername', 'badges');
82 $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
83 if (isset($CFG->badges_defaultissuercontact)) {
84 $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
86 $mform->setType('issuercontact', PARAM_RAW);
87 $mform->addHelpButton('issuercontact', 'contact', 'badges');
89 $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
91 $issuancedetails = array();
92 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
93 $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
94 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
95 $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
96 $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
97 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
98 $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
99 $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
101 $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
102 $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
103 $mform->setDefault('expiry', 0);
104 $mform->setDefault('expiredate', strtotime('+1 year'));
105 $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
106 $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
107 $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
108 $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
109 $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
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);
117 $mform->addElement('hidden', 'action', $action);
118 $mform->setType('action', PARAM_TEXT);
120 if ($action == 'new') {
121 $this->add_action_buttons(true, get_string('createbutton', 'badges'));
122 } else {
123 // Add hidden fields.
124 $mform->addElement('hidden', 'id', $badge->id);
125 $mform->setType('id', PARAM_INT);
127 $this->add_action_buttons();
128 $this->set_data($badge);
130 // Freeze all elements if badge is active or locked.
131 if ($badge->is_active() || $badge->is_locked()) {
132 $mform->hardFreezeAllVisibleExcept(array());
138 * Load in existing data as form defaults
140 * @param stdClass|array $default_values object or array of default values
142 public function set_data($badge) {
143 $default_values = array();
144 parent::set_data($badge);
146 if (!empty($badge->expiredate)) {
147 $default_values['expiry'] = 1;
148 $default_values['expiredate'] = $badge->expiredate;
149 } else if (!empty($badge->expireperiod)) {
150 $default_values['expiry'] = 2;
151 $default_values['expireperiod'] = $badge->expireperiod;
153 $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
155 parent::set_data($default_values);
159 * Validates form data
161 public function validation($data, $files) {
162 global $DB;
163 $errors = parent::validation($data, $files);
165 if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
166 $errors['issuercontact'] = get_string('invalidemail');
169 if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
170 $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
173 if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
174 $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
177 // Check for duplicate badge names.
178 if ($data['action'] == 'new') {
179 $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
180 array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
181 } else {
182 $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
183 array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
186 if ($duplicate) {
187 $errors['name'] = get_string('error:duplicatename', 'badges');
190 return $errors;
195 * Form to edit badge message.
198 class edit_message_form extends moodleform {
199 public function definition() {
200 global $CFG, $OUTPUT;
202 $mform = $this->_form;
203 $badge = $this->_customdata['badge'];
204 $action = $this->_customdata['action'];
205 $editoroptions = $this->_customdata['editoroptions'];
207 // Add hidden fields.
208 $mform->addElement('hidden', 'id', $badge->id);
209 $mform->setType('id', PARAM_INT);
211 $mform->addElement('hidden', 'action', $action);
212 $mform->setType('action', PARAM_TEXT);
214 $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
215 $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
217 $mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
218 $mform->setType('messagesubject', PARAM_TEXT);
219 $mform->addRule('messagesubject', null, 'required');
220 $mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
222 $mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
223 $mform->setType('message_editor', PARAM_RAW);
224 $mform->addRule('message_editor', null, 'required');
226 $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
227 $mform->addHelpButton('attachment', 'attachment', 'badges');
228 if (empty($CFG->allowattachments)) {
229 $mform->freeze('attachment');
232 $options = array(
233 BADGE_MESSAGE_NEVER => get_string('never'),
234 BADGE_MESSAGE_ALWAYS => get_string('notifyevery', 'badges'),
235 BADGE_MESSAGE_DAILY => get_string('notifydaily', 'badges'),
236 BADGE_MESSAGE_WEEKLY => get_string('notifyweekly', 'badges'),
237 BADGE_MESSAGE_MONTHLY => get_string('notifymonthly', 'badges'),
239 $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
240 $mform->addHelpButton('notification', 'notification', 'badges');
242 $this->add_action_buttons();
243 $this->set_data($badge);
246 public function validation($data, $files) {
247 $errors = parent::validation($data, $files);
249 return $errors;