Completed
Push — scrutinizer ( 9d9d33...7155b3 )
by Jesus
02:11
created

lib.php ➔ bigbluebuttonbn_get_completion_state()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 4
dl 0
loc 23
rs 9.552
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
// 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
}
58
59
/*
60
 * DURATIONCOMPENSATION: Feature removed by configuration
61
 */
62
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
63
/*
64
 * Remove this block when restored
65
 */
66
67
 /** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
68
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
69
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
70
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
71
/** @var BIGBLUEBUTTONBN_LOG_EVENT_ADD string defines the bigbluebuttonbn Add event */
72
const BIGBLUEBUTTONBN_LOG_EVENT_ADD = 'Add';
73
/** @var BIGBLUEBUTTONBN_LOG_EVENT_EDIT string defines the bigbluebuttonbn Edit event */
74
const BIGBLUEBUTTONBN_LOG_EVENT_EDIT = 'Edit';
75
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string defines the bigbluebuttonbn Create event */
76
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
77
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string defines the bigbluebuttonbn Join event */
78
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
79
/** @var BIGBLUEBUTTONBN_LOG_EVENT_PLAYED string defines the bigbluebuttonbn Playback event */
80
const BIGBLUEBUTTONBN_LOG_EVENT_PLAYED = 'Played';
81
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string defines the bigbluebuttonbn Logout event */
82
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
83
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string defines the bigbluebuttonbn Import event */
84
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
85
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string defines the bigbluebuttonbn Delete event */
86
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
87
/** @var BIGBLUEBUTTON_LOG_EVENT_CALLBACK string defines the bigbluebuttonbn Callback event */
88
const BIGBLUEBUTTON_LOG_EVENT_CALLBACK = 'Callback';
89
/** @var BIGBLUEBUTTON_LOG_EVENT_SUMMARY string defines the bigbluebuttonbn Summary event */
90
const BIGBLUEBUTTON_LOG_EVENT_SUMMARY = 'Summary';
91
/**
92
 * Indicates API features that the bigbluebuttonbn supports.
93
 *
94
 * @uses FEATURE_IDNUMBER
95
 * @uses FEATURE_GROUPS
96
 * @uses FEATURE_GROUPINGS
97
 * @uses FEATURE_GROUPMEMBERSONLY
98
 * @uses FEATURE_MOD_INTRO
99
 * @uses FEATURE_BACKUP_MOODLE2
100
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
101
 * @uses FEATURE_COMPLETION_HAS_RULES
102
 * @uses FEATURE_GRADE_HAS_GRADE
103
 * @uses FEATURE_GRADE_OUTCOMES
104
 * @uses FEATURE_SHOW_DESCRIPTION
105
 * @param string $feature
106
 * @return mixed True if yes (some features may use other values)
107
 */
108
function bigbluebuttonbn_supports($feature) {
109
    if (!$feature) {
110
        return null;
111
    }
112
    $features = array(
113
        (string) FEATURE_IDNUMBER => true,
114
        (string) FEATURE_GROUPS => true,
115
        (string) FEATURE_GROUPINGS => true,
116
        (string) FEATURE_GROUPMEMBERSONLY => true,
117
        (string) FEATURE_MOD_INTRO => true,
118
        (string) FEATURE_BACKUP_MOODLE2 => true,
119
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
120
        (string) FEATURE_COMPLETION_HAS_RULES => true,
121
        (string) FEATURE_GRADE_HAS_GRADE => false,
122
        (string) FEATURE_GRADE_OUTCOMES => false,
123
        (string) FEATURE_SHOW_DESCRIPTION => true,
124
    );
125
    if (isset($features[(string) $feature])) {
126
        return $features[$feature];
127
    }
128
    return null;
129
}
130
131
/**
132
 * Obtains the automatic completion state for this bigbluebuttonbn based on any conditions
133
 * in bigbluebuttonbn settings.
134
 *
135
 * @param object $course Course
136
 * @param object $cm Course-module
137
 * @param int $userid User ID
138
 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
139
 *
140
 * @return bool True if completed, false if not. (If no conditions, then return
141
 *   value depends on comparison type)
142
 */
143
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...
144
    global $DB;
145
146
    // Get bigbluebuttonbn details.
147
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance), '*',
148
            MUST_EXIST);
149
    if (!$bigbluebuttonbn) {
150
        throw new Exception("Can't find bigbluebuttonbn {$cm->instance}");
151
    }
152
153
    // Default return value.
154
    $result = $type;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
155
156
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
157
    $sql .= "WHERE bigbluebuttonbnid = ? AND userid = ? AND log = ?";
158
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, $userid, BIGBLUEBUTTON_LOG_EVENT_SUMMARY));
159
160
    $result = bigbluebuttonbn_validate_completion_state($bigbluebuttonbn->completionattendance, $logs, $type, 'duration');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
161
    $result = bigbluebuttonbn_validate_completion_state($bigbluebuttonbn->completionengagementchats, $logs, $type, 'engagement', 'chats');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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