-
Notifications
You must be signed in to change notification settings - Fork 13
/
session_form.php
245 lines (206 loc) · 11 KB
/
session_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/*
* This file is part of Totara LMS
*
* Copyright (C) 2010, 2011 Totara Learning Solutions LTD
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Alastair Munro <[email protected]>
* @author Aaron Barnes <[email protected]>
* @author Francois Marier <[email protected]>
* @package modules
* @subpackage facetoface
*/
defined('MOODLE_INTERNAL') || die();
require_once("{$CFG->libdir}/formslib.php");
require_once("{$CFG->dirroot}/mod/facetoface/lib.php");
class mod_facetoface_session_form extends moodleform {
function definition() {
global $CFG, $DB;
$mform =& $this->_form;
$mform->addElement('hidden', 'id', $this->_customdata['id']);
$mform->addElement('hidden', 'f', $this->_customdata['f']);
$mform->addElement('hidden', 's', $this->_customdata['s']);
$mform->addElement('hidden', 'c', $this->_customdata['c']);
$mform->addElement('header', 'general', get_string('general', 'form'));
$editoroptions = $this->_customdata['editoroptions'];
// Show all custom fields
$customfields = $this->_customdata['customfields'];
facetoface_add_customfields_to_form($mform, $customfields);
// Hack to put help files on these custom fields.
// TODO: add to the admin page a feature to put help text on custom fields
if ($mform->elementExists('custom_location')) {
$mform->addHelpButton('custom_location', 'location', 'facetoface');
}
if ($mform->elementExists('custom_venue')) {
$mform->addHelpButton('custom_venue', 'venue', 'facetoface');
}
if ($mform->elementExists('custom_room')) {
$mform->addHelpButton('custom_room', 'room', 'facetoface');
}
$formarray = array();
$formarray[] = $mform->createElement('selectyesno', 'datetimeknown', get_string('sessiondatetimeknown', 'facetoface'));
$formarray[] = $mform->createElement('static', 'datetimeknownhint', '', html_writer::tag('span', get_string('datetimeknownhinttext','facetoface'), array('class' => 'hint-text')));
$mform->addGroup($formarray,'datetimeknown_group', get_string('sessiondatetimeknown','facetoface'), array(' '),false);
$mform->addGroupRule('datetimeknown_group', null, 'required', null, 'client');
$mform->setDefault('datetimeknown', false);
$mform->addHelpButton('datetimeknown_group', 'sessiondatetimeknown', 'facetoface');
$repeatarray = array();
$repeatarray[] = &$mform->createElement('hidden', 'sessiondateid', 0);
$repeatarray[] = &$mform->createElement('date_time_selector', 'timestart', get_string('timestart', 'facetoface'));
$repeatarray[] = &$mform->createElement('date_time_selector', 'timefinish', get_string('timefinish', 'facetoface'));
$checkboxelement = &$mform->createElement('checkbox', 'datedelete', '', get_string('dateremove', 'facetoface'));
unset($checkboxelement->_attributes['id']); // necessary until MDL-20441 is fixed
$repeatarray[] = $checkboxelement;
$repeatarray[] = &$mform->createElement('html', html_writer::empty_tag('br')); // spacer
$repeatcount = $this->_customdata['nbdays'];
$repeatoptions = array();
$repeatoptions['timestart']['disabledif'] = array('datetimeknown', 'eq', 0);
$repeatoptions['timefinish']['disabledif'] = array('datetimeknown', 'eq', 0);
$mform->setType('timestart', PARAM_INT);
$mform->setType('timefinish', PARAM_INT);
$this->repeat_elements($repeatarray, $repeatcount, $repeatoptions, 'date_repeats', 'date_add_fields',
1, get_string('dateadd', 'facetoface'), true);
$mform->addElement('text', 'capacity', get_string('capacity', 'facetoface'), 'size="5"');
$mform->addRule('capacity', null, 'required', null, 'client');
$mform->setType('capacity', PARAM_INT);
$mform->setDefault('capacity', 10);
$mform->addHelpButton('capacity', 'capacity', 'facetoface');
$mform->addElement('checkbox', 'allowoverbook', get_string('allowoverbook', 'facetoface'));
$mform->addHelpButton('allowoverbook', 'allowoverbook', 'facetoface');
$mform->addElement('text', 'duration', get_string('duration', 'facetoface'), 'size="5"');
$mform->setType('duration', PARAM_TEXT);
$mform->addHelpButton('duration', 'duration', 'facetoface');
if (!get_config(NULL, 'facetoface_hidecost')) {
$formarray = array();
$formarray[] = $mform->createElement('text', 'normalcost', get_string('normalcost', 'facetoface'), 'size="5"');
$formarray[] = $mform->createElement('static', 'normalcosthint', '', html_writer::tag('span', get_string('normalcosthinttext','facetoface'), array('class' => 'hint-text')));
$mform->addGroup($formarray,'normalcost_group', get_string('normalcost','facetoface'), array(' '),false);
$mform->setType('normalcost', PARAM_TEXT);
$mform->addHelpButton('normalcost_group', 'normalcost', 'facetoface');
if (!get_config(NULL, 'facetoface_hidediscount')) {
$formarray = array();
$formarray[] = $mform->createElement('text', 'discountcost', get_string('discountcost', 'facetoface'), 'size="5"');
$formarray[] = $mform->createElement('static', 'discountcosthint', '', html_writer::tag('span', get_string('discountcosthinttext','facetoface'), array('class' => 'hint-text')));
$mform->addGroup($formarray,'discountcost_group', get_string('discountcost','facetoface'), array(' '),false);
$mform->setType('discountcost', PARAM_TEXT);
$mform->addHelpButton('discountcost_group', 'discountcost', 'facetoface');
}
}
$mform->addElement('editor', 'details_editor', get_string('details', 'facetoface'), null, $editoroptions);
$mform->setType('details_editor', PARAM_RAW);
$mform->addHelpButton('details_editor', 'details', 'facetoface');
// Choose users for trainer roles
$rolenames = facetoface_get_trainer_roles();
if ($rolenames) {
// Get current trainers
$current_trainers = facetoface_get_trainers($this->_customdata['s']);
// Loop through all selected roles
$header_shown = false;
foreach ($rolenames as $role => $rolename) {
$rolename = $rolename->name;
// Get course context
$context = context_course::instance($this->_customdata['course']->id);
// Attempt to load users with this role in this course
$rs = $DB->get_recordset_sql("
SELECT
u.id,
u.firstname,
u.lastname
FROM
{role_assignments} ra
LEFT JOIN
{user} u
ON ra.userid = u.id
WHERE
contextid = {$context->id}
AND roleid = {$role}
");
if (!$rs) {
continue;
}
$choices = array();
foreach ($rs as $roleuser) {
$choices[$roleuser->id] = fullname($roleuser);
}
$rs->close();
// Show header (if haven't already)
if ($choices && !$header_shown) {
$mform->addElement('header', 'trainerroles', get_string('sessionroles', 'facetoface'));
$header_shown = true;
}
// If only a few, use checkboxes
if (count($choices) < 4) {
$role_shown = false;
foreach ($choices as $cid => $choice) {
// Only display the role title for the first checkbox for each role
if (!$role_shown) {
$roledisplay = $rolename;
$role_shown = true;
} else {
$roledisplay = '';
}
$mform->addElement('advcheckbox', 'trainerrole['.$role.']['.$cid.']', $roledisplay, $choice, null, array('', $cid));
$mform->setType('trainerrole['.$role.']['.$cid.']', PARAM_INT);
}
} else {
$mform->addElement('select', 'trainerrole['.$role.']', $rolename, $choices, array('multiple' => 'multiple'));
$mform->setType('trainerrole['.$role.']', PARAM_SEQUENCE);
}
// Select current trainers
if ($current_trainers) {
foreach ($current_trainers as $role => $trainers) {
$t = array();
foreach ($trainers as $trainer) {
$t[] = $trainer->id;
$mform->setDefault('trainerrole['.$role.']['.$trainer->id.']', $trainer->id);
}
$mform->setDefault('trainerrole['.$role.']', implode(',', $t));
}
}
}
}
$this->add_action_buttons();
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
$dateids = $data['sessiondateid'];
$dates = count($dateids);
for ($i=0; $i < $dates; $i++) {
$starttime = $data["timestart[$i]"];
$endtime = $data["timefinish[$i]"];
$removecheckbox = empty($data["datedelete"]) ? array() : $data["datedelete"];
if ($starttime > $endtime && !isset($removecheckbox[$i])) {
$errstr = get_string('error:sessionstartafterend','facetoface');
$errors['timestart['.$i.']'] = $errstr;
$errors['timefinish['.$i.']'] = $errstr;
unset($errstr);
}
}
if (!empty($data['datetimeknown'])) {
$datefound = false;
for ($i = 0; $i < $data['date_repeats']; $i++) {
if (empty($data['datedelete'][$i])) {
$datefound = true;
break;
}
}
if (!$datefound) {
$errors['datetimeknown'] = get_string('validation:needatleastonedate', 'facetoface');
}
}
return $errors;
}
}