Completed
Push — master ( 2a9d8e...570efc )
by Jesus
02:25
created

lib.php ➔ bigbluebuttonbn_process_pre_save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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-2017 Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 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->libdir.'/accesslib.php');
35
require_once($CFG->libdir.'/completionlib.php');
36
require_once($CFG->libdir.'/datalib.php');
37
require_once($CFG->libdir.'/coursecatlib.php');
38
require_once($CFG->libdir.'/enrollib.php');
39
require_once($CFG->libdir.'/filelib.php');
40
require_once($CFG->libdir.'/formslib.php');
41
42
if (file_exists(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php')) {
43
    require_once(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php');
44
}
45
46
if (!isset($CFG->bigbluebuttonbn)) {
47
    $CFG->bigbluebuttonbn = array();
48
}
49
50
if (file_exists(dirname(__FILE__).'/config.php')) {
51
    require_once(dirname(__FILE__).'/config.php');
52
    // Old BigBlueButtonBN cfg schema. For backward compatibility.
53
    global $BIGBLUEBUTTONBN_CFG;
54
55
    if (isset($BIGBLUEBUTTONBN_CFG)) {
56
        foreach ((array) $BIGBLUEBUTTONBN_CFG as $key => $value) {
57
            $cfgkey = str_replace("bigbluebuttonbn_", "", $key);
58
            $CFG->bigbluebuttonbn[$cfgkey] = $value;
59
        }
60
    }
61
}
62
63
/*
64
 * DURATIONCOMPENSATION: Feature removed by configuration
65
 */
66
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
67
/*
68
 * Remove this block when restored
69
 */
70
71
 /** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
72
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
73
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
74
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
75
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string of event create for bigbluebuttonbn_logs */
76
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
77
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string of event join for bigbluebuttonbn_logs */
78
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
79
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string of event logout for bigbluebuttonbn_logs */
80
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
81
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string of event import for bigbluebuttonbn_logs */
82
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
83
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string of event delete for bigbluebuttonbn_logs */
84
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
85
86
/**
87
 * Indicates API features that the forum supports.
88
 *
89
 * @uses FEATURE_IDNUMBER
90
 * @uses FEATURE_GROUPS
91
 * @uses FEATURE_GROUPINGS
92
 * @uses FEATURE_GROUPMEMBERSONLY
93
 * @uses FEATURE_MOD_INTRO
94
 * @uses FEATURE_BACKUP_MOODLE2
95
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
96
 * @uses FEATURE_GRADE_HAS_GRADE
97
 * @uses FEATURE_GRADE_OUTCOMES
98
 * @uses FEATURE_SHOW_DESCRIPTION
99
 * @param string $feature
100
 * @return mixed True if yes (some features may use other values)
101
 */
102
function bigbluebuttonbn_supports($feature) {
103
    if (!$feature) {
104
        return null;
105
    }
106
    $features = array(
107
        (string) FEATURE_IDNUMBER => true,
108
        (string) FEATURE_GROUPS => true,
109
        (string) FEATURE_GROUPINGS => true,
110
        (string) FEATURE_GROUPMEMBERSONLY => true,
111
        (string) FEATURE_MOD_INTRO => true,
112
        (string) FEATURE_BACKUP_MOODLE2 => true,
113
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
114
        (string) FEATURE_GRADE_HAS_GRADE => false,
115
        (string) FEATURE_GRADE_OUTCOMES => false,
116
        (string) FEATURE_SHOW_DESCRIPTION => true,
117
    );
118
    if (isset($features[(string) $feature])) {
119
        return $features[$feature];
120
    }
121
    return null;
122
}
123
124
/**
125
 * Given an object containing all the necessary data,
126
 * (defined by the form in mod_form.php) this function
127
 * will create a new instance and return the id number
128
 * of the new instance.
129
 *
130
 * @param object $data  An object from the form in mod_form.php
131
 * @return int The id of the newly inserted bigbluebuttonbn record
132
 */
133
function bigbluebuttonbn_add_instance($data) {
134
    global $DB;
135
    // Excecute preprocess.
136
    bigbluebuttonbn_process_pre_save($data);
137
    // Pre-set initial values.
138
    $data->presentation = bigbluebuttonbn_get_media_file($data);
139
    // Insert a record.
140
    $data->id = $DB->insert_record('bigbluebuttonbn', $data);
141
    // Encode meetingid.
142
    $meetingid = bigbluebuttonbn_encode_meetingid($data->id);
143
    // Set the meetingid column in the bigbluebuttonbn table.
144
    $DB->set_field('bigbluebuttonbn', 'meetingid', $meetingid, array('id' => $data->id));
145
    // Complete the process.
146
    bigbluebuttonbn_process_post_save($data);
147
    return $data->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 $data  An object from the form in mod_form.php
156
 * @return bool Success/Fail
157
 */
158
function bigbluebuttonbn_update_instance($data) {
159
    global $DB;
160
    // Excecute preprocess.
161
    bigbluebuttonbn_process_pre_save($data);
162
    // Pre-set initial values.
163
    $data->id = $data->instance;
164
    $data->presentation = bigbluebuttonbn_get_media_file($data);
165
    // Update a record.
166
    $DB->update_record('bigbluebuttonbn', $data);
167
    // Complete the process.
168
    bigbluebuttonbn_process_post_save($data);
169
    return true;
170
}
171
172
/**
173
 * Given an ID of an instance of this module,
174
 * this function will permanently delete the instance
175
 * and any data that depends on it.
176
 *
177
 * @param int $id Id of the module instance
178
 *
179
 * @return bool Success/Failure
180
 */
181
function bigbluebuttonbn_delete_instance($id) {
182
    global $DB;
183
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
184
    if (!$bigbluebuttonbn) {
185
        return false;
186
    }
187
    // TODO: End the meeting if it is running.
188
189
    // Perform delete.
190
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
191
        return false;
192
    }
193
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
194
        return false;
195
    }
196
    // Log action performed.
197
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
198
}
199
200
/**
201
 * Given an ID of an instance of this module,
202
 * this function will permanently delete the data that depends on it.
203
 *
204
 * @param object $bigbluebuttonbn Id of the module instance
205
 *
206
 * @return bool Success/Failure
207
 */
208
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
209
    global $DB, $USER;
210
    $log = new stdClass();
211
    $log->meetingid = $bigbluebuttonbn->meetingid;
212
    $log->courseid = $bigbluebuttonbn->course;
213
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
214
    $log->userid = $USER->id;
215
    $log->timecreated = time();
216
    $log->log = BIGBLUEBUTTONBN_LOG_EVENT_DELETE;
217
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
218
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
219
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
220
    $log->meta = "{\"has_recordings\":false}";
221
    if (!empty($logs)) {
222
        $log->meta = "{\"has_recordings\":true}";
223
    }
224
    if (!$DB->insert_record('bigbluebuttonbn_logs', $log)) {
225
        return false;
226
    }
227
    return true;
228
}
229
230
/**
231
 * Return a small object with summary information about what a
232
 * user has done with a given particular instance of this module
233
 * Used for user activity reports.
234
 * $return->time = the time they did it
235
 * $return->info = a short text description.
236
 *
237
 * @param object $course
238
 * @param object $user
239
 * @param object $mod
240
 * @param object $bigbluebuttonbn
241
 *
242
 * @return bool
243
 */
244
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...
245
    global $DB;
246
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
247
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ), '*');
248
    if ($completed > 0) {
249
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
250
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
251
            get_string('view_message_times', 'bigbluebuttonbn');
252
    }
253
    return '';
254
}
255
256
/**
257
 * Print a detailed representation of what a user has done with
258
 * a given particular instance of this module, for user activity reports.
259
 *
260
 * @param object $course
261
 * @param object $user
262
 * @param object $mod
263
 * @param object $bigbluebuttonbn
264
 *
265
 * @return bool
266
 */
267
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...
268
    global $DB;
269
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
270
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ),
271
        '*', IGNORE_MULTIPLE);
272
    return $completed > 0;
273
}
274
275
/**
276
 * Returns all other caps used in module.
277
 *
278
 * @return string[]
279
 */
280
function bigbluebuttonbn_get_extra_capabilities() {
281
    return array('moodle/site:accessallgroups');
282
}
283
284
/**
285
 * List of view style log actions.
286
 *
287
 * @return string[]
288
 */
289
function bigbluebuttonbn_get_view_actions() {
290
    return array('view', 'view all');
291
}
292
293
/**
294
 * List of update style log actions.
295
 *
296
 * @return string[]
297
 */
298
function bigbluebuttonbn_get_post_actions() {
299
    return array('update', 'add', 'create', 'join', 'end', 'left', 'publish', 'unpublish', 'delete');
300
}
301
302
/**
303
 * Print an overview of all bigbluebuttonbn instances for the courses.
304
 *
305
 * @param array $courses
306
 * @param array $htmlarray Passed by reference
307
 *
308
 * @return void
309
 */
310
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
311
    if (empty($courses) || !is_array($courses)) {
312
        return array();
313
    }
314
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
315
    foreach ($bns as $bn) {
316
        $now = time();
317
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
318
            // A bigbluebuttonbn is scheduled.
319
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
320
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
321
            }
322
            $htmlarray[$bn->course]['bigbluebuttonbn'] = bigbluebuttonbn_print_overview_element($bn, $now);
323
        }
324
    }
325
}
326
327
/**
328
 * Print an overview of a bigbluebuttonbn instance.
329
 *
330
 * @param array $bigbluebuttonbn
331
 * @param int $now
332
 *
333
 * @return string
334
 */
335
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
336
    global $CFG;
337
    $start = 'started_at';
338
    if ($bigbluebuttonbn->openingtime > $now) {
339
        $start = 'starts_at';
340
    }
341
    $classes = '';
342
    if ($bigbluebuttonbn->visible) {
343
        $classes = 'class="dimmed" ';
344
    }
345
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
346
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
347
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
348
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
349
    $str .= '  </div>'."\n";
350
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
351
        '</div>'."\n";
352
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
353
      .'</div>'."\n";
354
    $str .= '</div>'."\n";
355
    return $str;
356
}
357
358
/**
359
 * Given a course_module object, this function returns any
360
 * "extra" information that may be needed when printing
361
 * this activity in a course listing.
362
 * See get_array_of_activities() in course/lib.php.
363
 *
364
 * @param object $coursemodule
365
 *
366
 * @return null|cached_cm_info
367
 */
368
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
369
    global $DB;
370
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance),
371
        'id, name, intro, introformat');
372
    if (!$bigbluebuttonbn) {
373
        return null;
374
    }
375
    $info = new cached_cm_info();
376
    $info->name = $bigbluebuttonbn->name;
377
    if ($coursemodule->showdescription) {
378
        // Convert intro to html. Do not filter cached version, filters run at display time.
379
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
380
    }
381
    return $info;
382
}
383
384
/**
385
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
386
 *
387
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
388
 *
389
 * @return void
390
 **/
391
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
392
    bigbluebuttonbn_process_pre_save_instance($bigbluebuttonbn);
393
    bigbluebuttonbn_process_pre_save_checkboxes($bigbluebuttonbn);
394
    bigbluebuttonbn_process_pre_save_common($bigbluebuttonbn);
395
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
396
}
397
398
/**
399
 * Runs process for defining the instance (insert/update).
400
 *
401
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
402
 *
403
 * @return void
404
 **/
405
function bigbluebuttonbn_process_pre_save_instance(&$bigbluebuttonbn) {
406
    $bigbluebuttonbn->timemodified = time();
407
    if ((integer)$bigbluebuttonbn->instance == 0) {
408
        $bigbluebuttonbn->timecreated = time();
409
        $bigbluebuttonbn->timemodified = 0;
410
        // As it is a new activity, assign passwords.
411
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
412
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12, $bigbluebuttonbn->moderatorpass);
413
    }
414
}
415
416
/**
417
 * Runs process for assigning default value to checkboxes.
418
 *
419
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
420
 *
421
 * @return void
422
 **/
423
function bigbluebuttonbn_process_pre_save_checkboxes(&$bigbluebuttonbn) {
424
    if (!isset($bigbluebuttonbn->wait)) {
425
        $bigbluebuttonbn->wait = 0;
426
    }
427
    if (!isset($bigbluebuttonbn->record)) {
428
        $bigbluebuttonbn->record = 0;
429
    }
430
    if (!isset($bigbluebuttonbn->recordings_html)) {
431
        $bigbluebuttonbn->recordings_html = 0;
432
    }
433
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
434
        $bigbluebuttonbn->recordings_deleted = 0;
435
    }
436
    if (!isset($bigbluebuttonbn->recordings_imported)) {
437
        $bigbluebuttonbn->recordings_imported = 0;
438
    }
439
}
440
441
/**
442
 * Runs process for wipping common settings when 'recordings only'.
443
 *
444
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
445
 *
446
 * @return void
447
 **/
448
function bigbluebuttonbn_process_pre_save_common(&$bigbluebuttonbn) {
449
    // Make sure common settings are removed when 'recordings only'.
450
    if ($bigbluebuttonbn->type == BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY) {
451
        $bigbluebuttonbn->groupmode = 0;
452
        $bigbluebuttonbn->groupingid = 0;
453
    }
454
}
455
456
/**
457
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
458
 *
459
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
460
 *
461
 * @return void
462
 **/
463
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
464
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
465
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
466
    }
467
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
468
}
469
470
/**
471
 * Generates a message on insert/update which is sent to all users enrolled.
472
 *
473
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
474
 *
475
 * @return void
476
 **/
477
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
478
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
479
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
480
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
481
    }
482
    $context = context_course::instance($bigbluebuttonbn->course);
483
    \mod_bigbluebuttonbn\locallib\notifier::notification_process($context, $bigbluebuttonbn, $action);
484
}
485
486
/**
487
 * Generates an event after a bigbluebuttonbn insert/update.
488
 *
489
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
490
 *
491
 * @return void
492
 **/
493
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
494
    global $DB;
495
    // Delete evento to the calendar when/if openingtime is NOT set.
496
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
497
        $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id));
498
        return;
499
    }
500
    // Add evento to the calendar as openingtime is set.
501
    $event = new stdClass();
502
    $event->name = $bigbluebuttonbn->name;
503
    $event->courseid = $bigbluebuttonbn->course;
504
    $event->groupid = 0;
505
    $event->userid = 0;
506
    $event->modulename = 'bigbluebuttonbn';
507
    $event->instance = $bigbluebuttonbn->id;
508
    $event->timestart = $bigbluebuttonbn->openingtime;
509
    $event->durationtime = 0;
510
    if ($bigbluebuttonbn->closingtime) {
511
        $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
512
    }
513
    $event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
514
        'instance' => $bigbluebuttonbn->id));
515
    if ($event->id) {
516
        $calendarevent = calendar_event::load($event->id);
517
        $calendarevent->update($event);
518
        return;
519
    }
520
    calendar_event::create($event);
521
}
522
523
/**
524
 * Get a full path to the file attached as a preuploaded presentation
525
 * or if there is none, set the presentation field will be set to blank.
526
 *
527
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
528
 *
529
 * @return string
530
 */
531
function bigbluebuttonbn_get_media_file(&$bigbluebuttonbn) {
532
    $draftitemid = isset($bigbluebuttonbn->presentation) ? $bigbluebuttonbn->presentation : null;
533
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
534
    // Set the filestorage object.
535
    $fs = get_file_storage();
536
    // Save the file if it exists that is currently in the draft area.
537
    file_save_draft_area_files($draftitemid, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
538
    // Get the file if it exists.
539
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
540
        'itemid, filepath, filename', false);
541
    // Check that there is a file to process.
542
    $filesrc = '';
543
    if (count($files) == 1) {
544
        // Get the first (and only) file.
545
        $file = reset($files);
546
        $filesrc = '/'.$file->get_filename();
547
    }
548
    return $filesrc;
549
}
550
551
/**
552
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
553
 *
554
 * @category files
555
 *
556
 * @param stdClass $course        course object
557
 * @param stdClass $cm            course module object
558
 * @param stdClass $context       context object
559
 * @param string   $filearea      file area
560
 * @param array    $args          extra arguments
561
 * @param bool     $forcedownload whether or not force download
562
 * @param array    $options       additional options affecting the file serving
563
 *
564
 * @return false|null false if file not found, does not return if found - justsend the file
565
 */
566
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
567
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
568
        return false;
569
    }
570
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
571
    if (empty($file)) {
572
        return false;
573
    }
574
    // Finally send the file.
575
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
576
}
577
578
/**
579
 * Helper for validating pluginfile.
580
 * @param stdClass $context       context object
581
 * @param string   $filearea      file area
582
 *
583
 * @return false|null false if file not valid
584
 */
585
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
586
    if ($context->contextlevel != CONTEXT_MODULE) {
587
        return false;
588
    }
589
    if ($filearea !== 'presentation') {
590
        return false;
591
    }
592
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
593
        return false;
594
    }
595
    return true;
596
}
597
598
/**
599
 * Helper for getting pluginfile.
600
 *
601
 * @param stdClass $course        course object
602
 * @param stdClass $cm            course module object
603
 * @param stdClass $context       context object
604
 * @param string   $filearea      file area
605
 * @param array    $args          extra arguments
606
 *
607
 * @return object
608
 */
609
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
610
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
611
    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...
612
        return false;
613
    }
614
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
615
    $fs = get_file_storage();
616
    $file = $fs->get_file_by_hash(sha1($fullpath));
617
    if (!$file || $file->is_directory()) {
618
        return false;
619
    }
620
    return $file;
621
}
622
623
/**
624
 * Helper for getting pluginfile name.
625
 *
626
 * @param stdClass $course        course object
627
 * @param stdClass $cm            course module object
628
 * @param stdClass $context       context object
629
 * @param array    $args          extra arguments
630
 *
631
 * @return array
632
 */
633
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
634
    global $DB;
635
    if (count($args) > 1) {
636
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
637
            return;
638
        }
639
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
640
        $noncekey = sha1($bigbluebuttonbn->id);
641
        $presentationnonce = $cache->get($noncekey);
642
        $noncevalue = $presentationnonce['value'];
643
        $noncecounter = $presentationnonce['counter'];
644
        if ($args['0'] != $noncevalue) {
645
            return;
646
        }
647
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
648
        $noncecounter += 1;
649
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
650
        if ($noncecounter == 2) {
651
            $cache->delete($noncekey);
652
        }
653
        return $args['1'];
654
    }
655
    require_course_login($course, true, $cm);
656
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
657
        return;
658
    }
659
    return implode('/', $args);
660
}
661
662
/**
663
 * Returns an array of file areas.
664
 *
665
 * @category files
666
 *
667
 * @return array a list of available file areas
668
 */
669
function bigbluebuttonbn_get_file_areas() {
670
    $areas = array();
671
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
672
    return $areas;
673
}
674
675
/**
676
 * Get icon mapping for font-awesome.
677
 */
678
function mod_bigbluebuttonbn_get_fontawesome_icon_map() {
679
    return [
680
        'mod_bigbluebuttonbn:i/bigbluebutton' => 'fa-bigbluebutton',
681
    ];
682
}
683