Completed
Push — master ( 12ecea...43b232 )
by Jesus
03:32
created

lib.php ➔ bigbluebuttonbn_process_pre_save()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 9
eloc 18
c 3
b 1
f 0
nc 48
nop 1
dl 0
loc 26
rs 4.909
1
<?php
2
/**
3
 * Library calls for Moodle and BigBlueButton.
4
 * 
5
 * @package   mod_bigbluebuttonbn
6
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
7
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
8
 * @copyright 2010-2015 Blindside Networks Inc.
9
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
10
 */
11
12
defined('MOODLE_INTERNAL') || die;
13
14
global $BIGBLUEBUTTONBN_CFG, $CFG;
15
16
require_once($CFG->dirroot.'/calendar/lib.php');
17
require_once($CFG->dirroot.'/message/lib.php');
18
require_once($CFG->dirroot.'/mod/lti/OAuth.php');
19
require_once($CFG->libdir.'/accesslib.php');
20
require_once($CFG->libdir.'/completionlib.php');
21
require_once($CFG->libdir.'/datalib.php');
22
require_once($CFG->libdir.'/coursecatlib.php');
23
require_once($CFG->libdir.'/enrollib.php');
24
require_once($CFG->libdir.'/filelib.php');
25
require_once($CFG->libdir.'/formslib.php');
26
27
require_once(dirname(__FILE__).'/JWT.php');
28
29
if( file_exists(dirname(__FILE__).'/config.php') ) {
30
    require_once(dirname(__FILE__).'/config.php');
31
    if( isset($BIGBLUEBUTTONBN_CFG) ) {
32
        $CFG = (object) array_merge((array)$CFG, (array)$BIGBLUEBUTTONBN_CFG);
33
    }
34
} else {
35
    $BIGBLUEBUTTONBN_CFG = new stdClass();
36
}
37
38
/*
39
 * DURATIONCOMPENSATION: Feature removed by configuration
40
 */
41
$BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_scheduled_duration_enabled = 0;
42
/*
43
 * Remove this block when restored
44
 */
45
46
const BIGBLUEBUTTONBN_LOG_EVENT_CREATE = "Create";
47
const BIGBLUEBUTTONBN_LOG_EVENT_JOIN = "Join";
48
const BIGBLUEBUTTONBN_LOG_EVENT_LOGOUT = "Logout";
49
const BIGBLUEBUTTONBN_LOG_EVENT_IMPORT = "Import";
50
const BIGBLUEBUTTONBN_LOG_EVENT_DELETE = "Delete";
51
52
function bigbluebuttonbn_supports($feature) {
53
    switch($feature) {
54
        case FEATURE_IDNUMBER:                return true;
55
        case FEATURE_GROUPS:                  return true;
56
        case FEATURE_GROUPINGS:               return true;
57
        case FEATURE_GROUPMEMBERSONLY:        return true;
58
        case FEATURE_MOD_INTRO:               return true;
59
        case FEATURE_BACKUP_MOODLE2:          return true;
60
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
61
        case FEATURE_GRADE_HAS_GRADE:         return false;
62
        case FEATURE_GRADE_OUTCOMES:          return false;
63
        case FEATURE_SHOW_DESCRIPTION:        return true;
64
        // case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
66
        default: return null;
67
    }
68
}
69
70
/**
71
 * Given an object containing all the necessary data,
72
 * (defined by the form in mod_form.php) this function
73
 * will create a new instance and return the id number
74
 * of the new instance.
75
 *
76
 * @param object $bigbluebuttonbn An object from the form in mod_form.php
0 ignored issues
show
Bug introduced by
There is no parameter named $bigbluebuttonbn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
 * @return int The id of the newly inserted bigbluebuttonbn record
78
 */
79
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...
80
    global $DB, $CFG;
81
82
    $draftitemid = isset($data->presentation)? $data->presentation: null;
83
    $context = bigbluebuttonbn_get_context_module($data->coursemodule);
84
85
    bigbluebuttonbn_process_pre_save($data);
86
87
    unset($data->presentation);
88
    $bigbluebuttonbn_id = $DB->insert_record('bigbluebuttonbn', $data);
89
    $data->id = $bigbluebuttonbn_id;
90
91
    bigbluebuttonbn_update_media_file($bigbluebuttonbn_id, $context, $draftitemid);
92
93
    bigbluebuttonbn_process_post_save($data);
94
95
    return $bigbluebuttonbn_id;
96
}
97
98
/**
99
 * Given an object containing all the necessary data,
100
 * (defined by the form in mod_form.php) this function
101
 * will update an existing instance with new data.
102
 *
103
 * @param object $bigbluebuttonbn An object from the form in mod_form.php
0 ignored issues
show
Bug introduced by
There is no parameter named $bigbluebuttonbn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
 * @return boolean Success/Fail
105
 */
106
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...
107
    global $DB, $CFG;
108
109
    $data->id = $data->instance;
110
    $draftitemid = isset($data->presentation)? $data->presentation: null;
111
    $context = bigbluebuttonbn_get_context_module($data->coursemodule);
112
113
    bigbluebuttonbn_process_pre_save($data);
114
115
    unset($data->presentation);
116
    $DB->update_record("bigbluebuttonbn", $data);
117
118
    bigbluebuttonbn_update_media_file($data->id, $context, $draftitemid);
119
120
    bigbluebuttonbn_process_post_save($data);
121
122
    return true;
123
}
124
125
/**
126
 * Given an ID of an instance of this module,
127
 * this function will permanently delete the instance
128
 * and any data that depends on it.
129
 *
130
 * @param int $id Id of the module instance
131
 * @return boolean Success/Failure
132
 */
133
function bigbluebuttonbn_delete_instance($id) {
134
    global $CFG, $DB, $USER;
135
136
    if (! $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $id))) {
137
        return false;
138
    }
139
140
    $result = true;
141
142
    //
143
    // End the session associated with this instance (if it's running)
144
    //
145
    //$meetingID = $bigbluebuttonbn->meetingid.'-'.$bigbluebuttonbn->course.'-'.$bigbluebuttonbn->id;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
    //
147
    //$modPW = $bigbluebuttonbn->moderatorpass;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148
    //$url = bigbluebuttonbn_get_cfg_server_url();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
149
    //$shared_secret = bigbluebuttonbn_get_cfg_shared_secret();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
    //
151
    //if( bigbluebuttonbn_isMeetingRunning($meetingID, $url, $shared_secret) )
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
152
    //    $getArray = bigbluebuttonbn_doEndMeeting( $meetingID, $modPW, $url, $shared_secret );
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
154
    if (! $DB->delete_records('bigbluebuttonbn', array('id' => $bigbluebuttonbn->id))) {
155
        $result = false;
156
    }
157
158
    if (! $DB->delete_records('event', array('modulename'=>'bigbluebuttonbn', 'instance'=>$bigbluebuttonbn->id))) {
159
        $result = false;
160
    }
161
162
    $log = new stdClass();
163
164
    $log->meetingid = $bigbluebuttonbn->meetingid;
165
    $log->courseid = $bigbluebuttonbn->course;
166
    $log->bigbluebuttonbnid = $bigbluebuttonbn->id;
167
    $log->userid = $USER->id;
168
    $log->timecreated = time();
169
    $log->log = BIGBLUEBUTTONBN_LOG_EVENT_DELETE;
170
171
    $logs = $DB->get_records('bigbluebuttonbn_logs', array('bigbluebuttonbnid' => $bigbluebuttonbn->id, 'log' => BIGBLUEBUTTONBN_LOG_EVENT_CREATE));
172
    error_log(json_encode($logs));
173
    $has_recordings = 'false';
174
    if (! empty($logs) ) {
175
        error_log("IS not empty");
176
        foreach ( $logs as $l ) {
177
            error_log(json_encode($l));
178
            $meta = json_decode($l->meta);
179
            if ( $meta->record ) {
180
                $has_recordings = 'true';
181
            }
182
        }
183
    }
184
    $log->meta = "{\"has_recordings\":{$has_recordings}}";
185
186
    if (! $returnid = $DB->insert_record('bigbluebuttonbn_logs', $log)) {
187
        $result = false;
188
    }
189
190
    return $result;
191
}
192
193
/**
194
 * Return a small object with summary information about what a
195
 * user has done with a given particular instance of this module
196
 * Used for user activity reports.
197
 * $return->time = the time they did it
198
 * $return->info = a short text description
199
 *
200
 * @return null
201
 */
202
function bigbluebuttonbn_user_outline($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $course is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $user 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...
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...
Unused Code introduced by
The parameter $bigbluebuttonbn 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...
203
    return true;
204
}
205
206
/**
207
 * Print a detailed representation of what a user has done with
208
 * a given particular instance of this module, for user activity reports.
209
 *
210
 * @return boolean
211
 */
212
function bigbluebuttonbn_user_complete($course, $user, $mod, $bigbluebuttonbn) {
0 ignored issues
show
Unused Code introduced by
The parameter $course is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $user 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...
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...
Unused Code introduced by
The parameter $bigbluebuttonbn 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...
213
    return true;
214
}
215
216
/**
217
 * Given a course and a time, this module should find recent activity
218
 * that has occurred in bigbluebuttonbn activities and print it out.
219
 * Return true if there was output, or false is there was none.
220
 *
221
 * @return boolean
222
 */
223
function bigbluebuttonbn_print_recent_activity($course, $isteacher, $timestart) {
0 ignored issues
show
Unused Code introduced by
The parameter $course is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $isteacher 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...
Unused Code introduced by
The parameter $timestart 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
    return false;  //  True if anything was printed, otherwise false
225
}
226
227
/**
228
 * Returns all activity in bigbluebuttonbn since a given time
229
 *
230
 * @param array $activities sequentially indexed array of objects
231
 * @param int $index
232
 * @param int $timestart
233
 * @param int $courseid
234
 * @param int $cmid
235
 * @param int $userid defaults to 0
236
 * @param int $groupid defaults to 0
237
 * @return void adds items into $activities and increases $index
238
 */
239
function bigbluebuttonbn_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
0 ignored issues
show
Unused Code introduced by
The parameter $activities 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...
Unused Code introduced by
The parameter $index 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...
Unused Code introduced by
The parameter $timestart 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...
Unused Code introduced by
The parameter $courseid 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...
Unused Code introduced by
The parameter $cmid 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...
Unused Code introduced by
The parameter $userid 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...
Unused Code introduced by
The parameter $groupid 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...
240
}
241
242
/**
243
 * Prints single activity item prepared by {@see recordingsbn_get_recent_mod_activity()}
244
245
 * @return void
246
 */
247
function bigbluebuttonbn_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
0 ignored issues
show
Unused Code introduced by
The parameter $activity 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...
Unused Code introduced by
The parameter $courseid 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...
Unused Code introduced by
The parameter $detail 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...
Unused Code introduced by
The parameter $modnames 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...
Unused Code introduced by
The parameter $viewfullnames 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...
248
}
249
250
/**
251
 * Function to be run periodically according to the moodle cron
252
 * This function searches for things that need to be done, such
253
 * as sending out mail, toggling flags etc ...
254
 *
255
 * @return boolean
256
 **/
257
function bigbluebuttonbn_cron () {
258
    return true;
259
}
260
261
/**
262
 * Must return an array of user records (all data) who are participants
263
 * for a given instance of bigbluebuttonbn. Must include every user involved
264
 * in the instance, independient of his role (student, teacher, admin...)
265
 * See other modules as example.
266
 *
267
 * @param int $bigbluebuttonbnid ID of an instance of this module
268
 * @return mixed boolean/array of students
269
 */
270
function bigbluebuttonbn_get_participants($bigbluebuttonbnid) {
0 ignored issues
show
Unused Code introduced by
The parameter $bigbluebuttonbnid 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...
271
    return false;
272
}
273
274
/**
275
 * Returns all other caps used in module
276
 * @return array
277
 */
278
function bigbluebuttonbn_get_extra_capabilities() {
279
    return array('moodle/site:accessallgroups');
280
}
281
282
////////////////////////////////////////////////////////////////////////////////
283
// Gradebook API                                                              //
284
////////////////////////////////////////////////////////////////////////////////
285
286
/**
287
 * This function returns if a scale is being used by one bigbluebuttonbn
288
 * if it has support for grading and scales. Commented code should be
289
 * modified if necessary. See forum, glossary or journal modules
290
 * as reference.
291
 *
292
 * @param int $bigbluebuttonbnid ID of an instance of this module
293
 * @return mixed
294
 */
295
function bigbluebuttonbn_scale_used($bigbluebuttonbnid, $scaleid) {
0 ignored issues
show
Unused Code introduced by
The parameter $bigbluebuttonbnid 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...
Unused Code introduced by
The parameter $scaleid 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...
296
    $return = false;
297
298
    return $return;
299
}
300
301
/**
302
 * Checks if scale is being used by any instance of bigbluebuttonbn.
303
 * This function was added in 1.9
304
 *
305
 * This is used to find out if scale used anywhere
306
 * @param $scaleid int
307
 * @return boolean True if the scale is used by any bigbluebuttonbn
308
 */
309
function bigbluebuttonbn_scale_used_anywhere($scaleid) {
0 ignored issues
show
Unused Code introduced by
The parameter $scaleid 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...
310
    $return = false;
311
312
    return $return;
313
}
314
315
/**
316
 * This function is used by the reset_course_userdata function in moodlelib.
317
 * @param $data the data submitted from the reset course.
318
 * @return array status array
319
 */
320
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...
321
    return array();
322
}
323
324
/**
325
 * List of view style log actions
326
 * @return array
327
 */
328
function bigbluebuttonbn_get_view_actions() {
329
    return array('view', 'view all');
330
}
331
332
/**
333
 * List of update style log actions
334
 * @return array
335
 */
336
function bigbluebuttonbn_get_post_actions() {
337
    return array('update', 'add', 'create', 'join', 'end', 'left', 'publish', 'unpublish', 'delete');
338
}
339
340
/**
341
 * Given a course_module object, this function returns any
342
 * "extra" information that may be needed when printing
343
 * this activity in a course listing.
344
 * See get_array_of_activities() in course/lib.php
345
 *
346
 * @global object
347
 * @param object $coursemodule
348
 * @return object|null
349
 */
350
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
351
    global $CFG, $DB;
352
353
    if ( !$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id'=>$coursemodule->instance), 'id, name, intro, introformat, newwindow')) {
354
        return NULL;
355
    }
356
357
    $info = new cached_cm_info();
358
    $info->name = $bigbluebuttonbn->name;
359
360
    if ($coursemodule->showdescription) {
361
        // Convert intro to html. Do not filter cached version, filters run at display time.
362
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
363
    }
364
365
    return $info;
366
}
367
368
/**
369
 * Runs any processes that must run before
370
 * a bigbluebuttonbn insert/update
371
 *
372
 * @global object
373
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
374
 * @return void
375
 **/
376
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
377
    global $DB, $CFG;
378
379
    if ( !isset($bigbluebuttonbn->timecreated) || !$bigbluebuttonbn->timecreated ) {
380
        $bigbluebuttonbn->timecreated = time();
381
        //Assign password only if it is a new activity
382
        if( isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add) ) {
383
            $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
384
            $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12);
385
        }
386
387
    } else {
388
        $bigbluebuttonbn->timemodified = time();
389
    }
390
391
    if (! isset($bigbluebuttonbn->newwindow))
392
        $bigbluebuttonbn->newwindow = 0;
393
    if (! isset($bigbluebuttonbn->wait))
394
        $bigbluebuttonbn->wait = 0;
395
    if (! isset($bigbluebuttonbn->record))
396
        $bigbluebuttonbn->record = 0;
397
    if (! isset($bigbluebuttonbn->tagging))
398
        $bigbluebuttonbn->tagging = 0;
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
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
409
 * @return void
410
 **/
411
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
412
    global $DB, $CFG, $USER;
413
414
    // Now that an id was assigned, generate and set the meetingid property based on 
415
    // [Moodle Instance + Activity ID + BBB Secret] (but only for new activities)
416
    if( isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add) ) {
417
        $bigbluebuttonbn_meetingid = sha1($CFG->wwwroot.$bigbluebuttonbn->id.bigbluebuttonbn_get_cfg_shared_secret());
418
        $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn_meetingid, array('id' => $bigbluebuttonbn->id));
419
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
420
    } else {
421
        $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
422
    }
423
    $at = get_string('mod_form_field_notification_msg_at', 'bigbluebuttonbn');
424
425
    // Add evento to the calendar when if openingtime is set
426
    if ( isset($bigbluebuttonbn->openingtime) && $bigbluebuttonbn->openingtime ){
427
        $event = new stdClass();
428
        $event->name        = $bigbluebuttonbn->name;
429
        $event->courseid    = $bigbluebuttonbn->course;
430
        $event->groupid     = 0;
431
        $event->userid      = 0;
432
        $event->modulename  = 'bigbluebuttonbn';
433
        $event->instance    = $bigbluebuttonbn->id;
434
        $event->timestart   = $bigbluebuttonbn->openingtime;
435
436
        if ( $bigbluebuttonbn->closingtime ){
437
            $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
438
        } else {
439
            $event->durationtime = 0;
440
        }
441
442
        if ( $event->id = $DB->get_field('event', 'id', array('modulename'=>'bigbluebuttonbn', 'instance'=>$bigbluebuttonbn->id)) ) {
443
            $calendarevent = calendar_event::load($event->id);
444
            $calendarevent->update($event);
445
        } else {
446
            calendar_event::create($event);
447
        }
448
449
    } else {
450
        $DB->delete_records('event', array('modulename'=>'bigbluebuttonbn', 'instance'=>$bigbluebuttonbn->id));
451
    }
452
453
    if( isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification ) {
454
        // Prepare message
455
        $msg = new stdClass();
456
457
        /// Build the message_body
458
        $msg->action = $action;
459
        $msg->activity_type = "";
460
        $msg->activity_title = $bigbluebuttonbn->name;
461
        $message_text = '<p>'.$msg->activity_type.' &quot;'.$msg->activity_title.'&quot; '.get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>';
462
463
        /// Add the meeting details to the message_body
464
        $msg->action = ucfirst($action);
465
        $msg->activity_description = "";
466
        if( !empty($bigbluebuttonbn->intro) )
467
            $msg->activity_description = trim($bigbluebuttonbn->intro);
468
        $msg->activity_openingtime = "";
469 View Code Duplication
        if ($bigbluebuttonbn->openingtime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
470
            $msg->activity_openingtime = calendar_day_representation($bigbluebuttonbn->openingtime).' '.$at.' '.calendar_time_representation($bigbluebuttonbn->openingtime);
471
        }
472
        $msg->activity_closingtime = "";
473 View Code Duplication
        if ($bigbluebuttonbn->closingtime ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
474
            $msg->activity_closingtime = calendar_day_representation($bigbluebuttonbn->closingtime).' '.$at.' '.calendar_time_representation($bigbluebuttonbn->closingtime);
475
        }
476
        $msg->activity_owner = $USER->firstname.' '.$USER->lastname;
477
478
        $message_text .= '<p><b>'.$msg->activity_title.'</b> '.get_string('email_body_notification_meeting_details', 'bigbluebuttonbn').':';
479
        $message_text .= '<table border="0" style="margin: 5px 0 0 20px"><tbody>';
480
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_title', 'bigbluebuttonbn').': </td><td>';
481
        $message_text .= $msg->activity_title.'</td></tr>';
482
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_description', 'bigbluebuttonbn').': </td><td>';
483
        $message_text .= $msg->activity_description.'</td></tr>';
484
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_start_date', 'bigbluebuttonbn').': </td><td>';
485
        $message_text .= $msg->activity_openingtime.'</td></tr>';
486
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_end_date', 'bigbluebuttonbn').': </td><td>';
487
        $message_text .= $msg->activity_closingtime.'</td></tr>';
488
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.$msg->action.' '.get_string('email_body_notification_meeting_by', 'bigbluebuttonbn').': </td><td>';
489
        $message_text .= $msg->activity_owner.'</td></tr></tbody></table></p>';
490
491
        // Send notification to all users enrolled
492
        bigbluebuttonbn_send_notification($USER, $bigbluebuttonbn, $message_text);
493
    }
494
}
495
496
/**
497
 * Update the bigbluebuttonbn activity to include any file
498
 * that was uploaded, or if there is none, set the
499
 * presentation field to blank.
500
 *
501
 * @param int $bigbluebuttonbn_id the bigbluebuttonbn id
502
 * @param stdClass $context the context
503
 * @param int $draftitemid the draft item
504
 */
505
function bigbluebuttonbn_update_media_file($bigbluebuttonbn_id, $context, $draftitemid) {
506
    global $DB;
507
508
    // Set the filestorage object.
509
    $fs = get_file_storage();
510
    // Save the file if it exists that is currently in the draft area.
511
    file_save_draft_area_files($draftitemid, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
512
    // Get the file if it exists.
513
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0, 'itemid, filepath, filename', false);
514
    // Check that there is a file to process.
515
    if (count($files) == 1) {
516
        // Get the first (and only) file.
517
        $file = reset($files);
518
        // Set the presentation column in the bigbluebuttonbn table.
519
        $DB->set_field('bigbluebuttonbn', 'presentation', '/' . $file->get_filename(), array('id' => $bigbluebuttonbn_id));
520
    } else {
521
        // Set the presentation column in the bigbluebuttonbn table.
522
        $DB->set_field('bigbluebuttonbn', 'presentation', '', array('id' => $bigbluebuttonbn_id));
523
    }
524
}
525
526
/**
527
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-)
528
 *
529
 * @package mod_bigbluebuttonbn
530
 * @category files
531
 * @param stdClass $course course object
532
 * @param stdClass $cm course module object
533
 * @param stdClass $context context object
534
 * @param string $filearea file area
535
 * @param array $args extra arguments
536
 * @param bool $forcedownload whether or not force download
537
 * @param array $options additional options affecting the file serving
538
 * @return bool false if file not found, does not return if found - justsend the file
539
 */
540
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
541
    global $CFG, $DB;
542
543
    if ($context->contextlevel != CONTEXT_MODULE) {
544
        return false;
545
    }
546
547
    $fileareas = bigbluebuttonbn_get_file_areas();
548
    if (!array_key_exists($filearea, $fileareas)) {
549
        return false;
550
    }
551
552
    if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id'=>$cm->instance))) {
553
        return false;
554
    }
555
556
    if( sizeof($args) > 1 ) {
557
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
558
        $presentation_nonce_key = sha1($bigbluebuttonbn->id);
559
        $presentation_nonce = $cache->get($presentation_nonce_key);
560
        $presentation_nonce_value = $presentation_nonce['value'];
561
        $presentation_nonce_counter = $presentation_nonce['counter'];
562
563
        if( $args["0"] != $presentation_nonce_value ) {
564
            return false;
565
        }
566
567
        //The nonce value is actually used twice because BigBlueButton reads the file two times
568
        $presentation_nonce_counter += 1;
569
        if( $presentation_nonce_counter < 2 ) {
570
            $cache->set($presentation_nonce_key, array( "value" => $presentation_nonce_value, "counter" => $presentation_nonce_counter ));
571
        } else {
572
            $cache->delete($presentation_nonce_key);
573
        }
574
575
        $filename = $args["1"];
576
577
    } else {
578
        require_course_login($course, true, $cm);
579
580
        if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
581
            return false;
582
        }
583
584
        $filename = implode('/', $args);
585
    }
586
587
    if ($filearea === 'presentation') {
588
        $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
589
    } else {
590
        return false;
591
    }
592
593
    $fs = get_file_storage();
594
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
595
        return false;
596
    }
597
598
    // finally send the file
599
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
600
}
601
602
/**
603
 * Returns an array of file areas
604
 *
605
 * @package  mod_bigbluebuttonbn
606
 * @category files
607
 * @return array a list of available file areas
608
 */
609
function bigbluebuttonbn_get_file_areas() {
610
    $areas = array();
611
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
612
613
    return $areas;
614
}
615
616
/**
617
 * Returns an array with all the roles contained in the database
618
 *
619
 * @package  mod_bigbluebuttonbn
620
 * @return array a list of available roles
621
 */
622
function bigbluebuttonbn_get_db_moodle_roles($rolename='all') {
623
    global $DB;
624
625
    if( $rolename != 'all')
626
        $roles = $DB->get_record('role', array('shortname' => $rolename));
627
    else
628
        $roles = $DB->get_records('role', array());
629
630
    return $roles;
631
}
632
633
/**
634
 * Returns an array with all the users enrolled in a given course
635
 *
636
 * @package  mod_bigbluebuttonbn
637
 * @return array a list of enrolled users in the course
638
 */
639
function bigbluebuttonbn_get_users($context) {
640
    global $DB;
641
642
    $roles = bigbluebuttonbn_get_db_moodle_roles();
643
    $sqluserids = array();
644
    foreach($roles as $role){
645
        $users = get_role_users($role->id, $context);
646
        foreach($users as $user) {
647
            array_push($sqluserids, $user->id);
648
        }
649
    }
650
651
    $users_array = array();
652
    if( !empty($sqluserids) ) {
653
        $users_array = $DB->get_records_select("user", "id IN (" . implode(', ', $sqluserids) . ") AND deleted = 0");
654
    }
655
656
    return $users_array;
657
}
658
659
function bigbluebuttonbn_send_notification($sender, $bigbluebuttonbn, $message="") {
660
    global $CFG, $DB;
661
662
    $context = bigbluebuttonbn_get_context_course($bigbluebuttonbn->course);
663
    $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
664
665
    //Complete message
666
    $msg = new stdClass();
667
    $msg->user_name = $sender->firstname.' '.$sender->lastname;
668
    $msg->user_email = $sender->email;
669
    $msg->course_name = "$course->fullname";
670
    $message .= '<p><hr/><br/>'.get_string('email_footer_sent_by', 'bigbluebuttonbn').' '.$msg->user_name.'('.$msg->user_email.') ';
671
    $message .= get_string('email_footer_sent_from', 'bigbluebuttonbn').' '.$msg->course_name.'.</p>';
672
    
673
    $users = bigbluebuttonbn_get_users($context);
674
    foreach( $users as $user ) {
675
        if( $user->id != $sender->id ){
676
            error_log("Sending msg to ".$user->firstname." ".$user->lastname.".");
677
            $messageid = message_post_message($sender, $user, $message, FORMAT_HTML);
678
            if (!empty($messageid)) {
679
                error_log("Msg sent to ".$user->firstname." ".$user->lastname.".");
680
            } else {
681
                error_log("Msg was NOT sent.");
682
            }
683
        }
684
    }
685
}
686
687 View Code Duplication
function bigbluebuttonbn_get_context_module($id) {
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...
688
    global $CFG;
689
690
    $version_major = bigbluebuttonbn_get_moodle_version_major();
691
    if ( $version_major < '2013111800' ) {
692
        //This is valid before v2.6
693
        $context = get_context_instance(CONTEXT_MODULE, $id);
694
    } else {
695
        //This is valid after v2.6
696
        $context = context_module::instance($id);
697
    }
698
699
    return $context;
700
}
701
702 View Code Duplication
function bigbluebuttonbn_get_context_course($id) {
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...
703
    global $CFG;
704
705
    $version_major = bigbluebuttonbn_get_moodle_version_major();
706
    if ( $version_major < '2013111800' ) {
707
        //This is valid before v2.6
708
        $context = get_context_instance(CONTEXT_COURSE, $id);
709
    } else {
710
        //This is valid after v2.6
711
        $context = context_course::instance($id);
712
    }
713
714
    return $context;
715
}
716
717
function bigbluebuttonbn_get_cfg_server_url() {
718
    global $BIGBLUEBUTTONBN_CFG, $CFG;
719
    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/'));
720
}
721
722
function bigbluebuttonbn_get_cfg_shared_secret() {
723
    global $BIGBLUEBUTTONBN_CFG, $CFG;
724
    return (isset($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_shared_secret)? trim($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_shared_secret): (isset($CFG->bigbluebuttonbn_shared_secret)? trim($CFG->bigbluebuttonbn_shared_secret): '8cd8ef52e8e101574e400365b55e11a6'));
725
}
726
727