Completed
Pull Request — master (#115)
by
unknown
01:55
created

lib.php ➔ bigbluebuttonbn_process_pre_save_checkboxes()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 512
nop 1
dl 0
loc 29
rs 4.1777
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
require_once($CFG->dirroot.'/calendar/lib.php');
32
require_once($CFG->dirroot.'/message/lib.php');
33
require_once($CFG->dirroot.'/mod/lti/OAuth.php');
34
require_once($CFG->dirroot.'/tag/lib.php');
35
require_once($CFG->libdir.'/accesslib.php');
36
require_once($CFG->libdir.'/completionlib.php');
37
require_once($CFG->libdir.'/datalib.php');
38
require_once($CFG->libdir.'/enrollib.php');
39
require_once($CFG->libdir.'/filelib.php');
40
require_once($CFG->libdir.'/formslib.php');
41
42
43
if (!class_exists('\Firebase\JWT\JWT') &&
44
    file_exists(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php')) {
45
    require_once(dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php');
46
}
47
48
if (!isset($CFG->bigbluebuttonbn)) {
49
    $CFG->bigbluebuttonbn = array();
50
}
51
52
if (file_exists(dirname(__FILE__).'/config.php')) {
53
    require_once(dirname(__FILE__).'/config.php');
54
}
55
56
/*
57
 * DURATIONCOMPENSATION: Feature removed by configuration
58
 */
59
$CFG->bigbluebuttonbn['scheduled_duration_enabled'] = 0;
60
/*
61
 * Remove this block when restored
62
 */
63
64
 /** @var BIGBLUEBUTTONBN_DEFAULT_SERVER_URL string of default bigbluebutton server url */
65
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
66
/** @var BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET string of default bigbluebutton server shared secret */
67
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
68
/** @var BIGBLUEBUTTONBN_LOG_EVENT_ADD string of event add for bigbluebuttonbn_logs */
69
const BIGBLUEBUTTONBN_LOG_EVENT_ADD = 'Add';
70
/** @var BIGBLUEBUTTONBN_LOG_EVENT_EDIT string of event edit for bigbluebuttonbn_logs */
71
const BIGBLUEBUTTONBN_LOG_EVENT_EDIT = 'Edit';
72
/** @var BIGBLUEBUTTONBN_LOG_EVENT_CREATE string of event create for bigbluebuttonbn_logs */
73
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
74
/** @var BIGBLUEBUTTONBN_LOG_EVENT_JOIN string of event join for bigbluebuttonbn_logs */
75
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
76
/** @var BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT string of event logout for bigbluebuttonbn_logs */
77
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
78
/** @var BIGBLUEBUTTONBN_LOG_EVENT_IMPORT string of event import for bigbluebuttonbn_logs */
79
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
80
/** @var BIGBLUEBUTTONBN_LOG_EVENT_DELETE string of event delete for bigbluebuttonbn_logs */
81
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
82
/** @var BIGBLUEBUTTON_LOG_EVENT_CALLBACK string defines the bigbluebuttonbn callback event */
83
const BIGBLUEBUTTON_LOG_EVENT_CALLBACK = 'Callback';
84
/**
85
 * Indicates API features that the forum supports.
86
 *
87
 * @uses FEATURE_IDNUMBER
88
 * @uses FEATURE_GROUPS
89
 * @uses FEATURE_GROUPINGS
90
 * @uses FEATURE_GROUPMEMBERSONLY
91
 * @uses FEATURE_MOD_INTRO
92
 * @uses FEATURE_BACKUP_MOODLE2
93
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
94
 * @uses FEATURE_COMPLETION_HAS_RULES
95
 * @uses FEATURE_GRADE_HAS_GRADE
96
 * @uses FEATURE_GRADE_OUTCOMES
97
 * @uses FEATURE_SHOW_DESCRIPTION
98
 * @param string $feature
99
 * @return mixed True if yes (some features may use other values)
100
 */
101
function bigbluebuttonbn_supports($feature) {
102
    if (!$feature) {
103
        return null;
104
    }
105
    $features = array(
106
        (string) FEATURE_IDNUMBER => true,
107
        (string) FEATURE_GROUPS => true,
108
        (string) FEATURE_GROUPINGS => true,
109
        (string) FEATURE_GROUPMEMBERSONLY => true,
110
        (string) FEATURE_MOD_INTRO => true,
111
        (string) FEATURE_BACKUP_MOODLE2 => true,
112
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
113
        (string) FEATURE_GRADE_HAS_GRADE => false,
114
        (string) FEATURE_GRADE_OUTCOMES => false,
115
        (string) FEATURE_SHOW_DESCRIPTION => true,
116
    );
117
    if (isset($features[(string) $feature])) {
118
        return $features[$feature];
119
    }
120
    return null;
121
}
122
123
/**
124
 * Given an object containing all the necessary data,
125
 * (defined by the form in mod_form.php) this function
126
 * will create a new instance and return the id number
127
 * of the new instance.
128
 *
129
 * @param object $bigbluebuttonbn  An object from the form in mod_form.php
130
 * @return int The id of the newly inserted bigbluebuttonbn record
131
 */
132 View Code Duplication
function bigbluebuttonbn_add_instance($bigbluebuttonbn) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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

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

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

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

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

Loading history...
160
    global $DB;
161
    // Excecute preprocess.
162
    bigbluebuttonbn_process_pre_save($bigbluebuttonbn);
163
    // Pre-set initial values.
164
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
165
    $bigbluebuttonbn->presentation = bigbluebuttonbn_get_media_file($bigbluebuttonbn);
166
    // Update a record.
167
    $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
168
    // Get the meetingid column in the bigbluebuttonbn table.
169
    $bigbluebuttonbn->meetingid = (string)$DB->get_field('bigbluebuttonbn', 'meetingid', array('id' => $bigbluebuttonbn->id));
170
    // Log update action.
171
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_EDIT);
172
    // Complete the process.
173
    bigbluebuttonbn_process_post_save($bigbluebuttonbn);
174
    return true;
175
}
176
177
/**
178
 * Given an ID of an instance of this module,
179
 * this function will permanently delete the instance
180
 * and any data that depends on it.
181
 *
182
 * @param int $id Id of the module instance
183
 *
184
 * @return bool Success/Failure
185
 */
186
function bigbluebuttonbn_delete_instance($id) {
187
    global $DB;
188
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id));
189
    if (!$bigbluebuttonbn) {
190
        return false;
191
    }
192
    // TODO: End the meeting if it is running.
193
194
    // Perform delete.
195
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
196
        return false;
197
    }
198
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
199
        return false;
200
    }
201
    // Log action performed.
202
    return bigbluebuttonbn_delete_instance_log($bigbluebuttonbn);
203
}
204
205
/**
206
 * Given an ID of an instance of this module,
207
 * this function will permanently delete the data that depends on it.
208
 *
209
 * @param object $bigbluebuttonbn Id of the module instance
210
 *
211
 * @return bool Success/Failure
212
 */
213
function bigbluebuttonbn_delete_instance_log($bigbluebuttonbn) {
214
    global $DB;
215
    $sql  = "SELECT * FROM {bigbluebuttonbn_logs} ";
216
    $sql .= "WHERE bigbluebuttonbnid = ? AND log = ? AND ". $DB->sql_compare_text('meta') . " = ?";
217
    $logs = $DB->get_records_sql($sql, array($bigbluebuttonbn->id, BIGBLUEBUTTONBN_LOG_EVENT_CREATE, "{\"record\":true}"));
218
    $meta = "{\"has_recordings\":" . empty($logs) ? "true" : "false" . "}";
219
    bigbluebuttonbn_log($bigbluebuttonbn, BIGBLUEBUTTONBN_LOG_EVENT_DELETE, [], $meta);
220
}
221
222
/**
223
 * Return a small object with summary information about what a
224
 * user has done with a given particular instance of this module
225
 * Used for user activity reports.
226
 * $return->time = the time they did it
227
 * $return->info = a short text description.
228
 *
229
 * @param object $course
230
 * @param object $user
231
 * @param object $mod
232
 * @param object $bigbluebuttonbn
233
 *
234
 * @return bool
235
 */
236
function bigbluebuttonbn_user_outline($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $mod is not used and could be removed.

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

Loading history...
237
    global $DB;
238
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
239
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ), '*');
240
    if ($completed > 0) {
241
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.
242
            get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.
243
            get_string('view_message_times', 'bigbluebuttonbn');
244
    }
245
    return '';
246
}
247
248
/**
249
 * Print a detailed representation of what a user has done with
250
 * a given particular instance of this module, for user activity reports.
251
 *
252
 * @param object $course
253
 * @param object $user
254
 * @param object $mod
255
 * @param object $bigbluebuttonbn
256
 *
257
 * @return bool
258
 */
259
function bigbluebuttonbn_user_complete($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $mod is not used and could be removed.

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

Loading history...
260
    global $DB;
261
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
262
        'bigbluebuttonbnid' => $bigbluebuttonbn->id, 'userid' => $user->id, 'log' => 'Join', ),
263
        '*', IGNORE_MULTIPLE);
264
    return $completed > 0;
265
}
266
267
/**
268
 * Returns all other caps used in module.
269
 *
270
 * @return string[]
271
 */
272
function bigbluebuttonbn_get_extra_capabilities() {
273
    return array('moodle/site:accessallgroups');
274
}
275
276
/**
277
 * This function is used by the reset_course_userdata function in moodlelib.
278
 * @param $data the data submitted from the reset course.
279
 * @return array status array
280
 */
281
function bigbluebuttonbn_reset_userdata($data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

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

Loading history...
282
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
283
    // See MDL-9367.
284
    return array();
285
}
286
287
/**
288
 * List of view style log actions.
289
 *
290
 * @return string[]
291
 */
292
function bigbluebuttonbn_get_view_actions() {
293
    return array('view', 'view all');
294
}
295
296
/**
297
 * List of update style log actions.
298
 *
299
 * @return string[]
300
 */
301
function bigbluebuttonbn_get_post_actions() {
302
    return array('update', 'add', 'delete');
303
}
304
305
/**
306
 * Print an overview of all bigbluebuttonbn instances for the courses.
307
 *
308
 * @param array $courses
309
 * @param array $htmlarray Passed by reference
310
 *
311
 * @return void
312
 */
313
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
314
    if (empty($courses) || !is_array($courses)) {
315
        return array();
316
    }
317
    $bns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
318
    foreach ($bns as $bn) {
319
        $now = time();
320
        if ($bn->openingtime and (!$bn->closingtime or $bn->closingtime > $now)) {
321
            // A bigbluebuttonbn is scheduled.
322
            if (empty($htmlarray[$bn->course]['bigbluebuttonbn'])) {
323
                $htmlarray[$bn->course]['bigbluebuttonbn'] = '';
324
            }
325
            $htmlarray[$bn->course]['bigbluebuttonbn'] = bigbluebuttonbn_print_overview_element($bn, $now);
326
        }
327
    }
328
}
329
330
/**
331
 * Print an overview of a bigbluebuttonbn instance.
332
 *
333
 * @param array $bigbluebuttonbn
334
 * @param int $now
335
 *
336
 * @return string
337
 */
338
function bigbluebuttonbn_print_overview_element($bigbluebuttonbn, $now) {
339
    global $CFG;
340
    $start = 'started_at';
341
    if ($bigbluebuttonbn->openingtime > $now) {
342
        $start = 'starts_at';
343
    }
344
    $classes = '';
345
    if ($bigbluebuttonbn->visible) {
346
        $classes = 'class="dimmed" ';
347
    }
348
    $str  = '<div class="bigbluebuttonbn overview">'."\n";
349
    $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
350
    $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
351
      '">'.$bigbluebuttonbn->name.'</a>'."\n";
352
    $str .= '  </div>'."\n";
353
    $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).
354
        '</div>'."\n";
355
    $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
356
      .'</div>'."\n";
357
    $str .= '</div>'."\n";
358
    return $str;
359
}
360
361
/**
362
 * Given a course_module object, this function returns any
363
 * "extra" information that may be needed when printing
364
 * this activity in a course listing.
365
 * See get_array_of_activities() in course/lib.php.
366
 *
367
 * @param object $coursemodule
368
 *
369
 * @return null|cached_cm_info
370
 */
371
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
372
    global $DB;
373
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance),
374
        'id, name, intro, introformat');
375
    if (!$bigbluebuttonbn) {
376
        return null;
377
    }
378
    $info = new cached_cm_info();
379
    $info->name = $bigbluebuttonbn->name;
380
    if ($coursemodule->showdescription) {
381
        // Convert intro to html. Do not filter cached version, filters run at display time.
382
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
383
    }
384
    return $info;
385
}
386
387
/**
388
 * Runs any processes that must run before a bigbluebuttonbn insert/update.
389
 *
390
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
391
 *
392
 * @return void
393
 **/
394
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
395
    bigbluebuttonbn_process_pre_save_instance($bigbluebuttonbn);
396
    bigbluebuttonbn_process_pre_save_checkboxes($bigbluebuttonbn);
397
    bigbluebuttonbn_process_pre_save_common($bigbluebuttonbn);
398
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
399
}
400
401
/**
402
 * Runs process for defining the instance (insert/update).
403
 *
404
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
405
 *
406
 * @return void
407
 **/
408
function bigbluebuttonbn_process_pre_save_instance(&$bigbluebuttonbn) {
409
    $bigbluebuttonbn->timemodified = time();
410
    if ((integer)$bigbluebuttonbn->instance == 0) {
411
        $bigbluebuttonbn->meetingid = 0;
412
        $bigbluebuttonbn->timecreated = time();
413
        $bigbluebuttonbn->timemodified = 0;
414
        // As it is a new activity, assign passwords.
415
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
416
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12, $bigbluebuttonbn->moderatorpass);
417
    }
418
}
419
420
/**
421
 * Runs process for assigning default value to checkboxes.
422
 *
423
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
424
 *
425
 * @return void
426
 **/
427
function bigbluebuttonbn_process_pre_save_checkboxes(&$bigbluebuttonbn) {
428
    if (!isset($bigbluebuttonbn->wait)) {
429
        $bigbluebuttonbn->wait = 0;
430
    }
431
    if (!isset($bigbluebuttonbn->record)) {
432
        $bigbluebuttonbn->record = 0;
433
    }
434
    if (!isset($bigbluebuttonbn->recordallfromstart)) {
435
        $bigbluebuttonbn->recordallfromstart = 0;
436
    }
437
    if (!isset($bigbluebuttonbn->recordhidebutton)) {
438
        $bigbluebuttonbn->recordhidebutton = 0;
439
    }
440
    if (!isset($bigbluebuttonbn->recordings_html)) {
441
        $bigbluebuttonbn->recordings_html = 0;
442
    }
443
    if (!isset($bigbluebuttonbn->recordings_deleted)) {
444
        $bigbluebuttonbn->recordings_deleted = 0;
445
    }
446
    if (!isset($bigbluebuttonbn->recordings_imported)) {
447
        $bigbluebuttonbn->recordings_imported = 0;
448
    }
449
    if (!isset($bigbluebuttonbn->recordings_preview)) {
450
        $bigbluebuttonbn->recordings_preview = 0;
451
    }
452
    if (!isset($bigbluebuttonbn->muteonstart)) {
453
        $bigbluebuttonbn->muteonstart = 0;
454
    }
455
}
456
457
/**
458
 * Runs process for wipping common settings when 'recordings only'.
459
 *
460
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
461
 *
462
 * @return void
463
 **/
464
function bigbluebuttonbn_process_pre_save_common(&$bigbluebuttonbn) {
465
    // Make sure common settings are removed when 'recordings only'.
466
    if ($bigbluebuttonbn->type == BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY) {
467
        $bigbluebuttonbn->groupmode = 0;
468
        $bigbluebuttonbn->groupingid = 0;
469
    }
470
}
471
472
/**
473
 * Runs any processes that must be run after a bigbluebuttonbn insert/update.
474
 *
475
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
476
 *
477
 * @return void
478
 **/
479
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
480
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
481
        bigbluebuttonbn_process_post_save_notification($bigbluebuttonbn);
482
    }
483
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
484
    bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn);
485
}
486
487
/**
488
 * Generates a message on insert/update which is sent to all users enrolled.
489
 *
490
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
491
 *
492
 * @return void
493
 **/
494
function bigbluebuttonbn_process_post_save_notification(&$bigbluebuttonbn) {
495
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
496
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
497
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
498
    }
499
    $context = context_course::instance($bigbluebuttonbn->course);
500
    \mod_bigbluebuttonbn\locallib\notifier::notification_process($context, $bigbluebuttonbn, $action);
501
}
502
503
/**
504
 * Generates an event after a bigbluebuttonbn insert/update.
505
 *
506
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
507
 *
508
 * @return void
509
 **/
510
function bigbluebuttonbn_process_post_save_event(&$bigbluebuttonbn) {
511
    global $DB;
512
    $eventid = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn',
513
        'instance' => $bigbluebuttonbn->id));
514
    // Delete the event from calendar when/if openingtime is NOT set.
515
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
516
        if ($eventid) {
517
            $calendarevent = calendar_event::load($eventid);
518
            $calendarevent->delete();
519
        }
520
        return;
521
    }
522
    // Add evento to the calendar as openingtime is set.
523
    $event = new stdClass();
524
    $event->eventtype = BIGBLUEBUTTON_EVENT_MEETING_START;
525
    $event->type = CALENDAR_EVENT_TYPE_ACTION;
526
    $event->name = get_string('calendarstarts', 'bigbluebuttonbn', $bigbluebuttonbn->name);
527
    $event->description = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $bigbluebuttonbn->coursemodule);
528
    $event->courseid = $bigbluebuttonbn->course;
529
    $event->groupid = 0;
530
    $event->userid = 0;
531
    $event->modulename = 'bigbluebuttonbn';
532
    $event->instance = $bigbluebuttonbn->id;
533
    $event->timestart = $bigbluebuttonbn->openingtime;
534
    $event->timeduration = 0;
535
    $event->timesort = $event->timestart;
536
    $event->visible = instance_is_visible('bigbluebuttonbn', $bigbluebuttonbn);
537
    $event->priority = null;
538
    // Update the event in calendar when/if eventid was found.
539
    if ($eventid) {
540
        $event->id = $eventid;
541
        $calendarevent = calendar_event::load($eventid);
542
        $calendarevent->update($event);
543
        return;
544
    }
545
    calendar_event::create($event);
546
}
547
548
function bigbluebuttonbn_process_post_save_completion($bigbluebuttonbn) {
549
    if (!empty($bigbluebuttonbn->completionexpected)) {
550
        \core_completion\api::update_completion_date_event(
551
            $bigbluebuttonbn->coursemodule,
552
            'bigbluebuttonbn',
553
            $bigbluebuttonbn->id, $bigbluebuttonbn->completionexpected
554
          );
555
    }
556
}
557
/**
558
 * Get a full path to the file attached as a preuploaded presentation
559
 * or if there is none, set the presentation field will be set to blank.
560
 *
561
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
562
 *
563
 * @return string
564
 */
565
function bigbluebuttonbn_get_media_file(&$bigbluebuttonbn) {
566
    if (!isset($bigbluebuttonbn->presentation) || $bigbluebuttonbn->presentation == '') {
567
        return '';
568
    }
569
    $context = context_module::instance($bigbluebuttonbn->coursemodule);
570
    // Set the filestorage object.
571
    $fs = get_file_storage();
572
    // Save the file if it exists that is currently in the draft area.
573
    file_save_draft_area_files($bigbluebuttonbn->presentation, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
574
    // Get the file if it exists.
575
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
576
        'itemid, filepath, filename', false);
577
    // Check that there is a file to process.
578
    $filesrc = '';
579
    if (count($files) == 1) {
580
        // Get the first (and only) file.
581
        $file = reset($files);
582
        $filesrc = '/'.$file->get_filename();
583
    }
584
    return $filesrc;
585
}
586
587
/**
588
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
589
 *
590
 * @category files
591
 *
592
 * @param stdClass $course        course object
593
 * @param stdClass $cm            course module object
594
 * @param stdClass $context       context object
595
 * @param string   $filearea      file area
596
 * @param array    $args          extra arguments
597
 * @param bool     $forcedownload whether or not force download
598
 * @param array    $options       additional options affecting the file serving
599
 *
600
 * @return false|null false if file not found, does not return if found - justsend the file
601
 */
602
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
603
    if (!bigbluebuttonbn_pluginfile_valid($context, $filearea)) {
604
        return false;
605
    }
606
    $file = bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args);
607
    if (empty($file)) {
608
        return false;
609
    }
610
    // Finally send the file.
611
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
612
}
613
614
/**
615
 * Helper for validating pluginfile.
616
 * @param stdClass $context       context object
617
 * @param string   $filearea      file area
618
 *
619
 * @return false|null false if file not valid
620
 */
621
function bigbluebuttonbn_pluginfile_valid($context, $filearea) {
622
623
    // Can be in context module or in context_system (if is the presentation by default).
624
    if (!in_array($context->contextlevel, array(CONTEXT_MODULE, CONTEXT_SYSTEM))) {
625
        return false;
626
    }
627
628
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
629
        return false;
630
    }
631
632
    return true;
633
}
634
635
/**
636
 * Helper for getting pluginfile.
637
 *
638
 * @param stdClass $course        course object
639
 * @param stdClass $cm            course module object
640
 * @param stdClass $context       context object
641
 * @param string   $filearea      file area
642
 * @param array    $args          extra arguments
643
 *
644
 * @return object
645
 */
646
function bigbluebuttonbn_pluginfile_file($course, $cm, $context, $filearea, $args) {
647
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
648
    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...
649
        return false;
650
    }
651
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
652
    $fs = get_file_storage();
653
    $file = $fs->get_file_by_hash(sha1($fullpath));
654
    if (!$file || $file->is_directory()) {
655
        return false;
656
    }
657
    return $file;
658
}
659
660
/**
661
 * Helper for give access to the file configured in setting as default presentation.
662
 *
663
 * @param stdClass $course        course object
664
 * @param stdClass $cm            course module object
665
 * @param stdClass $context       context object
666
 * @param array    $args          extra arguments
667
 *
668
 * @return array
669
 */
670
function bigbluebuttonbn_default_presentation_get_file($course, $cm, $context, $args) {
671
672
    // The difference with the standard bigbluebuttonbn_pluginfile_filename() are.
673
    // - Context is system, so we don't need to check the cmid in this case.
674
    // - The area is "presentationdefault_cache".
675
    if (count($args) > 1) {
676
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION,
677
            'mod_bigbluebuttonbn',
678
            'presentationdefault_cache');
679
680
        $noncekey = sha1($context->id);
681
        $presentationnonce = $cache->get($noncekey);
682
        $noncevalue = $presentationnonce['value'];
683
        $noncecounter = $presentationnonce['counter'];
684
        if ($args['0'] != $noncevalue) {
685
            return;
686
        }
687
688
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
689
        $noncecounter += 1;
690
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
691
        if ($noncecounter == 2) {
692
            $cache->delete($noncekey);
693
        }
694
        return($args['1']);
695
    }
696
    require_course_login($course, true, $cm);
697
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
698
        return;
699
    }
700
    return implode('/', $args);
701
}
702
703
/**
704
 * Helper for getting pluginfile name.
705
 *
706
 * @param stdClass $course        course object
707
 * @param stdClass $cm            course module object
708
 * @param stdClass $context       context object
709
 * @param array    $args          extra arguments
710
 *
711
 * @return array
712
 */
713
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args) {
714
    global $DB;
715
716
    if ($context->contextlevel == CONTEXT_SYSTEM) {
717
        // Plugin has a file to use as default in general setting.
718
        return(bigbluebuttonbn_default_presentation_get_file($course, $cm, $context, $args));
719
    }
720
721
    if (count($args) > 1) {
722
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
723
            return;
724
        }
725
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
726
        $noncekey = sha1($bigbluebuttonbn->id);
727
        $presentationnonce = $cache->get($noncekey);
728
        $noncevalue = $presentationnonce['value'];
729
        $noncecounter = $presentationnonce['counter'];
730
        if ($args['0'] != $noncevalue) {
731
            return;
732
        }
733
        // The nonce value is actually used twice because BigBlueButton reads the file two times.
734
        $noncecounter += 1;
735
        $cache->set($noncekey, array('value' => $noncevalue, 'counter' => $noncecounter));
736
        if ($noncecounter == 2) {
737
            $cache->delete($noncekey);
738
        }
739
        return $args['1'];
740
    }
741
    require_course_login($course, true, $cm);
742
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
743
        return;
744
    }
745
    return implode('/', $args);
746
}
747
748
/**
749
 * Returns an array of file areas.
750
 *
751
 * @category files
752
 *
753
 * @return array a list of available file areas
754
 */
755
function bigbluebuttonbn_get_file_areas() {
756
    $areas = array();
757
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
758
    $areas['presentationdefault'] = get_string('mod_form_block_presentation_default', 'bigbluebuttonbn');
759
    return $areas;
760
}
761
762
/**
763
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
764
 *
765
 * @param  stdClass $bigbluebuttonbn        bigbluebuttonbn object
766
 * @param  stdClass $course     course object
767
 * @param  stdClass $cm         course module object
768
 * @param  stdClass $context    context object
769
 * @since Moodle 3.0
770
 */
771
function bigbluebuttonbn_view($bigbluebuttonbn, $course, $cm, $context) {
772
773
    // Trigger course_module_viewed event.
774
    $params = array(
775
        'context' => $context,
776
        'objectid' => $bigbluebuttonbn->id
777
    );
778
779
    $event = \mod_bigbluebuttonbn\event\bigbluebuttonbn_activity_viewed::create($params);
780
    $event->add_record_snapshot('course_modules', $cm);
781
    $event->add_record_snapshot('course', $course);
782
    $event->add_record_snapshot('bigbluebuttonbn', $bigbluebuttonbn);
783
    $event->trigger();
784
785
    // Completion.
786
    $completion = new completion_info($course);
787
    $completion->set_module_viewed($cm);
788
}
789
790
/**
791
 * Check if the module has any update that affects the current user since a given time.
792
 *
793
 * @param  cm_info $cm course module data
794
 * @param  int $from the time to check updates from
795
 * @param  array $filter  if we need to check only specific updates
796
 * @return stdClass an object with the different type of areas indicating if they were updated or not
797
 * @since Moodle 3.2
798
 */
799
function bigbluebuttonbn_check_updates_since(cm_info $cm, $from, $filter = array()) {
800
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
801
    return $updates;
802
}
803
804
805
/**
806
 * Get icon mapping for font-awesome.
807
 */
808
function mod_bigbluebuttonbn_get_fontawesome_icon_map() {
809
    return [
810
        'mod_bigbluebuttonbn:icon' => 'icon-bigbluebutton',
811
    ];
812
}
813
814
/**
815
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
816
 *
817
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
818
 * is not displayed on the block.
819
 *
820
 * @param calendar_event $event
821
 * @param \core_calendar\action_factory $factory
822
 * @return \core_calendar\local\event\entities\action_interface|null
823
 */
824
function mod_bigbluebuttonbn_core_calendar_provide_event_action(calendar_event $event,
825
        \core_calendar\action_factory $factory) {
826
    global $CFG, $DB;
827
828
    require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
829
830
    $cm = get_fast_modinfo($event->courseid)->instances['bigbluebuttonbn'][$event->instance];
831
832
    // Check that the bigbluebuttonbn activity is open.
833
    $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $event->instance), '*', MUST_EXIST);
834
    $actionable = bigbluebuttonbn_get_availability_status($bigbluebuttonbn);
835
836
    $string = get_string('view_room', 'bigbluebuttonbn');
837
    $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', array('id' => $cm->id));
838
    if (groups_get_activity_groupmode($cm) == NOGROUPS) {
839
        // No groups mode.
840
        $string = get_string('view_conference_action_join', 'bigbluebuttonbn');
841
        $url = new \moodle_url('/mod/bigbluebuttonbn/bbb_view.php', array('action' => 'join',
842
            'id' => $cm->id, 'bn' => $bigbluebuttonbn->id, 'timeline' => 1));
843
    }
844
845
    return $factory->create_instance($string, $url, 1, $actionable);
846
}
847
848
/**
849
 * Register a bigbluebuttonbn event
850
 *
851
 * @param object $bigbluebuttonbn
852
 * @param string $event
853
 * @param array  $overrides
854
 * @param string $meta
855
 *
856
 * @return bool Success/Failure
857
 */
858
function bigbluebuttonbn_log($bigbluebuttonbn, $event, array $overrides = [], $meta = null) {
859
    global $DB, $USER;
860
    $log = new stdClass();
861
    // Default values.
862
    $log->courseid = $bigbluebuttonbn->course;
863
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
864
    $log->userid = $USER->id;
865
    $log->meetingid = $bigbluebuttonbn->meetingid;
866
    $log->timecreated = time();
867
    $log->log = $event;
868
    $log->meta = $meta;
869
    // Overrides.
870
    foreach ($overrides as $key => $value) {
871
        $log->$key = $value;
872
    }
873
    if ($DB->insert_record('bigbluebuttonbn_logs', $log)) {
874
        return true;
875
    }
876
    return false;
877
}
878