-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_form.php
executable file
·393 lines (334 loc) · 16.4 KB
/
mod_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Module form.
*
* @package mod_teammeeting
* @copyright 2020 Université Clermont Auvergne
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\notification;
use local_o365\utils;
use mod_teammeeting\helper;
use mod_teammeeting\manager;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/teammeeting/lib.php');
/**
* Module form.
*
* @package mod_teammeeting
* @copyright 2020 Université Clermont Auvergne
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_teammeeting_mod_form extends moodleform_mod {
/**
* Form construction.
*/
public function definition() {
global $USER, $OUTPUT;
$mform = $this->_form;
$cansetorganiser = has_capability('mod/teammeeting:setorganiser', $this->context);
$manager = manager::get_instance();
if (!$manager->is_available()) {
$this->standard_hidden_coursemodule_elements();
$notif = new notification(get_string('apinotconfigured', 'mod_teammeeting'), notification::NOTIFY_ERROR);
$notif->set_show_closebutton(false);
$mform->addElement('html', $OUTPUT->render($notif));
$mform->addElement('cancel', '', get_string('back', 'teammeeting'));
return;
} else if (!$manager->is_o365_user($USER->id)) {
$this->standard_hidden_coursemodule_elements();
$notif = new notification(get_string('noto365usercurrent', 'mod_teammeeting'), notification::NOTIFY_ERROR);
$notif->set_show_closebutton(false);
$mform->addElement('html', $OUTPUT->render($notif));
$mform->addElement('cancel', '', get_string('back', 'teammeeting'));
return;
}
$isedit = !empty($this->current) && !empty($this->current->id);
$mform->addElement('header', 'general', get_string('general'));
$mform->addElement('text', 'name', get_string('name', 'core'), ['size' => '64']);
$mform->addRule('name', get_string('maximumchars', 'core', 255), 'maxlength', 255, 'client');
$mform->setType('name', PARAM_TEXT);
$this->standard_intro_elements();
$mform->addElement('select', 'reusemeeting', get_string('reusemeeting', 'mod_teammeeting'), [
1 => get_string('reusemeetingyes', 'mod_teammeeting'),
0 => get_string('reusemeetingno', 'mod_teammeeting'),
]);
$mform->addHelpButton('reusemeeting', 'reusemeeting', 'mod_teammeeting');
$mform->setDefault('reusemeeting', 1);
// Disable the reusability when we edit the resource because we cannot update
// the meeting type (isBroadcast) once it has been created.
if ($isedit) {
$mform->freeze('reusemeeting');
$mform->setConstant('reusemeeting', $this->current->reusemeeting);
$mform->disabledIf('reusemeeting', 'team_id', 'neq', '');
}
// The date selectors.
$tz = core_date::get_user_timezone_object();
$defaultduration = (int) get_config('mod_teammeeting', 'meetingdefaultduration');
$nowplusone = (new DateTimeImmutable('now', $tz))->add(new DateInterval('PT1H'));
$defaultopen = $nowplusone->setTime($nowplusone->format('H'), 0, 0, 0);
$defaultclose = $defaultopen->add(new DateInterval("PT{$defaultduration}S"));
$mform->addElement('date_time_selector', 'opendate', get_string('opendate', 'mod_teammeeting'), [
'defaulttime' => $defaultopen->getTimestamp()
]);
$mform->addHelpButton('opendate', 'opendate', 'mod_teammeeting');
$mform->addElement('date_time_selector', 'closedate', get_string('closedate', 'mod_teammeeting'), [
'optional' => true,
'defaulttime' => $defaultclose->getTimestamp()
]);
$mform->addHelpButton('closedate', 'closedate', 'mod_teammeeting');
$mform->hideIf('opendate', 'reusemeeting', 'eq', 1);
$mform->hideIf('closedate', 'reusemeeting', 'eq', 1);
// Only some people can set the organiser here, hide from everybody else.
if ($cansetorganiser) {
$potentialorganisers = array_map(function($user) {
return fullname($user);
}, get_enrolled_users($this->context, 'mod/teammeeting:presentmeeting', 0, 'u.*', 'u.lastname ASC'));
$validorganiserids = utils::limit_to_o365_users(array_keys($potentialorganisers));
$potentialorganisers = [0 => get_string('nonespecified', 'mod_teammeeting')] + array_intersect_key($potentialorganisers,
array_flip($validorganiserids));
$mform->addElement('select', 'organiserid', get_string('defaultorganiser', 'mod_teammeeting'), $potentialorganisers);
$mform->addHelpButton('organiserid', 'defaultorganiser', 'mod_teammeeting');
$caneditorganiser = $this->can_organiser_be_changed();
if (!$caneditorganiser) {
$mform->hardFreeze('organiserid');
}
}
// Additional teachers.
$potentialpresenters = array_map(function($user) {
return fullname($user);
}, get_enrolled_users($this->context, 'mod/teammeeting:presentmeeting', 0, 'u.*', 'u.lastname ASC'));
$mform->addElement('autocomplete', 'teacherids', get_string('additionalteachers', 'mod_teammeeting'), $potentialpresenters,
[
'multiple' => true,
'noselectionstring' => get_string('noneselected', 'mod_teammeeting')
]
);
$mform->addHelpButton('teacherids', 'additionalteachers', 'mod_teammeeting');
// Student membership.
$mform->addElement('select', 'attendeesmode', get_string('attendeesmode', 'mod_teammeeting'), [
helper::ATTENDEES_NONE => get_string('attendeesmodenonedefault', 'mod_teammeeting'),
helper::ATTENDEES_FORCED => get_string('attendeesmodeforced', 'mod_teammeeting')
]);
$mform->addHelpButton('attendeesmode', 'attendeesmode', 'mod_teammeeting');
// Student role.
$mform->addElement('select', 'attendeesrole', get_string('attendeesrole', 'mod_teammeeting'), [
helper::ROLE_ATTENDEE => get_string('attendeesroleattendee', 'mod_teammeeting'),
helper::ROLE_PRESENTER => get_string('attendeesrolepresenter', 'mod_teammeeting')
]);
$mform->addHelpButton('attendeesrole', 'attendeesrole', 'mod_teammeeting');
// Chat settings.
$mform->addElement('select', 'allowchat', get_string('allowchat', 'mod_teammeeting'), [
helper::CHAT_DURING_MEETING => get_string('enabledduringmeeting', 'mod_teammeeting'),
helper::CHAT_ENABLED => get_string('alwaysenabled', 'mod_teammeeting')
]);
$mform->addHelpButton('allowchat', 'allowchat', 'mod_teammeeting');
// Group restriction.
$groups = ['' => get_string('noneselected', 'mod_teammeeting')] + array_map(function($group) {
return $group->name;
}, groups_get_all_groups($this->get_course()->id));
$mform->addElement('autocomplete', 'groupid', get_string('restrictedtogroup', 'mod_teammeeting'), $groups, [
'noselectionstring' => get_string('noneselected', 'mod_teammeeting')
]);
$mform->addHelpButton('groupid', 'restrictedtogroup', 'mod_teammeeting');
$mform->setDefault('groupid', '');
$mform->disabledIf('groupmode', 'groupid', 'neq', '');
$mform->hideIf('groupingid', 'groupid', 'neq', '');
$this->standard_coursemodule_elements();
// Upon activity creation, we remove "Save and display" to avoid
// for someone to nominate themselves right away on accident.
$this->add_action_buttons(true, empty($this->current->id) ? false : null);
// That button is adding confusion, and it's not required for us because
// we emulate its behaviour via our groupid field.
if ($mform->elementExists('restrictgroupbutton')) {
$mform->removeElement('restrictgroupbutton');
}
}
/**
* Form validation.
*
* @param array $data The data.
* @param array $files The files.
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$isedit = !empty($this->current->id);
$haschangeddates = !$isedit || ($this->current->opendate != $data['opendate']
|| $this->current->closedate != $data['closedate']);
if (!$data['reusemeeting']) {
if ($haschangeddates && (!empty($data['closedate']) && $data['closedate'] < time())) {
// Only validate this on new, or edits where dates have changed, otherwise
// we wouldn't be able to edit an instance that has been finished.
$errors['closedate'] = get_string('errordatespast', 'mod_teammeeting');
}
if (!empty($data['closedate']) && $data['opendate'] >= $data['closedate']) {
$errors['closedate'] = get_string('errordates', 'mod_teammeeting');
}
}
return $errors;
}
/**
* Fix default values.
*
* @param array $defaultvalues
*/
public function data_preprocessing(&$defaultvalues) {
parent::data_postprocessing($defaultvalues);
// Upon creation of a new instance, if the course does not enforce
// the group mode, we default to using "No groups" for simplicity.
if (empty($this->current->id) && !$this->get_course()->groupmodeforce) {
$defaultvalues['groupmode'] = NOGROUPS;
}
// Reverse the sequence.
if (!empty($defaultvalues['teacherids'])) {
$defaultvalues['teacherids'] = explode(',', $defaultvalues['teacherids']);
}
// Ensure value is zero when empty.
if (empty($defaultvalues['organiserid'])) {
$defaultvalues['organiserid'] = 0;
}
}
/**
* Post process the data.
*
* @param stdClass $data The form data.
*/
public function data_postprocessing($data) {
parent::data_postprocessing($data);
$ispermanent = !empty($data->reusemeeting);
$defaultduration = (int) get_config('mod_teammeeting', 'meetingdefaultduration');
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completionentriesenabled) || !$autocompletion) {
$data->completionentries = 0;
}
}
if ($ispermanent) {
// A permanent meeting should not have any dates.
$data->opendate = 0;
$data->closedate = 0;
} else if (!$ispermanent && !$data->closedate) {
// A non-permanent meeting should be given a close date if none.
$endtime = (new DateTimeImmutable('@' . $data->opendate))->add(new DateInterval("PT{$defaultduration}S"));
$data->closedate = $endtime->getTimestamp();
}
// Convert empty strings to 0.
$data->groupid = (int) $data->groupid;
// When our group ID is used, we hardcode other settings.
if ($data->groupid > 0) {
// We set to separate groups to trigger the various validation that is expected based on
// groups. More specifically, to access the activity the user should either belong to
// the group, or has the capability to access all groups. We do that even though we
// will set a restrict access rule preventing other groups from accessing the activity.
$data->groupmode = SEPARATEGROUPS;
$data->groupingid = 0;
$data->availabilityconditionsjson = $this->construct_availability_conditions_json($data);
}
// Convert the teacher IDs field to a sequence.
$data->teacherids = implode(',', array_map(function($value) {
return (int) $value;
}, (array) ($data->teacherids ?? [])));
// When organiserid has been provided, convert to int.
if ($data->organiserid ?? null !== null) {
$data->organiserid = (int) $data->organiserid;
}
}
/**
* Whether the organiser can be edited.
*
* @return bool
*/
protected function can_organiser_be_changed() {
global $DB;
$isedit = !empty($this->current) && !empty($this->current->id);
if (!$isedit) {
return true;
}
// We can't if any meeting already has an organiser.
return !$DB->record_exists_select('teammeeting_meetings', 'teammeetingid = ? AND organiserid > 0', [$this->current->id]);
}
/**
* Construct the availability conditions JSON.
*
* This attempts to automatically add a restrict access condition based on the group
* that was set (if any). This will not inject itself in complex restrict access
* condition trees, and will silently handle failures.
*
* @param object $data The submitted data.
*/
protected function construct_availability_conditions_json($data) {
global $CFG;
$origvalue = !empty($data->availabilityconditionsjson) ? $data->availabilityconditionsjson : '';
if (empty($CFG->enableavailability)) {
return $origvalue;
} else if (empty($data->groupid)) {
return $origvalue;
}
$groupconditionenabled = array_key_exists('group', \core\plugininfo\availability::get_enabled_plugins());
if (!$groupconditionenabled) {
return $origvalue;
}
// The basic structure with just our condition.
$structure = (object) [
'op' => '&',
'c' => [(object) [
'type' => 'group',
'id' => $data->groupid,
]],
'showc' => [false],
];
// If we received a value, let's see.
if (!empty($origvalue)) {
$origstructure = json_decode($origvalue);
$tree = new \core_availability\tree($origstructure);
// If the tree is not empty, let's add our condition to it there is no group
// condition in there yet. If there is a group condition, or if the operator
// is not a plain and simple AND, we do not do anything.
if (!$tree->is_empty()) {
$structure = clone $origstructure; // First, copy and assume we're not changing anything.
// Check if we find a group condition at the top level, and if yes apply our group ID.
$hasgroup = false;
foreach ($structure->c as $condition) {
if (!empty($condition->type) && $condition->type === 'group') {
$condition->id = $data->groupid; // Override in case our group ID changed.
$hasgroup = true;
}
}
// If we have not found our group condition, let's add it.
if (!$hasgroup && $structure->op === '&') {
$structure->c[] = (object) [
'type' => 'group',
'id' => $data->groupid,
];
$structure->showc[] = false;
} else if (!$hasgroup) {
debugging('Restrict access structure too advanced to check or modify.', DEBUG_DEVELOPER);
}
}
}
// Finally, validate the structure, or fallback on the original.
try {
new \core_availability\tree($structure);
} catch (Exception $e) {
debugging('Error in generated restrict access tree, reverting to original.', DEBUG_DEVELOPER);
return $origvalue;
}
return json_encode($structure);
}
}