Completed
Push — master ( ef28c1...9aefdf )
by Jesus
02:33
created

lib.php ➔ bigbluebuttonbn_print_overview()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 24
nc 12
nop 2
dl 0
loc 36
rs 5.1612
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
/**
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
/**
342
 * @global object
343
 * @global object
344
 * @param array $courses
345
 * @param array $htmlarray Passed by reference
346
 */
347
function bigbluebuttonbn_print_overview($courses, &$htmlarray) {
348
    global $USER, $CFG;
349
350
    if (empty($courses) || !is_array($courses) || count($courses) == 0) {
351
        return array();
352
    }
353
354
    if (!$bigbluebuttonbns = get_all_instances_in_courses('bigbluebuttonbn', $courses)) {
355
        return;
356
    }
357
358
    $strbigbluebuttonbn = get_string('modulename', 'bigbluebuttonbn');
359
    $strnextsession  = get_string('nextsession', 'bigbluebuttonbn');
0 ignored issues
show
Unused Code introduced by
$strnextsession is not used, you could remove the assignment.

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

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

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

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

Loading history...
360
361
    foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
362
        $now = time();
363
        if ( $bigbluebuttonbn->openingtime and (!$bigbluebuttonbn->closingtime or $bigbluebuttonbn->closingtime > $now)) { // A bigbluebuttonbn is scheduled.
364
            $str = '<div class="bigbluebuttonbn overview"><div class="name">'.
365
                 $strbigbluebuttonbn.': <a '.($bigbluebuttonbn->visible ? '' : ' class="dimmed"').
366
                 ' href="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$bigbluebuttonbn->coursemodule.'">'.
367
                 $bigbluebuttonbn->name.'</a></div>';
368
            if ( $bigbluebuttonbn->openingtime > $now ) {
369
                $str .= '<div class="info">'.get_string('starts_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).'</div>';
370
            } else {
371
                $str .= '<div class="info">'.get_string('started_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->openingtime).'</div>';
372
            }
373
            $str .= '<div class="info">'.get_string('ends_at', 'bigbluebuttonbn').': '.userdate($bigbluebuttonbn->closingtime).'</div></div>';
374
375
            if (empty($htmlarray[$bigbluebuttonbn->course]['bigbluebuttonbn'])) {
376
                $htmlarray[$bigbluebuttonbn->course]['bigbluebuttonbn'] = $str;
377
            } else {
378
                $htmlarray[$bigbluebuttonbn->course]['bigbluebuttonbn'] .= $str;
379
            }
380
        }
381
    }
382
}
383
384
385
/**
386
 * Given a course_module object, this function returns any
387
 * "extra" information that may be needed when printing
388
 * this activity in a course listing.
389
 * See get_array_of_activities() in course/lib.php
390
 *
391
 * @global object
392
 * @param object $coursemodule
393
 * @return object|null
394
 */
395
function bigbluebuttonbn_get_coursemodule_info($coursemodule) {
396
    global $CFG, $DB;
397
398
    if ( !$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id'=>$coursemodule->instance), 'id, name, intro, introformat, newwindow')) {
399
        return NULL;
400
    }
401
402
    $info = new cached_cm_info();
403
    $info->name = $bigbluebuttonbn->name;
404
405
    if ($coursemodule->showdescription) {
406
        // Convert intro to html. Do not filter cached version, filters run at display time.
407
        $info->content = format_module_intro('bigbluebuttonbn', $bigbluebuttonbn, $coursemodule->id, false);
408
    }
409
410
    return $info;
411
}
412
413
/**
414
 * Runs any processes that must run before
415
 * a bigbluebuttonbn insert/update
416
 *
417
 * @global object
418
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
419
 * @return void
420
 **/
421
function bigbluebuttonbn_process_pre_save(&$bigbluebuttonbn) {
422
    global $DB, $CFG;
423
424
    if ( !isset($bigbluebuttonbn->timecreated) || !$bigbluebuttonbn->timecreated ) {
425
        $bigbluebuttonbn->timecreated = time();
426
        //Assign password only if it is a new activity
427
        if( isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add) ) {
428
            $bigbluebuttonbn->moderatorpass = bigbluebuttonbn_random_password(12);
429
            $bigbluebuttonbn->viewerpass = bigbluebuttonbn_random_password(12);
430
        }
431
432
    } else {
433
        $bigbluebuttonbn->timemodified = time();
434
    }
435
436
    if (! isset($bigbluebuttonbn->newwindow))
437
        $bigbluebuttonbn->newwindow = 0;
438
    if (! isset($bigbluebuttonbn->wait))
439
        $bigbluebuttonbn->wait = 0;
440
    if (! isset($bigbluebuttonbn->record))
441
        $bigbluebuttonbn->record = 0;
442
    if (! isset($bigbluebuttonbn->tagging))
443
        $bigbluebuttonbn->tagging = 0;
444
445
    $bigbluebuttonbn->participants = htmlspecialchars_decode($bigbluebuttonbn->participants);
446
}
447
448
/**
449
 * Runs any processes that must be run
450
 * after a bigbluebuttonbn insert/update
451
 *
452
 * @global object
453
 * @param object $bigbluebuttonbn BigBlueButtonBN form data
454
 * @return void
455
 **/
456
function bigbluebuttonbn_process_post_save(&$bigbluebuttonbn) {
457
    global $DB, $CFG, $USER;
458
459
    // Now that an id was assigned, generate and set the meetingid property based on
460
    // [Moodle Instance + Activity ID + BBB Secret] (but only for new activities)
461
    if( isset($bigbluebuttonbn->add) && !empty($bigbluebuttonbn->add) ) {
462
        $bigbluebuttonbn_meetingid = sha1($CFG->wwwroot.$bigbluebuttonbn->id.bigbluebuttonbn_get_cfg_shared_secret());
463
        $DB->set_field('bigbluebuttonbn', 'meetingid', $bigbluebuttonbn_meetingid, array('id' => $bigbluebuttonbn->id));
464
        $action = get_string('mod_form_field_notification_msg_created', 'bigbluebuttonbn');
465
    } else {
466
        $action = get_string('mod_form_field_notification_msg_modified', 'bigbluebuttonbn');
467
    }
468
    $at = get_string('mod_form_field_notification_msg_at', 'bigbluebuttonbn');
469
470
    // Add evento to the calendar when if openingtime is set
471
    if ( isset($bigbluebuttonbn->openingtime) && $bigbluebuttonbn->openingtime ){
472
        $event = new stdClass();
473
        $event->name        = $bigbluebuttonbn->name;
474
        $event->courseid    = $bigbluebuttonbn->course;
475
        $event->groupid     = 0;
476
        $event->userid      = 0;
477
        $event->modulename  = 'bigbluebuttonbn';
478
        $event->instance    = $bigbluebuttonbn->id;
479
        $event->timestart   = $bigbluebuttonbn->openingtime;
480
481
        if ( $bigbluebuttonbn->closingtime ){
482
            $event->durationtime = $bigbluebuttonbn->closingtime - $bigbluebuttonbn->openingtime;
483
        } else {
484
            $event->durationtime = 0;
485
        }
486
487
        if ( $event->id = $DB->get_field('event', 'id', array('modulename'=>'bigbluebuttonbn', 'instance'=>$bigbluebuttonbn->id)) ) {
488
            $calendarevent = calendar_event::load($event->id);
489
            $calendarevent->update($event);
490
        } else {
491
            calendar_event::create($event);
492
        }
493
494
    } else {
495
        $DB->delete_records('event', array('modulename'=>'bigbluebuttonbn', 'instance'=>$bigbluebuttonbn->id));
496
    }
497
498
    if( isset($bigbluebuttonbn->notification) && $bigbluebuttonbn->notification ) {
499
        // Prepare message
500
        $msg = new stdClass();
501
502
        /// Build the message_body
503
        $msg->action = $action;
504
        $msg->activity_type = "";
505
        $msg->activity_title = $bigbluebuttonbn->name;
506
        $message_text = '<p>'.$msg->activity_type.' &quot;'.$msg->activity_title.'&quot; '.get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>';
507
508
        /// Add the meeting details to the message_body
509
        $msg->action = ucfirst($action);
510
        $msg->activity_description = "";
511
        if( !empty($bigbluebuttonbn->intro) )
512
            $msg->activity_description = trim($bigbluebuttonbn->intro);
513
        $msg->activity_openingtime = "";
514 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...
515
            $msg->activity_openingtime = calendar_day_representation($bigbluebuttonbn->openingtime).' '.$at.' '.calendar_time_representation($bigbluebuttonbn->openingtime);
516
        }
517
        $msg->activity_closingtime = "";
518 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...
519
            $msg->activity_closingtime = calendar_day_representation($bigbluebuttonbn->closingtime).' '.$at.' '.calendar_time_representation($bigbluebuttonbn->closingtime);
520
        }
521
        $msg->activity_owner = fullname($USER);
522
523
        $message_text .= '<p><b>'.$msg->activity_title.'</b> '.get_string('email_body_notification_meeting_details', 'bigbluebuttonbn').':';
524
        $message_text .= '<table border="0" style="margin: 5px 0 0 20px"><tbody>';
525
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_title', 'bigbluebuttonbn').': </td><td>';
526
        $message_text .= $msg->activity_title.'</td></tr>';
527
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_description', 'bigbluebuttonbn').': </td><td>';
528
        $message_text .= $msg->activity_description.'</td></tr>';
529
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_start_date', 'bigbluebuttonbn').': </td><td>';
530
        $message_text .= $msg->activity_openingtime.'</td></tr>';
531
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.get_string('email_body_notification_meeting_end_date', 'bigbluebuttonbn').': </td><td>';
532
        $message_text .= $msg->activity_closingtime.'</td></tr>';
533
        $message_text .= '<tr><td style="font-weight:bold;color:#555;">'.$msg->action.' '.get_string('email_body_notification_meeting_by', 'bigbluebuttonbn').': </td><td>';
534
        $message_text .= $msg->activity_owner.'</td></tr></tbody></table></p>';
535
536
        // Send notification to all users enrolled
537
        bigbluebuttonbn_send_notification($USER, $bigbluebuttonbn, $message_text);
538
    }
539
}
540
541
/**
542
 * Update the bigbluebuttonbn activity to include any file
543
 * that was uploaded, or if there is none, set the
544
 * presentation field to blank.
545
 *
546
 * @param int $bigbluebuttonbn_id the bigbluebuttonbn id
547
 * @param stdClass $context the context
548
 * @param int $draftitemid the draft item
549
 */
550
function bigbluebuttonbn_update_media_file($bigbluebuttonbn_id, $context, $draftitemid) {
551
    global $DB;
552
553
    // Set the filestorage object.
554
    $fs = get_file_storage();
555
    // Save the file if it exists that is currently in the draft area.
556
    file_save_draft_area_files($draftitemid, $context->id, 'mod_bigbluebuttonbn', 'presentation', 0);
557
    // Get the file if it exists.
558
    $files = $fs->get_area_files($context->id, 'mod_bigbluebuttonbn', 'presentation', 0, 'itemid, filepath, filename', false);
559
    // Check that there is a file to process.
560
    if (count($files) == 1) {
561
        // Get the first (and only) file.
562
        $file = reset($files);
563
        // Set the presentation column in the bigbluebuttonbn table.
564
        $DB->set_field('bigbluebuttonbn', 'presentation', '/' . $file->get_filename(), array('id' => $bigbluebuttonbn_id));
565
    } else {
566
        // Set the presentation column in the bigbluebuttonbn table.
567
        $DB->set_field('bigbluebuttonbn', 'presentation', '', array('id' => $bigbluebuttonbn_id));
568
    }
569
}
570
571
/**
572
 * Serves the bigbluebuttonbn attachments. Implements needed access control ;-)
573
 *
574
 * @package mod_bigbluebuttonbn
575
 * @category files
576
 * @param stdClass $course course object
577
 * @param stdClass $cm course module object
578
 * @param stdClass $context context object
579
 * @param string $filearea file area
580
 * @param array $args extra arguments
581
 * @param bool $forcedownload whether or not force download
582
 * @param array $options additional options affecting the file serving
583
 * @return bool false if file not found, does not return if found - justsend the file
584
 */
585
function bigbluebuttonbn_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
586
    global $CFG, $DB;
587
588
    if ($context->contextlevel != CONTEXT_MODULE) {
589
        return false;
590
    }
591
592
    $fileareas = bigbluebuttonbn_get_file_areas();
593
    if (!array_key_exists($filearea, $fileareas)) {
594
        return false;
595
    }
596
597
    if (!$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id'=>$cm->instance))) {
598
        return false;
599
    }
600
601
    if( sizeof($args) > 1 ) {
602
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'mod_bigbluebuttonbn', 'presentation_cache');
603
        $presentation_nonce_key = sha1($bigbluebuttonbn->id);
604
        $presentation_nonce = $cache->get($presentation_nonce_key);
605
        $presentation_nonce_value = $presentation_nonce['value'];
606
        $presentation_nonce_counter = $presentation_nonce['counter'];
607
608
        if( $args["0"] != $presentation_nonce_value ) {
609
            return false;
610
        }
611
612
        //The nonce value is actually used twice because BigBlueButton reads the file two times
613
        $presentation_nonce_counter += 1;
614
        if( $presentation_nonce_counter < 2 ) {
615
            $cache->set($presentation_nonce_key, array( "value" => $presentation_nonce_value, "counter" => $presentation_nonce_counter ));
616
        } else {
617
            $cache->delete($presentation_nonce_key);
618
        }
619
620
        $filename = $args["1"];
621
622
    } else {
623
        require_course_login($course, true, $cm);
624
625
        if (!has_capability('mod/bigbluebuttonbn:join', $context)) {
626
            return false;
627
        }
628
629
        $filename = implode('/', $args);
630
    }
631
632
    if ($filearea === 'presentation') {
633
        $fullpath = "/$context->id/mod_bigbluebuttonbn/$filearea/0/".$filename;
634
    } else {
635
        return false;
636
    }
637
638
    $fs = get_file_storage();
639
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
640
        return false;
641
    }
642
643
    // finally send the file
644
    send_stored_file($file, 0, 0, $forcedownload, $options); // download MUST be forced - security!
645
}
646
647
/**
648
 * Returns an array of file areas
649
 *
650
 * @package  mod_bigbluebuttonbn
651
 * @category files
652
 * @return array a list of available file areas
653
 */
654
function bigbluebuttonbn_get_file_areas() {
655
    $areas = array();
656
    $areas['presentation'] = get_string('mod_form_block_presentation', 'bigbluebuttonbn');
657
658
    return $areas;
659
}
660
661
/**
662
 * Returns an array with all the roles contained in the database
663
 *
664
 * @package  mod_bigbluebuttonbn
665
 * @return array a list of available roles
666
 */
667
function bigbluebuttonbn_get_db_moodle_roles($rolename='all') {
668
    global $DB;
669
670
    if( $rolename != 'all')
671
        $roles = $DB->get_record('role', array('shortname' => $rolename));
672
    else
673
        $roles = $DB->get_records('role', array());
674
675
    return $roles;
676
}
677
678
/**
679
 * Returns an array with all the users enrolled in a given course
680
 *
681
 * @package  mod_bigbluebuttonbn
682
 * @return array a list of enrolled users in the course
683
 */
684
function bigbluebuttonbn_get_users($context) {
685
    global $DB;
686
687
    $roles = bigbluebuttonbn_get_db_moodle_roles();
688
    $sqluserids = array();
689
    foreach($roles as $role){
690
        $users = get_role_users($role->id, $context);
691
        foreach($users as $user) {
692
            array_push($sqluserids, $user->id);
693
        }
694
    }
695
696
    $users_array = array();
697
    if( !empty($sqluserids) ) {
698
        $users_array = $DB->get_records_select("user", "id IN (" . implode(', ', $sqluserids) . ") AND deleted = 0");
699
    }
700
701
    return $users_array;
702
}
703
704
function bigbluebuttonbn_send_notification($sender, $bigbluebuttonbn, $message="") {
705
    global $CFG, $DB;
706
707
    $context = bigbluebuttonbn_get_context_course($bigbluebuttonbn->course);
708
    $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
709
710
    //Complete message
711
    $msg = new stdClass();
712
    $msg->user_name = fullname($sender);
713
    $msg->user_email = $sender->email;
714
    $msg->course_name = "$course->fullname";
715
    $message .= '<p><hr/><br/>'.get_string('email_footer_sent_by', 'bigbluebuttonbn').' '.$msg->user_name.'('.$msg->user_email.') ';
716
    $message .= get_string('email_footer_sent_from', 'bigbluebuttonbn').' '.$msg->course_name.'.</p>';
717
718
    $users = bigbluebuttonbn_get_users($context);
719
    foreach( $users as $user ) {
720
        if( $user->id != $sender->id ){
721
            $messageid = message_post_message($sender, $user, $message, FORMAT_HTML);
722
            if (!empty($messageid)) {
723
                error_log("Msg to ".$msg->user_name." was sent.");
724
            } else {
725
                error_log("Msg to ".$msg->user_name." was NOT sent.");
726
            }
727
        }
728
    }
729
}
730
731 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...
732
    global $CFG;
733
734
    $version_major = bigbluebuttonbn_get_moodle_version_major();
735
    if ( $version_major < '2013111800' ) {
736
        //This is valid before v2.6
737
        $context = get_context_instance(CONTEXT_MODULE, $id);
738
    } else {
739
        //This is valid after v2.6
740
        $context = context_module::instance($id);
741
    }
742
743
    return $context;
744
}
745
746 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...
747
    global $CFG;
748
749
    $version_major = bigbluebuttonbn_get_moodle_version_major();
750
    if ( $version_major < '2013111800' ) {
751
        //This is valid before v2.6
752
        $context = get_context_instance(CONTEXT_COURSE, $id);
753
    } else {
754
        //This is valid after v2.6
755
        $context = context_course::instance($id);
756
    }
757
758
    return $context;
759
}
760
761
function bigbluebuttonbn_get_cfg_server_url() {
762
    global $BIGBLUEBUTTONBN_CFG, $CFG;
763
    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/'));
764
}
765
766
function bigbluebuttonbn_get_cfg_shared_secret() {
767
    global $BIGBLUEBUTTONBN_CFG, $CFG;
768
    return (isset($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_shared_secret)? trim($BIGBLUEBUTTONBN_CFG->bigbluebuttonbn_shared_secret): (isset($CFG->bigbluebuttonbn_shared_secret)? trim($CFG->bigbluebuttonbn_shared_secret): '8cd8ef52e8e101574e400365b55e11a6'));
769
}
770