Completed
Push — master ( d8c9ad...1b8539 )
by Jesus
03:11
created

lib.php ➔ bigbluebuttonbn_process_post_save_completion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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