Completed
Pull Request — master (#56)
by Jesus
47:58 queued 04:51
created

lib.php ➔ bigbluebuttonbn_check_updates_since()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * Library calls for Moodle and BigBlueButton.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2010 onwards, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
24
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
25
 */
26
27
defined('MOODLE_INTERNAL') || die;
28
29
global $CFG;
30
31
require_once($CFG->dirroot.'/calendar/lib.php');
32
require_once($CFG->dirroot.'/message/lib.php');
33
require_once($CFG->dirroot.'/mod/lti/OAuth.php');
34
require_once($CFG->dirroot.'/tag/lib.php');
35
require_once($CFG->libdir.'/accesslib.php');
36
require_once($CFG->libdir.'/completionlib.php');
37
require_once($CFG->libdir.'/datalib.php');
38
require_once($CFG->libdir.'/coursecatlib.php');
39
require_once($CFG->libdir.'/enrollib.php');
40
require_once($CFG->libdir.'/filelib.php');
41
require_once($CFG->libdir.'/formslib.php');
42
43
44
if (file_exists(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php')) {
45
    require_once(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php');
46
}
47
48
if (!isset($CFG->bigbluebuttonbn)) {
49
    $CFG->bigbluebuttonbn = array();
50
}
51
52
if (file_exists(dirname(__FILE__).'/config.php')) {
53
    require_once(dirname(__FILE__).'/config.php');
54
}
55
56
/*
57
 * DURATIONCOMPENSATION: Feature removed by configuration
58
 */
59
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
60
/*
61
 * Remove this block when restored
62
 */
63
64
 /** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
65
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
66
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
67
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
68
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string of event create for bigbluebuttonbn_logs */
69
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
70
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string of event join for bigbluebuttonbn_logs */
71
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
72
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string of event logout for bigbluebuttonbn_logs */
73
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
74
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string of event import for bigbluebuttonbn_logs */
75
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
76
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string of event delete for bigbluebuttonbn_logs */
77
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
78
79
/**
80
 * Indicates API features that the forum supports.
81
 *
82
 * @uses FEATURE_IDNUMBER
83
 * @uses FEATURE_GROUPS
84
 * @uses FEATURE_GROUPINGS
85
 * @uses FEATURE_GROUPMEMBERSONLY
86
 * @uses FEATURE_MOD_INTRO
87
 * @uses FEATURE_BACKUP_MOODLE2
88
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
89
 * @uses FEATURE_COMPLETION_HAS_RULES
90
 * @uses FEATURE_GRADE_HAS_GRADE
91
 * @uses FEATURE_GRADE_OUTCOMES
92
 * @uses FEATURE_SHOW_DESCRIPTION
93
 * @param string $feature
94
 * @return mixed True if yes (some features may use other values)
95
 */
96
function bigbluebuttonbn_supports($feature) {
97
    if (!$feature) {
98
        return null;
99
    }
100
    $features = array(
101
        (string) FEATURE_IDNUMBER => true,
102
        (string) FEATURE_GROUPS => true,
103
        (string) FEATURE_GROUPINGS => true,
104
        (string) FEATURE_GROUPMEMBERSONLY => true,
105
        (string) FEATURE_MOD_INTRO => true,
106
        (string) FEATURE_BACKUP_MOODLE2 => true,
107
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
108
        (string) FEATURE_GRADE_HAS_GRADE => false,
109
        (string) FEATURE_GRADE_OUTCOMES => false,
110
        (string) FEATURE_SHOW_DESCRIPTION => true,
111
    );
112
    if (isset($features[(string) $feature])) {
113
        return $features[$feature];
114
    }
115
    return null;
116
}
117
118
/**
119
 * Given an object containing all the necessary data,
120
 * (defined by the form in mod_form.php) this function
121
 * will create a new instance and return the id number
122
 * of the new instance.
123
 *
124
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
125
 * @return int The id of the newly inserted bigbluebuttonbn record
126
 */
127
function bigbluebuttonbn_add_instance($bigbluebuttonbn) {
128
    global $DB;
129
    // Excecute preprocess.
130
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
131
    // Pre-set initial values.
132
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
133
    // Insert a record.
134
    $bigbluebuttonbn->id = $DB->insert_record('bigbluebuttonbn', $bigbluebuttonbn);
135
    // Encode meetingid.
136
    $meetingid = bigbluebuttonbn_encode_meetingid($bigbluebuttonbn->id);
137
    // Set the meetingid column in the bigbluebuttonbn table.
138
    $DB->set_field('bigbluebuttonbn', 'meetingid', $meetingid, array('id' => $bigbluebuttonbn->id));
139
    // Complete the process.
140
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
141
    return $bigbluebuttonbn->id;
142
}
143
144
/**
145
 * Given an object containing all the necessary data,
146
 * (defined by the form in mod_form.php) this function
147
 * will update an existing instance with new data.
148
 *
149
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
150
 * @return bool Success/Fail
151
 */
152
function bigbluebuttonbn_update_instance($bigbluebuttonbn) {
153
    global $DB;
154
    // Excecute preprocess.
155
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
156
    // Pre-set initial values.
157
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
158
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
159
    // Update a record.
160
    $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
161
    // Complete the process.
162
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
163
    return true;
164
}
165
166
/**
167
 * Given an ID of an instance of this module,
168
 * this function will permanently delete the instance
169
 * and any data that depends on it.
170
 *
171
 * @param int $id Id of the module instance
172
 *
173
 * @return bool Success/Failure
174
 */
175
function bigbluebuttonbn_delete_instance($id) {
176
    global $DB;
177
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
178
    if (!$bigbluebuttonbn) {
179
        return false;
180
    }
181
    // TODO: End the meeting if it is running.
182
183
    // Perform delete.
184
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
185
        return false;
186
    }
187
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
188
        return false;
189
    }
190
    // Log action performed.
191
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
192
}
193
194
/**
195
 * Given an ID of an instance of this module,
196
 * this function will permanently delete the data that depends on it.
197
 *
198
 * @param object $bigbluebuttonbn Id of the module instance
199
 *
200
 * @return bool Success/Failure
201
 */
202
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
203
    global $DB, $USER;
204
    $log = new stdClass();
205
    $log->meetingid = $bigbluebuttonbn->meetingid;
206
    $log->courseid = $bigbluebuttonbn->course;
207
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
208
    $log->userid = $USER->id;
209
    $log->timecreated = time();
210
    $log->log = BIGBLUEBUTTONBN_LOG_EVENT_DELETE;
211
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
212
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
213
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
214
    $log->meta = "{\"has_recordings\":false}";
215
    if (!empty($logs)) {
216
        $log->meta = "{\"has_recordings\":true}";
217
    }
218
    if (!$DB->insert_record('bigbluebuttonbn_logs', $log)) {
219
        return false;
220
    }
221
    return true;
222
}
223
224
/**
225
 * Return a small object with summary information about what a
226
 * user has done with a given particular instance of this module
227
 * Used for user activity reports.
228
 * $return->time = the time they did it
229
 * $return->info = a short text description.
230
 *
231
 * @param object $course
232
 * @param object $user
233
 * @param object $mod
234
 * @param object $bigbluebuttonbn
235
 *
236
 * @return bool
237
 */
238
function bigbluebuttonbn_user_outline($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $mod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
239
    global $DB;
240
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
241
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ), '*');
242
    if ($completed > 0) {
243
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
244
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
245
            get_string('view_message_times', 'bigbluebuttonbn');
246
    }
247
    return '';
248
}
249
250
/**
251
 * Print a detailed representation of what a user has done with
252
 * a given particular instance of this module, for user activity reports.
253
 *
254
 * @param object $course
255
 * @param object $user
256
 * @param object $mod
257
 * @param object $bigbluebuttonbn
258
 *
259
 * @return bool
260
 */
261
function bigbluebuttonbn_user_complete($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $mod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
262
    global $DB;
263
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
264
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ),
265
        '*', IGNORE_MULTIPLE);
266
    return $completed > 0;
267
}
268
269
/**
270
 * Returns all other caps used in module.
271
 *
272
 * @return string[]
273
 */
274
function bigbluebuttonbn_get_extra_capabilities() {
275
    return array('moodle/site:accessallgroups');
276
}
277
278
/**
279
 * This function is used by the reset_course_userdata function in moodlelib.
280
 * @param $data the data submitted from the reset course.
281
 * @return array status array
282
 */
283
function bigbluebuttonbn_reset_userdata($data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
285
    // See MDL-9367.
286
    return array();
287
}
288
289
/**
290
 * List of view style log actions.
291
 *
292
 * @return string[]
293
 */
294
function bigbluebuttonbn_get_view_actions() {
295
    return array('view', 'view all');
296
}
297
298
/**
299
 * List of update style log actions.
300
 *
301
 * @return string[]
302
 */
303
function bigbluebuttonbn_get_post_actions() {
304
    return array('update', 'add', 'delete');
305
}
306
307
/**
308
 * Print an overview of all bigbluebuttonbn instances for the courses.
309
 *
310
 * @param array $courses
311
 * @param array $htmlarray Passed by reference
312
 *
313
 * @return void
314
 */
315
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
316
    if (empty($courses) || !is_array($courses)) {
317
        return array();
318
    }
319
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
320
    foreach ($bns as $bn) {
321
        $now = time();
322
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
323
            // A bigbluebuttonbn is scheduled.
324
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
325
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
326
            }
327
            $htmlarray[$bn->course]['bigbluebuttonbn'] = bigbluebuttonbn_print_overview_element($bn, $now);
328
        }
329
    }
330
}
331
332
/**
333
 * Print an overview of a bigbluebuttonbn instance.
334
 *
335
 * @param array $bigbluebuttonbn
336
 * @param int $now
337
 *
338
 * @return string
339
 */
340
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
341
    global $CFG;
342
    $start = 'started_at';
343
    if ($bigbluebuttonbn->openingtime > $now) {
344
        $start = 'starts_at';
345
    }
346
    $classes = '';
347
    if ($bigbluebuttonbn->visible) {
348
        $classes = 'class="dimmed" ';
349
    }
350
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
351
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
352
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
353
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
354
    $str .= '  </div>'."\n";
355
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
356
        '</div>'."\n";
357
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
358
      .'</div>'."\n";
359
    $str .= '</div>'."\n";
360
    return $str;
361
}
362
363
/**
364
 * Given a course_module object, this function returns any
365
 * "extra" information that may be needed when printing
366
 * this activity in a course listing.
367
 * See get_array_of_activities() in course/lib.php.
368
 *
369
 * @param object $coursemodule
370
 *
371
 * @return null|cached_cm_info
372
 */
373
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
374
    global $DB;
375
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance),
376
        'id, name, intro, introformat');
377
    if (!$bigbluebuttonbn) {
378
        return null;
379
    }
380
    $info = new cached_cm_info();
381
    $info->name = $bigbluebuttonbn->name;
382
    if ($coursemodule->showdescription) {
383
        // Convert intro to html. Do not filter cached version, filters run at display time.
384
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
385
    }
386
    return $info;
387
}
388
389
/**
390
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
391
 *
392
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
393
 *
394
 * @return void
395
 **/
396
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
397
    bigbluebuttonbn_process_pre_save_instance($bigbluebuttonbn);
398
    bigbluebuttonbn_process_pre_save_checkboxes($bigbluebuttonbn);
399
    bigbluebuttonbn_process_pre_save_common($bigbluebuttonbn);
400
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
401
}
402
403
/**
404
 * Runs process for defining the instance (insert/update).
405
 *
406
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
407
 *
408
 * @return void
409
 **/
410
function bigbluebuttonbn_process_pre_save_instance(&$bigbluebuttonbn) {
411
    $bigbluebuttonbn->timemodified = time();
412
    if ((integer)$bigbluebuttonbn->instance == 0) {
413
        $bigbluebuttonbn->timecreated = time();
414
        $bigbluebuttonbn->timemodified = 0;
415
        // As it is a new activity, assign passwords.
416
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
417
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12, $bigbluebuttonbn->moderatorpass);
418
    }
419
}
420
421
/**
422
 * Runs process for assigning default value to checkboxes.
423
 *
424
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
425
 *
426
 * @return void
427
 **/
428
function bigbluebuttonbn_process_pre_save_checkboxes(&$bigbluebuttonbn) {
429
    if (!isset($bigbluebuttonbn->wait)) {
430
        $bigbluebuttonbn->wait = 0;
431
    }
432
    if (!isset($bigbluebuttonbn->record)) {
433
        $bigbluebuttonbn->record = 0;
434
    }
435
    if (!isset($bigbluebuttonbn->recordings_html)) {
436
        $bigbluebuttonbn->recordings_html = 0;
437
    }
438
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
439
        $bigbluebuttonbn->recordings_deleted = 0;
440
    }
441
    if (!isset($bigbluebuttonbn->recordings_imported)) {
442
        $bigbluebuttonbn->recordings_imported = 0;
443
    }
444
    if (!isset($bigbluebuttonbn->recordings_preview)) {
445
        $bigbluebuttonbn->recordings_preview = 0;
446
    }
447
}
448
449
/**
450
 * Runs process for wipping common settings when 'recordings only'.
451
 *
452
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
453
 *
454
 * @return void
455
 **/
456
function bigbluebuttonbn_process_pre_save_common(&$bigbluebuttonbn) {
457
    // Make sure common settings are removed when 'recordings only'.
458
    if ($bigbluebuttonbn->type == BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY) {
459
        $bigbluebuttonbn->groupmode = 0;
460
        $bigbluebuttonbn->groupingid = 0;
461
    }
462
}
463
464
/**
465
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
466
 *
467
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
468
 *
469
 * @return void
470
 **/
471
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
472
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
473
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
474
    }
475
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
476
    bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn);
477
}
478
479
/**
480
 * Generates a message on insert/update which is sent to all users enrolled.
481
 *
482
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
483
 *
484
 * @return void
485
 **/
486
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
487
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
488
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
489
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
490
    }
491
    $context = context_course::instance($bigbluebuttonbn->course);
492
    \mod_bigbluebuttonbn\locallib\notifier::notification_process($context, $bigbluebuttonbn, $action);
493
}
494
495
/**
496
 * Generates an event after a bigbluebuttonbn insert/update.
497
 *
498
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
499
 *
500
 * @return void
501
 **/
502
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
503
    global $DB;
504
    $eventid = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
505
        'instance' => $bigbluebuttonbn->id));
506
    // Delete the event from calendar when/if openingtime is NOT set.
507
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
508
        if ($eventid) {
509
            $calendarevent = calendar_event::load($eventid);
510
            $calendarevent->delete();
511
        }
512
        return;
513
    }
514
    // Add evento to the calendar as openingtime is set.
515
    $event = new stdClass();
516
    $event->eventtype = BIGBLUEBUTTON_EVENT_MEETING_START;
517
    $event->type = CALENDAR_EVENT_TYPE_ACTION;
518
    $event->name = $bigbluebuttonbn->name . ' (' . get_string('starts_at', 'bigbluebuttonbn') . ')';
519
    $event->description = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $bigbluebuttonbn->coursemodule);
520
    $event->courseid = $bigbluebuttonbn->course;
521
    $event->groupid = 0;
522
    $event->userid = 0;
523
    $event->modulename = 'bigbluebuttonbn';
524
    $event->instance = $bigbluebuttonbn->id;
525
    $event->timestart = $bigbluebuttonbn->openingtime;
526
    $event->timeduration = 0;
527
    if ($bigbluebuttonbn->closingtime) {
528
        $event->timeduration = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
529
    }
530
    $event->timesort = $event->timestart + $event->timeduration;
531
    $event->visible = instance_is_visible('bigbluebuttonbn', $bigbluebuttonbn);
532
    $event->priority = null;
533
    // Update the event in calendar when/if eventid was found.
534
    if ($eventid) {
535
        $event->id = $eventid;
536
        $calendarevent = calendar_event::load($eventid);
537
        $calendarevent->update($event);
538
        return;
539
    }
540
    calendar_event::create($event);
541
}
542
543
function bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn) {
544
    if (!empty($bigbluebuttonbn->completionexpected)) {
545
        \core_completion\api::update_completion_date_event(
546
            $bigbluebuttonbn->coursemodule,
547
            'bigbluebuttonbn',
548
            $bigbluebuttonbn->id, $bigbluebuttonbn->completionexpected
549
          );
550
    }
551
}
552
/**
553
 * Get a full path to the file attached as a preuploaded presentation
554
 * or if there is none, set the presentation field will be set to blank.
555
 *
556
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
557
 *
558
 * @return string
559
 */
560
function bigbluebuttonbn_get_media_file(&$bigbluebuttonbn) {
561
    if (!isset($bigbluebuttonbn->presentation) || $bigbluebuttonbn->presentation == '') {
562
        return '';
563
    }
564
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
565
    // Set the filestorage object.
566
    $fs = get_file_storage();
567
    // Save the file if it exists that is currently in the draft area.
568
    file_save_draft_area_files($bigbluebuttonbn->presentation, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
569
    // Get the file if it exists.
570
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
571
        'itemid, filepath, filename', false);
572
    // Check that there is a file to process.
573
    $filesrc = '';
574
    if (count($files) == 1) {
575
        // Get the first (and only) file.
576
        $file = reset($files);
577
        $filesrc = '/'.$file->get_filename();
578
    }
579
    return $filesrc;
580
}
581
582
/**
583
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
584
 *
585
 * @category files
586
 *
587
 * @param stdClass $course        course object
588
 * @param stdClass $cm            course module object
589
 * @param stdClass $context       context object
590
 * @param string   $filearea      file area
591
 * @param array    $args          extra arguments
592
 * @param bool     $forcedownload whether or not force download
593
 * @param array    $options       additional options affecting the file serving
594
 *
595
 * @return false|null false if file not found, does not return if found - justsend the file
596
 */
597
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
598
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
599
        return false;
600
    }
601
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
602
    if (empty($file)) {
603
        return false;
604
    }
605
    // Finally send the file.
606
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
607
}
608
609
/**
610
 * Helper for validating pluginfile.
611
 * @param stdClass $context       context object
612
 * @param string   $filearea      file area
613
 *
614
 * @return false|null false if file not valid
615
 */
616
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
617
    if ($context->contextlevel != CONTEXT_MODULE) {
618
        return false;
619
    }
620
    if ($filearea !== 'presentation') {
621
        return false;
622
    }
623
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
624
        return false;
625
    }
626
    return true;
627
}
628
629
/**
630
 * Helper for getting pluginfile.
631
 *
632
 * @param stdClass $course        course object
633
 * @param stdClass $cm            course module object
634
 * @param stdClass $context       context object
635
 * @param string   $filearea      file area
636
 * @param array    $args          extra arguments
637
 *
638
 * @return object
639
 */
640
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
641
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
642
    if (!$filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
643
        return false;
644
    }
645
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
646
    $fs = get_file_storage();
647
    $file = $fs->get_file_by_hash(sha1($fullpath));
648
    if (!$file || $file->is_directory()) {
649
        return false;
650
    }
651
    return $file;
652
}
653
654
/**
655
 * Helper for getting pluginfile name.
656
 *
657
 * @param stdClass $course        course object
658
 * @param stdClass $cm            course module object
659
 * @param stdClass $context       context object
660
 * @param array    $args          extra arguments
661
 *
662
 * @return array
663
 */
664
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
665
    global $DB;
666
    if (count($args) > 1) {
667
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
668
            return;
669
        }
670
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
671
        $noncekey = sha1($bigbluebuttonbn->id);
672
        $presentationnonce = $cache->get($noncekey);
673
        $noncevalue = $presentationnonce['value'];
674
        $noncecounter = $presentationnonce['counter'];
675
        if ($args['0'] != $noncevalue) {
676
            return;
677
        }
678
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
679
        $noncecounter += 1;
680
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
681
        if ($noncecounter == 2) {
682
            $cache->delete($noncekey);
683
        }
684
        return $args['1'];
685
    }
686
    require_course_login($course, true, $cm);
687
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
688
        return;
689
    }
690
    return implode('/', $args);
691
}
692
693
/**
694
 * Returns an array of file areas.
695
 *
696
 * @category files
697
 *
698
 * @return array a list of available file areas
699
 */
700
function bigbluebuttonbn_get_file_areas() {
701
    $areas = array();
702
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
703
    return $areas;
704
}
705
706
/**
707
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
708
 *
709
 * @param  stdClass $bigbluebuttonbn        bigbluebuttonbn object
710
 * @param  stdClass $course     course object
711
 * @param  stdClass $cm         course module object
712
 * @param  stdClass $context    context object
713
 * @since Moodle 3.0
714
 */
715
function bigbluebuttonbn_view($bigbluebuttonbn, $course, $cm, $context) {
716
717
    // Trigger course_module_viewed event.
718
    $params = array(
719
        'context' => $context,
720
        'objectid' => $bigbluebuttonbn->id
721
    );
722
723
    $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_activity_viewed::create($params);
724
    $event->add_record_snapshot('course_modules', $cm);
725
    $event->add_record_snapshot('course', $course);
726
    $event->add_record_snapshot('bigbluebuttonbn', $bigbluebuttonbn);
727
    $event->trigger();
728
729
    // Completion.
730
    $completion = new completion_info($course);
731
    $completion->set_module_viewed($cm);
732
}
733
734
/**
735
 * Check if the module has any update that affects the current user since a given time.
736
 *
737
 * @param  cm_info $cm course module data
738
 * @param  int $from the time to check updates from
739
 * @param  array $filter  if we need to check only specific updates
740
 * @return stdClass an object with the different type of areas indicating if they were updated or not
741
 * @since Moodle 3.2
742
 */
743
function bigbluebuttonbn_check_updates_since(cm_info $cm, $from, $filter = array()) {
744
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
745
    return $updates;
746
}
747
748
/**
749
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
750
 *
751
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
752
 * is not displayed on the block.
753
 *
754
 * @param calendar_event $event
755
 * @param \core_calendar\action_factory $factory
756
 * @return \core_calendar\local\event\entities\action_interface|null
757
 */
758
function mod_bigbluebuttonbn_core_calendar_provide_event_action(calendar_event $event,
759
                                                       \core_calendar\action_factory $factory) {
760
    $cm = get_fast_modinfo($event->courseid)->instances['bigbluebuttonbn'][$event->instance];
761
762
    $completion = new \completion_info($cm->get_course());
763
764
    $completiondata = $completion->get_data($cm, false);
765
766
    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
767
        return null;
768
    }
769
770
    return $factory->create_instance(
771
        get_string('view'),
772
        new \mod_bigbluebuttonbn('/mod/bigbluebuttonbn/view.php', ['id' => $cm->id]),
773
        1,
774
        true
775
    );
776
}
777
778
/**
779
 * Get icon mapping for font-awesome.
780
 */
781
function mod_bigbluebuttonbn_get_fontawesome_icon_map() {
782
    return [
783
        'mod_bigbluebuttonbn:i/bigbluebutton' => 'fa-bigbluebutton',
784
    ];
785
}
786
787
/**
788
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
789
 *
790
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
791
 * is not displayed on the block.
792
 *
793
 * @param calendar_event $event
794
 * @param \core_calendar\action_factory $factory
795
 * @return \core_calendar\local\event\entities\action_interface|null
796
 */
797
function mod_bigbluebuttonbn_core_calendar_provide_event_action(calendar_event $event,
0 ignored issues
show
Best Practice introduced by
The function mod_bigbluebuttonbn_core..._provide_event_action() has been defined more than once; this definition is ignored, only the first definition in this file (L758-776) is considered.

This check looks for functions that have already been defined in the same file.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
798
        \core_calendar\action_factory $factory) {
799
    global $CFG, $DB;
800
801
    require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
802
803
    $cm = get_fast_modinfo($event->courseid)->instances['bigbluebuttonbn'][$event->instance];
804
805
    // Check that the bigbluebuttonbn activity is open.
806
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $event->instance), '*', MUST_EXIST);
807
    $actionable = bigbluebuttonbn_get_availability_status($bigbluebuttonbn);
808
809
    $string = get_string('view_room', 'bigbluebuttonbn');
810
    $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
811
    if (groups_get_activity_groupmode($cm) == NOGROUPS) {
812
        // No groups mode.
813
        $string = get_string('view_conference_action_join', 'bigbluebuttonbn');
814
        $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
815
    }
816
817
    return $factory->create_instance($string, $url, 1, $actionable);
818
}