forked from ronefel/moodle-mod_googlemeet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
530 lines (444 loc) · 16.8 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?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/>.
/**
* Private googlemeet module utility functions
*
* @package mod_googlemeet
* @copyright 2020 Rone Santos <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use mod_googlemeet\client;
use mod_googlemeet\helper;
require_once("$CFG->dirroot/mod/googlemeet/lib.php");
/**
* Print googlemeet header.
* @param object $googlemeet
* @param object $cm
* @param object $course
* @return void
*/
function googlemeet_print_header($googlemeet, $cm, $course) {
global $PAGE, $OUTPUT;
$PAGE->set_title($course->shortname . ': ' . $googlemeet->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_activity_record($googlemeet);
echo $OUTPUT->header();
}
/**
* Print googlemeet heading.
* @param object $googlemeet
* @param object $cm
* @param object $course
* @param bool $notused This variable is no longer used.
* @return void
*/
function googlemeet_print_heading($googlemeet, $cm, $course, $notused = false) {
global $OUTPUT;
echo $OUTPUT->heading(format_string($googlemeet->name), 2);
}
/**
* Print googlemeet introduction.
* @param object $googlemeet
* @param object $cm
* @param object $course
* @param bool $ignoresettings print even if not specified in modedit
* @return void
*/
function googlemeet_print_intro($googlemeet, $cm, $course, $ignoresettings = false) {
global $OUTPUT;
$options = empty($googlemeet->displayoptions) ? array() : unserialize($googlemeet->displayoptions);
if ($ignoresettings || !empty($options['printintro'])) {
if (trim(strip_tags($googlemeet->intro))) {
echo $OUTPUT->box_start('mod_introbox', 'googlemeetintro');
echo format_module_intro('googlemeet', $googlemeet, $cm->id);
echo $OUTPUT->box_end();
}
}
}
/**
* Get event data from the form.
*
* @param stdClass $googlemeet moodleform.
* @return array list of events
*/
function googlemeet_construct_events_data_for_add($googlemeet) {
global $CFG;
$eventstarttime = $googlemeet->starthour * HOURSECS + $googlemeet->startminute * MINSECS;
$eventendtime = $googlemeet->endhour * HOURSECS + $googlemeet->endminute * MINSECS;
$eventdate = $googlemeet->eventdate + $eventstarttime;
$duration = $eventendtime - $eventstarttime;
$events = array();
$event = new stdClass();
$event->googlemeetid = $googlemeet->id;
$event->eventdate = $eventdate;
$event->duration = $duration;
$event->timemodified = time();
$events[] = $event;
if (isset($googlemeet->addmultiply)) {
$startdate = $eventdate + DAYSECS;
$enddate = $googlemeet->eventenddate + $eventendtime;
// Getting first day of week.
$sdate = $startdate;
$dayinfo = usergetdate($sdate);
if ($CFG->calendar_startwday === '0') { // Week start from sunday.
$startweek = $sdate - $dayinfo['wday'] * DAYSECS; // Call new variable.
} else {
$wday = $dayinfo['wday'] === 0 ? 7 : $dayinfo['wday'];
$startweek = $sdate - ($wday - 1) * DAYSECS;
}
$wdaydesc = [0 => 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
while ($sdate < $enddate) {
if ($sdate < $startweek + WEEKSECS) {
$dayinfo = usergetdate($sdate);
if (isset($googlemeet->days) && property_exists((object)$googlemeet->days, $wdaydesc[$dayinfo['wday']])) {
$event = new stdClass();
$event->googlemeetid = $googlemeet->id;
$event->eventdate = make_timestamp(
$dayinfo['year'],
$dayinfo['mon'],
$dayinfo['mday'],
$googlemeet->starthour,
$googlemeet->startminute
);
$event->duration = $duration;
$event->timemodified = time();
$events[] = $event;
}
$sdate += DAYSECS;
} else {
$startweek += WEEKSECS * $googlemeet->period;
$sdate = $startweek;
}
}
}
return $events;
}
/**
* This excludes all Google Meet events.
* @param int $googlemeetid
* @return void
*/
function googlemeet_delete_events($googlemeetid) {
global $DB;
$events = $DB->get_records('googlemeet_events', ['googlemeetid' => $googlemeetid]);
foreach ($events as $event) {
$DB->delete_records('googlemeet_notify_done', ['eventid' => $event->id]);
}
$DB->delete_records('googlemeet_events', ['googlemeetid' => $googlemeetid]);
// Delete Calendar Events.
$DB->delete_records('event', [
'modulename' => 'googlemeet',
'instance' => $googlemeetid,
'eventtype' => helper::GOOGLEMEET_EVENT_START
]);
}
/**
* This creates new events given as timeopen and timeclose by $googlemeet.
*
* @param stdClass $googlemeet moodleform
* @param array $events list of events
* @return void
*/
function googlemeet_set_events($googlemeet, $events) {
global $DB;
googlemeet_delete_events($events[0]->googlemeetid);
$DB->insert_records('googlemeet_events', $events);
foreach ($events as $event) {
helper::create_calendar_event($googlemeet, $event);
}
}
/**
* This creates new events given as timeopen and timeclose by googlemeet.
*
* @param object $googlemeet
* @param object $cm
* @param object $context
* @return void
*/
function googlemeet_print_recordings($googlemeet, $cm, $context) {
global $CFG, $PAGE, $OUTPUT;
$config = get_config('googlemeet');
$client = new client();
if (!$client->enabled) {
return;
}
$params = ['googlemeetid' => $googlemeet->id];
$hascapability = has_capability('mod/googlemeet:editrecording', $context);
if (!$hascapability) {
$params['visible'] = true;
}
$html = '<div id="googlemeet_recordings" class="googlemeet_recordings">';
$recordings = googlemeet_list_recordings($params);
$html .= $OUTPUT->render_from_template('mod_googlemeet/recordingstable', [
'recordings' => $recordings,
'coursemoduleid' => $cm->id,
'hascapability' => $hascapability
]);
$PAGE->requires->js(new moodle_url($CFG->wwwroot . '/mod/googlemeet/assets/js/build/jstable.min.js'));
if ($hascapability) {
$lastsync = get_string('never', 'googlemeet');
if ($googlemeet->lastsync) {
$lastsync = userdate($googlemeet->lastsync, get_string('timedate', 'googlemeet'));
}
$redordingname = '"' . substr($googlemeet->url, 24, 12) . '" ';
if ($googlemeet->originalname) {
$redordingname .= get_string('or', 'googlemeet') . ' "' . $googlemeet->originalname . '"';
}
$loginhtml = '';
$syncbutton = '';
$islogged = false;
$isloggedcreatoremail = $client->get_email() === $googlemeet->creatoremail;
if (!$client->check_login()) {
$loginhtml = $client->print_login_popup();
} else {
$islogged = true;
$loginhtml = $client->print_user_info('drive');
$url = new moodle_url($PAGE->url);
$url->param('sync', true);
$syncbutton = new single_button($url, get_string('syncwithgoogledrive', 'googlemeet'), 'post', true);
$syncbutton = $OUTPUT->render($syncbutton);
}
$html .= $OUTPUT->render_from_template('mod_googlemeet/syncbutton', [
'lastsync' => $lastsync,
'creatoremail' => $googlemeet->creatoremail,
'redordingname' => $redordingname,
'login' => $loginhtml,
'islogged' => $islogged,
'syncbutton' => $syncbutton,
'isloggedcreatoremail' => $isloggedcreatoremail
]);
}
$html .= '</div>';
echo $html;
}
/**
* This clears the url.
*
* @param string $url
* @return mixed The url if valid or false if invalid
*/
function googlemeet_clear_url($url) {
$pattern = "/meet.google.com\/[a-zA-Z0-9]{3}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{3}/";
preg_match($pattern, $url, $matches, PREG_OFFSET_CAPTURE);
if ($matches) {
return 'https://' . $matches[0][0];
}
return null;
}
/**
* This checks if have recordings from the googlemeet.
*
* @param int $googlemeetid
* @return boolean
*/
function googlemeet_has_recording($googlemeetid) {
global $DB;
$recordings = $DB->get_records('googlemeet_recordings', ['googlemeetid' => $googlemeetid]);
return $recordings ? true : false;
}
/**
* Generates a list of users who have not yet been notified.
*
* @param int $eventid the event ID
* @return stdClass list of users
*/
function googlemeet_get_users_to_notify($eventid) {
global $DB;
$sql = "SELECT DISTINCT
u.*
FROM {googlemeet_events} me
INNER JOIN {googlemeet} m
ON m.id = me.googlemeetid
INNER JOIN {course_modules} cm
ON (cm.instance = m.id AND cm.visible = 1 AND cm.deletioninprogress = 0)
INNER JOIN {course} c
ON (c.id = cm.course AND c.visible = 1)
INNER JOIN {modules} md
ON (md.id = cm.module AND md.name = 'googlemeet')
INNER JOIN {context} ctx
ON ctx.instanceid = c.id
INNER JOIN {role_assignments} ra
ON (ra.contextid = ctx.id AND ra.roleid = 5)
INNER JOIN {user} u
ON u.id = ra.userid
WHERE me.id = {$eventid}
AND (SELECT count(*) = 0
FROM {googlemeet_notify_done} nd
WHERE nd.eventid = me.id AND nd.userid = u.id)";
return $DB->get_records_sql($sql);
}
/**
* Returns a list of future events
*/
function googlemeet_get_future_events() {
global $DB;
$now = time();
$sql = "SELECT DISTINCT
me.id,
me.eventdate,
me.duration,
m.id AS googlemeetid,
m.name AS googlemeetname,
m.url,
cm.id AS cmid,
c.id AS courseid,
c.fullname AS coursename
FROM {googlemeet_events} me
INNER JOIN {googlemeet} m
ON m.id = me.googlemeetid
INNER JOIN {course_modules} cm
ON (cm.instance = m.id AND cm.visible = 1 AND cm.deletioninprogress = 0)
INNER JOIN {course} c
ON (c.id = cm.course AND c.visible = 1)
INNER JOIN {modules} md
ON (md.id = cm.module AND md.name = 'googlemeet')
WHERE {$now} BETWEEN me.eventdate - m.minutesbefore * 60 AND me.eventdate
AND m.notify = 1";
return $DB->get_records_sql($sql);
}
/**
* Send a notification to students in the class about the event.
*
* @param object $user
* @param object $event
* @return void
*/
function googlemeet_send_notification($user, $event) {
global $CFG;
$startdate = userdate($event->eventdate, get_string('strftimedmy', 'googlemeet'), $user->timezone);
$starttime = userdate($event->eventdate, get_string('strftimehm', 'googlemeet'), $user->timezone);
$endtime = userdate($event->eventdate + $event->duration, get_string('strftimehm', 'googlemeet'), $user->timezone);
$usertimezone = usertimezone($user->timezone);
$notificationstr = get_string('notification', 'googlemeet');
$subject = "{$notificationstr}: {$event->googlemeetname} - {$startdate} {$starttime} - {$endtime} ($usertimezone)";
$url = $CFG->wwwroot . '/mod/googlemeet/view.php?id=' . $event->cmid;
$message = new \core\message\message();
$message->component = 'mod_googlemeet';
$message->name = 'notification';
$message->userfrom = core_user::get_noreply_user();
$message->userto = $user;
$message->subject = $subject;
$message->fullmessage = googlemeet_get_messagehtml($user, $event);
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = googlemeet_get_messagehtml($user, $event);
$message->smallmessage = $subject;
$message->notification = 1;
$message->contexturl = $url;
$message->contexturlname = $event->googlemeetname;
$message->courseid = $event->courseid;
message_send($message);
}
/**
* Records the sending of the notification to not send repeated.
*
* @param int $userid
* @param int $eventid
*/
function googlemeet_notify_done($userid, $eventid) {
global $DB;
$notifydone = new stdClass();
$notifydone->userid = $userid;
$notifydone->eventid = $eventid;
$notifydone->timesent = time();
return $DB->insert_record('googlemeet_notify_done', $notifydone);
}
/**
* Removes records of past event notification notifications.
*/
function googlemeet_remove_notify_done_from_old_events() {
global $DB;
$now = time();
$sql = "SELECT id
FROM {googlemeet_events}
WHERE eventdate < {$now}";
$oldevents = $DB->get_records_sql($sql);
foreach ($oldevents as $oldevent) {
$DB->delete_records('googlemeet_notify_done', ['eventid' => $oldevent->id]);
}
}
/**
* Mount the body content of the notification.
*
* @param object $user db record of user
* @param object $event db record of event
* @return string - the content of the notification after assembly.
*/
function googlemeet_get_messagehtml($user, $event) {
global $CFG;
$config = get_config('googlemeet');
$startdate = userdate($event->eventdate, get_string('strftimedmy', 'googlemeet'), $user->timezone);
$starttime = userdate($event->eventdate, get_string('strftimehm', 'googlemeet'), $user->timezone);
$endtime = userdate($event->eventdate + $event->duration, get_string('strftimehm', 'googlemeet'), $user->timezone);
$url = "<a href=\"{$CFG->wwwroot}/mod/googlemeet/view.php?id={$event->cmid}\">
{$CFG->wwwroot}/mod/googlemeet/view.php?id={$event->cmid}</a>";
$templatevars = [
'/%userfirstname%/' => $user->firstname,
'/%userlastname%/' => $user->lastname,
'/%coursename%/' => $event->coursename,
'/%googlemeetname%/' => $event->googlemeetname,
'/%eventdate%/' => $startdate,
'/%duration%/' => $starttime . ' – ' . $endtime,
'/%timezone%/' => usertimezone($user->timezone),
'/%url%/' => $url,
'/%cmid%/' => $event->cmid,
];
$patterns = array_keys($templatevars); // The placeholders which are to be replaced.
$replacements = array_values($templatevars); // The values which are to be templated in for the placeholders.
// Replace %variable% with relevant value everywhere it occurs.
$emailcontent = preg_replace($patterns, $replacements, $config->emailcontent);
return $emailcontent;
}
/**
* upcoming googlemeet events.
*
* @param int $googlemeetid db record of user
*/
function googlemeet_get_upcoming_events($googlemeetid) {
global $DB, $OUTPUT, $USER;
$now = time() - MINSECS;
$sql = "SELECT id,eventdate,duration
FROM {googlemeet_events}
WHERE googlemeetid = {$googlemeetid}
AND (eventdate > {$now} OR eventdate = {$now})
LIMIT 5";
$events = $DB->get_records_sql($sql);
$upcomingevents = [];
if ($events) {
foreach ($events as $event) {
$start = $event->eventdate;
$end = $event->eventdate + $event->duration;
$duration = $event->duration;
$datetime = new DateTime();
$datetime->setTimestamp(time());
$nowdate = $datetime->format('Y-m-d');
$datetime->setTimestamp($start);
$startdate = $datetime->format('Y-m-d');
$upcomingevent = new stdClass();
$upcomingevent->today = $nowdate === $startdate;
$upcomingevent->startdate = userdate($start, get_string('strftimedm', 'googlemeet'), $USER->timezone);
array_push($upcomingevents, $upcomingevent);
}
return [
'hasupcomingevents' => true,
'upcomingevents' => $upcomingevents,
'starttime' => userdate($start, get_string('strftimehm', 'googlemeet'), $USER->timezone),
'endtime' => userdate($end, get_string('strftimehm', 'googlemeet'), $USER->timezone),
'duration' => $duration,
];
}
return false;
}