forked from MURBASLMS/moodle-mod_teammeeting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
executable file
·278 lines (237 loc) · 8.87 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
<?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 local_o365\utils;
use mod_teammeeting\helper;
use mod_teammeeting\manager;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/calendar/lib.php');
/**
* 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_MOD_ARCHETYPE:
return MOD_ARCHETYPE_RESOURCE;
case FEATURE_GROUPS:
return false;
case FEATURE_GROUPINGS:
return false;
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, $COURSE;
$context = context_course::instance($COURSE->id);
$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->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, $COURSE, $USER;
$context = context_course::instance($COURSE->id);
$manager = manager::get_instance();
$manager->require_is_available();
$data->name = $data->name;
$data->intro = $data->intro;
$data->introformat = $data->introformat;
$data->usermodified = $USER->id;
$data->timemodified = time();
$team = $DB->get_record('teammeeting', ['id' => $data->instance]);
$requiresupdate = $team->opendate != $data->opendate || $team->closedate != $data->closedate || $team->name != $data->name;
if ($requiresupdate && !empty($team->organiserid) && !empty($team->onlinemeetingid)) {
$manager->require_is_o365_user($team->organiserid);
// Updating the meeting at Microsoft.
$o365user = $manager->get_o365_user($team->organiserid);
$api = $manager->get_api();
$meetingdata = [
'subject' => format_string($data->name, true, ['context' => $context]),
];
if (!$data->reusemeeting) {
$meetingdata = array_merge($meetingdata, [
'startDateTime' => (new DateTimeImmutable("@{$data->opendate}", new DateTimeZone('UTC')))->format('Y-m-d\TH:i:s\Z'),
'endDateTime' => (new DateTimeImmutable("@{$data->closedate}", new DateTimeZone('UTC')))->format('Y-m-d\TH:i:s\Z'),
]);
}
// Note that attendees (presenters) do not need updating, they are periodically updated when the meeting page is viewed.
$meetingid = $team->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,
]);
}
$data->id = $data->instance;
$DB->update_record('teammeeting', $data);
// Update the calendar events.
teammeeting_set_events($data);
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 (!$team = $DB->get_record('teammeeting', array('id' => $id))) {
return false;
}
$cm = get_coursemodule_from_instance('teammeeting', $id);
\core_completion\api::update_completion_date_event($cm->id, 'teammeeting', $id, null);
// All context, files, calendar events, etc... are deleted automatically.
$DB->delete_records('teammeeting', array('id' => $team->id));
// Attempt to delete at Microsoft. This is temporarily disabled because it currently
// conflicts with the ability to duplicate an activity. If an activity is duplicated, and
// either copies are deleted, the meeting link will be invalidated for both. The better
// strategy would be to create a new meeting upon restore but that has broader implications.
if (false) {
$manager = manager::get_instance();
if ($manager->is_available() && $manager->is_o365_user($team->organiserid)) {
$o365user = $manager->get_o365_user($team->organiserid);
$api = $manager->get_api();
$meetingid = $team->onlinemeetingid;
$api->apicall('DELETE', "/users/{$o365user->objectid}/onlineMeetings/{$meetingid}");
}
}
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);
}
$info->customdata = ['fullurl' => $resource->externalurl];
return $info;
}
/**
* Add calendar events for the meeting.
*
* @param object $team The team data.
*/
function teammeeting_set_events($team) {
global $DB;
if ($events = $DB->get_records('event', array('modulename' => 'teammeeting', 'instance' => $team->id))) {
foreach ($events as $event) {
$event = calendar_event::load($event);
$event->delete();
}
}
// We do not create events when we're missing an open or close date.
if (!$team->opendate || !$team->closedate) {
return;
}
// The open-event.
$event = new stdClass;
$event->name = $team->name;
$event->description = $team->name;
$event->courseid = $team->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'teammeeting';
$event->instance = $team->id;
$event->eventtype = 'open';
$event->timestart = $team->opendate;
$event->visible = instance_is_visible('teammeeting', $team);
$event->timeduration = ($team->closedate - $team->opendate);
calendar_event::create($event);
}
/**
* 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
);
}
return '';
}