Completed
Push — master ( ab9e22...72dffb )
by Jesus
02:17
created

lib.php ➔ bigbluebuttonbn_log()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 4
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * Library calls for Moodle and BigBlueButton.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2010 onwards, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
24
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
25
 */
26
27
defined('MOODLE_INTERNAL') || die;
28
29
global $CFG;
30
31
require_once($CFG->dirroot.'/calendar/lib.php');
32
require_once($CFG->dirroot.'/message/lib.php');
33
require_once($CFG->dirroot.'/mod/lti/OAuth.php');
34
require_once($CFG->dirroot.'/tag/lib.php');
35
require_once($CFG->libdir.'/accesslib.php');
36
require_once($CFG->libdir.'/completionlib.php');
37
require_once($CFG->libdir.'/datalib.php');
38
require_once($CFG->libdir.'/enrollib.php');
39
require_once($CFG->libdir.'/filelib.php');
40
require_once($CFG->libdir.'/formslib.php');
41
42
43
if (file_exists(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php')) {
44
    require_once(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php');
45
}
46
47
if (!isset($CFG->bigbluebuttonbn)) {
48
    $CFG->bigbluebuttonbn = array();
49
}
50
51
if (file_exists(dirname(__FILE__).'/config.php')) {
52
    require_once(dirname(__FILE__).'/config.php');
53
}
54
55
/*
56
 * DURATIONCOMPENSATION: Feature removed by configuration
57
 */
58
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
59
/*
60
 * Remove this block when restored
61
 */
62
63
 /** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
64
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
65
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
66
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
67
/** @var BIGBLUEBUTTONBN_LOG_EVENT_ADD string of event add for bigbluebuttonbn_logs */
68
const BIGBLUEBUTTONBN_LOG_EVENT_ADD = 'Add';
69
/** @var BIGBLUEBUTTONBN_LOG_EVENT_EDIT string of event edit for bigbluebuttonbn_logs */
70
const BIGBLUEBUTTONBN_LOG_EVENT_EDIT = 'Edit';
71
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string of event create for bigbluebuttonbn_logs */
72
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
73
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string of event join for bigbluebuttonbn_logs */
74
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
75
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string of event logout for bigbluebuttonbn_logs */
76
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
77
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string of event import for bigbluebuttonbn_logs */
78
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
79
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string of event delete for bigbluebuttonbn_logs */
80
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
81
82
/**
83
 * Indicates API features that the forum supports.
84
 *
85
 * @uses FEATURE_IDNUMBER
86
 * @uses FEATURE_GROUPS
87
 * @uses FEATURE_GROUPINGS
88
 * @uses FEATURE_GROUPMEMBERSONLY
89
 * @uses FEATURE_MOD_INTRO
90
 * @uses FEATURE_BACKUP_MOODLE2
91
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
92
 * @uses FEATURE_COMPLETION_HAS_RULES
93
 * @uses FEATURE_GRADE_HAS_GRADE
94
 * @uses FEATURE_GRADE_OUTCOMES
95
 * @uses FEATURE_SHOW_DESCRIPTION
96
 * @param string $feature
97
 * @return mixed True if yes (some features may use other values)
98
 */
99
function bigbluebuttonbn_supports($feature) {
100
    if (!$feature) {
101
        return null;
102
    }
103
    $features = array(
104
        (string) FEATURE_IDNUMBER => true,
105
        (string) FEATURE_GROUPS => true,
106
        (string) FEATURE_GROUPINGS => true,
107
        (string) FEATURE_GROUPMEMBERSONLY => true,
108
        (string) FEATURE_MOD_INTRO => true,
109
        (string) FEATURE_BACKUP_MOODLE2 => true,
110
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
111
        (string) FEATURE_GRADE_HAS_GRADE => false,
112
        (string) FEATURE_GRADE_OUTCOMES => false,
113
        (string) FEATURE_SHOW_DESCRIPTION => true,
114
    );
115
    if (isset($features[(string) $feature])) {
116
        return $features[$feature];
117
    }
118
    return null;
119
}
120
121
/**
122
 * Given an object containing all the necessary data,
123
 * (defined by the form in mod_form.php) this function
124
 * will create a new instance and return the id number
125
 * of the new instance.
126
 *
127
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
128
 * @return int The id of the newly inserted bigbluebuttonbn record
129
 */
130
function bigbluebuttonbn_add_instance($bigbluebuttonbn) {
131
    global $DB;
132
    // Excecute preprocess.
133
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
134
    // Pre-set initial values.
135
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
136
    // Insert a record.
137
    $bigbluebuttonbn->id = $DB->insert_record('bigbluebuttonbn', $bigbluebuttonbn);
138
    // Encode meetingid.
139
    $bigbluebuttonbn->meetingid = bigbluebuttonbn_encode_meetingid($bigbluebuttonbn->id);
140
    // Set the meetingid column in the bigbluebuttonbn table.
141
    $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn->meetingid, array('id' => $bigbluebuttonbn->id));
142
    // Log insert action.
143
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_ADD);
144
    // Complete the process.
145
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
146
    return $bigbluebuttonbn->id;
147
}
148
149
/**
150
 * Given an object containing all the necessary data,
151
 * (defined by the form in mod_form.php) this function
152
 * will update an existing instance with new data.
153
 *
154
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
155
 * @return bool Success/Fail
156
 */
157
function bigbluebuttonbn_update_instance($bigbluebuttonbn) {
158
    global $DB;
159
    // Excecute preprocess.
160
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
161
    // Pre-set initial values.
162
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
163
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
164
    // Update a record.
165
    $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
166
    // Get the meetingid column in the bigbluebuttonbn table.
167
    $bigbluebuttonbn->meetingid = (string)$DB->get_field('bigbluebuttonbn', 'meetingid', array('id' => $bigbluebuttonbn->id));
168
    // Log update action.
169
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_EDIT);
170
    // Complete the process.
171
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
172
    return true;
173
}
174
175
/**
176
 * Given an ID of an instance of this module,
177
 * this function will permanently delete the instance
178
 * and any data that depends on it.
179
 *
180
 * @param int $id Id of the module instance
181
 *
182
 * @return bool Success/Failure
183
 */
184
function bigbluebuttonbn_delete_instance($id) {
185
    global $DB;
186
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
187
    if (!$bigbluebuttonbn) {
188
        return false;
189
    }
190
    // TODO: End the meeting if it is running.
191
192
    // Perform delete.
193
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
194
        return false;
195
    }
196
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
197
        return false;
198
    }
199
    // Log action performed.
200
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
201
}
202
203
/**
204
 * Given an ID of an instance of this module,
205
 * this function will permanently delete the data that depends on it.
206
 *
207
 * @param object $bigbluebuttonbn Id of the module instance
208
 *
209
 * @return bool Success/Failure
210
 */
211
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
212
    global $DB;
213
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
214
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
215
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
216
    $meta = "{\"has_recordings\":" . empty($logs) ? "true" : "false" . "}";
217
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_DELETE, [], $meta);
218
}
219
220
/**
221
 * Return a small object with summary information about what a
222
 * user has done with a given particular instance of this module
223
 * Used for user activity reports.
224
 * $return->time = the time they did it
225
 * $return->info = a short text description.
226
 *
227
 * @param object $course
228
 * @param object $user
229
 * @param object $mod
230
 * @param object $bigbluebuttonbn
231
 *
232
 * @return bool
233
 */
234
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...
235
    global $DB;
236
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
237
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ), '*');
238
    if ($completed > 0) {
239
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
240
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
241
            get_string('view_message_times', 'bigbluebuttonbn');
242
    }
243
    return '';
244
}
245
246
/**
247
 * Print a detailed representation of what a user has done with
248
 * a given particular instance of this module, for user activity reports.
249
 *
250
 * @param object $course
251
 * @param object $user
252
 * @param object $mod
253
 * @param object $bigbluebuttonbn
254
 *
255
 * @return bool
256
 */
257
function bigbluebuttonbn_user_complete($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $mod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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