Completed
Push — v2.2-stable ( 2d3598...3133d1 )
by Jesus
02:22
created

lib.php ➔ bigbluebuttonbn_reset_userdata()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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