-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.php
executable file
·319 lines (277 loc) · 10.7 KB
/
lib.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
<?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/>.
/**
* Plugin API.
*
* @package mod_teammeeting
* @copyright 2020 Université Clermont Auvergne
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_teammeeting\helper;
use mod_teammeeting\manager;
defined('MOODLE_INTERNAL') || die;
/**
* Supported features.
*
* @param string $feature FEATURE_xx constant for requested feature.
* @return mixed True or false, null when unknown.
*/
function teammeeting_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Add instance.
*
* @param object $data The form data.
* @param object $mform The form.
* @return int The new instance ID.
*/
function teammeeting_add_instance($data, $mform) {
global $DB, $USER;
$manager = manager::get_instance();
$manager->require_is_available();
$manager->require_is_o365_user($USER->id);
$data->name = $data->name;
$data->intro = $data->intro;
$data->introformat = $data->introformat;
$data->groupid = $data->groupid;
$data->timemodified = time();
$data->usermodified = $USER->id;
$data->id = $DB->insert_record('teammeeting', $data);
// Create the calendar events.
teammeeting_set_events($data);
return $data->id;
}
/**
* Update instance.
*
* @param object $data the form data.
* @param object $mform the form.
* @return bool Whether the update was successful.
*/
function teammeeting_update_instance($data, $mform) {
global $DB, $USER;
$manager = manager::get_instance();
$manager->require_is_available();
$context = context_course::instance($data->course);
$groupmode = $data->groupmode;
$data->name = $data->name;
$data->intro = $data->intro;
$data->introformat = $data->introformat;
$data->groupid = $data->groupid;
$data->usermodified = $USER->id;
$data->timemodified = time();
// Read current record to check what's changed.
$team = $DB->get_record('teammeeting', ['id' => $data->instance]);
$attendeesmodehaschanged = $team->attendeesmode != $data->attendeesmode;
$attendeesrolehaschanged = $team->attendeesrole != $data->attendeesrole;
$teacheridshaschanged = $team->teacherids != $data->teacherids;
$requiresupdate = $team->opendate != $data->opendate || $team->closedate != $data->closedate || $team->name != $data->name
|| $team->allowchat != $data->allowchat || $attendeesmodehaschanged || $attendeesrolehaschanged
|| $teacheridshaschanged;
// Commit the data.
$data->id = $data->instance;
$DB->update_record('teammeeting', $data);
// Re-read to get up-to-date values.
$team = $DB->get_record('teammeeting', ['id' => $team->id]);
// Update onlineMeeting if needed.
if ($requiresupdate) {
$api = $manager->get_api();
$shareddata = [
'allowedPresenters' => helper::get_allowedpresenters_value($team),
'allowMeetingChat' => helper::get_allowmeetingchat_value($team),
'subject' => helper::generate_onlinemeeting_name($team)
];
if (!$team->reusemeeting) {
$shareddata = array_merge($shareddata, [
'startDateTime' => (new DateTimeImmutable("@{$team->opendate}", new DateTimeZone('UTC')))->format('Y-m-d\TH:i:s\Z'),
'endDateTime' => (new DateTimeImmutable("@{$team->closedate}", new DateTimeZone('UTC')))->format('Y-m-d\TH:i:s\Z'),
]);
}
// Retrieving all meeting instances.
$meetings = $DB->get_records_select('teammeeting_meetings',
'teammeetingid = ? AND onlinemeetingid IS NOT NULL AND organiserid IS NOT NULL', [$team->id]);
// Updating the meetings at Microsoft.
foreach ($meetings as $meeting) {
$o365user = $manager->get_o365_user($meeting->organiserid);
$meetingdata = $shareddata;
// Always include the participants as this field may be required by the API if other fields have
// been set. Moreover, we may need to empty the participants list when some other settings changed.
$meetingdata['participants'] = [
'attendees' => helper::make_attendee_list($context, $meeting->organiserid, $meeting->groupid,
$groupmode, $data->attendeesmode, helper::get_selected_teacher_ids($team))
];
$meetingid = $meeting->onlinemeetingid;
$resp = $api->apicall('PATCH', "/users/{$o365user->objectid}/onlineMeetings/{$meetingid}", json_encode($meetingdata));
$api->process_apicall_response($resp, [
'id' => null,
'startDateTime' => null,
'endDateTime' => null,
'joinWebUrl' => null,
]);
// Save the last time we synced the presenters.
$meeting->lastpresenterssync = time();
$DB->set_field('teammeeting_meetings', 'lastpresenterssync', $meeting->lastpresenterssync, ['id' => $meeting->id]);
}
}
// Update the calendar events.
teammeeting_set_events($team);
return true;
}
/**
* Delete instance.
*
* @param int $id The id of the instance to delete.
* @return bool true.
*/
function teammeeting_delete_instance($id) {
global $DB;
if (!$teammeeting = $DB->get_record('teammeeting', ['id' => $id])) {
return false;
}
$manager = manager::get_instance();
$cm = get_coursemodule_from_instance('teammeeting', $id);
\core_completion\api::update_completion_date_event($cm->id, 'teammeeting', $id, null);
// Delete remote meeting instances.
$meetings = $DB->get_records('teammeeting_meetings', ['teammeetingid' => $teammeeting->id]);
foreach ($meetings as $meeting) {
if (empty($meeting->organiserid) || empty($meeting->onlinemeetingid)) {
continue;
} else if (!$manager->is_o365_user($meeting->organiserid)) {
continue;
}
try {
helper::delete_meeting_instance($teammeeting, $meeting);
} catch (\moodle_exception $e) {
debugging('Exception caught: ' . $e->getMessage(), DEBUG_DEVELOPER);
}
}
// Delete the team meeting instance and related meetings.
// All context, files, calendar events, etc... are deleted automatically.
$DB->delete_records('teammeeting_meetings', ['teammeetingid' => $teammeeting->id]);
$DB->delete_records('teammeeting', ['id' => $teammeeting->id]);
return true;
}
/**
* Get the course module info.
*
* @param cm_info $coursemodule The course module.
* @return cached_cm_info The info.
*/
function teammeeting_get_coursemodule_info($coursemodule) {
global $DB;
if (!$resource = $DB->get_record('teammeeting', ['id' => $coursemodule->instance], '*')) {
return null;
}
$fullurl = new moodle_url('/mod/teammeeting/view.php', ['id' => $coursemodule->id, 'redirect' => 1]);
$info = new cached_cm_info();
$info->name = $resource->name;
$info->onclick = "window.open('{$fullurl->out(false)}'); return false;";
if ($coursemodule->showdescription) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$info->content = format_module_intro('teammeeting', $resource, $coursemodule->id, false);
}
return $info;
}
/**
* Refresh the events.
*
* @param object $course The course.
* @param object $teammeeting The instance.
* @param object $cm The cm.
*/
function teammeeting_refresh_events($course, $teammeeting, $cm) {
helper::update_teammeeting_calendar_events($teammeeting);
}
/**
* Add calendar events for the meeting.
*
* @param object $teammeeting The team data.
*/
function teammeeting_set_events($teammeeting) {
try {
helper::get_cm_info_from_teammeeting($teammeeting);
} catch (coding_exception $e) {
// At this stage, the cm_info does not exist yet, which is probably because we
// are calling this from within the add_instance hook. We need to schedule an
// adhoc task to take over.
$task = new \mod_teammeeting\task\update_calendar_events();
$task->set_custom_data(['teammeetingid' => $teammeeting->id]);
\core\task\manager::queue_adhoc_task($task);
return;
}
helper::update_teammeeting_calendar_events($teammeeting);
}
/**
* Prints information about the availability of the online meeting.
*
* @param object $team The instance.
* @param string $format The format ('html' by default, 'text' can be used for notification).
* @return string The information about the meeting.
*/
function teammeeting_print_details_dates($team, $format = 'html') {
global $OUTPUT;
if (!$team->reusemeeting) {
$msg = get_string('meetingavailablebetween', 'mod_teammeeting', [
'from' => userdate($team->opendate, get_string('strftimedatetimeshort', 'core_langconfig')),
'to' => userdate($team->closedate, get_string('strftimedatetimeshort', 'core_langconfig')),
]);
if ($format != 'html') {
return $msg;
}
return html_writer::div(
$OUTPUT->pix_icon('i/info', '', 'core') .
$msg,
'my-2'
);
}
return '';
}
/**
* Extend the course navigation.
*
* @param navigation_node $coursenode The course node.
* @param object $course The course.
* @param context_course $context The course context.
*/
function teammeeting_extend_navigation_course(navigation_node $coursenode, $course, $context) {
if (!has_capability('mod/teammeeting:viewindexlink', $context)) {
return;
}
$coursenode->add(
get_string('modulenameplural', 'mod_teammeeting'),
new moodle_url('/mod/teammeeting/index.php', ['id' => $course->id])
);
}