forked from uofr/moodle-mod_zoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
executable file
·328 lines (286 loc) · 11.1 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
327
328
<?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 types.
define('ZOOM_INSTANT_MEETING', 1);
define('ZOOM_SCHEDULED_MEETING', 2);
define('ZOOM_RECURRING_MEETING', 3);
define('ZOOM_SCHEDULED_WEBINAR', 5);
define('ZOOM_RECURRING_WEBINAR', 6);
// 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);
// User types. Numerical values from Zoom API.
define('ZOOM_USER_TYPE_BASIC', 1);
define('ZOOM_USER_TYPE_PRO', 2);
define('ZOOM_USER_TYPE_CORP', 3);
/**
* Entry not found on Zoom.
*/
class zoom_not_found_exception extends moodle_exception {
// Web service response.
public $response = null;
/**
* @param string $response Web service response
*/
public function __construct($response) {
$this->response = $response;
parent::__construct('errorwebservice_notfound', 'mod_zoom', '', $response);
}
}
/**
* Couldn't succeed within the allowed number of retries.
*/
class zoom_api_retry_failed_exception extends moodle_exception {
// Web service response.
public $response = null;
/**
* @param string $response Web service response
*/
public function __construct($response) {
$this->response = $response;
$a = new stdClass();
$a->response = $response;
$a->maxretries = mod_zoom_webservice::MAX_RETRIES;
parent::__construct('zoomerr_maxretries', 'mod_zoom', '', $a);
}
}
/**
* Terminate the current script with a fatal error.
*
* Adapted from core_renderer's fatal_error() method. Needed because throwing errors with HTML links in them will convert links
* to text using htmlentities. See MDL-66161 - Reflected XSS possible from some fatal error messages.
*
* So need custom error handler for fatal Zoom errors that have links to help people.
*
* @param string $errorcode The name of the string from error.php to print
* @param string $module name of module
* @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the
* site index page.
* @param mixed $a Extra words and phrases that might be required in the error string
*/
function zoom_fatal_error($errorcode, $module='', $continuelink='', $a=null) {
global $CFG, $COURSE, $OUTPUT, $PAGE;
$output = '';
$obbuffer = '';
// Assumes that function is run before output is generated.
if ($OUTPUT->has_started()) {
// If not then have to default to standard error.
throw new moodle_exception($errorcode, $module, $continuelink, $a);
}
$PAGE->set_heading($COURSE->fullname);
$output .= $OUTPUT->header();
// Output message without messing with HTML content of error.
$message = '<p class="errormessage">' . get_string($errorcode, $module, $a) . '</p>';
$output .= $OUTPUT->box($message, 'errorbox alert alert-danger', null, array('data-rel' => 'fatalerror'));
if ($CFG->debugdeveloper) {
if (!empty($debuginfo)) {
$debuginfo = s($debuginfo); // Removes all nasty JS.
$debuginfo = str_replace("\n", '<br />', $debuginfo); // Keep newlines.
$output .= $OUTPUT->notification('<strong>Debug info:</strong> '.$debuginfo, 'notifytiny');
}
if (!empty($backtrace)) {
$output .= $OUTPUT->notification('<strong>Stack trace:</strong> '.format_backtrace($backtrace), 'notifytiny');
}
if ($obbuffer !== '' ) {
$output .= $OUTPUT->notification('<strong>Output buffer:</strong> '.s($obbuffer), 'notifytiny');
}
}
if (!empty($continuelink)) {
$output .= $OUTPUT->continue_button($continuelink);
}
$output .= $OUTPUT->footer();
// Padding to encourage IE to display our error page, rather than its own.
$output .= str_repeat(' ', 512);
echo $output;
exit(1); // General error code.
}
/**
* 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(get_string('zoomerr_id_missing', 'zoom'));
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/zoom:view', $context);
return array($course, $cm, $zoom);
}
/**
* Retrieves information for a meeting.
*
* @param int $meetingid
* @param bool $webinar
* @param string $hostid the host's uuid
* @return array information about the meeting
*/
function zoom_get_sessions_for_display($meetingid, $webinar, $hostid) {
require_once(__DIR__.'/../../lib/moodlelib.php');
global $DB;
$service = new mod_zoom_webservice();
$sessions = array();
$format = get_string('strftimedatetimeshort', 'langconfig');
$instances = $DB->get_records('zoom_meeting_details', array('meeting_id' => $meetingid));
foreach ($instances as $instance) {
// The meeting uuid, not the participant's uuid.
$uuid = $instance->uuid;
$participantlist = zoom_get_participants_report($instance->id);
$sessions[$uuid]['participants'] = $participantlist;
$sessions[$uuid]['count'] = count($participantlist);
$sessions[$uuid]['topic'] = $instance->topic;
$sessions[$uuid]['duration'] = $instance->duration;
$sessions[$uuid]['starttime'] = userdate($instance->start_time, $format);
$sessions[$uuid]['endtime'] = userdate($instance->start_time + $instance->duration * 60, $format);
}
return $sessions;
}
/**
* 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();
try {
$zoomuser = $service->get_user($USER->email);
if ($zoomuser !== false) {
$zoomuserid = $zoomuser->id;
}
} catch (moodle_exception $error) {
if ($required) {
throw $error;
} else {
$zoomuserid = $zoomuser->id;
}
}
$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 or does not belong to the current account.
*
* @param string $error
* @return bool
*/
function zoom_is_user_not_found_error($error) {
return strpos($error, 'not exist') !== false || strpos($error, 'not belong to this account') !== false
|| strpos($error, 'not found on this account') !== 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;
}
/**
* Get the data of each user for the participants report.
* @param string $detailsid The meeting ID that you want to get the participants report for.
* @return array The user data as an array of records (array of arrays).
*/
function zoom_get_participants_report($detailsid) {
global $DB;
$service = new mod_zoom_webservice();
$sql = 'SELECT zmp.id,
zmp.name,
zmp.userid,
zmp.user_email,
zmp.join_time,
zmp.leave_time,
zmp.duration,
zmp.uuid
FROM {zoom_meeting_participants} zmp
WHERE zmp.detailsid = :detailsid
';
$params = [
'detailsid' => $detailsid
];
$participants = $DB->get_records_sql($sql, $params);
return $participants;
}