Completed
Pull Request — master (#110)
by
unknown
03:01
created

lib.php ➔ bigbluebuttonbn_default_presentation_get_file()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 4
dl 0
loc 32
rs 9.0968
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 (!class_exists('\Firebase\JWT\JWT') &&
44
    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_ADD string of event add for bigbluebuttonbn_logs */
69
const BIGBLUEBUTTONBN_LOG_EVENT_ADD = 'Add';
70
/** @var BIGBLUEBUTTONBN_LOG_EVENT_EDIT string of event edit for bigbluebuttonbn_logs */
71
const BIGBLUEBUTTONBN_LOG_EVENT_EDIT = 'Edit';
72
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string of event create for bigbluebuttonbn_logs */
73
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
74
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string of event join for bigbluebuttonbn_logs */
75
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
76
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string of event logout for bigbluebuttonbn_logs */
77
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
78
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string of event import for bigbluebuttonbn_logs */
79
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
80
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string of event delete for bigbluebuttonbn_logs */
81
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
82
/** @var BIGBLUEBUTTON_LOG_EVENT_CALLBACK string defines the bigbluebuttonbn callback event */
83
const BIGBLUEBUTTON_LOG_EVENT_CALLBACK = 'Callback';
84
/**
85
 * Indicates API features that the forum supports.
86
 *
87
 * @uses FEATURE_IDNUMBER
88
 * @uses FEATURE_GROUPS
89
 * @uses FEATURE_GROUPINGS
90
 * @uses FEATURE_GROUPMEMBERSONLY
91
 * @uses FEATURE_MOD_INTRO
92
 * @uses FEATURE_BACKUP_MOODLE2
93
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
94
 * @uses FEATURE_COMPLETION_HAS_RULES
95
 * @uses FEATURE_GRADE_HAS_GRADE
96
 * @uses FEATURE_GRADE_OUTCOMES
97
 * @uses FEATURE_SHOW_DESCRIPTION
98
 * @param string $feature
99
 * @return mixed True if yes (some features may use other values)
100
 */
101
function bigbluebuttonbn_supports($feature) {
102
    if (!$feature) {
103
        return null;
104
    }
105
    $features = array(
106
        (string) FEATURE_IDNUMBER => true,
107
        (string) FEATURE_GROUPS => true,
108
        (string) FEATURE_GROUPINGS => true,
109
        (string) FEATURE_GROUPMEMBERSONLY => true,
110
        (string) FEATURE_MOD_INTRO => true,
111
        (string) FEATURE_BACKUP_MOODLE2 => true,
112
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
113
        (string) FEATURE_GRADE_HAS_GRADE => false,
114
        (string) FEATURE_GRADE_OUTCOMES => false,
115
        (string) FEATURE_SHOW_DESCRIPTION => true,
116
    );
117
    if (isset($features[(string) $feature])) {
118
        return $features[$feature];
119
    }
120
    return null;
121
}
122
123
/**
124
 * Given an object containing all the necessary data,
125
 * (defined by the form in mod_form.php) this function
126
 * will create a new instance and return the id number
127
 * of the new instance.
128
 *
129
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
130
 * @return int The id of the newly inserted bigbluebuttonbn record
131
 */
132 View Code Duplication
function bigbluebuttonbn_add_instance($bigbluebuttonbn) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    global $DB;
134
    // Excecute preprocess.
135
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
136
    // Pre-set initial values.
137
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
138
    // Insert a record.
139
    $bigbluebuttonbn->id = $DB->insert_record('bigbluebuttonbn', $bigbluebuttonbn);
140
    // Encode meetingid.
141
    $bigbluebuttonbn->meetingid = bigbluebuttonbn_unique_meetingid_seed();
142
    // Set the meetingid column in the bigbluebuttonbn table.
143
    $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn->meetingid, array('id' => $bigbluebuttonbn->id));
144
    // Log insert action.
145
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_ADD);
146
    // Complete the process.
147
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
148
    return $bigbluebuttonbn->id;
149
}
150
151
/**
152
 * Given an object containing all the necessary data,
153
 * (defined by the form in mod_form.php) this function
154
 * will update an existing instance with new data.
155
 *
156
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
157
 * @return bool Success/Fail
158
 */
159 View Code Duplication
function bigbluebuttonbn_update_instance($bigbluebuttonbn) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    global $DB;
161
    // Excecute preprocess.
162
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
163
    // Pre-set initial values.
164
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
165
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
166
    // Update a record.
167
    $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
168
    // Get the meetingid column in the bigbluebuttonbn table.
169
    $bigbluebuttonbn->meetingid = (string)$DB->get_field('bigbluebuttonbn', 'meetingid', array('id' => $bigbluebuttonbn->id));
170
    // Log update action.
171
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_EDIT);
172
    // Complete the process.
173
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
174
    return true;
175
}
176
177
/**
178
 * Given an ID of an instance of this module,
179
 * this function will permanently delete the instance
180
 * and any data that depends on it.
181
 *
182
 * @param int $id Id of the module instance
183
 *
184
 * @return bool Success/Failure
185
 */
186
function bigbluebuttonbn_delete_instance($id) {
187
    global $DB;
188
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
189
    if (!$bigbluebuttonbn) {
190
        return false;
191
    }
192
    // TODO: End the meeting if it is running.
193
194
    // Perform delete.
195
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
196
        return false;
197
    }
198
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
199
        return false;
200
    }
201
    // Log action performed.
202
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
203
}
204
205
/**
206
 * Given an ID of an instance of this module,
207
 * this function will permanently delete the data that depends on it.
208
 *
209
 * @param object $bigbluebuttonbn Id of the module instance
210
 *
211
 * @return bool Success/Failure
212
 */
213
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
214
    global $DB;
215
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
216
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
217
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
218
    $meta = "{\"has_recordings\":" . empty($logs) ? "true" : "false" . "}";
219
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_DELETE, [], $meta);
220
}
221
222
/**
223
 * Return a small object with summary information about what a
224
 * user has done with a given particular instance of this module
225
 * Used for user activity reports.
226
 * $return->time = the time they did it
227
 * $return->info = a short text description.
228
 *
229
 * @param object $course
230
 * @param object $user
231
 * @param object $mod
232
 * @param object $bigbluebuttonbn
233
 *
234
 * @return bool
235
 */
236
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...
237
    global $DB;
238
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
239
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ), '*');
240
    if ($completed > 0) {
241
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
242
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
243
            get_string('view_message_times', 'bigbluebuttonbn');
244
    }
245
    return '';
246
}
247
248
/**
249
 * Print a detailed representation of what a user has done with
250
 * a given particular instance of this module, for user activity reports.
251
 *
252
 * @param object $course
253
 * @param object $user
254
 * @param object $mod
255
 * @param object $bigbluebuttonbn
256
 *
257
 * @return bool
258
 */
259
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...
260
    global $DB;
261
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
262
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ),
263
        '*', IGNORE_MULTIPLE);
264
    return $completed > 0;
265
}
266
267
/**
268
 * Returns all other caps used in module.
269
 *
270
 * @return string[]
271
 */
272
function bigbluebuttonbn_get_extra_capabilities() {
273
    return array('moodle/site:accessallgroups');
274
}
275
276
/**
277
 * This function is used by the reset_course_userdata function in moodlelib.
278
 * @param $data the data submitted from the reset course.
279
 * @return array status array
280
 */
281
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...
282
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
283
    // See MDL-9367.
284
    return array();
285
}
286
287
/**
288
 * List of view style log actions.
289
 *
290
 * @return string[]
291
 */
292
function bigbluebuttonbn_get_view_actions() {
293
    return array('view', 'view all');
294
}
295
296
/**
297
 * List of update style log actions.
298
 *
299
 * @return string[]
300
 */
301
function bigbluebuttonbn_get_post_actions() {
302
    return array('update', 'add', 'delete');
303
}
304
305
/**
306
 * Print an overview of all bigbluebuttonbn instances for the courses.
307
 *
308
 * @param array $courses
309
 * @param array $htmlarray Passed by reference
310
 *
311
 * @return void
312
 */
313
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
314
    if (empty($courses) || !is_array($courses)) {
315
        return array();
316
    }
317
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
318
    foreach ($bns as $bn) {
319
        $now = time();
320
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
321
            // A bigbluebuttonbn is scheduled.
322
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
323
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
324
            }
325
            $htmlarray[$bn->course]['bigbluebuttonbn'] = bigbluebuttonbn_print_overview_element($bn, $now);
326
        }
327
    }
328
}
329
330
/**
331
 * Print an overview of a bigbluebuttonbn instance.
332
 *
333
 * @param array $bigbluebuttonbn
334
 * @param int $now
335
 *
336
 * @return string
337
 */
338
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
339
    global $CFG;
340
    $start = 'started_at';
341
    if ($bigbluebuttonbn->openingtime > $now) {
342
        $start = 'starts_at';
343
    }
344
    $classes = '';
345
    if ($bigbluebuttonbn->visible) {
346
        $classes = 'class="dimmed" ';
347
    }
348
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
349
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
350
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
351
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
352
    $str .= '  </div>'."\n";
353
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
354
        '</div>'."\n";
355
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
356
      .'</div>'."\n";
357
    $str .= '</div>'."\n";
358
    return $str;
359
}
360
361
/**
362
 * Given a course_module object, this function returns any
363
 * "extra" information that may be needed when printing
364
 * this activity in a course listing.
365
 * See get_array_of_activities() in course/lib.php.
366
 *
367
 * @param object $coursemodule
368
 *
369
 * @return null|cached_cm_info
370
 */
371
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
372
    global $DB;
373
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance),
374
        'id, name, intro, introformat');
375
    if (!$bigbluebuttonbn) {
376
        return null;
377
    }
378
    $info = new cached_cm_info();
379
    $info->name = $bigbluebuttonbn->name;
380
    if ($coursemodule->showdescription) {
381
        // Convert intro to html. Do not filter cached version, filters run at display time.
382
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
383
    }
384
    return $info;
385
}
386
387
/**
388
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
389
 *
390
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
391
 *
392
 * @return void
393
 **/
394
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
395
    bigbluebuttonbn_process_pre_save_instance($bigbluebuttonbn);
396
    bigbluebuttonbn_process_pre_save_checkboxes($bigbluebuttonbn);
397
    bigbluebuttonbn_process_pre_save_common($bigbluebuttonbn);
398
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
399
}
400
401
/**
402
 * Runs process for defining the instance (insert/update).
403
 *
404
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
405
 *
406
 * @return void
407
 **/
408
function bigbluebuttonbn_process_pre_save_instance(&$bigbluebuttonbn) {
409
    $bigbluebuttonbn->timemodified = time();
410
    if ((integer)$bigbluebuttonbn->instance == 0) {
411
        $bigbluebuttonbn->meetingid = 0;
412
        $bigbluebuttonbn->timecreated = time();
413
        $bigbluebuttonbn->timemodified = 0;
414
        // As it is a new activity, assign passwords.
415
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
416
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12, $bigbluebuttonbn->moderatorpass);
417
    }
418
}
419
420
/**
421
 * Runs process for assigning default value to checkboxes.
422
 *
423
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
424
 *
425
 * @return void
426
 **/
427
function bigbluebuttonbn_process_pre_save_checkboxes(&$bigbluebuttonbn) {
428
    if (!isset($bigbluebuttonbn->wait)) {
429
        $bigbluebuttonbn->wait = 0;
430
    }
431
    if (!isset($bigbluebuttonbn->record)) {
432
        $bigbluebuttonbn->record = 0;
433
    }
434
    if (!isset($bigbluebuttonbn->recordings_html)) {
435
        $bigbluebuttonbn->recordings_html = 0;
436
    }
437
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
438
        $bigbluebuttonbn->recordings_deleted = 0;
439
    }
440
    if (!isset($bigbluebuttonbn->recordings_imported)) {
441
        $bigbluebuttonbn->recordings_imported = 0;
442
    }
443
    if (!isset($bigbluebuttonbn->recordings_preview)) {
444
        $bigbluebuttonbn->recordings_preview = 0;
445
    }
446
    if (!isset($bigbluebuttonbn->muteonstart)) {
447
        $bigbluebuttonbn->muteonstart = 0;
448
    }
449
}
450
451
/**
452
 * Runs process for wipping common settings when 'recordings only'.
453
 *
454
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
455
 *
456
 * @return void
457
 **/
458
function bigbluebuttonbn_process_pre_save_common(&$bigbluebuttonbn) {
459
    // Make sure common settings are removed when 'recordings only'.
460
    if ($bigbluebuttonbn->type == BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY) {
461
        $bigbluebuttonbn->groupmode = 0;
462
        $bigbluebuttonbn->groupingid = 0;
463
    }
464
}
465
466
/**
467
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
468
 *
469
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
470
 *
471
 * @return void
472
 **/
473
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
474
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
475
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
476
    }
477
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
478
    bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn);
479
}
480
481
/**
482
 * Generates a message on insert/update which is sent to all users enrolled.
483
 *
484
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
485
 *
486
 * @return void
487
 **/
488
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
489
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
490
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
491
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
492
    }
493
    $context = context_course::instance($bigbluebuttonbn->course);
494
    \mod_bigbluebuttonbn\locallib\notifier::notification_process($context, $bigbluebuttonbn, $action);
495
}
496
497
/**
498
 * Generates an event after a bigbluebuttonbn insert/update.
499
 *
500
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
501
 *
502
 * @return void
503
 **/
504
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
505
    global $DB;
506
    $eventid = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
507
        'instance' => $bigbluebuttonbn->id));
508
    // Delete the event from calendar when/if openingtime is NOT set.
509
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
510
        if ($eventid) {
511
            $calendarevent = calendar_event::load($eventid);
512
            $calendarevent->delete();
513
        }
514
        return;
515
    }
516
    // Add evento to the calendar as openingtime is set.
517
    $event = new stdClass();
518
    $event->eventtype = BIGBLUEBUTTON_EVENT_MEETING_START;
519
    $event->type = CALENDAR_EVENT_TYPE_ACTION;
520
    $event->name = get_string('calendarstarts', 'bigbluebuttonbn', $bigbluebuttonbn->name);
521
    $event->description = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $bigbluebuttonbn->coursemodule);
522
    $event->courseid = $bigbluebuttonbn->course;
523
    $event->groupid = 0;
524
    $event->userid = 0;
525
    $event->modulename = 'bigbluebuttonbn';
526
    $event->instance = $bigbluebuttonbn->id;
527
    $event->timestart = $bigbluebuttonbn->openingtime;
528
    $event->timeduration = 0;
529
    $event->timesort = $event->timestart;
530
    $event->visible = instance_is_visible('bigbluebuttonbn', $bigbluebuttonbn);
531
    $event->priority = null;
532
    // Update the event in calendar when/if eventid was found.
533
    if ($eventid) {
534
        $event->id = $eventid;
535
        $calendarevent = calendar_event::load($eventid);
536
        $calendarevent->update($event);
537
        return;
538
    }
539
    calendar_event::create($event);
540
}
541
542
function bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn) {
543
    if (!empty($bigbluebuttonbn->completionexpected)) {
544
        \core_completion\api::update_completion_date_event(
545
            $bigbluebuttonbn->coursemodule,
546
            'bigbluebuttonbn',
547
            $bigbluebuttonbn->id, $bigbluebuttonbn->completionexpected
548
          );
549
    }
550
}
551
/**
552
 * Get a full path to the file attached as a preuploaded presentation
553
 * or if there is none, set the presentation field will be set to blank.
554
 *
555
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
556
 *
557
 * @return string
558
 */
559
function bigbluebuttonbn_get_media_file(&$bigbluebuttonbn) {
560
    if (!isset($bigbluebuttonbn->presentation) || $bigbluebuttonbn->presentation == '') {
561
        return '';
562
    }
563
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
564
    // Set the filestorage object.
565
    $fs = get_file_storage();
566
    // Save the file if it exists that is currently in the draft area.
567
    file_save_draft_area_files($bigbluebuttonbn->presentation, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
568
    // Get the file if it exists.
569
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
570
        'itemid, filepath, filename', false);
571
    // Check that there is a file to process.
572
    $filesrc = '';
573
    if (count($files) == 1) {
574
        // Get the first (and only) file.
575
        $file = reset($files);
576
        $filesrc = '/'.$file->get_filename();
577
    }
578
    return $filesrc;
579
}
580
581
/**
582
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
583
 *
584
 * @category files
585
 *
586
 * @param stdClass $course        course object
587
 * @param stdClass $cm            course module object
588
 * @param stdClass $context       context object
589
 * @param string   $filearea      file area
590
 * @param array    $args          extra arguments
591
 * @param bool     $forcedownload whether or not force download
592
 * @param array    $options       additional options affecting the file serving
593
 *
594
 * @return false|null false if file not found, does not return if found - justsend the file
595
 */
596
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
597
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
598
        return false;
599
    }
600
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
601
    if (empty($file)) {
602
        return false;
603
    }
604
    // Finally send the file.
605
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
606
}
607
608
/**
609
 * Helper for validating pluginfile.
610
 * @param stdClass $context       context object
611
 * @param string   $filearea      file area
612
 *
613
 * @return false|null false if file not valid
614
 */
615
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
616
617
    // Can be in context module or in context_system (if is the presentation by default).
618
    if (!in_array($context->contextlevel, array(CONTEXT_MODULE, CONTEXT_SYSTEM))) {
619
        return false;
620
    }
621
622
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
623
        return false;
624
    }
625
626
    return true;
627
}
628
629
/**
630
 * Helper for getting pluginfile.
631
 *
632
 * @param stdClass $course        course object
633
 * @param stdClass $cm            course module object
634
 * @param stdClass $context       context object
635
 * @param string   $filearea      file area
636
 * @param array    $args          extra arguments
637
 *
638
 * @return object
639
 */
640
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
641
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
642
    if (!$filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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

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

Loading history...
643
        return false;
644
    }
645
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
646
    $fs = get_file_storage();
647
    $file = $fs->get_file_by_hash(sha1($fullpath));
648
    if (!$file || $file->is_directory()) {
649
        return false;
650
    }
651
    return $file;
652
}
653
654
/**
655
 * Helper for give access to the file configured in setting as default presentation.
656
 *
657
 * @param stdClass $course        course object
658
 * @param stdClass $cm            course module object
659
 * @param stdClass $context       context object
660
 * @param array    $args          extra arguments
661
 *
662
 * @return array
663
 */
664
function bigbluebuttonbn_default_presentation_get_file($course, $cm, $context, $args) {
665
666
    // The difference with the standard bigbluebuttonbn_pluginfile_filename() are.
667
    // - Context is system, so we don't need to check the cmid in this case.
668
    // - The area is "presentationdefault_cache".
669
    if (count($args) > 1) {
670
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION,
671
            'mod_bigbluebuttonbn',
672
            'presentationdefault_cache');
673
674
        $noncekey = sha1($context->id);
675
        $presentationnonce = $cache->get($noncekey);
676
        $noncevalue = $presentationnonce['value'];
677
        $noncecounter = $presentationnonce['counter'];
678
        if ($args['0'] != $noncevalue) {
679
            return;
680
        }
681
682
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
683
        $noncecounter += 1;
684
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
685
        if ($noncecounter == 2) {
686
            $cache->delete($noncekey);
687
        }
688
        return($args['1']);
689
    }
690
    require_course_login($course, true, $cm);
691
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
692
        return;
693
    }
694
    return implode('/', $args);
695
}
696
697
/**
698
 * Helper for getting pluginfile name.
699
 *
700
 * @param stdClass $course        course object
701
 * @param stdClass $cm            course module object
702
 * @param stdClass $context       context object
703
 * @param array    $args          extra arguments
704
 *
705
 * @return array
706
 */
707
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
708
    global $DB;
709
710
    if ($context->contextlevel == CONTEXT_SYSTEM) {
711
        // Plugin has a file to use as default in general setting.
712
        return(bigbluebuttonbn_default_presentation_get_file($course, $cm, $context, $args));
713
    }
714
715
    if (count($args) > 1) {
716
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
717
            return;
718
        }
719
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
720
        $noncekey = sha1($bigbluebuttonbn->id);
721
        $presentationnonce = $cache->get($noncekey);
722
        $noncevalue = $presentationnonce['value'];
723
        $noncecounter = $presentationnonce['counter'];
724
        if ($args['0'] != $noncevalue) {
725
            return;
726
        }
727
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
728
        $noncecounter += 1;
729
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
730
        if ($noncecounter == 2) {
731
            $cache->delete($noncekey);
732
        }
733
        return $args['1'];
734
    }
735
    require_course_login($course, true, $cm);
736
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
737
        return;
738
    }
739
    return implode('/', $args);
740
}
741
742
/**
743
 * Returns an array of file areas.
744
 *
745
 * @category files
746
 *
747
 * @return array a list of available file areas
748
 */
749
function bigbluebuttonbn_get_file_areas() {
750
    $areas = array();
751
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
752
    $areas['presentationdefault'] = get_string('mod_form_block_presentation_default', 'bigbluebuttonbn');
753
    return $areas;
754
}
755
756
/**
757
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
758
 *
759
 * @param  stdClass $bigbluebuttonbn        bigbluebuttonbn object
760
 * @param  stdClass $course     course object
761
 * @param  stdClass $cm         course module object
762
 * @param  stdClass $context    context object
763
 * @since Moodle 3.0
764
 */
765
function bigbluebuttonbn_view($bigbluebuttonbn, $course, $cm, $context) {
766
767
    // Trigger course_module_viewed event.
768
    $params = array(
769
        'context' => $context,
770
        'objectid' => $bigbluebuttonbn->id
771
    );
772
773
    $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_activity_viewed::create($params);
774
    $event->add_record_snapshot('course_modules', $cm);
775
    $event->add_record_snapshot('course', $course);
776
    $event->add_record_snapshot('bigbluebuttonbn', $bigbluebuttonbn);
777
    $event->trigger();
778
779
    // Completion.
780
    $completion = new completion_info($course);
781
    $completion->set_module_viewed($cm);
782
}
783
784
/**
785
 * Check if the module has any update that affects the current user since a given time.
786
 *
787
 * @param  cm_info $cm course module data
788
 * @param  int $from the time to check updates from
789
 * @param  array $filter  if we need to check only specific updates
790
 * @return stdClass an object with the different type of areas indicating if they were updated or not
791
 * @since Moodle 3.2
792
 */
793
function bigbluebuttonbn_check_updates_since(cm_info $cm, $from, $filter = array()) {
794
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
795
    return $updates;
796
}
797
798
799
/**
800
 * Get icon mapping for font-awesome.
801
 */
802
function mod_bigbluebuttonbn_get_fontawesome_icon_map() {
803
    return [
804
        'mod_bigbluebuttonbn:icon' => 'icon-bigbluebutton',
805
    ];
806
}
807
808
/**
809
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
810
 *
811
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
812
 * is not displayed on the block.
813
 *
814
 * @param calendar_event $event
815
 * @param \core_calendar\action_factory $factory
816
 * @return \core_calendar\local\event\entities\action_interface|null
817
 */
818
function mod_bigbluebuttonbn_core_calendar_provide_event_action(calendar_event $event,
819
        \core_calendar\action_factory $factory) {
820
    global $CFG, $DB;
821
822
    require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
823
824
    $cm = get_fast_modinfo($event->courseid)->instances['bigbluebuttonbn'][$event->instance];
825
826
    // Check that the bigbluebuttonbn activity is open.
827
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $event->instance), '*', MUST_EXIST);
828
    $actionable = bigbluebuttonbn_get_availability_status($bigbluebuttonbn);
829
830
    $string = get_string('view_room', 'bigbluebuttonbn');
831
    $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
832
    if (groups_get_activity_groupmode($cm) == NOGROUPS) {
833
        // No groups mode.
834
        $string = get_string('view_conference_action_join', 'bigbluebuttonbn');
835
        $url = new \moodle_url('/mod/bigbluebuttonbn/bbb_view.php', array('action' => 'join',
836
            'id' => $cm->id, 'bn' => $bigbluebuttonbn->id, 'timeline' => 1));
837
    }
838
839
    return $factory->create_instance($string, $url, 1, $actionable);
840
}
841
842
/**
843
 * Register a bigbluebuttonbn event
844
 *
845
 * @param object $bigbluebuttonbn
846
 * @param string $event
847
 * @param array  $overrides
848
 * @param string $meta
849
 *
850
 * @return bool Success/Failure
851
 */
852
function bigbluebuttonbn_log($bigbluebuttonbn, $event, array $overrides = [], $meta = null) {
853
    global $DB, $USER;
854
    $log = new stdClass();
855
    // Default values.
856
    $log->courseid = $bigbluebuttonbn->course;
857
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
858
    $log->userid = $USER->id;
859
    $log->meetingid = $bigbluebuttonbn->meetingid;
860
    $log->timecreated = time();
861
    $log->log = $event;
862
    $log->meta = $meta;
863
    // Overrides.
864
    foreach ($overrides as $key => $value) {
865
        $log->$key = $value;
866
    }
867
    if ($DB->insert_record('bigbluebuttonbn_logs', $log)) {
868
        return true;
869
    }
870
    return false;
871
}
872