Completed
Pull Request — master (#56)
by Jesus
07:02
created

lib.php ➔ bigbluebuttonbn_view()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 18
rs 9.6666
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.'/enrollib.php');
39
require_once($CFG->libdir.'/filelib.php');
40
require_once($CFG->libdir.'/formslib.php');
41
42
43
if (file_exists(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php')) {
44
    require_once(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php');
45
}
46
47
if (!isset($CFG->bigbluebuttonbn)) {
48
    $CFG->bigbluebuttonbn = array();
49
}
50
51
if (file_exists(dirname(__FILE__).'/config.php')) {
52
    require_once(dirname(__FILE__).'/config.php');
53
}
54
55
/*
56
 * DURATIONCOMPENSATION: Feature removed by configuration
57
 */
58
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
59
/*
60
 * Remove this block when restored
61
 */
62
63
 /** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
64
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
65
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
66
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
67
/** @var BIGBLUEBUTTONBN_LOG_EVENT_ADD string of event add for bigbluebuttonbn_logs */
68
const BIGBLUEBUTTONBN_LOG_EVENT_ADD = 'Add';
69
/** @var BIGBLUEBUTTONBN_LOG_EVENT_EDIT string of event edit for bigbluebuttonbn_logs */
70
const BIGBLUEBUTTONBN_LOG_EVENT_EDIT = 'Edit';
71
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string of event create for bigbluebuttonbn_logs */
72
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
73
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string of event join for bigbluebuttonbn_logs */
74
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
75
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string of event logout for bigbluebuttonbn_logs */
76
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
77
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string of event import for bigbluebuttonbn_logs */
78
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
79
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string of event delete for bigbluebuttonbn_logs */
80
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
81
/** @var BIGBLUEBUTTON_LOG_EVENT_CALLBACK string defines the bigbluebuttonbn callback event */
82
const BIGBLUEBUTTON_LOG_EVENT_CALLBACK = 'Callback';
83
/**
84
 * Indicates API features that the forum supports.
85
 *
86
 * @uses FEATURE_IDNUMBER
87
 * @uses FEATURE_GROUPS
88
 * @uses FEATURE_GROUPINGS
89
 * @uses FEATURE_GROUPMEMBERSONLY
90
 * @uses FEATURE_MOD_INTRO
91
 * @uses FEATURE_BACKUP_MOODLE2
92
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
93
 * @uses FEATURE_COMPLETION_HAS_RULES
94
 * @uses FEATURE_GRADE_HAS_GRADE
95
 * @uses FEATURE_GRADE_OUTCOMES
96
 * @uses FEATURE_SHOW_DESCRIPTION
97
 * @param string $feature
98
 * @return mixed True if yes (some features may use other values)
99
 */
100
function bigbluebuttonbn_supports($feature) {
101
    if (!$feature) {
102
        return null;
103
    }
104
    $features = array(
105
        (string) FEATURE_IDNUMBER => true,
106
        (string) FEATURE_GROUPS => true,
107
        (string) FEATURE_GROUPINGS => true,
108
        (string) FEATURE_GROUPMEMBERSONLY => true,
109
        (string) FEATURE_MOD_INTRO => true,
110
        (string) FEATURE_BACKUP_MOODLE2 => true,
111
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
112
        (string) FEATURE_GRADE_HAS_GRADE => false,
113
        (string) FEATURE_GRADE_OUTCOMES => false,
114
        (string) FEATURE_SHOW_DESCRIPTION => true,
115
    );
116
    if (isset($features[(string) $feature])) {
117
        return $features[$feature];
118
    }
119
    return null;
120
}
121
122
/**
123
 * Given an object containing all the necessary data,
124
 * (defined by the form in mod_form.php) this function
125
 * will create a new instance and return the id number
126
 * of the new instance.
127
 *
128
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
129
 * @return int The id of the newly inserted bigbluebuttonbn record
130
 */
131
function bigbluebuttonbn_add_instance($bigbluebuttonbn) {
132
    global $DB;
133
    // Excecute preprocess.
134
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
135
    // Pre-set initial values.
136
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
137
    // Insert a record.
138
    $bigbluebuttonbn->id = $DB->insert_record('bigbluebuttonbn', $bigbluebuttonbn);
139
    // Encode meetingid.
140
    $bigbluebuttonbn->meetingid = bigbluebuttonbn_encode_meetingid($bigbluebuttonbn->id);
141
    // Set the meetingid column in the bigbluebuttonbn table.
142
    $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn->meetingid, array('id' => $bigbluebuttonbn->id));
143
    // Log insert action.
144
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_ADD);
145
    // Complete the process.
146
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
147
    return $bigbluebuttonbn->id;
148
}
149
150
/**
151
 * Given an object containing all the necessary data,
152
 * (defined by the form in mod_form.php) this function
153
 * will update an existing instance with new data.
154
 *
155
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
156
 * @return bool Success/Fail
157
 */
158
function bigbluebuttonbn_update_instance($bigbluebuttonbn) {
159
    global $DB;
160
    // Excecute preprocess.
161
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
162
    // Pre-set initial values.
163
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
164
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
165
    // Update a record.
166
    $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
167
    // Get the meetingid column in the bigbluebuttonbn table.
168
    $bigbluebuttonbn->meetingid = (string)$DB->get_field('bigbluebuttonbn', 'meetingid', array('id' => $bigbluebuttonbn->id));
169
    // Log update action.
170
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_EDIT);
171
    // Complete the process.
172
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
173
    return true;
174
}
175
176
/**
177
 * Given an ID of an instance of this module,
178
 * this function will permanently delete the instance
179
 * and any data that depends on it.
180
 *
181
 * @param int $id Id of the module instance
182
 *
183
 * @return bool Success/Failure
184
 */
185
function bigbluebuttonbn_delete_instance($id) {
186
    global $DB;
187
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
188
    if (!$bigbluebuttonbn) {
189
        return false;
190
    }
191
    // TODO: End the meeting if it is running.
192
193
    // Perform delete.
194
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
195
        return false;
196
    }
197
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
198
        return false;
199
    }
200
    // Log action performed.
201
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
202
}
203
204
/**
205
 * Given an ID of an instance of this module,
206
 * this function will permanently delete the data that depends on it.
207
 *
208
 * @param object $bigbluebuttonbn Id of the module instance
209
 *
210
 * @return bool Success/Failure
211
 */
212
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
213
    global $DB;
214
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
215
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
216
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
217
    $meta = "{\"has_recordings\":" . empty($logs) ? "true" : "false" . "}";
218
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_DELETE, [], $meta);
219
}
220
221
/**
222
 * Return a small object with summary information about what a
223
 * user has done with a given particular instance of this module
224
 * Used for user activity reports.
225
 * $return->time = the time they did it
226
 * $return->info = a short text description.
227
 *
228
 * @param object $course
229
 * @param object $user
230
 * @param object $mod
231
 * @param object $bigbluebuttonbn
232
 *
233
 * @return bool
234
 */
235
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...
236
    global $DB;
237
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
238
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ), '*');
239
    if ($completed > 0) {
240
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
241
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
242
            get_string('view_message_times', 'bigbluebuttonbn');
243
    }
244
    return '';
245
}
246
247
/**
248
 * Print a detailed representation of what a user has done with
249
 * a given particular instance of this module, for user activity reports.
250
 *
251
 * @param object $course
252
 * @param object $user
253
 * @param object $mod
254
 * @param object $bigbluebuttonbn
255
 *
256
 * @return bool
257
 */
258
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...
259
    global $DB;
260
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
261
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ),
262
        '*', IGNORE_MULTIPLE);
263
    return $completed > 0;
264
}
265
266
/**
267
 * Returns all other caps used in module.
268
 *
269
 * @return string[]
270
 */
271
function bigbluebuttonbn_get_extra_capabilities() {
272
    return array('moodle/site:accessallgroups');
273
}
274
275
/**
276
 * This function is used by the reset_course_userdata function in moodlelib.
277
 * @param $data the data submitted from the reset course.
278
 * @return array status array
279
 */
280
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...
281
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
282
    // See MDL-9367.
283
    return array();
284
}
285
286
/**
287
 * List of view style log actions.
288
 *
289
 * @return string[]
290
 */
291
function bigbluebuttonbn_get_view_actions() {
292
    return array('view', 'view all');
293
}
294
295
/**
296
 * List of update style log actions.
297
 *
298
 * @return string[]
299
 */
300
function bigbluebuttonbn_get_post_actions() {
301
    return array('update', 'add', 'delete');
302
}
303
304
/**
305
 * Print an overview of all bigbluebuttonbn instances for the courses.
306
 *
307
 * @param array $courses
308
 * @param array $htmlarray Passed by reference
309
 *
310
 * @return void
311
 */
312
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
313
    if (empty($courses) || !is_array($courses)) {
314
        return array();
315
    }
316
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
317
    foreach ($bns as $bn) {
318
        $now = time();
319
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
320
            // A bigbluebuttonbn is scheduled.
321
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
322
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
323
            }
324
            $htmlarray[$bn->course]['bigbluebuttonbn'] = bigbluebuttonbn_print_overview_element($bn, $now);
325
        }
326
    }
327
}
328
329
/**
330
 * Print an overview of a bigbluebuttonbn instance.
331
 *
332
 * @param array $bigbluebuttonbn
333
 * @param int $now
334
 *
335
 * @return string
336
 */
337
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
338
    global $CFG;
339
    $start = 'started_at';
340
    if ($bigbluebuttonbn->openingtime > $now) {
341
        $start = 'starts_at';
342
    }
343
    $classes = '';
344
    if ($bigbluebuttonbn->visible) {
345
        $classes = 'class="dimmed" ';
346
    }
347
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
348
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
349
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
350
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
351
    $str .= '  </div>'."\n";
352
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
353
        '</div>'."\n";
354
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
355
      .'</div>'."\n";
356
    $str .= '</div>'."\n";
357
    return $str;
358
}
359
360
/**
361
 * Given a course_module object, this function returns any
362
 * "extra" information that may be needed when printing
363
 * this activity in a course listing.
364
 * See get_array_of_activities() in course/lib.php.
365
 *
366
 * @param object $coursemodule
367
 *
368
 * @return null|cached_cm_info
369
 */
370
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
371
    global $DB;
372
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance),
373
        'id, name, intro, introformat');
374
    if (!$bigbluebuttonbn) {
375
        return null;
376
    }
377
    $info = new cached_cm_info();
378
    $info->name = $bigbluebuttonbn->name;
379
    if ($coursemodule->showdescription) {
380
        // Convert intro to html. Do not filter cached version, filters run at display time.
381
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
382
    }
383
    return $info;
384
}
385
386
/**
387
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
388
 *
389
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
390
 *
391
 * @return void
392
 **/
393
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
394
    bigbluebuttonbn_process_pre_save_instance($bigbluebuttonbn);
395
    bigbluebuttonbn_process_pre_save_checkboxes($bigbluebuttonbn);
396
    bigbluebuttonbn_process_pre_save_common($bigbluebuttonbn);
397
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
398
}
399
400
/**
401
 * Runs process for defining the instance (insert/update).
402
 *
403
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
404
 *
405
 * @return void
406
 **/
407
function bigbluebuttonbn_process_pre_save_instance(&$bigbluebuttonbn) {
408
    $bigbluebuttonbn->timemodified = time();
409
    if ((integer)$bigbluebuttonbn->instance == 0) {
410
        $bigbluebuttonbn->meetingid = 0;
411
        $bigbluebuttonbn->timecreated = time();
412
        $bigbluebuttonbn->timemodified = 0;
413
        // As it is a new activity, assign passwords.
414
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
415
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12, $bigbluebuttonbn->moderatorpass);
416
    }
417
}
418
419
/**
420
 * Runs process for assigning default value to checkboxes.
421
 *
422
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
423
 *
424
 * @return void
425
 **/
426
function bigbluebuttonbn_process_pre_save_checkboxes(&$bigbluebuttonbn) {
427
    if (!isset($bigbluebuttonbn->wait)) {
428
        $bigbluebuttonbn->wait = 0;
429
    }
430
    if (!isset($bigbluebuttonbn->record)) {
431
        $bigbluebuttonbn->record = 0;
432
    }
433
    if (!isset($bigbluebuttonbn->recordings_html)) {
434
        $bigbluebuttonbn->recordings_html = 0;
435
    }
436
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
437
        $bigbluebuttonbn->recordings_deleted = 0;
438
    }
439
    if (!isset($bigbluebuttonbn->recordings_imported)) {
440
        $bigbluebuttonbn->recordings_imported = 0;
441
    }
442
    if (!isset($bigbluebuttonbn->recordings_preview)) {
443
        $bigbluebuttonbn->recordings_preview = 0;
444
    }
445
}
446
447
/**
448
 * Runs process for wipping common settings when 'recordings only'.
449
 *
450
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
451
 *
452
 * @return void
453
 **/
454
function bigbluebuttonbn_process_pre_save_common(&$bigbluebuttonbn) {
455
    // Make sure common settings are removed when 'recordings only'.
456
    if ($bigbluebuttonbn->type == BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY) {
457
        $bigbluebuttonbn->groupmode = 0;
458
        $bigbluebuttonbn->groupingid = 0;
459
    }
460
}
461
462
/**
463
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
464
 *
465
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
466
 *
467
 * @return void
468
 **/
469
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
470
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
471
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
472
    }
473
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
474
    bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn);
475
}
476
477
/**
478
 * Generates a message on insert/update which is sent to all users enrolled.
479
 *
480
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
481
 *
482
 * @return void
483
 **/
484
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
485
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
486
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
487
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
488
    }
489
    $context = context_course::instance($bigbluebuttonbn->course);
490
    \mod_bigbluebuttonbn\locallib\notifier::notification_process($context, $bigbluebuttonbn, $action);
491
}
492
493
/**
494
 * Generates an event after a bigbluebuttonbn insert/update.
495
 *
496
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
497
 *
498
 * @return void
499
 **/
500
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
501
    global $DB;
502
    $eventid = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
503
        'instance' => $bigbluebuttonbn->id));
504
    // Delete the event from calendar when/if openingtime is NOT set.
505
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
506
        if ($eventid) {
507
            $calendarevent = calendar_event::load($eventid);
508
            $calendarevent->delete();
509
        }
510
        return;
511
    }
512
    // Add evento to the calendar as openingtime is set.
513
    $event = new stdClass();
514
    $event->eventtype = BIGBLUEBUTTON_EVENT_MEETING_START;
515
    $event->type = CALENDAR_EVENT_TYPE_ACTION;
516
    $event->name = $bigbluebuttonbn->name . ' (' . get_string('starts_at', 'bigbluebuttonbn') . ')';
517
    $event->description = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $bigbluebuttonbn->coursemodule);
518
    $event->courseid = $bigbluebuttonbn->course;
519
    $event->groupid = 0;
520
    $event->userid = 0;
521
    $event->modulename = 'bigbluebuttonbn';
522
    $event->instance = $bigbluebuttonbn->id;
523
    $event->timestart = $bigbluebuttonbn->openingtime;
524
    $event->timeduration = 0;
525
    if ($bigbluebuttonbn->closingtime) {
526
        $event->timeduration = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
527
    }
528
    $event->timesort = $event->timestart + $event->timeduration;
529
    $event->visible = instance_is_visible('bigbluebuttonbn', $bigbluebuttonbn);
530
    $event->priority = null;
531
    // Update the event in calendar when/if eventid was found.
532
    if ($eventid) {
533
        $event->id = $eventid;
534
        $calendarevent = calendar_event::load($eventid);
535
        $calendarevent->update($event);
536
        return;
537
    }
538
    calendar_event::create($event);
539
}
540
541
function bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn) {
542
    if (!empty($bigbluebuttonbn->completionexpected)) {
543
        \core_completion\api::update_completion_date_event(
544
            $bigbluebuttonbn->coursemodule,
545
            'bigbluebuttonbn',
546
            $bigbluebuttonbn->id, $bigbluebuttonbn->completionexpected
547
          );
548
    }
549
}
550
/**
551
 * Get a full path to the file attached as a preuploaded presentation
552
 * or if there is none, set the presentation field will be set to blank.
553
 *
554
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
555
 *
556
 * @return string
557
 */
558
function bigbluebuttonbn_get_media_file(&$bigbluebuttonbn) {
559
    if (!isset($bigbluebuttonbn->presentation) || $bigbluebuttonbn->presentation == '') {
560
        return '';
561
    }
562
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
563
    // Set the filestorage object.
564
    $fs = get_file_storage();
565
    // Save the file if it exists that is currently in the draft area.
566
    file_save_draft_area_files($bigbluebuttonbn->presentation, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
567
    // Get the file if it exists.
568
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
569
        'itemid, filepath, filename', false);
570
    // Check that there is a file to process.
571
    $filesrc = '';
572
    if (count($files) == 1) {
573
        // Get the first (and only) file.
574
        $file = reset($files);
575
        $filesrc = '/'.$file->get_filename();
576
    }
577
    return $filesrc;
578
}
579
580
/**
581
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
582
 *
583
 * @category files
584
 *
585
 * @param stdClass $course        course object
586
 * @param stdClass $cm            course module object
587
 * @param stdClass $context       context object
588
 * @param string   $filearea      file area
589
 * @param array    $args          extra arguments
590
 * @param bool     $forcedownload whether or not force download
591
 * @param array    $options       additional options affecting the file serving
592
 *
593
 * @return false|null false if file not found, does not return if found - justsend the file
594
 */
595
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
596
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
597
        return false;
598
    }
599
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
600
    if (empty($file)) {
601
        return false;
602
    }
603
    // Finally send the file.
604
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
605
}
606
607
/**
608
 * Helper for validating pluginfile.
609
 * @param stdClass $context       context object
610
 * @param string   $filearea      file area
611
 *
612
 * @return false|null false if file not valid
613
 */
614
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
615
    if ($context->contextlevel != CONTEXT_MODULE) {
616
        return false;
617
    }
618
    if ($filearea !== 'presentation') {
619
        return false;
620
    }
621
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
622
        return false;
623
    }
624
    return true;
625
}
626
627
/**
628
 * Helper for getting pluginfile.
629
 *
630
 * @param stdClass $course        course object
631
 * @param stdClass $cm            course module object
632
 * @param stdClass $context       context object
633
 * @param string   $filearea      file area
634
 * @param array    $args          extra arguments
635
 *
636
 * @return object
637
 */
638
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
639
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
640
    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...
641
        return false;
642
    }
643
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
644
    $fs = get_file_storage();
645
    $file = $fs->get_file_by_hash(sha1($fullpath));
646
    if (!$file || $file->is_directory()) {
647
        return false;
648
    }
649
    return $file;
650
}
651
652
/**
653
 * Helper for getting pluginfile name.
654
 *
655
 * @param stdClass $course        course object
656
 * @param stdClass $cm            course module object
657
 * @param stdClass $context       context object
658
 * @param array    $args          extra arguments
659
 *
660
 * @return array
661
 */
662
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
663
    global $DB;
664
    if (count($args) > 1) {
665
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
666
            return;
667
        }
668
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
669
        $noncekey = sha1($bigbluebuttonbn->id);
670
        $presentationnonce = $cache->get($noncekey);
671
        $noncevalue = $presentationnonce['value'];
672
        $noncecounter = $presentationnonce['counter'];
673
        if ($args['0'] != $noncevalue) {
674
            return;
675
        }
676
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
677
        $noncecounter += 1;
678
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
679
        if ($noncecounter == 2) {
680
            $cache->delete($noncekey);
681
        }
682
        return $args['1'];
683
    }
684
    require_course_login($course, true, $cm);
685
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
686
        return;
687
    }
688
    return implode('/', $args);
689
}
690
691
/**
692
 * Returns an array of file areas.
693
 *
694
 * @category files
695
 *
696
 * @return array a list of available file areas
697
 */
698
function bigbluebuttonbn_get_file_areas() {
699
    $areas = array();
700
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
701
    return $areas;
702
}
703
704
/**
705
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
706
 *
707
 * @param  stdClass $bigbluebuttonbn        bigbluebuttonbn object
708
 * @param  stdClass $course     course object
709
 * @param  stdClass $cm         course module object
710
 * @param  stdClass $context    context object
711
 * @since Moodle 3.0
712
 */
713
function bigbluebuttonbn_view($bigbluebuttonbn, $course, $cm, $context) {
714
715
    // Trigger course_module_viewed event.
716
    $params = array(
717
        'context' => $context,
718
        'objectid' => $bigbluebuttonbn->id
719
    );
720
721
    $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_activity_viewed::create($params);
722
    $event->add_record_snapshot('course_modules', $cm);
723
    $event->add_record_snapshot('course', $course);
724
    $event->add_record_snapshot('bigbluebuttonbn', $bigbluebuttonbn);
725
    $event->trigger();
726
727
    // Completion.
728
    $completion = new completion_info($course);
729
    $completion->set_module_viewed($cm);
730
}
731
732
/**
733
 * Check if the module has any update that affects the current user since a given time.
734
 *
735
 * @param  cm_info $cm course module data
736
 * @param  int $from the time to check updates from
737
 * @param  array $filter  if we need to check only specific updates
738
 * @return stdClass an object with the different type of areas indicating if they were updated or not
739
 * @since Moodle 3.2
740
 */
741
function bigbluebuttonbn_check_updates_since(cm_info $cm, $from, $filter = array()) {
742
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
743
    return $updates;
744
}
745
746
747
/**
748
 * Get icon mapping for font-awesome.
749
 */
750
function mod_bigbluebuttonbn_get_fontawesome_icon_map() {
751
    return [
752
        'mod_bigbluebuttonbn:i/bigbluebutton' => 'fa-bigbluebutton',
753
    ];
754
}
755
756
/**
757
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
758
 *
759
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
760
 * is not displayed on the block.
761
 *
762
 * @param calendar_event $event
763
 * @param \core_calendar\action_factory $factory
764
 * @return \core_calendar\local\event\entities\action_interface|null
765
 */
766
function mod_bigbluebuttonbn_core_calendar_provide_event_action(calendar_event $event,
767
        \core_calendar\action_factory $factory) {
768
    global $CFG, $DB;
769
770
    require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
771
772
    $cm = get_fast_modinfo($event->courseid)->instances['bigbluebuttonbn'][$event->instance];
773
774
    // Check that the bigbluebuttonbn activity is open.
775
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $event->instance), '*', MUST_EXIST);
776
    $actionable = bigbluebuttonbn_get_availability_status($bigbluebuttonbn);
777
778
    $string = get_string('view_room', 'bigbluebuttonbn');
779
    $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
780
    if (groups_get_activity_groupmode($cm) == NOGROUPS) {
781
        // No groups mode.
782
        $string = get_string('view_conference_action_join', 'bigbluebuttonbn');
783
        $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
784
    }
785
786
    return $factory->create_instance($string, $url, 1, $actionable);
787
}
788
789
/**
790
 * Register a bigbluebuttonbn event
791
 *
792
 * @param object $bigbluebuttonbn
793
 * @param string $event
794
 * @param array  $overrides
795
 * @param string $meta
796
 *
797
 * @return bool Success/Failure
798
 */
799
function bigbluebuttonbn_log($bigbluebuttonbn, $event, array $overrides = [], $meta = null) {
800
    global $DB, $USER;
801
    $log = new stdClass();
802
    // Default values.
803
    $log->courseid = $bigbluebuttonbn->course;
804
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
805
    $log->userid = $USER->id;
806
    $log->meetingid = $bigbluebuttonbn->meetingid;
807
    $log->timecreated = time();
808
    $log->log = $event;
809
    $log->meta = $meta;
810
    // Overrides.
811
    foreach ($overrides as $key => $value) {
812
        $log->$key = $value;
813
    }
814
    if ($DB->insert_record('bigbluebuttonbn_logs', $log)) {
815
        return true;
816
    }
817
    return false;
818
}
819