-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlocallib.php
1248 lines (1145 loc) · 50.4 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 the Moodle module "EJSApp"
//
// EJSApp 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.
//
// EJSApp 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/>.
//
// EJSApp has been developed by:
// - Luis de la Torre: [email protected]
// - Ruben Heradio: [email protected]
//
// at the Computer Science and Automatic Control, Spanish Open University
// (UNED), Madrid, Spain.
/**
* Internal library of functions for module ejsapp
*
* All the ejsapp specific functions, needed to implement the module logic, are here.
*
* @package mod_ejsapp
* @copyright 2012 Luis de la Torre and Ruben Heradio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Updates the ejsapp, ejsapp_personal_vars, tables and files according to the .jar/.zip information
*
* @param stdClass $ejsapp record from table ejsapp
* @param object $context context module
* @return boolean ejs_ok
* @throws
*
*/
function update_ejsapp_files_and_tables($ejsapp, $context) {
global $CFG, $DB;
$maxbytes = get_max_upload_file_size($CFG->maxbytes);
// Creating the .jar or .zip file in dataroot and updating the files table in the database.
if ($ejsapp->appletfile) {
file_save_draft_area_files($ejsapp->appletfile, $context->id, 'mod_ejsapp', 'compressed',
$ejsapp->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 1,
'accepted_types' => array('application/java-archive', 'application/zip')));
}
// Creating the state file in dataroot and updating the files table in the database.
if ($ejsapp->statefile) {
file_save_draft_area_files($ejsapp->statefile, $context->id, 'mod_ejsapp', 'xmlfiles',
$ejsapp->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 1,
'accepted_types' => array('text/xml', 'application/xml', 'application/json')));
}
// Creating the recording file in dataroot and updating the files table in the database.
if ($ejsapp->recordingfile) {
file_save_draft_area_files($ejsapp->recordingfile, $context->id, 'mod_ejsapp', 'recfiles',
$ejsapp->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 1));
}
// Creating the blockly file in dataroot and updating the files table in the database.
if ($ejsapp->use_blockly == 1) {
if ($ejsapp->blocklyfile) {
file_save_draft_area_files($ejsapp->blocklyfile, $context->id, 'mod_ejsapp', 'blkfiles',
$ejsapp->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 1));
}
}
// Same with the content of the wording element.
if ($ejsapp->ejsappwording['itemid']) {
$ejsapp->appwording = file_save_draft_area_files($ejsapp->ejsappwording['itemid'], $context->id, 'mod_ejsapp',
'appwording', 0, array('subdirs' => 1, 'maxbytes' => $CFG->maxbytes, 'changeformat' => 1,
'context' => $context, 'noclean' => 1, 'trusttext' => 0), $ejsapp->appwording);
}
// Get EjsS file regardless it is stored in Moodle filesystem or an alias to a file in a repository
$syncfileinfo = get_sync_file($ejsapp->id);
$file = $syncfileinfo[0];
// Get params and set their corresponding values in the mod_form elements and update the ejsapp table.
if (pathinfo($file->get_filename(), PATHINFO_EXTENSION) == 'jar') { // Java.
$ejsok = modifications_for_java($ejsapp, $file);
} else { // Javascript.
$ejsok = modifications_for_javascript($context, $ejsapp, $file);
}
// Configuration of blockly.
$functions = array();
$func_names = $ejsapp->func_name;
$remote = $ejsapp->remote_function;
for ($i = 0; $i < count($func_names); $i++) {
array_push($functions, [$func_names[$i], $remote[$i]]);
}
$blocklyconf = array();
array_push($blocklyconf, $ejsapp->use_blockly);
array_push($blocklyconf, $ejsapp->charts_blockly);
array_push($blocklyconf, $ejsapp->events_blockly);
array_push($blocklyconf, $ejsapp->functions);
array_push($blocklyconf, $ejsapp->func_language);
array_push($blocklyconf, $functions);
$ejsapp->blockly_conf = json_encode($blocklyconf);
$DB->update_record('ejsapp', $ejsapp);
// Update ejsapp_personal_vars table.
// Personalizing EJS variables: update ejsapp_personal_vars table.
$oldejsapp = $DB->get_records('ejsapp_personal_vars', array('ejsappid' => $ejsapp->id));
if (isset($oldejsapp)) { // We clean all the personalized variables configuration and start over again.
$DB->delete_records('ejsapp_personal_vars', array('ejsappid' => $ejsapp->id));
}
if ($ejsapp->personalvars == 1) {
$personalvars = new stdClass();
$personalvars->ejsappid = $ejsapp->id;
for ($i = 0; $i < count($ejsapp->var_name); $i++) {
if (strcmp($ejsapp->var_name[$i], '') != 0) { // Variables without name are ignored.
$personalvars->name = $ejsapp->var_name[$i];
$typeinfo = 'Boolean';
$minvalue = 0;
$maxvalue = 1;
if ($ejsapp->var_type[$i] == 1) {
$typeinfo = 'Integer';
$minvalue = $ejsapp->min_value[$i];
$maxvalue = $ejsapp->max_value[$i];
} else if ($ejsapp->var_type[$i] == 2) {
$typeinfo = 'Double';
$minvalue = $ejsapp->min_value[$i];
$maxvalue = $ejsapp->max_value[$i];
}
$personalvars->type = $typeinfo;
$personalvars->minval = $minvalue + 0;
$personalvars->maxval = $maxvalue + 0;
$DB->insert_record('ejsapp_personal_vars', $personalvars);
}
}
}
return $ejsok;
} // End of function update_ejsapp_files_and_tables.
/**
* Get EjsS file regardless it is stored in Moodle filesystem or an alias to a file in a repository
*
* @param int $ejsappid
* @return array[stdclass|bool bool] [$file $modified]
* @throws
*
*/
function get_sync_file($ejsappid) {
global $DB;
// Obtain the uploaded .zip or .jar file from moodledata using the information in the files table.
$filerecords = $DB->get_records('files', array('component' => 'mod_ejsapp', 'filearea' => 'compressed',
'itemid' => $ejsappid), 'filesize DESC');
$filerecord = reset($filerecords);
$fs = get_file_storage();
$file = $fs->get_file_by_id($filerecord->id);
$modified = false;
// In case it is an alias to an external repository.
if (!is_null($filerecord->referencefileid)) {
$file->sync_external_file();
$contenthash = $DB->get_field('files_reference', 'referencehash',
array('id' => $filerecord->referencefileid));
if ($filerecord->contenthash != $contenthash) {
$modified = true;
$filerecord->contenthash = $contenthash;
$DB->update_record('files', $filerecord);
}
}
return [$file, $modified];
}
/**
* Generates the values of the personalized variables in a particular EJS application for a given user.
*
* @param stdClass $ejsapp
* @param stdClass $user
* @param boolean $shuffle
* @return stdClass $personalvarsinfo
* @throws
*
*/
function personalize_vars($ejsapp, $user, $shuffle) {
global $DB;
$personalvarsinfo = null;
if ($ejsapp->personalvars == 1) {
$personalvarsinfo = new stdClass();
$personalvars = $DB->get_records('ejsapp_personal_vars', array('ejsappid' => $ejsapp->id));
$i = 0;
foreach ($personalvars as $personalvar) {
$personalvarsinfo->name[$i] = $personalvar->name;
$factor = 1;
if ($personalvar->type == 'Double') {
$factor = 100;
}
$seed1 = array($user->firstname, $user->lastname, $user->id, $personalvar->name);
$seed2 = array($user->email, $personalvar->minval, $user->username, $personalvar->maxval);
if ($shuffle) {
shuffle($seed1);
shuffle($seed2);
}
$seedvalue1 = filter_var(md5($seed1[0] . $seed1[1] . $seed1[2] . $seed1[3]), FILTER_SANITIZE_NUMBER_INT);
$seedvalue2 = filter_var(md5($seed2[0] . $seed2[1] . $seed2[2] . $seed1[3]), FILTER_SANITIZE_NUMBER_INT);
$seedval = $seedvalue1 + $seedvalue2;
mt_srand(intval($seedval));
$personalvarsinfo->value[$i] = mt_rand($factor * $personalvar->minval, $factor * $personalvar->maxval) / $factor;
$personalvarsinfo->type[$i] = $personalvar->type;
$i++;
}
}
return $personalvarsinfo;
}
/**
* Generates the values of the personalized variables in a particular EJS application for all the users in the course
* that ejsapp activity is.
*
* @param stdClass $ejsapp
* @return array $userspersonalvarsinfo
* @throws
*
*/
function users_personalized_vars($ejsapp) {
global $DB;
$enrolids = $DB->get_fieldset_select('enrol', 'id', 'courseid = :courseid',
array('courseid' => $ejsapp->course));
$usersids = $DB->get_fieldset_sql('SELECT userid FROM {user_enrolments} WHERE enrolid IN (' .
implode(',', $enrolids) . ')');
$users = $DB->get_records_sql('SELECT * FROM {user} WHERE id IN (' . implode(',', $usersids) . ')');
$userspersonalvarsinfo = array();
foreach ($users as $user) {
$userspersonalvarsinfo[$user->id.''] = personalize_vars($ejsapp, $user, false);
}
return $userspersonalvarsinfo;
}
/**
* For EjsS java applications.
*
* @param stdClass $ejsapp
* @param stored_file $file
* @return boolean $ejsok
* @throws
*
*/
function modifications_for_java($ejsapp, $file) {
$ejsapp->main_file = $file->get_filename();
// Sign the applet.
// Check whether a certificate is installed and in use.
if (file_exists(get_config('mod_ejsapp', 'certificate_path')) &&
get_config('mod_ejsapp', 'certificate_password') != '' &&
get_config('mod_ejsapp', 'certificate_alias') != '') {
if (is_null($file->get_referencefileid())) { // Linked files won't get signed.
// Sign the applet.
shell_exec("jarsigner -storetype pkcs12 -keystore " . get_config('mod_ejsapp',
'certificate_path') . " -storepass " .
get_config('mod_ejsapp', 'certificate_password') .
" -tsa http://timestamp.comodoca.com/rfc3161 " .
$file . " " . get_config('mod_ejsapp', 'certificate_alias'));
// We replace the file stored in Moodle's filesystem and its table with the signed version.
/*$file->delete();
$fs = get_file_storage();
$fs->create_file_from_pathname($filerecord, $filepath);*/
}
}
return true;
}
/**
* For EjsS javascript applications.
*
* @param stdClass $context
* @param stdClass $ejsapp
* @param stored_file $file
* @return boolean $ejsok
* @throws
*
*/
function modifications_for_javascript($context, $ejsapp, $file) {
global $DB;
$ejsok = false;
$packer = get_file_packer('application/zip');
if ($file->extract_to_storage($packer, $context->id, 'mod_ejsapp', 'content', $ejsapp->id, '/')) {
// Search in _metadata for the name of the main Javascript file and save it.
$filerecords = $DB->get_records('files', array('component' => 'mod_ejsapp', 'filearea' => 'content',
'itemid' => $ejsapp->id, 'filename' => '_metadata.txt'), 'filesize DESC');
$filerecord = reset($filerecords);
$fs = get_file_storage();
$file = $fs->get_file_by_id($filerecord->id);
$metadata = $file->get_content();
$pattern = '/main-simulation\s*:\s*(.+)\s*/';
preg_match($pattern, $metadata, $matches, PREG_OFFSET_CAPTURE);
$substr = $matches[1][0];
if (strlen($matches[1][0]) == 59) {
$pattern = '/^\s(.+)\s*/m';
if ((preg_match($pattern, $metadata, $matches, PREG_OFFSET_CAPTURE) > 0)) {
$substr = $substr . $matches[1][0];
}
}
$ejsapp->main_file = pathinfo(rtrim($substr), PATHINFO_FILENAME);
if (!empty($filerecords = $DB->get_records('files', array('component' => 'mod_ejsapp', 'filearea' => 'content',
'itemid' => $ejsapp->id, 'filename' => $ejsapp->main_file . '.js'), 'filesize DESC'))) {
$mainfile = 'javascript';
} else if (!empty($filerecords = $DB->get_records('files', array('component' => 'mod_ejsapp', 'filearea' => 'content',
'itemid' => $ejsapp->id, 'filename' => $ejsapp->main_file . '.xhtml'), 'filesize DESC'))) {
$mainfile = 'xhtml';
}
if (!empty($filerecords)) {
// Edit file content to replace context-dependent content (i.e. the EjsS' _model variable declaration)
$filerecord = reset($filerecords);
$originalfile = $fs->get_file_by_id($filerecord->id);
$originalfilecontent = $originalfile->get_content();
$pathfiles = new moodle_url("/pluginfile.php/" . $file->get_contextid() . "/" . $file->get_component() .
"/content/" . $file->get_itemid());
$ejsslibpathfiles = $pathfiles . "/_ejs_library/";
$replacedfilecontent = str_replace("(\"_topFrame\",\"_ejs_library/\",null);",
"(\"_topFrame\",\"$ejsslibpathfiles\",\"$pathfiles\");",
$originalfilecontent);
$originalfile->delete();
// Extract needed javascript code from the .xhtml file's header into the new .js file
$doc = new DOMDocument();
if ($mainfile == "javascript") { // If javascript, the xhtml can be used as it is
$xhtmlfilerecords = $DB->get_records('files', array('component' => 'mod_ejsapp', 'filearea' => 'content',
'itemid' => $ejsapp->id, 'filename' => $ejsapp->main_file . '.xhtml'), 'filesize DESC');
$xhtmlfilerecord = reset($xhtmlfilerecords);
$originalfile = $fs->get_file_by_id($xhtmlfilerecord->id);
$originalfilecontent = $originalfile->get_content();
$originalfile->delete();
$doc->loadHTML($originalfilecontent);
} else { //If xhtml, the replaced content is needed
$doc->loadHTML($replacedfilecontent);
}
$xpath = new DOMXpath($doc);
$n = 1;
$newfilecontent = "";
foreach ($xpath->query('/html/head/script[string-length(text()) > 1]') as $queryResult) {
$newfilecontent .= $xpath->evaluate('string(/html/head/script[string-length(text()) > 1][' . $n . '])');
$n++;
}
// If main file is xhtml, we also need the javascript code in the body
if ($mainfile == "xhtml") {
$newfilecontent .= $xpath->evaluate('string(/html/body/script[string-length(text()) > 1][1])');
} else { // if not, we need to merge the replaced contents from the .js file with the contents from the .xhtml file
$newfilecontent = $replacedfilecontent . $newfilecontent;
}
$filerecord->filename = $ejsapp->main_file . '.js';
$fs->create_file_from_string($filerecord, $newfilecontent);
$ejsok = true;
}
// Delete unneeded xhtml, html, xml and ejss files
$DB->delete_records_select('files', "filename LIKE '%ml'", array('component' => 'mod_ejsapp',
'filearea' => 'content', 'itemid' => $ejsapp->id));
$DB->delete_records_select('files', "filename LIKE '%ejss'", array('component' => 'mod_ejsapp',
'filearea' => 'content', 'itemid' => $ejsapp->id));
}
return $ejsok;
}
/**
* Checks if a remote lab equipment is alive or not, either directly when it has a public IP or by asking myFrontier.
*
* @param string $host
* @param int $port
* @param boolean|string|int $myFrontierinstance
* @param string $expid
* @param int $timeout
* @return int 0, not alive; 1, alive; 2, not checkable
*
*/
function ping($host, $port, $myFrontierinstance, $expid=null, $timeout=3) {
global $devicesinfo;
$alive = fsockopen($host, $port, $errno, $errstr, $timeout);
if ($alive && $myFrontierinstance !== false) {
($port == 443) ? $protocol = 'https://' : $protocol = 'http://';
$uri = $protocol . $host . '/SARLABV8.0/';
$headers = @get_headers($uri);
if (substr($headers[0], 9, 3) == 200) { // Valid url.
if ($expid != null) {
$expid = explode('@', $expid);
$expid = urlencode($expid[0]);
}
$url = $uri . 'webresources/net/isLive?idExp=' . $expid;
if ($response = file_get_contents($url)) {
if ($response === 'true') {
return 1;
} else {
// Get list of devices in the experience that are not alive and see which ones are down.
$devices = explode("Ping to ", $response);
/**
* Gets a string between an initial and a final string.
*
* @param string $string
* @param string $start
* @param string $end
* @return string
*
*/
function get_string_between($string, $start, $end) {
$string = " ".$string;
$ini = strpos($string, $start);
if ($ini == 0) {
return "";
}
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
foreach ($devices as $device) {
$devicesinfo[]->name = get_string_between($device, ": ", "ping ");
$ip = get_string_between($device, "ping ", "Reply from ");
$devicesinfo[]->ip = $ip;
$url = $uri . 'isAlive?' . $ip;
if ($info = file_get_contents($url)) {
$devicesinfo[]->alive = (mb_strtoupper(trim($info[1])) === mb_strtoupper("true")) ? true : false;
}
}
return 0;
}
} else {
return 2;
}
} else {
return 2;
}
}
return 0;
}
/**
* Creates the list of all experiences in linked myFrontier systems. If a username is provided, it only returns those
* practices accessible by this particular user.
*
* @param array $myFrontierips
* @param string $username
* @param int $ejsappcontext 0 if block remlab_manager, 1 if mod ejsapp
* @return string $listexperiences
* @throws
*
*/
function get_experiences_myfrontier($myFrontierips, $username = "", $ejsappcontext = 0) {
global $USER;
$listexperiences = '';
$context = context_system::instance();
if ($username != "" && !has_capability('ltisource/enlarge:editexp', $context, $USER->id, false)) {
return $listexperiences;
}
if ($ejsappcontext == 0) {
$cap = get_capability_info('ltisource/enlarge:editexp');
} else {
$cap = get_capability_info('ltisource/enlarge:useexp');
}
if (!$cap) {
return $listexperiences;
}
foreach ($myFrontierips as $myFrontierip) {
$lastquotemark = strrpos($myFrontierip, "'");
if ($lastquotemark != 0) {
$lastquotemark++;
}
$ip = substr($myFrontierip, $lastquotemark);
$firstquotemark = strpos($myFrontierip, "'");
if ($firstquotemark !== false) {
$firstquotemark++;
$name = substr($myFrontierip, $firstquotemark, $lastquotemark - 1 - $firstquotemark);
} else {
$name = $ip;
}
if ($ip != '127.0.0.1' && $ip != '') {
if ($fp = fsockopen($ip, '443', $errorcode, $errorstring, 3)) { // IP is alive.
fclose($fp);
$uri = 'https://' . $ip . '/SARLABV8.0/gexlab';
$headers = get_headers($uri);
if (substr($headers[0], 9, 3) == 200) { // Valid file.
if ($xml = simplexml_load_file($uri)) {
$map = $xml->MapExperience;
$listmyFrontierexperiences = $map->Experience; // Get list of experiences.
foreach ($listmyFrontierexperiences as $experience) {
$experimentsettings = $experience->ExperimentSettings;
// Get list of Moodle servers and users who can access the experience.
$listownermoodleusers = $experimentsettings->listOfOwnersExperience;
// Get list of users in this Moodle server who can access the experience
$moodleservers = $listownermoodleusers->ServerMoodle;
if ($moodleservers == null) {
continue;
}
foreach ($moodleservers as $moodleserver) {
// Check whether this Moodle server is registered in the experience.
if ($moodleserver['IdMoodle'] == get_config('mod_ejsapp', 'server_id')) {
if ($username != "") {
// If username is provided, check users permissions both in Moodle and myFrontier.
$ownerusers = $moodleserver->Owner;
foreach ($ownerusers as $owneruser) {
// Check whether the required user has access to the experience.
if($USER->id == $owneruser){
$listexperiences .= $experience['IdExp'] . '@' . $name . ';';
break;
}
}
} else {
// If not, the whole list of myFrontier experiences must be returned, so add it.
$listexperiences .= $experience['IdExp'] . '@' . $name . ';';
}
}
}
}
}
}
}
}
}
$listexperiences = substr($listexperiences, 0, -1);
return $listexperiences;
}
/**
* Creates the list of all myGateway experiences. If a username is provided, it only returns those practices accessible
* by this particular user.
*
* @param string $username
* @param int $ejsappcontext 0 if block remlab_manager, 1 if mod ejsapp
* @param boolean $returnaddress true to return mygateway addresses too
* @return string $listexperiences
* @throws
*
*/
function get_experiences_mygateway($username = "", $ejsappcontext = 0, $returnaddress = false) {
global $USER;
$listexperiences = '';
$context = context_system::instance();
if ($username != "" && !has_capability('ltisource/enlarge:editexp', $context, $USER->id, false)) {
return $listexperiences;
}
if ($ejsappcontext == 0) {
$cap = get_capability_info('ltisource/enlarge:editexp');
} else {
$cap = get_capability_info('ltisource/enlarge:useexp');
}
if (!$cap) {
return $listexperiences;
}
// Ask ENLARGE IRS about the myVirtualFrontier servers linked to this Moodle and the myGateway devices linked to each
// of these myVirtualFrontier servers
$enlargeIRS_IP = 'irs.nebsyst.com';
if ($fp = fsockopen($enlargeIRS_IP, '443', $errorcode, $errorstring, 3)) { // IP is alive.
fclose($fp);
$uri = 'https://' . $enlargeIRS_IP . '/moodle/listmygatewaylinks?siteId=' . get_config('mod_ejsapp', 'server_id');
$headers = get_headers($uri);
$myGatewayDevices = [];
if (substr($headers[0], 9, 3) == 200) { // Valid file.
$myGatewayDevices = simplexml_load_file($uri);
}
}
// Connect to each of the myGateway devices to ask them about the defined experiences
foreach ($myGatewayDevices as $myGatewayDevice) {
if ($fp = fsockopen($myGatewayDevice->address, '443', $errorcode, $errorstring, 3)) { // IP is alive.
fclose($fp);
$uri = 'https://' . $myGatewayDevice->address . '/' . strtolower($myGatewayDevice->name) . '/gexlab';
$headers = get_headers($uri);
if (substr($headers[0], 9, 3) == 200) { // Valid file.
if ($xml = simplexml_load_file($uri)) {
$map = $xml->MapExperience;
$listmyFrontierexperiences = $map->Experience; // Get list of experiences.
foreach ($listmyFrontierexperiences as $experience) {
$experimentsettings = $experience->ExperimentSettings;
// Get list of Moodle servers and users who can access the experience.
$listownermoodleusers = $experimentsettings->listOfOwnersExperience;
// Get list of users in this Moodle server who can access the experience
$moodleservers = $listownermoodleusers->ServerMoodle;
if ($moodleservers == null) {
break;
}
foreach ($moodleservers as $moodleserver) {
// Check whether this Moodle server is registered in the experience.
if ($moodleserver['IdMoodle'] == get_config('mod_ejsapp', 'server_id')) {
if ($username != "") {
// If username is provided, check users permissions both in Moodle and myFrontier.
$ownerusers = $moodleserver->Owner;
foreach ($ownerusers as $owneruser) {
// Check whether the required user has access to the experience.
if($USER->id == $owneruser){
if ($returnaddress) $listexperiences .= $myGatewayDevice->address . ':';
$listexperiences .= $experience['IdExp'] . '@' . $myGatewayDevice->name . ';';
break;
}
}
} else {
// If not, the whole list of myFrontier experiences must be returned, so add it.
if ($returnaddress) $listexperiences .= $myGatewayDevice->address . ':';
$listexperiences .= $experience['IdExp'] . '@' . $myGatewayDevice->name . ';';
}
}
}
}
}
}
}
}
$listexperiences = substr($listexperiences, 0, -1);
return $listexperiences;
}
/**
* Gets the local experiences (defined without ENLARGE) and combines them with those in myFrontier in a unique, ordered list.
*
* @param array $usermyFrontierexperiences
* @param array $allmyFrontierexperiences
* @return array $combinedexperiences
* @throws
*
*/
function combine_experiences($usermyFrontierexperiences, $allmyFrontierexperiences) {
global $DB;
$remlabmanager = $DB->get_records('block', array('name' => 'remlab_manager'));
$remlabmanager = !empty($remlabmanager);
if ($remlabmanager) {
$localexperiences = $DB->get_records('block_remlab_manager_conf');
$combinedexperiences = array();
if ($usermyFrontierexperiences[0] != '') {
$combinedexperiences = $usermyFrontierexperiences;
}
foreach ($localexperiences as $localexperience) {
if (!in_array($localexperience->practiceintro, $allmyFrontierexperiences) &&
!in_array($localexperience->practiceintro, $combinedexperiences)) {
$combinedexperiences[] = $localexperience->practiceintro;
}
}
} else {
$combinedexperiences = $usermyFrontierexperiences;
}
// Order the list alphabetically.
sort($combinedexperiences);
return $combinedexperiences;
}
/**
* Gets the experiences defined without ENLARGE and combines them with those in myFrontier in a unique, ordered list.
*
* @param string $username
* @param int $ejsappcontext 0 if block remlab_manager, 1 if mod ejsapp
* @return array $showableexperiences
* @throws
*
*/
function get_showable_experiences($username = "", $ejsappcontext = 0) {
$myFrontierips = explode(";", get_config('block_remlab_manager', 'myFrontier_IP'));
if (empty($myFrontierips)) {
$myFrontierips = explode(";", get_config('block_remlab_manager', 'myFrontier_IP') . ';');
}
// Get experiences from myFrontier.
$userlistexperiences = ($username != "") ? get_experiences_myfrontier($myFrontierips, $username, $ejsappcontext) : "";
if ($userlistexperiences != '') $userlistexperiences .= ';';
$userlistexperiences .= ($username != "") ? get_experiences_mygateway($username, $ejsappcontext) : "";
$alllistexperiences = get_experiences_myfrontier($myFrontierips, "") . get_experiences_mygateway("");
$usermyFrontierexperiences = ($username != "") ? explode(";", $userlistexperiences) : array("");
$allmyFrontierexperiences = explode(";", $alllistexperiences);
// Also get experiences NOT in myFrontier and add them to the list.
$showableexperiences = combine_experiences($usermyFrontierexperiences, $allmyFrontierexperiences);
if (empty($showableexperiences[0])) {
array_shift($showableexperiences);
}
return $showableexperiences;
}
/**
* Creates a default practice record for the block_remlab_manager_conf table
*
* @param string $practice
* @param string $username
* @return stdClass $defaultconf
* @throws
*
*/
function default_rem_lab_conf($practice, $username = "") {
// Get experiences from myFrontier and check whether this practice is in a myFrontier server or not.
$myFrontierinstance = is_practice_in_enlarge($practice, $username, 1);
$defaultconf = new stdClass();
if ($myFrontierinstance !== false) { // Practice is defined in a myFrontier server
$myFrontierips = explode(";", get_config('block_remlab_manager', 'myFrontier_IP'));
if (empty($myFrontierips)) {
$myFrontierips = explode(";", get_config('block_remlab_manager', 'myFrontier_IP') . ';');
}
$initchar = strrpos($myFrontierips[intval($myFrontierinstance)], "'");
if ($initchar != 0) {
$initchar++;
}
$ip = substr($myFrontierips[intval($myFrontierinstance)], $initchar);
$defaultconf->ip = $ip;
} else {
$arr = explode("@", $practice, 2);
$practice = $arr[0];
$defaultconf->ip = '127.0.0.1';
}
$defaultconf->port = 443;
$defaultconf->practiceintro = $practice;
$defaultconf->slotsduration = 1;
$defaultconf->totalslots = 18;
$defaultconf->weeklyslots = 9;
$defaultconf->dailyslots = 3;
$defaultconf->reboottime = 2;
$defaultconf->active = 1;
$defaultconf->free_access = 0;
return $defaultconf;
}
/**
* Checks whether a user-accessible practice is defined in an operative myFrontier server or myGateway device and
* returns the myFrontier/myVirtualFrontier instance if it is, or false if it is not.
*
* @param string $practice
* @param string $username
* @param int $ejsappcontext 0 if block remlab_manager, 1 if mod ejsapp
* @return false|int $myFrontierinstance
* @throws
*
*/
function is_practice_in_enlarge($practice, $username = "", $ejsappcontext = 0) {
$myFrontierips = explode(";", get_config('block_remlab_manager', 'myFrontier_IP'));
if (empty($myFrontierips)) {
$myFrontierips = explode(";", get_config('block_remlab_manager', 'myFrontier_IP') . ';');
}
$myFrontierinstance = false;
$instance = 0;
foreach ($myFrontierips as $myFrontierip) {
$myFrontieriparray = array($myFrontierip);
$listexperiences = get_experiences_myfrontier($myFrontieriparray, $username, $ejsappcontext);
if ($listexperiences != '') $listexperiences .= ';';
$listexperiences .= get_experiences_mygateway($username, $ejsappcontext);
$enlargeExperiences = explode(";", $listexperiences);
if (in_array($practice, $enlargeExperiences)) {
$myFrontierinstance = $instance;
break;
}
$instance++;
}
return $myFrontierinstance;
}
/**
* Checks whether the lab experience has a local configuration defined, creates a default configuration if not, and
* returns the data.
*
* @param string $practice
* @return stdClass $remlab_conf
* @throws
*
*/
function check_create_remlab_conf($practice) {
global $DB;
if ($DB->record_exists('block_remlab_manager_conf', array('practiceintro' => $practice))) {
$remlab_conf = $DB->get_record('block_remlab_manager_conf', array('practiceintro' => $practice));
} else {
if ($practice != '') {
$remlab_conf = default_rem_lab_conf($practice);
$DB->insert_record('block_remlab_manager_conf', $remlab_conf);
} else {
$remlab_conf = null;
}
}
return $remlab_conf;
}
/**
* Creates the record for the block_remlab_manager_exp2prc table
*
* @param stdClass $ejsapp
* @return void
* @throws
*
*/
function ejsapp_expsyst2pract($ejsapp) {
global $DB;
$exp2pract = new stdClass();
$exp2pract->ejsappid = $ejsapp->id;
$exp2pract->practiceid = 1;
$exp2practlist = $ejsapp->list_practices;
$exp2practlist = explode(';', $exp2practlist);
$exp2pract->practiceintro = $exp2practlist[$ejsapp->practiceintro];
$DB->insert_record('block_remlab_manager_exp2prc', $exp2pract);
}
/**
* Checks whether a the booking system is being used in the course of a particular ejsapp activity or not.
*
* @param stdClass $ejsapp
* @return bool $usingbookings
* @throws
*
*/
function check_booking_system($ejsapp) {
global $DB;
$usingbookings = false;
// Check whether EJSApp Booking System plugins is installed or not.
if ($DB->record_exists('modules', array('name' => 'ejsappbooking'))) {
$module = $DB->get_record('modules', array('name' => 'ejsappbooking'));
// Check whether there is an ejsappbooking instance in the course or not.
if ($DB->record_exists('course_modules', array('course' => $ejsapp->course, 'module' => $module->id))) {
// Check whether the booking system resource is visible (in use) or not.
if ($DB->get_field('course_modules', 'visible', array('course' => $ejsapp->course,
'module' => $module->id))) {
$usingbookings = true;
}
}
}
return $usingbookings;
}
/**
* Checks if there is an active booking made by the current user for the remote lab and gets information needed by
* myFrontier
*
* @param object $DB
* @param object $USER
* @param stdClass $ejsapp
* @param string $currenttime
* @param boolean|string|int $myFrontierinstance
* @param int $labmanager
* @param int $maxusetime
* param int $maxusetime
* @return stdClass $remlabinfo
*
*/
function check_users_booking($DB, $USER, $ejsapp, $currenttime, $myFrontierinstance, $labmanager, $maxusetime) {
$remlabinfo = null;
if ($DB->record_exists('ejsappbooking_remlab_access', array('username' => $USER->username, 'ejsappid' => $ejsapp->id,
'valid' => 1))) {
$bookings = $DB->get_records('ejsappbooking_remlab_access', array('username' => $USER->username,
'ejsappid' => $ejsapp->id, 'valid' => 1));
$currenttime = strtotime($currenttime) + 60; // User must still have at least one minute of lab time ahead.
$currenttime = date('Y-m-d H:i:s', $currenttime);
foreach ($bookings as $booking) { // If the user has an active booking, use that info.
if ($currenttime >= $booking->starttime && $currenttime < $booking->endtime) {
$expsyst2pract = $DB->get_record('block_remlab_manager_exp2prc', array('ejsappid' => $ejsapp->id,
'practiceid' => $booking->practiceid));
$practice = $expsyst2pract->practiceintro;
$remlabinfo = define_remlab($myFrontierinstance, 0, $practice, $labmanager, $maxusetime);
break;
}
}
}
return $remlabinfo;
}
/**
* Checks if there is an active booking made by the current user and if there is, it gets the ending time of the farest
* consecutive booking.
*
* @param object $DB
* @param string $username
* @param int $ejsappid
* @return string $endtime
*
*/
function check_last_valid_booking($DB, $username, $ejsappid) {
$endtime = 0;
$currenttime = date('Y-m-d H:i:s');
if ($DB->record_exists('ejsappbooking_remlab_access', array('username' => $username, 'ejsappid' => $ejsappid,
'valid' => 1))) {
$bookings = $DB->get_records('ejsappbooking_remlab_access', array('username' => $username,
'ejsappid' => $ejsappid, 'valid' => 1));
/**
* Substracts a time from another.
*
* @param object $a
* @param object $b
* @return int|false
*
*/
if (!function_exists('cmp')) {
function cmp($a, $b) {
return strtotime($a->starttime) - strtotime($b->starttime);
}
}
usort($bookings, "cmp");
foreach ($bookings as $booking) { // If the user has an active booking, check the time.
if ($currenttime >= $booking->starttime && $currenttime < $booking->endtime) {
$endtime = $booking->endtime;
$currenttime = strtotime($endtime) + 1;
$currenttime = date("Y-m-d H:i:s", $currenttime);
}
}
}
return $endtime;
}
/**
* Checks if there is an active booking made by any user and if it is, it returns the username.
*
* @param object $DB
* @param stdClass $ejsapp
* @return string $username
*
*/
function check_anyones_booking($DB, $ejsapp) {
$username = '';
if ($DB->record_exists('ejsappbooking_remlab_access', array('ejsappid' => $ejsapp->id, 'valid' => 1))) {
$bookings = $DB->get_records('ejsappbooking_remlab_access', array('ejsappid' => $ejsapp->id, 'valid' => 1));
$currenttime = date('Y-m-d H:i:s');
foreach ($bookings as $booking) { // If the user has an active booking, use that info.
if ($currenttime >= $booking->starttime && $currenttime < $booking->endtime) {
$username = $booking->username;
}
}
}
return $username;
}
/**
* Gets information about the first and last access a user made to the remote lab and the allowed maximum time of use.
*
* @param array $repeatedlabs
* @param stdClass $ejsapp
* @return stdClass $timeinfo
* @throws
*
*/
function remote_lab_use_time_info($repeatedlabs, $ejsapp) {
global $DB;
$userwithbooking = check_anyones_booking($DB, $ejsapp);
$repeatedlab = reset($repeatedlabs);
$practiceintro = $DB->get_field('block_remlab_manager_exp2prc', 'practiceintro',
array('ejsappid' => $repeatedlab->id));
$currenttime = time();
if ($userwithbooking !== '') {
$maxusetime = strtotime(check_last_valid_booking($DB, $userwithbooking, $ejsapp->id)) - $currenttime;
} else {
$slotsdurationconf = $DB->get_field('block_remlab_manager_conf',
'slotsduration', array('practiceintro' => $practiceintro));
if ($slotsdurationconf > 4) {
$slotsdurationconf = 0;
}
$maxslots = $DB->get_field('block_remlab_manager_conf',
'dailyslots', array('practiceintro' => $practiceintro));
$slotsduration = array(60, 30, 15, 5, 2);
$maxusetime = $maxslots * 60 * $slotsduration[$slotsdurationconf];
}
$ids = array();
$names = array();
foreach ($repeatedlabs as $repeatedlab) {
$ids[] = $repeatedlab->id;
$names[] = $repeatedlab->name;
}
$dbman = $DB->get_manager();
$standardlog = $dbman->table_exists('logstore_standard_log');
// Retrieve information from Moodle's or ejsapp's logging table.
// TODO: Change queries when we stop resetting time when a user in a remote lab refreshes the page.
if ($standardlog) {
$select = 'component = :component AND action = :action AND timecreated > :timecreated AND objectid ';