Completed
Push — master ( 44031d...70a753 )
by Jesus
01:59
created

lib.php ➔ bigbluebuttonbn_process_post_save_notification()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 8
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
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
21
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
22
 * @copyright 2010-2017 Blindside Networks Inc
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
24
 */
25
26
defined('MOODLE_INTERNAL') || die;
27
28
global $CFG;
29
30
require_once($CFG->dirroot.'/calendar/lib.php');
31
require_once($CFG->dirroot.'/message/lib.php');
32
require_once($CFG->dirroot.'/mod/lti/OAuth.php');
33
require_once($CFG->libdir.'/accesslib.php');
34
require_once($CFG->libdir.'/completionlib.php');
35
require_once($CFG->libdir.'/datalib.php');
36
require_once($CFG->libdir.'/coursecatlib.php');
37
require_once($CFG->libdir.'/enrollib.php');
38
require_once($CFG->libdir.'/filelib.php');
39
require_once($CFG->libdir.'/formslib.php');
40
41
if (file_exists(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php')) {
42
    require_once(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php');
43
}
44
45
if (!isset($CFG->bigbluebuttonbn)) {
46
    $CFG->bigbluebuttonbn = array();
47
}
48
49
if (file_exists(dirname(__FILE__).'/config.php')) {
50
    require_once(dirname(__FILE__).'/config.php');
51
    // Old BigBlueButtonBN cfg schema. For backward compatibility.
52
    global $BIGBLUEBUTTONBN_CFG;
53
54
    if (isset($BIGBLUEBUTTONBN_CFG)) {
55
        foreach ((array) $BIGBLUEBUTTONBN_CFG as $key => $value) {
56
            $cfgkey = str_replace("bigbluebuttonbn_", "", $key);
57
            $CFG->bigbluebuttonbn[$cfgkey] = $value;
58
        }
59
    }
60
}
61
62
/*
63
 * DURATIONCOMPENSATION: Feature removed by configuration
64
 */
65
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
66
/*
67
 * Remove this block when restored
68
 */
69
70
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
71
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
72
73
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
74
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
75
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
76
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
77
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
78
79
function bigbluebuttonbn_supports($feature) {
80
81
    if (!$feature) {
82
        return null;
83
    }
84
85
    $features = array(
86
        (string) FEATURE_IDNUMBER => true,
87
        (string) FEATURE_GROUPS => true,
88
        (string) FEATURE_GROUPINGS => true,
89
        (string) FEATURE_GROUPMEMBERSONLY => true,
90
        (string) FEATURE_MOD_INTRO => true,
91
        (string) FEATURE_BACKUP_MOODLE2 => true,
92
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
93
        (string) FEATURE_GRADE_HAS_GRADE => false,
94
        (string) FEATURE_GRADE_OUTCOMES => false,
95
        (string) FEATURE_SHOW_DESCRIPTION => true,
96
    );
97
98
    if (isset($features[(string) $feature])) {
99
        return $features[$feature];
100
    }
101
102
    return null;
103
}
104
105
/**
106
 * Given an object containing all the necessary data,
107
 * (defined by the form in mod_form.php) this function
108
 * will create a new instance and return the id number
109
 * of the new instance.
110
 *
111
 * @param object $data  An object from the form in mod_form.php
112
 * @return int The id of the newly inserted bigbluebuttonbn record
113
 */
114
function bigbluebuttonbn_add_instance($data) {
115
    global $CFG, $DB;
116
    $data->id = null;
117
    bigbluebuttonbn_process_pre_save($data);
118
    unset($data->presentation);
119
    try {
120
        $transaction = $DB->start_delegated_transaction();
121
        // Insert a record.
122
        $data->id = $DB->insert_record('bigbluebuttonbn', $data);
123
        // Generate and set the meetingid property based on [Moodle Instance + Activity ID + BBB Secret].
124
        $meetingid = sha1($CFG->wwwroot . $data->id . \mod_bigbluebuttonbn\locallib\config::get('shared_secret'));
125
        $DB->set_field('bigbluebuttonbn', 'meetingid', $meetingid, array('id' => $data->id));
126
        // Assuming the inserts work, we get to the following line.
127
        $transaction->allow_commit();
128
    } catch(Exception $e) {
129
        $transaction->rollback($e);
130
    }
131
    if ($data->id) {
132
        bigbluebuttonbn_process_post_save($data);
133
    }
134
    return $data->id;
135
}
136
137
/**
138
 * Given an object containing all the necessary data,
139
 * (defined by the form in mod_form.php) this function
140
 * will update an existing instance with new data.
141
 *
142
 * @param object $data  An object from the form in mod_form.php
143
 * @return bool Success/Fail
144
 */
145
function bigbluebuttonbn_update_instance($data) {
146
    global $DB;
147
    $data->id = $data->instance;
148
    bigbluebuttonbn_process_pre_save($data);
149
    unset($data->presentation);
150
    try {
151
        $transaction = $DB->start_delegated_transaction();
152
        // Update a record.
153
        $DB->update_record('bigbluebuttonbn', $data);
154
        // Assuming the inserts work, we get to the following line.
155
        $transaction->allow_commit();
156
        $updated = true;
157
    } catch(Exception $e) {
158
        $transaction->rollback($e);
159
        $updated = false;
160
    }
161
    if ($updated) {
162
        bigbluebuttonbn_process_post_save($data);
163
    }
164
    return $updated;
165
}
166
167
/**
168
 * Given an ID of an instance of this module,
169
 * this function will permanently delete the instance
170
 * and any data that depends on it.
171
 *
172
 * @param int $id Id of the module instance
173
 *
174
 * @return bool Success/Failure
175
 */
176
function bigbluebuttonbn_delete_instance($id) {
177
    global $DB;
178
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
179
    if (!$bigbluebuttonbn) {
180
        return false;
181
    }
182
    // TODO: End the meeting if it is running.
183
184
    // Perform delete.
185
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
186
        return false;
187
    }
188
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
189
        return false;
190
    }
191
    // Log action performed.
192
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
193
}
194
195
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
196
    global $DB, $USER;
197
    $log = new stdClass();
198
    $log->meetingid = $bigbluebuttonbn->meetingid;
199
    $log->courseid = $bigbluebuttonbn->course;
200
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
201
    $log->userid = $USER->id;
202
    $log->timecreated = time();
203
    $log->log = BIGBLUEBUTTONBN_LOG_EVENT_DELETE;
204
    $sql = "SELECT * FROM {bigbluebuttonbn_logs} WHERE bigbluebuttonbnid = ? AND log = ? AND " . $DB->sql_compare_text('meta') . " = ?";
205
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
206
    $log->meta = "{\"has_recordings\":false}";
207
    if (!empty($logs)) {
208
        $log->meta = "{\"has_recordings\":true}";
209
    }
210
    if (!$DB->insert_record('bigbluebuttonbn_logs', $log)) {
211
        return false;
212
    }
213
    return true;
214
}
215
/**
216
 * Return a small object with summary information about what a
217
 * user has done with a given particular instance of this module
218
 * Used for user activity reports.
219
 * $return->time = the time they did it
220
 * $return->info = a short text description.
221
 *
222
 * @return bool
223
 */
224
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...
225
    global $DB;
226
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
227
                                                              'bigbluebuttonbnid' => $bigbluebuttonbn->id,
228
                                                              'userid' => $user->id,
229
                                                              'log' => 'Join', ), '*');
230
    if ($completed > 0) {
231
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
232
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
233
            get_string('view_message_times', 'bigbluebuttonbn');
234
    }
235
    return '';
236
}
237
238
/**
239
 * Print a detailed representation of what a user has done with
240
 * a given particular instance of this module, for user activity reports.
241
 *
242
 * @return bool
243
 */
244
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...
245
    global $DB;
246
    $completed = $DB->count_recorda('bigbluebuttonbn_logs', array('courseid' => $course->id,
247
                                                              'bigbluebuttonbnid' => $bigbluebuttonbn->id,
248
                                                              'userid' => $user->id,
249
                                                              'log' => 'Join', ), '*', IGNORE_MULTIPLE);
250
    return $completed > 0;
251
}
252
253
/**
254
 * Returns all other caps used in module.
255
 *
256
 * @return string[]
257
 */
258
function bigbluebuttonbn_get_extra_capabilities() {
259
    return array('moodle/site:accessallgroups');
260
}
261
262
/**
263
 * List of view style log actions.
264
 *
265
 * @return string[]
266
 */
267
function bigbluebuttonbn_get_view_actions() {
268
    return array('view', 'view all');
269
}
270
271
/**
272
 * List of update style log actions.
273
 *
274
 * @return string[]
275
 */
276
function bigbluebuttonbn_get_post_actions() {
277
    return array('update', 'add', 'create', 'join', 'end', 'left', 'publish', 'unpublish', 'delete');
278
}
279
280
/**
281
 * @param array $courses
282
 * @param array $htmlarray Passed by reference
283
 */
284
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
285
    if (empty($courses) || !is_array($courses)) {
286
        return array();
287
    }
288
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
289
    foreach ($bns as $bn) {
290
        $now = time();
291
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
292
            // A bigbluebuttonbn is scheduled.
293
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
294
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
295
            }
296
            $htmlarray[$bn->course]['bigbluebuttonbn'] = bigbluebuttonbn_print_overview_element($bn, $now);
297
        }
298
    }
299
}
300
301
/**
302
 * @global object
303
 *
304
 * @param array $bigbluebuttonbn
305
 * @param int $now
306
 */
307
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
308
    global $CFG;
309
    $start = 'started_at';
310
    if ($bigbluebuttonbn->openingtime > $now) {
311
        $start = 'starts_at';
312
    }
313
    $classes = '';
314
    if ($bigbluebuttonbn->visible) {
315
        $classes = 'class="dimmed" ';
316
    }
317
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
318
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
319
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
320
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
321
    $str .= '  </div>'."\n";
322
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
323
        '</div>'."\n";
324
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
325
      .'</div>'."\n";
326
    $str .= '</div>'."\n";
327
    return $str;
328
}
329
330
/**
331
 * Given a course_module object, this function returns any
332
 * "extra" information that may be needed when printing
333
 * this activity in a course listing.
334
 * See get_array_of_activities() in course/lib.php.
335
 *
336
 * @global object
337
 *
338
 * @param object $coursemodule
339
 *
340
 * @return null|cached_cm_info
341
 */
342
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
343
    global $DB;
344
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance),
345
        'id, name, intro, introformat');
346
    if (!$bigbluebuttonbn) {
347
        return null;
348
    }
349
    $info = new cached_cm_info();
350
    $info->name = $bigbluebuttonbn->name;
351
    if ($coursemodule->showdescription) {
352
        // Convert intro to html. Do not filter cached version, filters run at display time.
353
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
354
    }
355
    return $info;
356
}
357
358
/**
359
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
360
 *
361
 * @global object
362
 *
363
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
364
 **/
365
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
366
    $bigbluebuttonbn->timemodified = time();
367
    if (!isset($bigbluebuttonbn->timecreated) || !$bigbluebuttonbn->timecreated) {
368
        $bigbluebuttonbn->timecreated = time();
369
        // Assign password only if it is a new activity.
370
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
371
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12);
372
        $bigbluebuttonbn->timemodified = 0;
373
    }
374
    if (!isset($bigbluebuttonbn->wait)) {
375
        $bigbluebuttonbn->wait = 0;
376
    }
377
    if (!isset($bigbluebuttonbn->record)) {
378
        $bigbluebuttonbn->record = 0;
379
    }
380
    if (!isset($bigbluebuttonbn->recordings_html)) {
381
        $bigbluebuttonbn->recordings_html = 0;
382
    }
383
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
384
        $bigbluebuttonbn->recordings_deleted = 0;
385
    }
386
    if (!isset($bigbluebuttonbn->recordings_imported)) {
387
        $bigbluebuttonbn->recordings_imported = 0;
388
    }
389
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
390
}
391
392
/**
393
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
394
 *
395
 * @global object
396
 *
397
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
398
 **/
399
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
400
    bigbluebuttonbn_update_media_file($bigbluebuttonbn);
401
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
402
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
403
    }
404
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
405
}
406
407
/**
408
 * Generates a message on insert/update which is sent to all users enrolled.
409
 *
410
 * @global object
411
 *
412
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
413
 **/
414
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
415
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
416
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
417
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
418
    }
419
    $context = context_course::instance($bigbluebuttonbn->course);
420
    \mod_bigbluebuttonbn\locallib\notifier::notification_process($context, $bigbluebuttonbn, $action);
421
}
422
423
/**
424
 * Generates an event after a bigbluebuttonbn insert/update.
425
 *
426
 * @global object
427
 *
428
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
429
 **/
430
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
431
    global $DB;
432
    // Delete evento to the calendar when/if openingtime is NOT set.
433
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
434
        $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id));
435
        return;
436
    }
437
    // Add evento to the calendar as openingtime is set.
438
    $event = new stdClass();
439
    $event->name = $bigbluebuttonbn->name;
440
    $event->courseid = $bigbluebuttonbn->course;
441
    $event->groupid = 0;
442
    $event->userid = 0;
443
    $event->modulename = 'bigbluebuttonbn';
444
    $event->instance = $bigbluebuttonbn->id;
445
    $event->timestart = $bigbluebuttonbn->openingtime;
446
    $event->durationtime = 0;
447
    if ($bigbluebuttonbn->closingtime) {
448
        $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
449
    }
450
    $event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
451
        'instance' => $bigbluebuttonbn->id));
452
    if ($event->id) {
453
        $calendarevent = calendar_event::load($event->id);
454
        $calendarevent->update($event);
455
        return;
456
    }
457
    calendar_event::create($event);
458
}
459
460
/**
461
 * Update the bigbluebuttonbn activity to include any file
462
 * that was uploaded, or if there is none, set the
463
 * presentation field to blank.
464
 *
465
 * @param int      $bigbluebuttonbnid the bigbluebuttonbn id
0 ignored issues
show
Documentation introduced by
There is no parameter named $bigbluebuttonbnid. Did you maybe mean $bigbluebuttonbn?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
466
 * @param stdClass $context            the context
0 ignored issues
show
Bug introduced by
There is no parameter named $context. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
467
 * @param int      $draftitemid        the draft item
0 ignored issues
show
Bug introduced by
There is no parameter named $draftitemid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
468
 */
469
function bigbluebuttonbn_update_media_file(&$bigbluebuttonbn) {
470
    global $DB;
471
    $draftitemid = isset($bigbluebuttonbn->presentation) ? $bigbluebuttonbn->presentation : null;
472
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
473
    // Set the filestorage object.
474
    $fs = get_file_storage();
475
    // Save the file if it exists that is currently in the draft area.
476
    file_save_draft_area_files($draftitemid, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
477
    // Get the file if it exists.
478
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
479
        'itemid, filepath, filename', false);
480
    // Check that there is a file to process.
481
    $filesrc = '';
482
    if (count($files) == 1) {
483
        // Get the first (and only) file.
484
        $file = reset($files);
485
        $filesrc = '/'.$file->get_filename();
486
    }
487
    // Set the presentation column in the bigbluebuttonbn table.
488
    $DB->set_field('bigbluebuttonbn', 'presentation', $filesrc, array('id' => $bigbluebuttonbn->id));
489
}
490
491
/**
492
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
493
 *
494
 * @category files
495
 *
496
 * @param stdClass $course        course object
497
 * @param stdClass $cm            course module object
498
 * @param stdClass $context       context object
499
 * @param string   $filearea      file area
500
 * @param array    $args          extra arguments
501
 * @param bool     $forcedownload whether or not force download
502
 * @param array    $options       additional options affecting the file serving
503
 *
504
 * @return false|null false if file not found, does not return if found - justsend the file
505
 */
506
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
507
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
508
        return false;
509
    }
510
511
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
512
    if (!$file) {
513
        return false;
514
    }
515
516
    // Finally send the file.
517
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
518
}
519
520
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
521
    if ($context->contextlevel != CONTEXT_MODULE) {
522
        return false;
523
    }
524
525
    if ($filearea !== 'presentation') {
526
        return false;
527
    }
528
529
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
530
        return false;
531
    }
532
533
    return true;
534
}
535
536
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
537
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
538
    if (!$filename) {
539
        return false;
540
    }
541
542
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
543
    $fs = get_file_storage();
544
    $file = $fs->get_file_by_hash(sha1($fullpath));
545
    if (!$file || $file->is_directory()) {
546
        return false;
547
    }
548
549
    return $file;
550
}
551
552
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
553
    global $DB;
554
555
    if (count($args) > 1) {
556
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
557
            return;
558
        }
559
560
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
561
        $noncekey = sha1($bigbluebuttonbn->id);
562
        $presentationnonce = $cache->get($noncekey);
563
        $noncevalue = $presentationnonce['value'];
564
        $noncecounter = $presentationnonce['counter'];
565
566
        if ($args['0'] != $noncevalue) {
567
            return;
568
        }
569
570
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
571
        $noncecounter += 1;
572
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
573
        if ($noncecounter == 2) {
574
            $cache->delete($noncekey);
575
        }
576
577
        return $args['1'];
578
    }
579
580
    require_course_login($course, true, $cm);
581
582
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
583
        return;
584
    }
585
586
    return implode('/', $args);
587
}
588
589
/**
590
 * Returns an array of file areas.
591
 *
592
 * @category files
593
 *
594
 * @return array a list of available file areas
595
 */
596
function bigbluebuttonbn_get_file_areas() {
597
    $areas = array();
598
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
599
600
    return $areas;
601
}
602