forked from ncstate-delta/moodle-mod_zoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
executable file
·326 lines (286 loc) · 11.5 KB
/
locallib.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
<?php
// This file is part of the Zoom plugin for 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/>.
/**
* Internal library of functions for module zoom
*
* All the zoom specific functions, needed to implement the module
* logic, should go here. Never include this file from your lib.php!
*
* @package mod_zoom
* @copyright 2015 UC Regents
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/mod/zoom/lib.php');
require_once($CFG->dirroot.'/mod/zoom/classes/webservice.php');
// Constants.
// Audio options.
define('ZOOM_AUDIO_TELEPHONY', 'telephony');
define('ZOOM_AUDIO_VOIP', 'voip');
define('ZOOM_AUDIO_BOTH', 'both');
// Meeting statuses.
define('ZOOM_MEETING_NOT_STARTED', 0);
define('ZOOM_MEETING_STARTED', 1);
define('ZOOM_MEETING_FINISHED', 2);
define('ZOOM_MEETING_EXPIRED', -1);
// Meeting types.
define('ZOOM_INSTANT_MEETING', 1);
define('ZOOM_SCHEDULED_MEETING', 2);
define('ZOOM_RECURRING_MEETING', 3);
define('ZOOM_WEBINAR', 5);
define('ZOOM_RECURRING_WEBINAR', 6);
// Authentication methods.
define('ZOOM_SNS_FACEBOOK', 0);
define('ZOOM_SNS_GOOGLE', 1);
define('ZOOM_SNS_API', 99);
define('ZOOM_SNS_ZOOM', 100);
define('ZOOM_SNS_SSO', 101);
// Number of meetings per page from zoom's get user report.
define('ZOOM_DEFAULT_RECORDS_PER_CALL', 30);
define('ZOOM_MAX_RECORDS_PER_CALL', 300);
/**
* Get course/cm/zoom objects from url parameters, and check for login/permissions.
*
* @return array Array of ($course, $cm, $zoom)
*/
function zoom_get_instance_setup() {
global $DB;
$id = optional_param('id', 0, PARAM_INT); // Course_module ID, or
$n = optional_param('n', 0, PARAM_INT); // ... zoom instance ID - it should be named as the first character of the module.
if ($id) {
$cm = get_coursemodule_from_id('zoom', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$zoom = $DB->get_record('zoom', array('id' => $cm->instance), '*', MUST_EXIST);
} else if ($n) {
$zoom = $DB->get_record('zoom', array('id' => $n), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $zoom->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('zoom', $zoom->id, $course->id, false, MUST_EXIST);
} else {
print_error('You must specify a course_module ID or an instance ID');
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/zoom:view', $context);
return array($course, $cm, $zoom);
}
/**
* Get the user report for display and caching.
*
* @param stdClass $zoom
* @param string $from same format as webservice->get_user_report
* @param string $to
* @return class->sessions[meetingid][starttime]
* ->reqfrom string same as param
* ->reqto string same as param
* ->resfrom array string "from" field of zoom response
*/
function zoom_get_sessions_for_display($zoom, $from, $to) {
$service = new mod_zoom_webservice();
$return = new stdClass();
// If the from or to fields change, report.php will issue a new request.
$return->reqfrom = $from;
$return->reqto = $to;
$hostsessions = array();
if ($zoom->webinar) {
if (!$service->webinar_uuid_list($zoom)) {
zoom_print_error('webinar/uuid/list', $service->lasterror);
}
$uuidlist = $service->lastresponse;
foreach ($uuidlist->webinars as $session) {
// Get participants for this uuid/session. May have multiple pages.
$participants = array();
$pagenumber = 1;
do {
if (!$service->metrics_webinar_detail($session->uuid, ZOOM_MAX_RECORDS_PER_CALL, $pagenumber)) {
zoom_print_error('metrics/webinardetail', $service->lasterror);
}
$result = $service->lastresponse;
$participants = array_merge($participants, $result->participants);
} while ($pagenumber++ < $result->page_count);
// Rename user_name to name to match report API.
foreach ($participants as $participant) {
// For some reason, the Dashboard API replaces ',' with '#' in names.
$participant->name = str_replace('#', ',', $participant->user_name);
}
// Create a 'meeting' like the one from the report API.
$meeting = $result;
$meeting->topic = $zoom->name;
$meeting->participants = $participants;
$hostsessions[$meeting->id][strtotime($meeting->start_time)] = $meeting;
}
// The webinar/uuid/list call doesn't actually use the from/to dates.
$return->resfrom = sscanf($from, '%u-%u-%u');
} else {
// Zoom may return multiple pages of results.
$pagenumber = 1;
do {
if (!$service->get_user_report($zoom->host_id, $from, $to, ZOOM_MAX_RECORDS_PER_CALL, $pagenumber)) {
zoom_print_error('report/getuserreport', $service->lasterror);
}
$result = $service->lastresponse;
foreach ($result->meetings as $meet) {
$starttime = strtotime($meet->start_time);
$hostsessions[$meet->id][$starttime] = $meet;
}
// Rename user_name to name to match report API.
foreach ($hostsessions as $session) {
foreach ($session as $sess) {
foreach ($sess->participants as $participant) {
// For some reason, the Dashboard API replaces ',' with '#' in names.
$participant->name = str_replace('#', ',', $participant->name);
}
}
}
} while ($pagenumber++ < $result->page_count);
// If the time period is longer than a month, Zoom will only return the latest month in range.
// Return the response "from" field to check.
$return->resfrom = sscanf($result->from, '%u-%u-%u');
}
$return->sessions = $hostsessions;
return $return;
}
/**
* Determine if a zoom meeting is in progress, is available, and/or is finished.
*
* @param stdClass $zoom
* @return array Array of booleans: [in progress, available, finished].
*/
function zoom_get_state($zoom) {
$config = get_config('mod_zoom');
$now = time();
$firstavailable = $zoom->start_time - ($config->firstabletojoin * 60);
$lastavailable = $zoom->start_time + $zoom->duration;
$inprogress = ($firstavailable <= $now && $now <= $lastavailable);
$available = $zoom->recurring || $inprogress;
$finished = !$zoom->recurring && $now > $lastavailable;
return array($inprogress, $available, $finished);
}
/**
* Get the Zoom id of the currently logged-in user.
*
* @param boolean $required If true, will error if the user doesn't have a Zoom account.
* @return string
*/
function zoom_get_user_id($required = true) {
global $USER;
$cache = cache::make('mod_zoom', 'zoomid');
if (!($zoomuserid = $cache->get($USER->id))) {
$zoomuserid = false;
$service = new mod_zoom_webservice();
if ($service->user_getbyemail($USER->email)) {
$zoomuserid = $service->lastresponse->id;
} else if ($required) {
zoom_print_error('user/getbyemail', $service->lasterror);
}
$cache->set($USER->id, $zoomuserid);
}
return $zoomuserid;
}
/**
* Check if the error indicates that a meeting is gone.
*
* @param string $error
* @return bool
*/
function zoom_is_meeting_gone_error($error) {
// If the meeting's owner/user cannot be found, we consider the meeting to be gone.
return strpos($error, 'not found') !== false || zoom_is_user_not_found_error($error);
}
/**
* Check if the error indicates that a user is not found.
*
* @param string $error
* @return bool
*/
function zoom_is_user_not_found_error($error) {
return strpos($error, 'User not exist') !== false;
}
/**
* Return the string parameter for zoomerr_meetingnotfound.
*
* @param string $cmid
* @return stdClass
*/
function zoom_meetingnotfound_param($cmid) {
// Provide links to recreate and delete.
$recreate = new moodle_url('/mod/zoom/recreate.php', array('id' => $cmid, 'sesskey' => sesskey()));
$delete = new moodle_url('/course/mod.php', array('delete' => $cmid, 'sesskey' => sesskey()));
// Convert links to strings and pass as error parameter.
$param = new stdClass();
$param->recreate = $recreate->out();
$param->delete = $delete->out();
return $param;
}
/**
* Update local copy of zoom meetings by getting the latest Zoom data through the API.
*
* @param Traversable $zooms Traversable collection of zoom objects, perhaps from a recordset
* (although this function does not close the recordset).
*/
function zoom_update_records(Traversable $zooms) {
global $CFG, $DB;
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/lib/modinfolib.php');
$service = new mod_zoom_webservice();
$coursestoupdate = array();
$calendarfields = array('intro',
'introformat',
'start_time',
'duration',
'recurring');
foreach ($zooms as $z) {
if ($service->get_meeting_info($z)) {
$response = &$service->lastresponse;
// Check for changes.
$changed = false;
foreach ($z as $field => $value) {
// The start_url has a parameter that always changes, so it doesn't really count as a change.
if ($field != 'start_url' && $response->$field != $value) {
$changed = true;
break;
}
}
if ($changed) {
// Save in database.
$response->timemodified = time();
$DB->update_record('zoom', $response);
// If the topic/title was changed, mark this course for cache clearing.
if ($z->name != $response->name) {
$coursestoupdate[$z->course] = 1;
}
// Check if calendar needs updating.
$calendarchanged = false;
foreach ($calendarfields as $field) {
if ($z->$field != $response->$field) {
$calendarchanged = true;
}
}
if ($calendarchanged) {
// Update calendar.
zoom_calendar_item_update($response);
}
}
} else {
$z->status = ZOOM_MEETING_EXPIRED;
$DB->update_record('zoom', $z);
}
}
// Clear caches for meetings whose topic/title changed (and rebuild as needed).
foreach (array_flip($coursestoupdate) as $course) {
rebuild_course_cache($course, true);
}
}