Completed
Push — master ( f98fb6...800781 )
by Jesus
02:02
created

lib.php ➔ bigbluebuttonbn_process_pre_save_checkboxes()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 1024
nop 1
dl 0
loc 32
rs 3.15
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
// JWT is included in Moodle 3.7 core, but a local package is still needed for backward compatibility.
32
if (!class_exists('\Firebase\JWT\JWT')) {
33
    if (file_exists($CFG->libdir.'/php-jwt/src/JWT.php')) {
34
        require_once($CFG->libdir.'/php-jwt/src/JWT.php');
35
    } else {
36
        require_once($CFG->dirroot.'/mod/bigbluebuttonbn/vendor/firebase/php-jwt/src/JWT.php');
37
    }
38
}
39
40
if (!isset($CFG->bigbluebuttonbn)) {
41
    $CFG->bigbluebuttonbn = array();
42
}
43
44
if (file_exists(dirname(__FILE__).'/config.php')) {
45
    require_once(dirname(__FILE__).'/config.php');
46
}
47
48
/*
49
 * DURATIONCOMPENSATION: Feature removed by configuration
50
 */
51
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
52
/*
53
 * Remove this block when restored
54
 */
55
56
/** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
57
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
58
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
59
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
60
/** @var BIGBLUEBUTTONBN_LOG_EVENT_ADD string defines the bigbluebuttonbn Add event */
61
const BIGBLUEBUTTONBN_LOG_EVENT_ADD = 'Add';
62
/** @var BIGBLUEBUTTONBN_LOG_EVENT_EDIT string defines the bigbluebuttonbn Edit event */
63
const BIGBLUEBUTTONBN_LOG_EVENT_EDIT = 'Edit';
64
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string defines the bigbluebuttonbn Create event */
65
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
66
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string defines the bigbluebuttonbn Join event */
67
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
68
/** @var BIGBLUEBUTTONBN_LOG_EVENT_PLAYED string defines the bigbluebuttonbn Playback event */
69
const BIGBLUEBUTTONBN_LOG_EVENT_PLAYED = 'Played';
70
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string defines the bigbluebuttonbn Logout event */
71
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
72
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string defines the bigbluebuttonbn Import event */
73
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
74
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string defines the bigbluebuttonbn Delete event */
75
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
76
/** @var BIGBLUEBUTTON_LOG_EVENT_CALLBACK string defines the bigbluebuttonbn Callback event */
77
const BIGBLUEBUTTON_LOG_EVENT_CALLBACK = 'Callback';
78
/** @var BIGBLUEBUTTON_LOG_EVENT_SUMMARY string defines the bigbluebuttonbn Summary event */
79
const BIGBLUEBUTTON_LOG_EVENT_SUMMARY = 'Summary';
80
/**
81
 * Indicates API features that the bigbluebuttonbn supports.
82
 *
83
 * @uses FEATURE_IDNUMBER
84
 * @uses FEATURE_GROUPS
85
 * @uses FEATURE_GROUPINGS
86
 * @uses FEATURE_GROUPMEMBERSONLY
87
 * @uses FEATURE_MOD_INTRO
88
 * @uses FEATURE_BACKUP_MOODLE2
89
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
90
 * @uses FEATURE_COMPLETION_HAS_RULES
91
 * @uses FEATURE_GRADE_HAS_GRADE
92
 * @uses FEATURE_GRADE_OUTCOMES
93
 * @uses FEATURE_SHOW_DESCRIPTION
94
 * @param string $feature
95
 * @return mixed True if yes (some features may use other values)
96
 */
97
function bigbluebuttonbn_supports($feature) {
98
    if (!$feature) {
99
        return null;
100
    }
101
    $features = array(
102
        (string) FEATURE_IDNUMBER => true,
103
        (string) FEATURE_GROUPS => true,
104
        (string) FEATURE_GROUPINGS => true,
105
        (string) FEATURE_GROUPMEMBERSONLY => true,
106
        (string) FEATURE_MOD_INTRO => true,
107
        (string) FEATURE_BACKUP_MOODLE2 => true,
108
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
109
        (string) FEATURE_COMPLETION_HAS_RULES => true,
110
        (string) FEATURE_GRADE_HAS_GRADE => false,
111
        (string) FEATURE_GRADE_OUTCOMES => false,
112
        (string) FEATURE_SHOW_DESCRIPTION => true,
113
    );
114
    if (isset($features[(string) $feature])) {
115
        return $features[$feature];
116
    }
117
    return null;
118
}
119
120
/**
121
 * Obtains the automatic completion state for this bigbluebuttonbn based on any conditions
122
 * in bigbluebuttonbn settings.
123
 *
124
 * @param object $course Course
125
 * @param object $cm Course-module
126
 * @param int $userid User ID
127
 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
128
 *
129
 * @return bool True if completed, false if not. (If no conditions, then return
130
 *   value depends on comparison type)
131
 */
132
function bigbluebuttonbn_get_completion_state($course, $cm, $userid, $type) {
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...
133
    global $DB;
134
135
    // Get bigbluebuttonbn details.
136
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance), '*',
137
            MUST_EXIST);
138
    if (!$bigbluebuttonbn) {
139
        throw new Exception("Can't find bigbluebuttonbn {$cm->instance}");
140
    }
141
142
    // Default return value.
143
    $result = $type;
144
145
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
146
    $sql .= "WHERE bigbluebuttonbnid = ? AND userid = ? AND log = ?";
147
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, $userid, BIGBLUEBUTTON_LOG_EVENT_SUMMARY));
148
149
    if ($bigbluebuttonbn->completionattendance) {
150
        if (!$logs) {
151
            // As completion by attendance was required, the activity hasn't been completed.
152
            return false;
153
        }
154
        $attendancecount = 0;
155
        foreach ($logs as $log) {
156
            $summary = json_decode($log->meta);
157
            $attendancecount += $summary->data->duration;
158
        }
159
        $attendancecount /= 60;
160
        $value = $bigbluebuttonbn->completionattendance <= $attendancecount;
161
        if ($type == COMPLETION_AND) {
162
            $result = $result && $value;
163
        } else {
164
            $result = $result || $value;
165
        }
166
    }
167
168 View Code Duplication
    if ($bigbluebuttonbn->completionengagementchats) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
169
        if (!$logs) {
170
            // As completion by engagement with chat was required, the activity hasn't been completed.
171
            return false;
172
        }
173
        $engagementchatscount = 0;
174
        foreach ($logs as $log) {
175
            $summary = json_decode($log->meta);
176
            $engagementchatscount += $summary->data->engagement->chats;
177
        }
178
        $value = $bigbluebuttonbn->completionengagementchats <= $engagementchatscount;
179
        if ($type == COMPLETION_AND) {
180
            $result = $result && $value;
181
        } else {
182
            $result = $result || $value;
183
        }
184
    }
185
186 View Code Duplication
    if ($bigbluebuttonbn->completionengagementtalks) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
187
        if (!$logs) {
188
            // As completion by engagement with talk was required, the activity hasn't been completed.
189
            return false;
190
        }
191
        $engagementtalkscount = 0;
192
        foreach ($logs as $log) {
193
            $summary = json_decode($log->meta);
194
            $engagementtalkscount += $summary->data->engagement->talks;
195
        }
196
        $value = $bigbluebuttonbn->completionengagementtalks <= $engagementtalkscount;
197
        if ($type == COMPLETION_AND) {
198
            $result = $result && $value;
199
        } else {
200
            $result = $result || $value;
201
        }
202
    }
203
204
    return $result;
205
}
206
207
/**
208
 * Given an object containing all the necessary data,
209
 * (defined by the form in mod_form.php) this function
210
 * will create a new instance and return the id number
211
 * of the new instance.
212
 *
213
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
214
 * @return int The id of the newly inserted bigbluebuttonbn record
215
 */
216 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...
217
    global $DB;
218
    // Excecute preprocess.
219
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
220
    // Pre-set initial values.
221
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
222
    // Insert a record.
223
    $bigbluebuttonbn->id = $DB->insert_record('bigbluebuttonbn', $bigbluebuttonbn);
224
    // Encode meetingid.
225
    $bigbluebuttonbn->meetingid = bigbluebuttonbn_unique_meetingid_seed();
226
    // Set the meetingid column in the bigbluebuttonbn table.
227
    $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn->meetingid, array('id' => $bigbluebuttonbn->id));
228
    // Log insert action.
229
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_ADD);
230
    // Complete the process.
231
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
232
    return $bigbluebuttonbn->id;
233
}
234
235
/**
236
 * Given an object containing all the necessary data,
237
 * (defined by the form in mod_form.php) this function
238
 * will update an existing instance with new data.
239
 *
240
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
241
 * @return bool Success/Fail
242
 */
243 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...
244
    global $DB;
245
    // Excecute preprocess.
246
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
247
    // Pre-set initial values.
248
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
249
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
250
    // Update a record.
251
    $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
252
    // Get the meetingid column in the bigbluebuttonbn table.
253
    $bigbluebuttonbn->meetingid = (string)$DB->get_field('bigbluebuttonbn', 'meetingid', array('id' => $bigbluebuttonbn->id));
254
    // Log update action.
255
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_EDIT);
256
    // Complete the process.
257
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
258
    return true;
259
}
260
261
/**
262
 * Given an ID of an instance of this module,
263
 * this function will permanently delete the instance
264
 * and any data that depends on it.
265
 *
266
 * @param int $id Id of the module instance
267
 *
268
 * @return bool Success/Failure
269
 */
270
function bigbluebuttonbn_delete_instance($id) {
271
    global $DB;
272
273
    if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id))) {
274
        return false;
275
    }
276
277
    // TODO: End the meeting if it is running.
278
279
    $result = true;
280
281
    // Delete any dependent records here.
282
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
283
        $result = false;
284
    }
285
286
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
287
        $result = false;
288
    }
289
290
    // Log action performed.
291
    bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
292
293
    return $result;
294
}
295
296
/**
297
 * Given an ID of an instance of this module,
298
 * this function will permanently delete the data that depends on it.
299
 *
300
 * @param object $bigbluebuttonbn Id of the module instance
301
 *
302
 * @return bool Success/Failure
303
 */
304
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
305
    global $DB;
306
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
307
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
308
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
309
    $meta = "{\"has_recordings\":" . empty($logs) ? "true" : "false" . "}";
310
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_DELETE, [], $meta);
311
}
312
313
/**
314
 * Return a small object with summary information about what a
315
 * user has done with a given particular instance of this module
316
 * Used for user activity reports.
317
 *
318
 * @param object $course
319
 * @param object $user
320
 * @param object $mod
321
 * @param object $bigbluebuttonbn
322
 *
323
 * @return bool
324
 */
325
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...
326
    if ($completed = bigbluebuttonbn_user_complete($course, $user, $bigbluebuttonbn)) {
327
        return fullname($user) . ' ' . get_string('view_message_has_joined', 'bigbluebuttonbn') . ' ' .
328
            get_string('view_message_session_for', 'bigbluebuttonbn') . ' ' . (string) $completed . ' ' .
329
            get_string('view_message_times', 'bigbluebuttonbn');
330
    }
331
    return '';
332
}
333
334
/**
335
 * Print a detailed representation of what a user has done with
336
 * a given particular instance of this module, for user activity reports.
337
 *
338
 * @param object|int $courseorid
339
 * @param object|int $userorid
340
 * @param object $bigbluebuttonbn
341
 *
342
 * @return bool
343
 */
344
function bigbluebuttonbn_user_complete($courseorid, $userorid, $bigbluebuttonbn) {
345
    global $DB;
346
    if (is_object($courseorid)) {
347
        $course = $courseorid;
348
    } else {
349
        $course = (object)array('id' => $courseorid);
350
    }
351
    if (is_object($userorid)) {
352
        $user = $userorid;
353
    } else {
354
        $user = (object)array('id' => $userorid);
355
    }
356
    $sql = "SELECT COUNT(*) FROM {bigbluebuttonbn_logs} ";
357
    $sql .= "WHERE courseid = ? AND bigbluebuttonbnid = ? AND userid = ? AND (log = ? OR log = ?)";
358
    $result = $DB->count_records_sql($sql, array($course->id, $bigbluebuttonbn->id, $user->id,
359
                                              BIGBLUEBUTTONBN_LOG_EVENT_JOIN, BIGBLUEBUTTONBN_LOG_EVENT_PLAYED));
360
    return $result;
361
}
362
363
/**
364
 * Returns all other caps used in module.
365
 *
366
 * @return string[]
367
 */
368
function bigbluebuttonbn_get_extra_capabilities() {
369
    return array('moodle/site:accessallgroups');
370
}
371
372
/**
373
 * Define items to be reset by course/reset.php
374
 *
375
 * @return array
376
 */
377
function bigbluebuttonbn_reset_course_items() {
378
    $items = array("events" => 0, "tags" => 0, "logs" => 0);
379
    // Include recordings only if enabled.
380
    if ((boolean)\mod_bigbluebuttonbn\locallib\config::recordings_enabled()) {
381
        $items["recordings"] = 0;
382
    }
383
    return $items;
384
}
385
386
/**
387
 * Called by course/reset.php
388
 *
389
 * @param object $mform
390
 * @return void
391
 */
392
function bigbluebuttonbn_reset_course_form_definition(&$mform) {
393
    $items = bigbluebuttonbn_reset_course_items();
394
    $mform->addElement('header', 'bigbluebuttonbnheader', get_string('modulenameplural', 'bigbluebuttonbn'));
395
    foreach ($items as $item => $default) {
396
        $mform->addElement(
397
            'advcheckbox',
398
            "reset_bigbluebuttonbn_{$item}",
399
            get_string("reset{$item}", 'bigbluebuttonbn')
400
        );
401
        if ($item == 'logs' || $item == 'recordings') {
402
            $mform->addHelpButton("reset_bigbluebuttonbn_{$item}", "reset{$item}", 'bigbluebuttonbn');
403
        }
404
    }
405
}
406
407
/**
408
 * Course reset form defaults.
409
 *
410
 * @param object $course
411
 * @return array
412
 */
413
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...
414
    $formdefaults = array();
415
    $items = bigbluebuttonbn_reset_course_items();
416
    // All unchecked by default.
417
    foreach ($items as $item => $default) {
418
        $formdefaults["reset_bigbluebuttonbn_{$item}"] = $default;
419
    }
420
    return $formdefaults;
421
}
422
423
/**
424
 * This function is used by the reset_course_userdata function in moodlelib.
425
 *
426
 * @param array $data the data submitted from the reset course.
427
 * @return array status array
428
 */
429
function bigbluebuttonbn_reset_userdata($data) {
430
    $items = bigbluebuttonbn_reset_course_items();
431
    $status = array();
432
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
433
    // See MDL-9367.
434
    if (array_key_exists('recordings', $items) && !empty($data->reset_bigbluebuttonbn_recordings)) {
435
        // Remove all the recordings from a BBB server that are linked to the room/activities in this course.
436
        bigbluebuttonbn_reset_recordings($data->courseid);
437
        unset($items['recordings']);
438
        $status[] = bigbluebuttonbn_reset_getstatus('recordings');
439
    }
440
    if (!empty($data->reset_bigbluebuttonbn_tags)) {
441
        // Remove all the tags linked to the room/activities in this course.
442
        bigbluebuttonbn_reset_tags($data->courseid);
443
        unset($items['tags']);
444
        $status[] = bigbluebuttonbn_reset_getstatus('tags');
445
    }
446
    // TODO : seems to be duplicated code unless we just want to force reset tags.
447
    foreach ($items as $item => $default) {
448
        // Remove instances or elements linked to this course, others than recordings or tags.
449
        if (!empty($data->{"reset_bigbluebuttonbn_{$item}"})) {
450
            call_user_func("bigbluebuttonbn_reset_{$item}", $data->courseid);
451
            $status[] = bigbluebuttonbn_reset_getstatus($item);
452
        }
453
    }
454
    return $status;
455
}
456
457
/**
458
 * Returns status used on every defined reset action.
459
 *
460
 * @param string $item
461
 * @return array status array
462
 */
463
function bigbluebuttonbn_reset_getstatus($item) {
464
    return array('component' => get_string('modulenameplural', 'bigbluebuttonbn')
465
        , 'item' => get_string("removed{$item}", 'bigbluebuttonbn')
466
        , 'error' => false);
467
}
468
469
/**
470
 * Used by the reset_course_userdata for deleting events linked to bigbluebuttonbn instances in the course.
471
 *
472
 * @param string $courseid
473
 * @return array status array
474
 */
475
function bigbluebuttonbn_reset_events($courseid) {
476
    global $DB;
477
    // Remove all the events.
478
    return $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'courseid' => $courseid));
479
}
480
481
/**
482
 * Used by the reset_course_userdata for deleting tags linked to bigbluebuttonbn instances in the course.
483
 *
484
 * @param array $courseid
485
 * @return array status array
486
 */
487
function bigbluebuttonbn_reset_tags($courseid) {
488
    global $DB;
489
    // Remove all the tags linked to the room/activities in this course.
490
    if ($bigbluebuttonbns = $DB->get_records('bigbluebuttonbn', array('course' => $courseid))) {
491
        foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
492
            if (!$cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $courseid)) {
493
                continue;
494
            }
495
            $context = context_module::instance($cm->id);
496
            core_tag_tag::delete_instances('mod_bigbluebuttonbn', null, $context->id);
497
        }
498
    }
499
}
500
501
/**
502
 * Used by the reset_course_userdata for deleting bigbluebuttonbn_logs linked to bigbluebuttonbn instances in the course.
503
 *
504
 * @param string $courseid
505
 * @return array status array
506
 */
507
function bigbluebuttonbn_reset_logs($courseid) {
508
    global $DB;
509
    // Remove all the logs.
510
    return $DB->delete_records('bigbluebuttonbn_logs', array('courseid' => $courseid));
511
}
512
513
/**
514
 * Used by the reset_course_userdata for deleting recordings in a BBB server linked to bigbluebuttonbn instances in the course.
515
 *
516
 * @param string $courseid
517
 * @return array status array
518
 */
519
function bigbluebuttonbn_reset_recordings($courseid) {
520
    require_once(__DIR__.'/locallib.php');
521
    // Criteria for search [courseid | bigbluebuttonbn=null | subset=false | includedeleted=true].
522
    $recordings = bigbluebuttonbn_get_recordings($courseid, null, false, true);
523
    // Remove all the recordings.
524
    bigbluebuttonbn_delete_recordings(implode(",", array_keys($recordings)));
525
}
526
527
/**
528
 * List of view style log actions.
529
 *
530
 * @return string[]
531
 */
532
function bigbluebuttonbn_get_view_actions() {
533
    return array('view', 'view all');
534
}
535
536
/**
537
 * List of update style log actions.
538
 *
539
 * @return string[]
540
 */
541
function bigbluebuttonbn_get_post_actions() {
542
    return array('update', 'add', 'delete');
543
}
544
545
/**
546
 * Print an overview of all bigbluebuttonbn instances for the courses.
547
 *
548
 * @param array $courses
549
 * @param array $htmlarray Passed by reference
550
 *
551
 * @return void
552
 */
553
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
554
    if (empty($courses) || !is_array($courses)) {
555
        return array();
556
    }
557
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
558
    foreach ($bns as $bn) {
559
        $now = time();
560
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
561
            // A bigbluebuttonbn is scheduled.
562
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
563
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
564
            }
565
            // Make sure we print all bigbluebutton instances.
566
            $htmlarray[$bn->course]['bigbluebuttonbn'] .= bigbluebuttonbn_print_overview_element($bn, $now);
567
        }
568
    }
569
}
570
571
/**
572
 * Print an overview of a bigbluebuttonbn instance.
573
 *
574
 * @param array $bigbluebuttonbn
575
 * @param int $now
576
 *
577
 * @return string
578
 */
579
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
580
    global $CFG;
581
    $start = 'started_at';
582
    if ($bigbluebuttonbn->openingtime > $now) {
583
        $start = 'starts_at';
584
    }
585
    $classes = '';
586
    if ($bigbluebuttonbn->visible) {
587
        $classes = 'class="dimmed" ';
588
    }
589
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
590
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
591
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
592
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
593
    $str .= '  </div>'."\n";
594
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
595
        '</div>'."\n";
596
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
597
      .'</div>'."\n";
598
    $str .= '</div>'."\n";
599
    return $str;
600
}
601
602
/**
603
 * Given a course_module object, this function returns any
604
 * "extra" information that may be needed when printing
605
 * this activity in a course listing.
606
 * See get_array_of_activities() in course/lib.php.
607
 *
608
 * @param object $coursemodule
609
 *
610
 * @return null|cached_cm_info
611
 */
612
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
613
    global $DB;
614
615
    $dbparams = ['id' => $coursemodule->instance];
616
    $fields = 'id, name, intro, introformat, completionattendance';
617
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', $dbparams, $fields);
618
    if (!$bigbluebuttonbn) {
619
        return false;
620
    }
621
    $info = new cached_cm_info();
622
    $info->name = $bigbluebuttonbn->name;
623
    if ($coursemodule->showdescription) {
624
        // Convert intro to html. Do not filter cached version, filters run at display time.
625
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
626
    }
627
    // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
628
    if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
629
        $info->customdata['customcompletionrules']['completionattendance'] = $bigbluebuttonbn->completionattendance;
630
    }
631
632
    return $info;
633
}
634
635
/**
636
 * Callback which returns human-readable strings describing the active completion custom rules for the module instance.
637
 *
638
 * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
639
 * @return array $descriptions the array of descriptions for the custom rules.
640
 */
641
function mod_bigbluebuttonbn_get_completion_active_rule_descriptions($cm) {
642
    // Values will be present in cm_info, and we assume these are up to date.
643
    if (empty($cm->customdata['customcompletionrules'])
644
        || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
645
        return [];
646
    }
647
648
    $descriptions = [];
649
    foreach ($cm->customdata['customcompletionrules'] as $key => $val) {
650
        switch ($key) {
651
            case 'completionattendance':
652
                if (!empty($val)) {
653
                    $descriptions[] = get_string('completionattendancedesc', 'bigbluebuttonbn', $val);
654
                    $descriptions[] = get_string('completionengagementdesc', 'bigbluebuttonbn', $val);
655
                }
656
                break;
657
            default:
658
                break;
659
        }
660
    }
661
    return $descriptions;
662
}
663
664
/**
665
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
666
 *
667
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
668
 *
669
 * @return void
670
 **/
671
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
672
    bigbluebuttonbn_process_pre_save_instance($bigbluebuttonbn);
673
    bigbluebuttonbn_process_pre_save_checkboxes($bigbluebuttonbn);
674
    bigbluebuttonbn_process_pre_save_common($bigbluebuttonbn);
675
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
676
}
677
678
/**
679
 * Runs process for defining the instance (insert/update).
680
 *
681
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
682
 *
683
 * @return void
684
 **/
685
function bigbluebuttonbn_process_pre_save_instance(&$bigbluebuttonbn) {
686
    require_once(__DIR__.'/locallib.php');
687
    $bigbluebuttonbn->timemodified = time();
688
    if ((integer)$bigbluebuttonbn->instance == 0) {
689
        $bigbluebuttonbn->meetingid = 0;
690
        $bigbluebuttonbn->timecreated = time();
691
        $bigbluebuttonbn->timemodified = 0;
692
        // As it is a new activity, assign passwords.
693
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
694
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12, $bigbluebuttonbn->moderatorpass);
695
    }
696
}
697
698
/**
699
 * Runs process for assigning default value to checkboxes.
700
 *
701
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
702
 *
703
 * @return void
704
 **/
705
function bigbluebuttonbn_process_pre_save_checkboxes(&$bigbluebuttonbn) {
706
    if (!isset($bigbluebuttonbn->wait)) {
707
        $bigbluebuttonbn->wait = 0;
708
    }
709
    if (!isset($bigbluebuttonbn->record)) {
710
        $bigbluebuttonbn->record = 0;
711
    }
712
    if (!isset($bigbluebuttonbn->recordallfromstart)) {
713
        $bigbluebuttonbn->recordallfromstart = 0;
714
    }
715
    if (!isset($bigbluebuttonbn->recordhidebutton)) {
716
        $bigbluebuttonbn->recordhidebutton = 0;
717
    }
718
    if (!isset($bigbluebuttonbn->recordings_html)) {
719
        $bigbluebuttonbn->recordings_html = 0;
720
    }
721
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
722
        $bigbluebuttonbn->recordings_deleted = 0;
723
    }
724
    if (!isset($bigbluebuttonbn->recordings_imported)) {
725
        $bigbluebuttonbn->recordings_imported = 0;
726
    }
727
    if (!isset($bigbluebuttonbn->recordings_preview)) {
728
        $bigbluebuttonbn->recordings_preview = 0;
729
    }
730
    if (!isset($bigbluebuttonbn->muteonstart)) {
731
        $bigbluebuttonbn->muteonstart = 0;
732
    }
733
    if (!isset($bigbluebuttonbn->recordings_validate_url)) {
734
        $bigbluebuttonbn->recordings_validate_url = 1;
735
    }
736
}
737
738
/**
739
 * Runs process for wipping common settings when 'recordings only'.
740
 *
741
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
742
 *
743
 * @return void
744
 **/
745
function bigbluebuttonbn_process_pre_save_common(&$bigbluebuttonbn) {
746
    // Make sure common settings are removed when 'recordings only'.
747
    if ($bigbluebuttonbn->type == BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY) {
748
        $bigbluebuttonbn->groupmode = 0;
749
        $bigbluebuttonbn->groupingid = 0;
750
    }
751
}
752
753
/**
754
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
755
 *
756
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
757
 *
758
 * @return void
759
 **/
760
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
761
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
762
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
763
    }
764
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
765
    bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn);
766
}
767
768
/**
769
 * Generates a message on insert/update which is sent to all users enrolled.
770
 *
771
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
772
 *
773
 * @return void
774
 **/
775
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
776
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
777
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
778
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
779
    }
780
    \mod_bigbluebuttonbn\locallib\notifier::notify_instance_updated($bigbluebuttonbn, $action);
781
}
782
783
/**
784
 * Generates an event after a bigbluebuttonbn insert/update.
785
 *
786
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
787
 *
788
 * @return void
789
 **/
790
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
791
    global $CFG, $DB;
792
    require_once($CFG->dirroot.'/calendar/lib.php');
793
    $eventid = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
794
        'instance' => $bigbluebuttonbn->id));
795
    // Delete the event from calendar when/if openingtime is NOT set.
796
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
797
        if ($eventid) {
798
            $calendarevent = calendar_event::load($eventid);
799
            $calendarevent->delete();
800
        }
801
        return;
802
    }
803
    // Add evento to the calendar as openingtime is set.
804
    $event = new stdClass();
805
    $event->eventtype = BIGBLUEBUTTON_EVENT_MEETING_START;
806
    $event->type = CALENDAR_EVENT_TYPE_ACTION;
807
    $event->name = get_string('calendarstarts', 'bigbluebuttonbn', $bigbluebuttonbn->name);
808
    $event->description = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $bigbluebuttonbn->coursemodule);
809
    $event->courseid = $bigbluebuttonbn->course;
810
    $event->groupid = 0;
811
    $event->userid = 0;
812
    $event->modulename = 'bigbluebuttonbn';
813
    $event->instance = $bigbluebuttonbn->id;
814
    $event->timestart = $bigbluebuttonbn->openingtime;
815
    $event->timeduration = 0;
816
    $event->timesort = $event->timestart;
817
    $event->visible = instance_is_visible('bigbluebuttonbn', $bigbluebuttonbn);
818
    $event->priority = null;
819
    // Update the event in calendar when/if eventid was found.
820
    if ($eventid) {
821
        $event->id = $eventid;
822
        $calendarevent = calendar_event::load($eventid);
823
        $calendarevent->update($event);
824
        return;
825
    }
826
    calendar_event::create($event);
827
}
828
829
/**
830
 * Generates an event after a bigbluebuttonbn activity is completed.
831
 *
832
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
833
 *
834
 * @return void
835
 **/
836
function bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn) {
837
    if (!empty($bigbluebuttonbn->completionexpected)) {
838
        \core_completion\api::update_completion_date_event(
839
            $bigbluebuttonbn->coursemodule,
840
            'bigbluebuttonbn',
841
            $bigbluebuttonbn->id,
842
            $bigbluebuttonbn->completionexpected
843
          );
844
    }
845
}
846
847
/**
848
 * Get a full path to the file attached as a preuploaded presentation
849
 * or if there is none, set the presentation field will be set to blank.
850
 *
851
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
852
 *
853
 * @return string
854
 */
855
function bigbluebuttonbn_get_media_file(&$bigbluebuttonbn) {
856
    if (!isset($bigbluebuttonbn->presentation) || $bigbluebuttonbn->presentation == '') {
857
        return '';
858
    }
859
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
860
    // Set the filestorage object.
861
    $fs = get_file_storage();
862
    // Save the file if it exists that is currently in the draft area.
863
    file_save_draft_area_files($bigbluebuttonbn->presentation, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
864
    // Get the file if it exists.
865
    $files = $fs->get_area_files(
866
        $context->id,
867
        'mod_bigbluebuttonbn',
868
        'presentation',
869
        0,
870
        'itemid, filepath, filename',
871
        false
872
    );
873
    // Check that there is a file to process.
874
    $filesrc = '';
875
    if (count($files) == 1) {
876
        // Get the first (and only) file.
877
        $file = reset($files);
878
        $filesrc = '/'.$file->get_filename();
879
    }
880
    return $filesrc;
881
}
882
883
/**
884
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
885
 *
886
 * @category files
887
 *
888
 * @param stdClass $course        course object
889
 * @param stdClass $cm            course module object
890
 * @param stdClass $context       context object
891
 * @param string   $filearea      file area
892
 * @param array    $args          extra arguments
893
 * @param bool     $forcedownload whether or not force download
894
 * @param array    $options       additional options affecting the file serving
895
 *
896
 * @return false|null false if file not found, does not return if found - justsend the file
897
 */
898
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
899
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
900
        return false;
901
    }
902
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
903
    if (empty($file)) {
904
        return false;
905
    }
906
    // Finally send the file.
907
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
908
}
909
910
/**
911
 * Helper for validating pluginfile.
912
 * @param stdClass $context       context object
913
 * @param string   $filearea      file area
914
 *
915
 * @return false|null false if file not valid
916
 */
917
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
918
919
    // Can be in context module or in context_system (if is the presentation by default).
920
    if (!in_array($context->contextlevel, array(CONTEXT_MODULE, CONTEXT_SYSTEM))) {
921
        return false;
922
    }
923
924
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
925
        return false;
926
    }
927
928
    return true;
929
}
930
931
/**
932
 * Helper for getting pluginfile.
933
 *
934
 * @param stdClass $course        course object
935
 * @param stdClass $cm            course module object
936
 * @param stdClass $context       context object
937
 * @param string   $filearea      file area
938
 * @param array    $args          extra arguments
939
 *
940
 * @return object
941
 */
942
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
943
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
944
    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...
945
        return false;
946
    }
947
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
948
    $fs = get_file_storage();
949
    $file = $fs->get_file_by_hash(sha1($fullpath));
950
    if (!$file || $file->is_directory()) {
951
        return false;
952
    }
953
    return $file;
954
}
955
956
/**
957
 * Helper for give access to the file configured in setting as default presentation.
958
 *
959
 * @param stdClass $course        course object
960
 * @param stdClass $cm            course module object
961
 * @param stdClass $context       context object
962
 * @param array    $args          extra arguments
963
 *
964
 * @return array
965
 */
966
function bigbluebuttonbn_default_presentation_get_file($course, $cm, $context, $args) {
967
968
    // The difference with the standard bigbluebuttonbn_pluginfile_filename() are.
969
    // - Context is system, so we don't need to check the cmid in this case.
970
    // - The area is "presentationdefault_cache".
971
    if (count($args) > 1) {
972
        $cache = cache::make_from_params(
973
            cache_store::MODE_APPLICATION,
974
            'mod_bigbluebuttonbn',
975
            'presentationdefault_cache'
976
        );
977
978
        $noncekey = sha1($context->id);
979
        $presentationnonce = $cache->get($noncekey);
980
        $noncevalue = $presentationnonce['value'];
981
        $noncecounter = $presentationnonce['counter'];
982
        if ($args['0'] != $noncevalue) {
983
            return;
984
        }
985
986
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
987
        $noncecounter += 1;
988
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
989
        if ($noncecounter == 2) {
990
            $cache->delete($noncekey);
991
        }
992
        return($args['1']);
993
    }
994
    require_course_login($course, true, $cm);
995
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
996
        return;
997
    }
998
    return implode('/', $args);
999
}
1000
1001
/**
1002
 * Helper for getting pluginfile name.
1003
 *
1004
 * @param stdClass $course        course object
1005
 * @param stdClass $cm            course module object
1006
 * @param stdClass $context       context object
1007
 * @param array    $args          extra arguments
1008
 *
1009
 * @return array
1010
 */
1011
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
1012
    global $DB;
1013
1014
    if ($context->contextlevel == CONTEXT_SYSTEM) {
1015
        // Plugin has a file to use as default in general setting.
1016
        return(bigbluebuttonbn_default_presentation_get_file($course, $cm, $context, $args));
1017
    }
1018
1019
    if (count($args) > 1) {
1020
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
1021
            return;
1022
        }
1023
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
1024
        $noncekey = sha1($bigbluebuttonbn->id);
1025
        $presentationnonce = $cache->get($noncekey);
1026
        $noncevalue = $presentationnonce['value'];
1027
        $noncecounter = $presentationnonce['counter'];
1028
        if ($args['0'] != $noncevalue) {
1029
            return;
1030
        }
1031
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
1032
        $noncecounter += 1;
1033
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
1034
        if ($noncecounter == 2) {
1035
            $cache->delete($noncekey);
1036
        }
1037
        return $args['1'];
1038
    }
1039
    require_course_login($course, true, $cm);
1040
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
1041
        return;
1042
    }
1043
    return implode('/', $args);
1044
}
1045
1046
/**
1047
 * Returns an array of file areas.
1048
 *
1049
 * @category files
1050
 *
1051
 * @return array a list of available file areas
1052
 */
1053
function bigbluebuttonbn_get_file_areas() {
1054
    $areas = array();
1055
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
1056
    $areas['presentationdefault'] = get_string('mod_form_block_presentation_default', 'bigbluebuttonbn');
1057
    return $areas;
1058
}
1059
1060
/**
1061
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
1062
 *
1063
 * @param  stdClass $bigbluebuttonbn        bigbluebuttonbn object
1064
 * @param  stdClass $course     course object
1065
 * @param  stdClass $cm         course module object
1066
 * @param  stdClass $context    context object
1067
 * @since Moodle 3.0
1068
 */
1069
function bigbluebuttonbn_view($bigbluebuttonbn, $course, $cm, $context) {
1070
1071
    // Trigger course_module_viewed event.
1072
    $params = array(
1073
        'context' => $context,
1074
        'objectid' => $bigbluebuttonbn->id
1075
    );
1076
1077
    $event = \mod_bigbluebuttonbn\event\activity_viewed::create($params); // Fix event name.
1078
    $event->add_record_snapshot('course_modules', $cm);
1079
    $event->add_record_snapshot('course', $course);
1080
    $event->add_record_snapshot('bigbluebuttonbn', $bigbluebuttonbn);
1081
    $event->trigger();
1082
1083
    // Completion.
1084
    $completion = new completion_info($course);
1085
    $completion->set_module_viewed($cm);
1086
}
1087
1088
/**
1089
 * Check if the module has any update that affects the current user since a given time.
1090
 *
1091
 * @param  cm_info $cm course module data
1092
 * @param  int $from the time to check updates from
1093
 * @param  array $filter  if we need to check only specific updates
1094
 * @return stdClass an object with the different type of areas indicating if they were updated or not
1095
 * @since Moodle 3.2
1096
 */
1097
function bigbluebuttonbn_check_updates_since(cm_info $cm, $from, $filter = array()) {
1098
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
1099
    return $updates;
1100
}
1101
1102
1103
/**
1104
 * Get icon mapping for font-awesome.
1105
 */
1106
function mod_bigbluebuttonbn_get_fontawesome_icon_map() {
1107
    return [
1108
        'mod_bigbluebuttonbn:icon' => 'icon-bigbluebutton',
1109
    ];
1110
}
1111
1112
/**
1113
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
1114
 *
1115
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
1116
 * is not displayed on the block.
1117
 *
1118
 * @param calendar_event $event
1119
 * @param \core_calendar\action_factory $factory
1120
 * @return \core_calendar\local\event\entities\action_interface|null
1121
 */
1122
function mod_bigbluebuttonbn_core_calendar_provide_event_action(
1123
    calendar_event $event,
1124
    \core_calendar\action_factory $factory
1125
) {
1126
    global $CFG, $DB;
1127
1128
    require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
1129
1130
    // Get mod info.
1131
    $cm = get_fast_modinfo($event->courseid)->instances['bigbluebuttonbn'][$event->instance];
1132
1133
    // Get bigbluebuttonbn activity.
1134
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $event->instance), '*', MUST_EXIST);
1135
1136
    // Get if the user has joined in live session or viewed the recorded.
1137
    $usercomplete = bigbluebuttonbn_user_complete($event->courseid, $event->userid, $bigbluebuttonbn);
1138
    // Get if the room is available.
1139
    list($roomavailable) = bigbluebuttonbn_room_is_available($bigbluebuttonbn);
1140
    // Get if the user can join.
1141
    list($usercanjoin) = bigbluebuttonbn_user_can_join_meeting($bigbluebuttonbn);
1142
    // Get if the time has already passed.
1143
    $haspassed = $bigbluebuttonbn->openingtime < time();
1144
1145
    // Check if the room is closed and the user has already joined this session or played the record.
1146
    if ($haspassed && !$roomavailable && $usercomplete) {
1147
        return null;
1148
    }
1149
1150
    // Check if the user can join this session.
1151
    $actionable = ($roomavailable && $usercanjoin) || $haspassed;
1152
1153
    // Action data.
1154
    $string = get_string('view_room', 'bigbluebuttonbn');
1155
    $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
1156
    if (groups_get_activity_groupmode($cm) == NOGROUPS) {
1157
        // No groups mode.
1158
        $string = get_string('view_conference_action_join', 'bigbluebuttonbn');
1159
        $url = new \moodle_url('/mod/bigbluebuttonbn/bbb_view.php', array('action' => 'join',
1160
            'id' => $cm->id, 'bn' => $bigbluebuttonbn->id, 'timeline' => 1));
1161
    }
1162
1163
    return $factory->create_instance($string, $url, 1, $actionable);
1164
}
1165
1166
/**
1167
 * Register a bigbluebuttonbn event
1168
 *
1169
 * @param object $bigbluebuttonbn
1170
 * @param string $event
1171
 * @param array  $overrides
1172
 * @param string $meta
1173
 *
1174
 * @return bool Success/Failure
1175
 */
1176
function bigbluebuttonbn_log($bigbluebuttonbn, $event, array $overrides = [], $meta = null) {
1177
    global $DB, $USER;
1178
    $log = new stdClass();
1179
    // Default values.
1180
    $log->courseid = $bigbluebuttonbn->course;
1181
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
1182
    $log->userid = $USER->id;
1183
    $log->meetingid = $bigbluebuttonbn->meetingid;
1184
    $log->timecreated = time();
1185
    $log->log = $event;
1186
    $log->meta = $meta;
1187
    // Overrides.
1188
    foreach ($overrides as $key => $value) {
1189
        $log->$key = $value;
1190
    }
1191
    if (!$DB->insert_record('bigbluebuttonbn_logs', $log)) {
1192
        return false;
1193
    }
1194
    return true;
1195
}
1196
1197
/**
1198
 * Adds module specific settings to the settings block
1199
 *
1200
 * @param settings_navigation $settingsnav The settings navigation object
1201
 * @param navigation_node $nodenav The node to add module settings to
1202
 */
1203
function bigbluebuttonbn_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $nodenav) {
0 ignored issues
show
Unused Code introduced by
The parameter $settingsnav 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...
1204
    global $PAGE, $USER;
1205
    // Don't add validate completion if the callback for meetingevents is NOT enabled.
1206
    if (!(boolean)\mod_bigbluebuttonbn\locallib\config::get('meetingevents_enabled')) {
1207
        return;
1208
    }
1209
    // Don't add validate completion if user is not allowed to edit the activity.
1210
    $context = context_module::instance($PAGE->cm->id);
1211
    if (!has_capability('moodle/course:manageactivities', $context, $USER->id)) {
1212
        return;
1213
    }
1214
    $completionvalidate = '#action=completion_validate&bigbluebuttonbn=' . $PAGE->cm->instance;
1215
    $nodenav->add(get_string('completionvalidatestate', 'bigbluebuttonbn'),
1216
        $completionvalidate, navigation_node::TYPE_CONTAINER);
1217
}
1218