Merge branch 'MDL-38880' of git://github.com/jmvedrine/moodle
[moodle.git] / badges / edit_form.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
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->libdir . '/formslib.php');
30require_once($CFG->libdir . '/badgeslib.php');
31require_once($CFG->libdir . '/filelib.php');
32
33/**
34 * Form to edit badge details.
35 *
36 */
37class edit_details_form extends moodleform {
38
39 /**
40 * Defines the form
41 */
42 public function definition() {
43 global $CFG;
44
45 $mform = $this->_form;
46 $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
47 $action = $this->_customdata['action'];
48
49 $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
50 $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
51 // Using PARAM_FILE to avoid problems later when downloading badge files.
52 $mform->setType('name', PARAM_FILE);
53 $mform->addRule('name', null, 'required');
54 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
55
56 $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
57 $mform->setType('description', PARAM_CLEANHTML);
58 $mform->addRule('description', null, 'required');
59
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);
63
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');
69 }
70 $mform->addHelpButton('image', 'badgeimage', 'badges');
71
72 $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
73
74 $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
75 $mform->setType('issuername', PARAM_NOTAGS);
76 $mform->addRule('issuername', null, 'required');
60d72efb
SC
77 if (isset($CFG->badges_defaultissuername)) {
78 $mform->setDefault('issuername', $CFG->badges_defaultissuername);
79 }
27806552
YB
80 $mform->addHelpButton('issuername', 'issuername', 'badges');
81
82 $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
60d72efb
SC
83 if (isset($CFG->badges_defaultissuercontact)) {
84 $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
85 }
27806552
YB
86 $mform->setType('issuercontact', PARAM_EMAIL);
87 $mform->addRule('issuercontact', get_string('invalidemail', 'moodle'), 'email', null, 'client', true);
88 $mform->addHelpButton('issuercontact', 'contact', 'badges');
89
90 $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
91
92 $issuancedetails = array();
93 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
94 $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
95 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
96 $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
97 $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
98 $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
99 $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
100 $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
101
102 $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
103 $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
104 $mform->setDefault('expiry', 0);
105 $mform->setDefault('expiredate', strtotime('+1 year'));
106 $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
107 $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
108 $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
109 $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
110 $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
111
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 $mform->addElement('hidden', 'action', $action);
119 $mform->setType('action', PARAM_TEXT);
120
121 if ($action == 'new') {
122 $this->add_action_buttons(true, get_string('createbutton', 'badges'));
123 } else {
124 // Add hidden fields.
125 $mform->addElement('hidden', 'id', $badge->id);
126 $mform->setType('id', PARAM_INT);
127
128 $this->add_action_buttons();
129 $this->set_data($badge);
130
131 // Freeze all elements if badge is active or locked.
132 if ($badge->is_active() || $badge->is_locked()) {
133 $mform->hardFreezeAllVisibleExcept(array());
134 }
135 }
136 }
137
138 /**
139 * Load in existing data as form defaults
140 *
141 * @param stdClass|array $default_values object or array of default values
142 */
143 public function set_data($badge) {
144 $default_values = array();
145 parent::set_data($badge);
146
147 if (!empty($badge->expiredate)) {
148 $default_values['expiry'] = 1;
149 $default_values['expiredate'] = $badge->expiredate;
150 } else if (!empty($badge->expireperiod)) {
151 $default_values['expiry'] = 2;
152 $default_values['expireperiod'] = $badge->expireperiod;
153 }
154 $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
155
156 parent::set_data($default_values);
157 }
158
159 /**
160 * Validates form data
161 */
162 public function validation($data, $files) {
163 global $DB;
164 $errors = parent::validation($data, $files);
165
166 if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
167 $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
168 }
169
170 if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
171 $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
172 }
173
174 // Check for duplicate badge names.
175 if ($data['action'] == 'new') {
176 $duplicate = $DB->record_exists_select('badge', 'name = :name',
177 array('name' => $data['name']));
178 } else {
179 $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid',
180 array('name' => $data['name'], 'badgeid' => $data['id']));
181 }
182
183 if ($duplicate) {
184 $errors['name'] = get_string('error:duplicatename', 'badges');
185 }
186
187 return $errors;
188 }
189}
190
191/**
192 * Form to edit badge message.
193 *
194 */
195class edit_message_form extends moodleform {
196 public function definition() {
197 global $CFG, $OUTPUT;
198
199 $mform = $this->_form;
200 $badge = $this->_customdata['badge'];
201 $action = $this->_customdata['action'];
202 $editoroptions = $this->_customdata['editoroptions'];
203
204 // Add hidden fields.
205 $mform->addElement('hidden', 'id', $badge->id);
206 $mform->setType('id', PARAM_INT);
207
208 $mform->addElement('hidden', 'action', $action);
209 $mform->setType('action', PARAM_TEXT);
210
211 $mform->addElement('header', 'badgemessage', get_string('configuremessage', 'badges'));
212 $mform->addHelpButton('badgemessage', 'variablesubstitution', 'badges');
213
214 $mform->addElement('text', 'messagesubject', get_string('subject', 'badges'), array('size' => '70'));
215 $mform->setType('messagesubject', PARAM_TEXT);
216 $mform->addRule('messagesubject', null, 'required');
217 $mform->addRule('messagesubject', get_string('maximumchars', '', 255), 'maxlength', 255);
218
219 $mform->addElement('editor', 'message_editor', get_string('message', 'badges'), null, $editoroptions);
220 $mform->setType('message_editor', PARAM_RAW);
221 $mform->addRule('message_editor', null, 'required');
222
223 $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1));
224 $mform->addHelpButton('attachment', 'attachment', 'badges');
225
226 $options = array(
227 BADGE_MESSAGE_NEVER => get_string('never'),
228 BADGE_MESSAGE_ALWAYS => get_string('notifyevery', 'badges'),
229 BADGE_MESSAGE_DAILY => get_string('notifydaily', 'badges'),
230 BADGE_MESSAGE_WEEKLY => get_string('notifyweekly', 'badges'),
231 BADGE_MESSAGE_MONTHLY => get_string('notifymonthly', 'badges'),
232 );
233 $mform->addElement('select', 'notification', get_string('notification', 'badges'), $options);
234 $mform->addHelpButton('notification', 'notification', 'badges');
235
236 $this->add_action_buttons();
237 $this->set_data($badge);
238 }
239
240 public function validation($data, $files) {
241 $errors = parent::validation($data, $files);
242
243 return $errors;
244 }
60d72efb 245}