Completed
Push — master ( 411be6...041992 )
by Jesus
03:50
created

lib.php ➔ bigbluebuttonbn_delete_instance()   C

Complexity

Conditions 9
Paths 13

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 13
nop 1
dl 0
loc 52
rs 6.5703
c 0
b 0
f 0

How to fix   Long Method   

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
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
21
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
22
 * @copyright 2010-2015 Blindside Networks Inc
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
24
 */
25
defined('MOODLE_INTERNAL') || die;
26
27
global $BIGBLUEBUTTONBN_CFG, $CFG;
28
29
require_once $CFG->dirroot.'/calendar/lib.php';
30
require_once $CFG->dirroot.'/message/lib.php';
31
require_once $CFG->dirroot.'/mod/lti/OAuth.php';
32
require_once $CFG->libdir.'/accesslib.php';
33
require_once $CFG->libdir.'/completionlib.php';
34
require_once $CFG->libdir.'/datalib.php';
35
require_once $CFG->libdir.'/coursecatlib.php';
36
require_once $CFG->libdir.'/enrollib.php';
37
require_once $CFG->libdir.'/filelib.php';
38
require_once $CFG->libdir.'/formslib.php';
39
40
require_once dirname(__FILE__).'/vendor/firebase/php-jwt/src/JWT.php';
41
42
if (file_exists(dirname(__FILE__).'/config.php')) {
43
    require_once dirname(__FILE__).'/config.php';
44
    if (isset($BIGBLUEBUTTONBN_CFG)) {
45
        $CFG = (object) array_merge((array) $CFG, (array) $BIGBLUEBUTTONBN_CFG);
46
    }
47
} else {
48
    $BIGBLUEBUTTONBN_CFG = new stdClass();
49
}
50
51
/*
52
 * DURATIONCOMPENSATION: Feature removed by configuration
53
 */
54
$BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_scheduled_duration_enabled = 0;
55
/*
56
 * Remove this block when restored
57
 */
58
59
const BIGBLUEBUTTONBN_DEFAULT_SERVER_URL = 'http://test-install.blindsidenetworks.com/bigbluebutton/';
60
const BIGBLUEBUTTONBN_DEFAULT_SHARED_SECRET = '8cd8ef52e8e101574e400365b55e11a6';
61
62
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = 'Create';
63
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = 'Join';
64
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = 'Logout';
65
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = 'Import';
66
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = 'Delete';
67
68
function bigbluebuttonbn_supports($feature)
69
{
70
    if (!$feature) {
71
        return null;
72
    }
73
74
    $features = array(
75
        (string) FEATURE_IDNUMBER => true,
76
        (string) FEATURE_GROUPS => true,
77
        (string) FEATURE_GROUPINGS => true,
78
        (string) FEATURE_GROUPMEMBERSONLY => true,
79
        (string) FEATURE_MOD_INTRO => true,
80
        (string) FEATURE_BACKUP_MOODLE2 => true,
81
        (string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
82
        (string) FEATURE_GRADE_HAS_GRADE => false,
83
        (string) FEATURE_GRADE_OUTCOMES => false,
84
        (string) FEATURE_SHOW_DESCRIPTION => true,
85
    );
86
87
    if (isset($features[(string) $feature])) {
88
        return $features[$feature];
89
    }
90
91
    return null;
92
}
93
94
/**
95
 * Given an object containing all the necessary data,
96
 * (defined by the form in mod_form.php) this function
97
 * will create a new instance and return the id number
98
 * of the new instance.
99
 *
100
 * @param object $data  An object from the form in mod_form.php
101
 * @param object $mform An object from the form in mod_form.php
102
 *
103
 * @return int The id of the newly inserted bigbluebuttonbn record
104
 */
105
function bigbluebuttonbn_add_instance($data, $mform)
0 ignored issues
show
Unused Code introduced by
The parameter $mform 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...
106
{
107
    global $DB;
108
109
    $draftitemid = isset($data->presentation) ? $data->presentation : null;
110
    $context = bigbluebuttonbn_get_context_module($data->coursemodule);
111
112
    bigbluebuttonbn_process_pre_save($data);
113
114
    unset($data->presentation);
115
    $bigbluebuttonbn_id = $DB->insert_record('bigbluebuttonbn', $data);
116
    $data->id = $bigbluebuttonbn_id;
117
118
    bigbluebuttonbn_update_media_file($bigbluebuttonbn_id, $context, $draftitemid);
119
120
    bigbluebuttonbn_process_post_save($data);
121
122
    return $bigbluebuttonbn_id;
123
}
124
125
/**
126
 * Given an object containing all the necessary data,
127
 * (defined by the form in mod_form.php) this function
128
 * will update an existing instance with new data.
129
 *
130
 * @return bool Success/Fail
131
 */
132
function bigbluebuttonbn_update_instance($data, $mform)
0 ignored issues
show
Unused Code introduced by
The parameter $mform is not used and could be removed.

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

Loading history...
133
{
134
    global $DB;
135
136
    $data->id = $data->instance;
137
    $draftitemid = isset($data->presentation) ? $data->presentation : null;
138
    $context = bigbluebuttonbn_get_context_module($data->coursemodule);
139
140
    bigbluebuttonbn_process_pre_save($data);
141
142
    unset($data->presentation);
143
    $DB->update_record('bigbluebuttonbn', $data);
144
145
    bigbluebuttonbn_update_media_file($data->id, $context, $draftitemid);
146
147
    bigbluebuttonbn_process_post_save($data);
148
149
    return true;
150
}
151
152
/**
153
 * Given an ID of an instance of this module,
154
 * this function will permanently delete the instance
155
 * and any data that depends on it.
156
 *
157
 * @param int $id Id of the module instance
158
 *
159
 * @return bool Success/Failure
160
 */
161
function bigbluebuttonbn_delete_instance($id)
162
{
163
    global $DB, $USER;
164
165
    if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id))) {
166
        return false;
167
    }
168
169
    // End the session associated with this instance (if it's running)
170
    $meetingID = $bigbluebuttonbn->meetingid.'-'.$bigbluebuttonbn->course.'-'.$bigbluebuttonbn->id;
171
    $modPW = $bigbluebuttonbn->moderatorpass;
172
173
    if (bigbluebuttonbn_isMeetingRunning($meetingID)) {
174
        bigbluebuttonbn_doEndMeeting($meetingID, $modPW);
175
    }
176
177
    // Perform delete
178
    if (!$DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
179
        return false;
180
    }
181
182
    if (!$DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
183
        return false;
184
    }
185
186
    $log = new stdClass();
187
188
    $log->meetingid = $bigbluebuttonbn->meetingid;
189
    $log->courseid = $bigbluebuttonbn->course;
190
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
191
    $log->userid = $USER->id;
192
    $log->timecreated = time();
193
    $log->log = BIGBLUEBUTTONBN_LOG_EVENT_DELETE;
194
195
    $logs = $DB->get_records('bigbluebuttonbn_logs', array('bigbluebuttonbnid' => $bigbluebuttonbn->id, 'log' => BIGBLUEBUTTONBN_LOG_EVENT_CREATE));
196
    $has_recordings = 'false';
197
    if (!empty($logs)) {
198
        foreach ($logs as $l) {
199
            $meta = json_decode($l->meta);
200
            if ($meta->record) {
201
                $has_recordings = 'true';
202
            }
203
        }
204
    }
205
    $log->meta = "{\"has_recordings\":{$has_recordings}}";
206
207
    if (!$DB->insert_record('bigbluebuttonbn_logs', $log)) {
208
        return false;
209
    }
210
211
    return true;
212
}
213
214
/**
215
 * Return a small object with summary information about what a
216
 * user has done with a given particular instance of this module
217
 * Used for user activity reports.
218
 * $return->time = the time they did it
219
 * $return->info = a short text description.
220
 *
221
 * @return bool
222
 */
223
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...
224
{
225
    global $DB;
226
227
    $completed = $DB->count_records('bigbluebuttonbn_logs', array('courseid' => $course->id,
228
                                                              'bigbluebuttonbnid' => $bigbluebuttonbn->id,
229
                                                              'userid' => $user->id,
230
                                                              'log' => 'Join', ), '*');
231
232
    if ($completed > 0) {
233
        return fullname($user).' '.get_string('view_message_has_joined', 'bigbluebuttonbn').' '.get_string('view_message_session_for', 'bigbluebuttonbn').' '.(string) $completed.' '.get_string('view_message_times', 'bigbluebuttonbn');
234
    }
235
236
    return '';
237
}
238
239
/**
240
 * Print a detailed representation of what a user has done with
241
 * a given particular instance of this module, for user activity reports.
242
 *
243
 * @return bool
244
 */
245
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...
246
{
247
    global $DB;
248
    $completed = $DB->count_recorda('bigbluebuttonbn_logs', array('courseid' => $course->id,
249
                                                              'bigbluebuttonbnid' => $bigbluebuttonbn->id,
250
                                                              'userid' => $user->id,
251
                                                              'log' => 'Join', ), '*', IGNORE_MULTIPLE);
252
253
    return $completed > 0;
254
}
255
256
/**
257
 * Returns all other caps used in module.
258
 *
259
 * @return string[]
260
 */
261
function bigbluebuttonbn_get_extra_capabilities()
262
{
263
    return array('moodle/site:accessallgroups');
264
}
265
266
/**
267
 * List of view style log actions.
268
 *
269
 * @return string[]
270
 */
271
function bigbluebuttonbn_get_view_actions()
272
{
273
    return array('view', 'view all');
274
}
275
276
/**
277
 * List of update style log actions.
278
 *
279
 * @return string[]
280
 */
281
function bigbluebuttonbn_get_post_actions()
282
{
283
    return array('update', 'add', 'create', 'join', 'end', 'left', 'publish', 'unpublish', 'delete');
284
}
285
286
/**
287
 * @global object
288
 * @global object
289
 *
290
 * @param array $courses
291
 * @param array $htmlarray Passed by reference
292
 */
293
function bigbluebuttonbn_print_overview($courses, &$htmlarray)
294
{
295
    global $CFG;
296
297
    if (empty($courses) || !is_array($courses) || count($courses) == 0) {
298
        return array();
299
    }
300
301
    $bigbluebuttonbns = get_all_instances_in_courses('bigbluebuttonbn', $courses);
302
303
    foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
304
        $now = time();
305
        if ($bigbluebuttonbn->openingtime and (!$bigbluebuttonbn->closingtime or $bigbluebuttonbn->closingtime > $now)) {
306
            // A bigbluebuttonbn is scheduled.
307
            $start = 'started_at';
308
            if ($bigbluebuttonbn->openingtime > $now) {
309
                $start = 'starts_at';
310
            }
311
            $classes = '';
312
            if ($bigbluebuttonbn->visible) {
313
                $classes = 'class="dimmed" ';
314
            }
315
            $str = '<div class="bigbluebuttonbn overview">'."\n";
316
            $str .= '  <div class="name">'.get_string('modulename', 'bigbluebuttonbn').':&nbsp;'."\n";
317
            $str .= '    <a '.$classes.'href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.
318
              '">'.$bigbluebuttonbn->name.'</a>'."\n";
319
            $str .= '  </div>'."\n";
320
            $str .= '  <div class="info">'.get_string($start, 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).'</div>'."\n";
321
            $str .= '  <div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime)
322
              .'</div>'."\n";
323
            $str .= '</div>'."\n";
324
325
            if (empty($htmlarray[$bigbluebuttonbn->course]['bigbluebuttonbn'])) {
326
                $htmlarray[$bigbluebuttonbn->course]['bigbluebuttonbn'] = '';
327
            }
328
            $htmlarray[$bigbluebuttonbn->course]['bigbluebuttonbn'] .= $str;
329
        }
330
    }
331
}
332
333
/**
334
 * Given a course_module object, this function returns any
335
 * "extra" information that may be needed when printing
336
 * this activity in a course listing.
337
 * See get_array_of_activities() in course/lib.php.
338
 *
339
 * @global object
340
 *
341
 * @param object $coursemodule
342
 *
343
 * @return null|cached_cm_info
344
 */
345
function bigbluebuttonbn_get_coursemodule_info($coursemodule)
346
{
347
    global $DB;
348
349
    if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $coursemodule->instance), 'id, name, intro, introformat')) {
350
        return null;
351
    }
352
353
    $info = new cached_cm_info();
354
    $info->name = $bigbluebuttonbn->name;
355
356
    if ($coursemodule->showdescription) {
357
        // Convert intro to html. Do not filter cached version, filters run at display time.
358
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
359
    }
360
361
    return $info;
362
}
363
364
/**
365
 * Runs any processes that must run before
366
 * a bigbluebuttonbn insert/update.
367
 *
368
 * @global object
369
 *
370
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
371
 **/
372
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn)
373
{
374
    $bigbluebuttonbn->timemodified = time();
375
376
    if (!isset($bigbluebuttonbn->timecreated) || !$bigbluebuttonbn->timecreated) {
377
        $bigbluebuttonbn->timecreated = time();
378
        //Assign password only if it is a new activity
379
        $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
380
        $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12);
381
        $bigbluebuttonbn->timemodified = null;
382
    }
383
384
    if (!isset($bigbluebuttonbn->wait)) {
385
        $bigbluebuttonbn->wait = 0;
386
    }
387
    if (!isset($bigbluebuttonbn->record)) {
388
        $bigbluebuttonbn->record = 0;
389
    }
390
    if (!isset($bigbluebuttonbn->tagging)) {
391
        $bigbluebuttonbn->tagging = 0;
392
    }
393
    if (!isset($bigbluebuttonbn->recordings_deleted_activities)) {
394
        $bigbluebuttonbn->recordings_deleted_activities = 0;
395
    }
396
    if (!isset($bigbluebuttonbn->recordings_html)) {
397
        $bigbluebuttonbn->recordings_html = 0;
398
    }
399
400
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
401
}
402
403
/**
404
 * Runs any processes that must be run
405
 * after a bigbluebuttonbn insert/update.
406
 *
407
 * @global object
408
 *
409
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
410
 **/
411
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn)
412
{
413
    global $DB, $CFG;
414
415
    $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
416
417
    // Now that an id was assigned, generate and set the meetingid property based on
418
    // [Moodle Instance + Activity ID + BBB Secret] (but only for new activities)
419
    if (isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add)) {
420
        $meetingid = sha1($CFG->wwwroot.$bigbluebuttonbn->id.bigbluebuttonbn_get_cfg_shared_secret());
421
        $DB->set_field('bigbluebuttonbn', 'meetingid', $meetingid, array('id' => $bigbluebuttonbn->id));
422
423
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
424
    }
425
426
    bigbluebuttonbn_process_post_save_event($bigbluebuttonbn);
427
428
    if (isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification) {
429
        bigbluebuttonbn_notification_process($bigbluebuttonbn, $action);
430
    }
431
}
432
433
function bigbluebuttonbn_process_post_save_event($bigbluebuttonbn)
434
{
435
    global $DB;
436
437
    // Delete evento to the calendar when/if openingtime is NOT set
438
    if (!isset($bigbluebuttonbn->openingtime) || !$bigbluebuttonbn->openingtime) {
439
        $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id));
440
441
        return;
442
    }
443
444
    // Add evento to the calendar as openingtime is set
445
    $event = new stdClass();
446
    $event->name = $bigbluebuttonbn->name;
447
    $event->courseid = $bigbluebuttonbn->course;
448
    $event->groupid = 0;
449
    $event->userid = 0;
450
    $event->modulename = 'bigbluebuttonbn';
451
    $event->instance = $bigbluebuttonbn->id;
452
    $event->timestart = $bigbluebuttonbn->openingtime;
453
    $event->durationtime = 0;
454
455
    if ($bigbluebuttonbn->closingtime) {
456
        $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
457
    }
458
459
    $event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id));
460
    if ($event->id) {
461
        $calendarevent = calendar_event::load($event->id);
462
        $calendarevent->update($event);
463
464
        return;
465
    }
466
467
    calendar_event::create($event);
468
}
469
470
/**
471
 * Update the bigbluebuttonbn activity to include any file
472
 * that was uploaded, or if there is none, set the
473
 * presentation field to blank.
474
 *
475
 * @param int      $bigbluebuttonbn_id the bigbluebuttonbn id
476
 * @param stdClass $context            the context
477
 * @param int      $draftitemid        the draft item
478
 */
479
function bigbluebuttonbn_update_media_file($bigbluebuttonbn_id, $context, $draftitemid)
480
{
481
    global $DB;
482
483
    // Set the filestorage object.
484
    $fs = get_file_storage();
485
    // Save the file if it exists that is currently in the draft area.
486
    file_save_draft_area_files($draftitemid, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
487
    // Get the file if it exists.
488
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0, 'itemid, filepath, filename', false);
489
    // Check that there is a file to process.
490
    if (count($files) == 1) {
491
        // Get the first (and only) file.
492
        $file = reset($files);
493
        // Set the presentation column in the bigbluebuttonbn table.
494
        $DB->set_field('bigbluebuttonbn', 'presentation', '/'.$file->get_filename(), array('id' => $bigbluebuttonbn_id));
495
    } else {
496
        // Set the presentation column in the bigbluebuttonbn table.
497
        $DB->set_field('bigbluebuttonbn', 'presentation', '', array('id' => $bigbluebuttonbn_id));
498
    }
499
}
500
501
/**
502
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-).
503
 *
504
 * @category files
505
 *
506
 * @param stdClass $course        course object
507
 * @param stdClass $cm            course module object
508
 * @param stdClass $context       context object
509
 * @param string   $filearea      file area
510
 * @param array    $args          extra arguments
511
 * @param bool     $forcedownload whether or not force download
512
 * @param array    $options       additional options affecting the file serving
513
 *
514
 * @return false|null false if file not found, does not return if found - justsend the file
515
 */
516
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array())
517
{
518
    if ($context->contextlevel != CONTEXT_MODULE) {
519
        return false;
520
    }
521
522
    if ($filearea !== 'presentation') {
523
        return false;
524
    }
525
526
    if (!array_key_exists($filearea, bigbluebuttonbn_get_file_areas())) {
527
        return false;
528
    }
529
530
    $filename = bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args);
531
    if (!$filename) {
532
        return false;
533
    }
534
535
    $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
536
    $fs = get_file_storage();
537
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
538
        return false;
539
    }
540
541
    // finally send the file
542
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
543
}
544
545
function bigbluebuttonbn_pluginfile_filename($course, $cm, $context, $args)
546
{
547
    global $DB;
548
549
    if (sizeof($args) > 1) {
550
        if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance))) {
551
            return;
552
        }
553
554
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
555
        $nonce_key = sha1($bigbluebuttonbn->id);
556
        $presentation_nonce = $cache->get($nonce_key);
557
        $nonce_value = $presentation_nonce['value'];
558
        $nonce_counter = $presentation_nonce['counter'];
559
560
        if ($args['0'] != $nonce_value) {
561
            return;
562
        }
563
564
        //The nonce value is actually used twice because BigBlueButton reads the file two times
565
        $nonce_counter += 1;
566
        if ($nonce_counter < 2) {
567
            $cache->set($nonce_key, array('value' => $nonce_value, 'counter' => $nonce_counter));
568
        } else {
569
            $cache->delete($nonce_key);
570
        }
571
572
        return $args['1'];
573
    }
574
575
    require_course_login($course, true, $cm);
576
577
    if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
578
        return;
579
    }
580
581
    return implode('/', $args);
582
}
583
584
/**
585
 * Returns an array of file areas.
586
 *
587
 * @category files
588
 *
589
 * @return array a list of available file areas
590
 */
591
function bigbluebuttonbn_get_file_areas()
592
{
593
    $areas = array();
594
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
595
596
    return $areas;
597
}
598
599
/**
600
 * Returns an array with all the roles contained in the database.
601
 *
602
 * @return array a list of available roles
603
 */
604
function bigbluebuttonbn_get_db_moodle_roles($rolename = 'all')
605
{
606
    global $DB;
607
608
    if ($rolename != 'all') {
609
        $roles = $DB->get_record('role', array('shortname' => $rolename));
610
    } else {
611
        $roles = $DB->get_records('role', array());
612
    }
613
614
    return $roles;
615
}
616
617
function bigbluebuttonbn_notification_process($bigbluebuttonbn, $action)
618
{
619
    global $USER;
620
621
    // Prepare message
622
    $msg = new stdClass();
623
624
    /// Build the message_body
625
    $msg->action = $action;
626
    $msg->activity_type = '';
627
    $msg->activity_title = $bigbluebuttonbn->name;
628
629
    /// Add the meeting details to the message_body
630
    $msg->action = ucfirst($action);
631
    $msg->activity_description = '';
632
    if (!empty($bigbluebuttonbn->intro)) {
633
        $msg->activity_description = trim($bigbluebuttonbn->intro);
634
    }
635
    $msg->activity_openingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->openingtime);
636
    $msg->activity_closingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->closingtime);
637
    $msg->activity_owner = fullname($USER);
638
639
    // Send notification to all users enrolled
640
    bigbluebuttonbn_notification_send($USER, $bigbluebuttonbn, bigbluebuttonbn_notification_msg_html($msg));
641
}
642
643
function bigbluebuttonbn_notification_msg_html($msg)
644
{
645
    $message_text = '<p>'.$msg->activity_type.' &quot;'.$msg->activity_title.'&quot; '.get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>'."\n";
646
    $message_text .= '<p><b>'.$msg->activity_title.'</b> '.get_string('email_body_notification_meeting_details', 'bigbluebuttonbn').':'."\n";
647
    $message_text .= '<table border="0" style="margin: 5px 0 0 20px"><tbody>'."\n";
648
    $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_title', 'bigbluebuttonbn').': </td><td>'."\n";
649
    $message_text .= $msg->activity_title.'</td></tr>'."\n";
650
    $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_description', 'bigbluebuttonbn').': </td><td>'."\n";
651
    $message_text .= $msg->activity_description.'</td></tr>'."\n";
652
    $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_start_date', 'bigbluebuttonbn').': </td><td>'."\n";
653
    $message_text .= $msg->activity_openingtime.'</td></tr>'."\n";
654
    $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_end_date', 'bigbluebuttonbn').': </td><td>'."\n";
655
    $message_text .= $msg->activity_closingtime.'</td></tr>'."\n";
656
    $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.$msg->action.' '.get_string('email_body_notification_meeting_by', 'bigbluebuttonbn').': </td><td>'."\n";
657
    $message_text .= $msg->activity_owner.'</td></tr></tbody></table></p>'."\n";
658
}
659
660
function bigbluebuttonbn_notification_send($sender, $bigbluebuttonbn, $message = '')
661
{
662
    global $DB;
663
664
    $context = bigbluebuttonbn_get_context_course($bigbluebuttonbn->course);
665
    $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
666
667
    //Complete message
668
    $msg = new stdClass();
669
    $msg->user_name = fullname($sender);
670
    $msg->user_email = $sender->email;
671
    $msg->course_name = "$course->fullname";
672
    $message .= '<p><hr/><br/>'.get_string('email_footer_sent_by', 'bigbluebuttonbn').' '.$msg->user_name.'('.$msg->user_email.') ';
673
    $message .= get_string('email_footer_sent_from', 'bigbluebuttonbn').' '.$msg->course_name.'.</p>';
674
675
    $users = get_enrolled_users($context);
676
    foreach ($users as $user) {
677
        if ($user->id != $sender->id) {
678
            $messageid = message_post_message($sender, $user, $message, FORMAT_HTML);
679
            if (!empty($messageid)) {
680
                error_log('Msg to '.$msg->user_name.' was sent.');
681
            } else {
682
                error_log('Msg to '.$msg->user_name.' was NOT sent.');
683
            }
684
        }
685
    }
686
}
687
688
function bigbluebuttonbn_get_context($id, $context_type)
0 ignored issues
show
Unused Code introduced by
The parameter $context_type 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...
689
{
690
    return context_module::instance($id);
691
}
692
693
function bigbluebuttonbn_get_context_module($id)
694
{
695
    return bigbluebuttonbn_get_context($id, CONTEXT_MODULE);
696
}
697
698
function bigbluebuttonbn_get_context_course($id)
699
{
700
    return bigbluebuttonbn_get_context($id, CONTEXT_COURSE);
701
}
702
703
function bigbluebuttonbn_get_cfg_server_url()
704
{
705
    global $BIGBLUEBUTTONBN_CFG, $CFG;
706
707
    return isset($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_server_url) ? trim(trim($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_server_url), '/').'/' : (isset($CFG->bigbluebuttonbn_server_url) ? trim(trim($CFG->bigbluebuttonbn_server_url), '/').'/' : 'http://test-install.blindsidenetworks.com/bigbluebutton/');
708
}
709
710
function bigbluebuttonbn_get_cfg_shared_secret()
711
{
712
    global $BIGBLUEBUTTONBN_CFG, $CFG;
713
714
    return isset($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_shared_secret) ? trim($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_shared_secret) : (isset($CFG->bigbluebuttonbn_shared_secret) ? trim($CFG->bigbluebuttonbn_shared_secret) : '8cd8ef52e8e101574e400365b55e11a6');
715
}
716