forked from blindsidenetworks-ps/moodle-mod_bigbluebuttonbn
-
Notifications
You must be signed in to change notification settings - Fork 3
/
locallib.php
3568 lines (3403 loc) · 129 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Internal library of functions for module BigBlueButtonBN.
*
* @package mod_bigbluebuttonbn
* @copyright 2010 onwards, Blindside Networks Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Jesus Federico (jesus [at] blindsidenetworks [dt] com)
* @author Fred Dixon (ffdixon [at] blindsidenetworks [dt] com)
*/
use mod_bigbluebuttonbn\locallib;
use mod_bigbluebuttonbn\plugin;
use mod_bigbluebuttonbn\task;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once(__DIR__ . '/lib.php');
/** @var BIGBLUEBUTTONBN_UPDATE_CACHE boolean set to true indicates that cache has to be updated */
const BIGBLUEBUTTONBN_UPDATE_CACHE = true;
/** @var BIGBLUEBUTTONBN_TYPE_ALL integer set to 0 defines an instance type that inclueds room and recordings */
const BIGBLUEBUTTONBN_TYPE_ALL = 0;
/** @var BIGBLUEBUTTONBN_TYPE_ROOM_ONLY integer set to 1 defines an instance type that inclueds only room */
const BIGBLUEBUTTONBN_TYPE_ROOM_ONLY = 1;
/** @var BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY integer set to 2 defines an instance type that inclueds only recordings */
const BIGBLUEBUTTONBN_TYPE_RECORDING_ONLY = 2;
/** @var BIGBLUEBUTTONBN_ROLE_VIEWER string defines the bigbluebutton viewer role */
const BIGBLUEBUTTONBN_ROLE_VIEWER = 'viewer';
/** @var BIGBLUEBUTTONBN_ROLE_MODERATOR string defines the bigbluebutton moderator role */
const BIGBLUEBUTTONBN_ROLE_MODERATOR = 'moderator';
/** @var BIGBLUEBUTTON_EVENT_ACTIVITY_VIEWED string defines the bigbluebuttonbn activity_viewed event */
const BIGBLUEBUTTON_EVENT_ACTIVITY_VIEWED = 'activity_viewed';
/** @var BIGBLUEBUTTON_EVENT_ACTIVITY_MANAGEMENT_VIEWED string defines the bigbluebuttonbn activity_management_viewed event */
const BIGBLUEBUTTON_EVENT_ACTIVITY_MANAGEMENT_VIEWED = 'activity_management_viewed';
/** @var BIGBLUEBUTTON_EVENT_LIVE_SESSION string defines the bigbluebuttonbn live_session event */
const BIGBLUEBUTTON_EVENT_LIVE_SESSION = 'live_session';
/** @var BIGBLUEBUTTON_EVENT_MEETING_CREATED string defines the bigbluebuttonbn meeting_created event */
const BIGBLUEBUTTON_EVENT_MEETING_CREATED = 'meeting_created';
/** @var BIGBLUEBUTTON_EVENT_MEETING_ENDED string defines the bigbluebuttonbn meeting_ended event */
const BIGBLUEBUTTON_EVENT_MEETING_ENDED = 'meeting_ended';
/** @var BIGBLUEBUTTON_EVENT_MEETING_JOINED string defines the bigbluebuttonbn meeting_joined event */
const BIGBLUEBUTTON_EVENT_MEETING_JOINED = 'meeting_joined';
/** @var BIGBLUEBUTTON_EVENT_MEETING_LEFT string defines the bigbluebuttonbn meeting_left event */
const BIGBLUEBUTTON_EVENT_MEETING_LEFT = 'meeting_left';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_DELETED string defines the bigbluebuttonbn recording_deleted event */
const BIGBLUEBUTTON_EVENT_RECORDING_DELETED = 'recording_deleted';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_IMPORTED string defines the bigbluebuttonbn recording_imported event */
const BIGBLUEBUTTON_EVENT_RECORDING_IMPORTED = 'recording_imported';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_PROTECTED string defines the bigbluebuttonbn recording_protected event */
const BIGBLUEBUTTON_EVENT_RECORDING_PROTECTED = 'recording_protected';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_PUBLISHED string defines the bigbluebuttonbn recording_published event */
const BIGBLUEBUTTON_EVENT_RECORDING_PUBLISHED = 'recording_published';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_UNPROTECTED string defines the bigbluebuttonbn recording_unprotected event */
const BIGBLUEBUTTON_EVENT_RECORDING_UNPROTECTED = 'recording_unprotected';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_UNPUBLISHED string defines the bigbluebuttonbn recording_unpublished event */
const BIGBLUEBUTTON_EVENT_RECORDING_UNPUBLISHED = 'recording_unpublished';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_EDITED string defines the bigbluebuttonbn recording_edited event */
const BIGBLUEBUTTON_EVENT_RECORDING_EDITED = 'recording_edited';
/** @var BIGBLUEBUTTON_EVENT_RECORDING_VIEWED string defines the bigbluebuttonbn recording_viewed event */
const BIGBLUEBUTTON_EVENT_RECORDING_VIEWED = 'recording_viewed';
/** @var BIGBLUEBUTTON_EVENT_MEETING_START string defines the bigbluebuttonbn meeting_start event */
const BIGBLUEBUTTON_EVENT_MEETING_START = 'meeting_start';
/** @var BIGBLUEBUTTON_CLIENTTYPE_FLASH integer that defines the bigbluebuttonbn default web client based on Adobe FLASH */
const BIGBLUEBUTTON_CLIENTTYPE_FLASH = 0;
/** @var BIGBLUEBUTTON_CLIENTTYPE_HTML5 integer that defines the bigbluebuttonbn default web client based on HTML5 */
const BIGBLUEBUTTON_CLIENTTYPE_HTML5 = 1;
/** @var BIGBLUEBUTTON_ORIGIN_BASE integer set to 0 defines that the user acceded the session from activity page */
const BIGBLUEBUTTON_ORIGIN_BASE = 0;
/** @var BIGBLUEBUTTON_ORIGIN_TIMELINE integer set to 1 defines that the user acceded the session from Timeline */
const BIGBLUEBUTTON_ORIGIN_TIMELINE = 1;
/** @var BIGBLUEBUTTON_ORIGIN_INDEX integer set to 2 defines that the user acceded the session from Index */
const BIGBLUEBUTTON_ORIGIN_INDEX = 2;
/**
* Builds and retunrs a url for joining a bigbluebutton meeting.
*
* @param string $meetingid
* @param string $username
* @param string $pw
* @param string $logouturl
* @param string $configtoken
* @param string $userid
* @param string $clienttype
*
* @return string
*/
function bigbluebuttonbn_get_join_url(
$meetingid,
$username,
$pw,
$logouturl,
$configtoken = null,
$userid = null,
$clienttype = BIGBLUEBUTTON_CLIENTTYPE_FLASH
) {
$data = ['meetingID' => $meetingid,
'fullName' => $username,
'password' => $pw,
'logoutURL' => $logouturl,
];
// Choose between Adobe Flash or HTML5 Client.
if ($clienttype == BIGBLUEBUTTON_CLIENTTYPE_HTML5) {
$data['joinViaHtml5'] = 'true';
}
if (!is_null($configtoken)) {
$data['configToken'] = $configtoken;
}
if (!is_null($userid)) {
$data['userID'] = $userid;
}
return \mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('join', $data);
}
/**
* Creates a bigbluebutton meeting and returns the response in an array.
*
* @param array $data
* @param array $metadata
* @param string $pname
* @param string $purl
*
* @return array
*/
function bigbluebuttonbn_get_create_meeting_array($data, $metadata = array(), $pname = null, $purl = null) {
$createmeetingurl = \mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('create', $data, $metadata);
$method = 'GET';
$data = null;
if (!is_null($pname) && !is_null($purl)) {
$method = 'POST';
$data = "<?xml version='1.0' encoding='UTF-8'?><modules><module name='presentation'><document url='" .
$purl . "' /></module></modules>";
}
$xml = bigbluebuttonbn_wrap_xml_load_file($createmeetingurl, $method, $data);
if ($xml) {
$response = array('returncode' => $xml->returncode, 'message' => $xml->message, 'messageKey' => $xml->messageKey);
if ($xml->meetingID) {
$response += array('meetingID' => $xml->meetingID, 'attendeePW' => $xml->attendeePW,
'moderatorPW' => $xml->moderatorPW, 'hasBeenForciblyEnded' => $xml->hasBeenForciblyEnded);
}
return $response;
}
return array('returncode' => 'FAILED', 'message' => 'unreachable', 'messageKey' => 'Server is unreachable');
}
/**
* Fetch meeting info and wrap response in array.
*
* @param string $meetingid
*
* @return array
*/
function bigbluebuttonbn_get_meeting_info_array($meetingid) {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('getMeetingInfo', ['meetingID' => $meetingid])
);
if ($xml && $xml->returncode == 'SUCCESS' && empty($xml->messageKey)) {
// Meeting info was returned.
return array('returncode' => $xml->returncode,
'meetingID' => $xml->meetingID,
'moderatorPW' => $xml->moderatorPW,
'attendeePW' => $xml->attendeePW,
'hasBeenForciblyEnded' => $xml->hasBeenForciblyEnded,
'running' => $xml->running,
'recording' => $xml->recording,
'startTime' => $xml->startTime,
'endTime' => $xml->endTime,
'participantCount' => $xml->participantCount,
'moderatorCount' => $xml->moderatorCount,
'attendees' => $xml->attendees,
'metadata' => $xml->metadata,
);
}
if ($xml) {
// Either failure or success without meeting info.
return (array) $xml;
}
// If the server is unreachable, then prompts the user of the necessary action.
return array('returncode' => 'FAILED', 'message' => 'unreachable', 'messageKey' => 'Server is unreachable');
}
/**
* Helper function to retrieve recordings from a BigBlueButton server.
*
* @param string|array $meetingids list of meetingIDs "mid1,mid2,mid3" or array("mid1","mid2","mid3")
* @param string|array $recordingids list of $recordingids "rid1,rid2,rid3" or array("rid1","rid2","rid3") for filtering
*
* @return associative array with recordings indexed by recordID, each recording is a non sequential associative array
*/
function bigbluebuttonbn_get_recordings_array($meetingids, $recordingids = []) {
$meetingidsarray = $meetingids;
if (!is_array($meetingids)) {
$meetingidsarray = explode(',', $meetingids);
}
// If $meetingidsarray is empty there is no need to go further.
if (empty($meetingidsarray)) {
return array();
}
$recordings = bigbluebuttonbn_get_recordings_array_fetch($meetingidsarray);
// Sort recordings.
uasort($recordings, 'bigbluebuttonbn_recording_build_sorter');
// Filter recordings based on recordingIDs.
$recordingidsarray = $recordingids;
if (!is_array($recordingids)) {
$recordingidsarray = explode(',', $recordingids);
}
if (empty($recordingidsarray)) {
// No recording ids, no need to filter.
return $recordings;
}
return bigbluebuttonbn_get_recordings_array_filter($recordingidsarray, $recordings);
}
/**
* Helper function to fetch recordings from a BigBlueButton server.
*
* @param array $meetingidsarray array with meeting ids in the form array("mid1","mid2","mid3")
*
* @return array (associative) with recordings indexed by recordID, each recording is a non sequential associative array
*/
function bigbluebuttonbn_get_recordings_array_fetch($meetingidsarray) {
if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST)
|| defined('BEHAT_SITE_RUNNING')
|| defined('BEHAT_TEST')
|| defined('BEHAT_UTIL')) {
// Just return the fake recording.
global $CFG;
require_once($CFG->libdir . '/testing/generator/lib.php');
require_once(__DIR__ . '/tests/generator/lib.php');
return mod_bigbluebuttonbn_generator::bigbluebuttonbn_get_recordings_array_fetch($meetingidsarray);
}
$recordings = array();
// Execute a paginated getRecordings request.
$pagecount = 25;
$pages = floor(count($meetingidsarray) / $pagecount) + 1;
if (count($meetingidsarray) > 0 && count($meetingidsarray) % $pagecount == 0) {
$pages--;
}
for ($page = 1; $page <= $pages; ++$page) {
$mids = array_slice($meetingidsarray, ($page - 1) * $pagecount, $pagecount);
$recordings += bigbluebuttonbn_get_recordings_array_fetch_page($mids);
}
return $recordings;
}
/**
* Helper function to fetch one page of upto 25 recordings from a BigBlueButton server.
*
* @param array $mids
*
* @return array
*/
function bigbluebuttonbn_get_recordings_array_fetch_page($mids) {
$recordings = array();
// Do getRecordings is executed using a method GET (supported by all versions of BBB).
$url = \mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('getRecordings', ['meetingID' => implode(',', $mids)]);
$xml = bigbluebuttonbn_wrap_xml_load_file($url);
if ($xml && $xml->returncode == 'SUCCESS' && isset($xml->recordings)) {
// If there were meetings already created.
foreach ($xml->recordings->recording as $recordingxml) {
$recording = bigbluebuttonbn_get_recording_array_value($recordingxml);
$recordings[$recording['recordID']] = $recording;
// Check if there is childs.
if (isset($recordingxml->breakoutRooms->breakoutRoom)) {
foreach ($recordingxml->breakoutRooms->breakoutRoom as $breakoutroom) {
$url = \mod_bigbluebuttonbn\locallib\bigbluebutton::action_url(
'getRecordings',
['recordID' => implode(',', (array) $breakoutroom)]
);
$xml = bigbluebuttonbn_wrap_xml_load_file($url);
if ($xml && $xml->returncode == 'SUCCESS' && isset($xml->recordings)) {
// If there were meetings already created.
foreach ($xml->recordings->recording as $recordingxml) {
$recording = bigbluebuttonbn_get_recording_array_value($recordingxml);
$recordings[$recording['recordID']] = $recording;
}
}
}
}
}
}
return $recordings;
}
/**
* Helper function to remove a set of recordings from an array.
*
* @param array $rids
* @param array $recordings
*
* @return array
*/
function bigbluebuttonbn_get_recordings_array_filter($rids, &$recordings) {
foreach ($recordings as $key => $recording) {
if (!in_array($recording['recordID'], $rids)) {
unset($recordings[$key]);
}
}
return $recordings;
}
/**
* Helper function to retrieve imported recordings from the Moodle database.
* The references are stored as events in bigbluebuttonbn_logs.
*
* @param string $courseid
* @param string $bigbluebuttonbnid
* @param bool $subset
*
* @return associative array with imported recordings indexed by recordID, each recording
* is a non sequential associative array that corresponds to the actual recording in BBB
*/
function bigbluebuttonbn_get_recordings_imported_array($courseid = 0, $bigbluebuttonbnid = null, $subset = true) {
global $DB;
$select = bigbluebuttonbn_get_recordings_imported_sql_select($courseid, $bigbluebuttonbnid, $subset);
$recordsimported = $DB->get_records_select('bigbluebuttonbn_logs', $select);
$recordsimportedarray = array();
foreach ($recordsimported as $recordimported) {
$meta = json_decode($recordimported->meta, true);
$recording = $meta['recording'];
// Override imported flag with actual ID.
$recording['imported'] = $recordimported->id;
if (isset($recordimported->protected)) {
$recording['protected'] = (string) $recordimported->protected;
}
$recordsimportedarray[$recording['recordID']] = $recording;
}
return $recordsimportedarray;
}
/**
* Helper function to retrive the default config.xml file.
*
* @return string
*/
function bigbluebuttonbn_get_default_config_xml() {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('getDefaultConfigXML')
);
return $xml;
}
/**
* Helper function to convert an xml recording object to an array in the format used by the plugin.
*
* @param object $recording
*
* @return array
*/
function bigbluebuttonbn_get_recording_array_value($recording) {
// Add formats.
$playbackarray = array();
foreach ($recording->playback->format as $format) {
$playbackarray[(string) $format->type] = array('type' => (string) $format->type,
'url' => trim((string) $format->url), 'length' => (string) $format->length);
// Add preview per format when existing.
if ($format->preview) {
$playbackarray[(string) $format->type]['preview'] = bigbluebuttonbn_get_recording_preview_images($format->preview);
}
}
// Add the metadata to the recordings array.
$metadataarray = bigbluebuttonbn_get_recording_array_meta(get_object_vars($recording->metadata));
$recordingarray = array('recordID' => (string) $recording->recordID,
'meetingID' => (string) $recording->meetingID, 'meetingName' => (string) $recording->name,
'published' => (string) $recording->published, 'startTime' => (string) $recording->startTime,
'endTime' => (string) $recording->endTime, 'playbacks' => $playbackarray);
if (isset($recording->protected)) {
$recordingarray['protected'] = (string) $recording->protected;
}
return $recordingarray + $metadataarray;
}
/**
* Helper function to convert an xml recording preview images to an array in the format used by the plugin.
*
* @param object $preview
*
* @return array
*/
function bigbluebuttonbn_get_recording_preview_images($preview) {
$imagesarray = array();
foreach ($preview->images->image as $image) {
$imagearray = array('url' => trim((string) $image));
foreach ($image->attributes() as $attkey => $attvalue) {
$imagearray[$attkey] = (string) $attvalue;
}
array_push($imagesarray, $imagearray);
}
return $imagesarray;
}
/**
* Helper function to convert an xml recording metadata object to an array in the format used by the plugin.
*
* @param array $metadata
*
* @return array
*/
function bigbluebuttonbn_get_recording_array_meta($metadata) {
$metadataarray = array();
foreach ($metadata as $key => $value) {
if (is_object($value)) {
$value = '';
}
$metadataarray['meta_' . $key] = $value;
}
return $metadataarray;
}
/**
* Helper function to sort an array of recordings. It compares the startTime in two recording objecs.
*
* @param object $a
* @param object $b
*
* @return array
*/
function bigbluebuttonbn_recording_build_sorter($a, $b) {
global $CFG;
$resultless = !empty($CFG->bigbluebuttonbn_recordings_sortorder) ? -1 : 1;
$resultmore = !empty($CFG->bigbluebuttonbn_recordings_sortorder) ? 1 : -1;
if ($a['startTime'] < $b['startTime']) {
return $resultless;
}
if ($a['startTime'] == $b['startTime']) {
return 0;
}
return $resultmore;
}
/**
* Perform deleteRecordings on BBB.
*
* @param string $recordids
*
* @return boolean
*/
function bigbluebuttonbn_delete_recordings($recordids) {
$ids = explode(',', $recordids);
foreach ($ids as $id) {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('deleteRecordings', ['recordID' => $id])
);
if ($xml && $xml->returncode != 'SUCCESS') {
return false;
}
}
return true;
}
/**
* Perform publishRecordings on BBB.
*
* @param string $recordids
* @param string $publish
*/
function bigbluebuttonbn_publish_recordings($recordids, $publish = 'true') {
$ids = explode(',', $recordids);
foreach ($ids as $id) {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('publishRecordings', ['recordID' => $id, 'publish' => $publish])
);
if ($xml && $xml->returncode != 'SUCCESS') {
return false;
}
}
return true;
}
/**
* Perform updateRecordings on BBB.
*
* @param string $recordids
* @param array $params ['key'=>param_key, 'value']
*/
function bigbluebuttonbn_update_recordings($recordids, $params) {
$ids = explode(',', $recordids);
foreach ($ids as $id) {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('updateRecordings', ['recordID' => $id] + (array) $params)
);
if ($xml && $xml->returncode != 'SUCCESS') {
return false;
}
}
return true;
}
/**
* Perform end on BBB.
*
* @param string $meetingid
* @param string $modpw
*/
function bigbluebuttonbn_end_meeting($meetingid, $modpw) {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url('end', ['meetingID' => $meetingid, 'password' => $modpw])
);
if ($xml) {
// If the xml packet returned failure it displays the message to the user.
return array('returncode' => $xml->returncode, 'message' => $xml->message, 'messageKey' => $xml->messageKey);
}
// If the server is unreachable, then prompts the user of the necessary action.
return null;
}
/**
* Perform api request on BBB.
*
* @return string
*/
function bigbluebuttonbn_get_server_version() {
$xml = bigbluebuttonbn_wrap_xml_load_file(
\mod_bigbluebuttonbn\locallib\bigbluebutton::action_url()
);
if ($xml && $xml->returncode == 'SUCCESS') {
return $xml->version;
}
return null;
}
/**
* Perform api request on BBB and wraps the response in an XML object
*
* @param string $url
* @param string $method
* @param string $data
* @param string $contenttype
*
* @return object
*/
function bigbluebuttonbn_wrap_xml_load_file($url, $method = 'GET', $data = null, $contenttype = 'text/xml') {
if (extension_loaded('curl')) {
$response = bigbluebuttonbn_wrap_xml_load_file_curl_request($url, $method, $data, $contenttype);
if (!$response) {
debugging('No response on wrap_simplexml_load_file', DEBUG_DEVELOPER);
return null;
}
$previous = libxml_use_internal_errors(true);
try {
$xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
return $xml;
} catch (Exception $e) {
libxml_use_internal_errors($previous);
$error = 'Caught exception: ' . $e->getMessage();
debugging($error, DEBUG_DEVELOPER);
return null;
}
}
// Alternative request non CURL based.
$previous = libxml_use_internal_errors(true);
try {
$response = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
return $response;
} catch (Exception $e) {
$error = 'Caught exception: ' . $e->getMessage();
debugging($error, DEBUG_DEVELOPER);
libxml_use_internal_errors($previous);
return null;
}
}
/**
* Perform api request on BBB using CURL and wraps the response in an XML object
*
* @param string $url
* @param string $method
* @param string $data
* @param string $contenttype
*
* @return object
*/
function bigbluebuttonbn_wrap_xml_load_file_curl_request($url, $method = 'GET', $data = null, $contenttype = 'text/xml') {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$c = new curl();
$c->setopt(array('SSL_VERIFYPEER' => true));
if ($method == 'POST') {
if (is_null($data) || is_array($data)) {
return $c->post($url);
}
$options = array();
$options['CURLOPT_HTTPHEADER'] = array(
'Content-Type: ' . $contenttype,
'Content-Length: ' . strlen($data),
'Content-Language: en-US',
);
return $c->post($url, $data, $options);
}
if ($method == 'HEAD') {
$c->head($url, array('followlocation' => true, 'timeout' => 1));
return $c->get_info();
}
return $c->get($url);
}
/**
* End the session associated with this instance (if it's running).
*
* @param object $bigbluebuttonbn
*
* @return void
*/
function bigbluebuttonbn_end_meeting_if_running($bigbluebuttonbn) {
$meetingid = $bigbluebuttonbn->meetingid . '-' . $bigbluebuttonbn->course . '-' . $bigbluebuttonbn->id;
if (bigbluebuttonbn_is_meeting_running($meetingid)) {
bigbluebuttonbn_end_meeting($meetingid, $bigbluebuttonbn->moderatorpass);
}
}
/**
* Returns user roles in a context.
*
* @param object $context
* @param integer $userid
*
* @return array $userroles
*/
function bigbluebuttonbn_get_user_roles($context, $userid) {
global $DB;
$userroles = get_user_roles($context, $userid);
if ($userroles) {
$where = '';
foreach ($userroles as $userrole) {
$where .= (empty($where) ? ' WHERE' : ' OR') . ' id=' . $userrole->roleid;
}
$userroles = $DB->get_records_sql('SELECT * FROM {role}' . $where);
}
return $userroles;
}
/**
* Returns guest role wrapped in an array.
*
* @return array
*/
function bigbluebuttonbn_get_guest_role() {
$guestrole = get_guest_role();
return array($guestrole->id => $guestrole);
}
/**
* Returns an array containing all the users in a context.
*
* @param context $context
*
* @return array $users
*/
function bigbluebuttonbn_get_users(context $context = null) {
$users = (array) get_enrolled_users($context, '', 0, 'u.*', null, 0, 0, true);
foreach ($users as $key => $value) {
$users[$key] = fullname($value);
}
return $users;
}
/**
* Returns an array containing all the users in a context wrapped for html select element.
*
* @param context_course $context
* @param null $bbactivity
* @return array $users
* @throws coding_exception
* @throws moodle_exception
*/
function bigbluebuttonbn_get_users_select(context_course $context, $bbactivity = null) {
// CONTRIB-7972, check the group of current user and course group mode.
$groups = null;
$users = (array) get_enrolled_users($context, '', 0, 'u.*', null, 0, 0, true);
$course = get_course($context->instanceid);
$groupmode = groups_get_course_groupmode($course);
if ($bbactivity) {
list($bbcourse, $cm) = get_course_and_cm_from_instance($bbactivity->id, 'bigbluebuttonbn');
$groupmode = groups_get_activity_groupmode($cm);
}
if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
global $USER;
$groups = groups_get_all_groups($course->id, $USER->id);
$users = [];
foreach ($groups as $g) {
$users += (array) get_enrolled_users($context, '', $g->id, 'u.*', null, 0, 0, true);
}
}
return array_map(
function($u) {
return array('id' => $u->id, 'name' => fullname($u));
},
$users);
}
/**
* Returns an array containing all the roles in a context.
*
* @param context $context
* @param bool $onlyviewableroles
*
* @return array $roles
*/
function bigbluebuttonbn_get_roles(context $context = null, bool $onlyviewableroles = true) {
global $CFG;
if ($onlyviewableroles == true && $CFG->branch >= 35) {
$roles = (array) get_viewable_roles($context);
foreach ($roles as $key => $value) {
$roles[$key] = $value;
}
} else {
$roles = (array) role_get_names($context);
foreach ($roles as $key => $value) {
$roles[$key] = $value->localname;
}
}
return $roles;
}
/**
* Returns an array containing all the roles in a context wrapped for html select element.
*
* @param context $context
* @param bool $onlyviewableroles
*
* @return array $users
*/
function bigbluebuttonbn_get_roles_select(context $context = null, bool $onlyviewableroles = true) {
global $CFG;
if ($onlyviewableroles == true && $CFG->branch >= 35) {
$roles = (array) get_viewable_roles($context);
foreach ($roles as $key => $value) {
$roles[$key] = array('id' => $key, 'name' => $value);
}
} else {
$roles = (array) role_get_names($context);
foreach ($roles as $key => $value) {
$roles[$key] = array('id' => $value->id, 'name' => $value->localname);
}
}
return $roles;
}
/**
* Returns role that corresponds to an id.
*
* @param string|integer $id
*
* @return object $role
*/
function bigbluebuttonbn_get_role($id) {
$roles = (array) role_get_names();
if (is_numeric($id) && isset($roles[$id])) {
return (object) $roles[$id];
}
foreach ($roles as $role) {
if ($role->shortname == $id) {
return $role;
}
}
}
/**
* Returns an array to populate a list of participants used in mod_form.js.
*
* @param context $context
* @param null|object $bbactivity
* @return array $data
*/
function bigbluebuttonbn_get_participant_data($context, $bbactivity = null) {
$data = array(
'all' => array(
'name' => get_string('mod_form_field_participant_list_type_all', 'bigbluebuttonbn'),
'children' => []
),
);
$data['role'] = array(
'name' => get_string('mod_form_field_participant_list_type_role', 'bigbluebuttonbn'),
'children' => bigbluebuttonbn_get_roles_select($context, true)
);
$data['user'] = array(
'name' => get_string('mod_form_field_participant_list_type_user', 'bigbluebuttonbn'),
'children' => bigbluebuttonbn_get_users_select($context, $bbactivity),
);
return $data;
}
/**
* Returns an array to populate a list of participants used in mod_form.php.
*
* @param object $bigbluebuttonbn
* @param context $context
*
* @return array
*/
function bigbluebuttonbn_get_participant_list($bigbluebuttonbn, $context) {
global $USER;
if ($bigbluebuttonbn == null) {
return bigbluebuttonbn_get_participant_rules_encoded(
bigbluebuttonbn_get_participant_list_default($context, $USER->id)
);
}
if (empty($bigbluebuttonbn->participants)) {
$bigbluebuttonbn->participants = "[]";
}
$rules = json_decode($bigbluebuttonbn->participants, true);
if (empty($rules)) {
$rules = bigbluebuttonbn_get_participant_list_default($context, bigbluebuttonbn_instance_ownerid($bigbluebuttonbn));
}
return bigbluebuttonbn_get_participant_rules_encoded($rules);
}
/**
* Returns an array to populate a list of participants used in mod_form.php with default values.
*
* @param context $context
* @param integer $ownerid
*
* @return array
*/
function bigbluebuttonbn_get_participant_list_default($context, $ownerid = null) {
$participantlist = array();
$participantlist[] = array(
'selectiontype' => 'all',
'selectionid' => 'all',
'role' => BIGBLUEBUTTONBN_ROLE_VIEWER,
);
$defaultrules = explode(',', \mod_bigbluebuttonbn\locallib\config::get('participant_moderator_default'));
foreach ($defaultrules as $defaultrule) {
if ($defaultrule == '0') {
if (!empty($ownerid) && is_enrolled($context, $ownerid)) {
$participantlist[] = array(
'selectiontype' => 'user',
'selectionid' => (string) $ownerid,
'role' => BIGBLUEBUTTONBN_ROLE_MODERATOR);
}
continue;
}
$participantlist[] = array(
'selectiontype' => 'role',
'selectionid' => $defaultrule,
'role' => BIGBLUEBUTTONBN_ROLE_MODERATOR);
}
return $participantlist;
}
/**
* Returns an array to populate a list of participants used in mod_form.php with bigbluebuttonbn values.
*
* @param array $rules
*
* @return array
*/
function bigbluebuttonbn_get_participant_rules_encoded($rules) {
foreach ($rules as $key => $rule) {
if ($rule['selectiontype'] !== 'role' || is_numeric($rule['selectionid'])) {
continue;
}
$role = bigbluebuttonbn_get_role($rule['selectionid']);
if ($role == null) {
unset($rules[$key]);
continue;
}
$rule['selectionid'] = $role->id;
$rules[$key] = $rule;
}
return $rules;
}
/**
* Returns an array to populate a list of participant_selection used in mod_form.php.
*
* @return array
*/
function bigbluebuttonbn_get_participant_selection_data() {
return [
'type_options' => [
'all' => get_string('mod_form_field_participant_list_type_all', 'bigbluebuttonbn'),
'role' => get_string('mod_form_field_participant_list_type_role', 'bigbluebuttonbn'),
'user' => get_string('mod_form_field_participant_list_type_user', 'bigbluebuttonbn'),
],
'type_selected' => 'all',
'options' => ['all' => '---------------'],
'selected' => 'all',
];
}
/**
* Evaluate if a user in a context is moderator based on roles and participation rules.
*
* @param context $context
* @param array $participantlist
* @param integer $userid
*
* @return boolean
*/
function bigbluebuttonbn_is_moderator($context, $participantlist, $userid = null) {
global $USER;
if (!is_array($participantlist)) {
return false;
}
if (empty($userid)) {
$userid = $USER->id;
}
$userroles = bigbluebuttonbn_get_guest_role();
if (!isguestuser()) {
$userroles = bigbluebuttonbn_get_user_roles($context, $userid);
}
return bigbluebuttonbn_is_moderator_validator($participantlist, $userid, $userroles);
}
/**
* Iterates participant list rules to evaluate if a user is moderator.
*
* @param array $participantlist
* @param integer $userid
* @param array $userroles
*
* @return boolean
*/
function bigbluebuttonbn_is_moderator_validator($participantlist, $userid, $userroles) {
// Iterate participant rules.
foreach ($participantlist as $participant) {
if (bigbluebuttonbn_is_moderator_validate_rule($participant, $userid, $userroles)) {
return true;
}
}
return false;
}
/**
* Evaluate if a user is moderator based on roles and a particular participation rule.
*
* @param object $participant
* @param integer $userid
* @param array $userroles
*
* @return boolean
*/
function bigbluebuttonbn_is_moderator_validate_rule($participant, $userid, $userroles) {
if ($participant['role'] == BIGBLUEBUTTONBN_ROLE_VIEWER) {
return false;
}
// Validation for the 'all' rule.
if ($participant['selectiontype'] == 'all') {
return true;
}
// Validation for a 'user' rule.
if ($participant['selectiontype'] == 'user') {
if ($participant['selectionid'] == $userid) {
return true;
}
return false;
}
// Validation for a 'role' rule.
$role = bigbluebuttonbn_get_role($participant['selectionid']);
if ($role != null && array_key_exists($role->id, $userroles)) {
return true;
}
return false;
}
/**
* Helper returns error message key for the language file that corresponds to a bigbluebutton error key.
*
* @param string $messagekey
* @param string $defaultkey
*
* @return string
*/
function bigbluebuttonbn_get_error_key($messagekey, $defaultkey = null) {
if ($messagekey == 'checksumError') {
return 'index_error_checksum';
}
if ($messagekey == 'maxConcurrent') {
return 'view_error_max_concurrent';
}
return $defaultkey;
}
/**