-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.php
757 lines (649 loc) · 24.9 KB
/
lib.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
<?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: ldelatorre@dia.uned.es
// - Ruben Heradio: rheradio@issi.uned.es
//
// at the Computer Science and Automatic Control, Spanish Open University
// (UNED), Madrid, Spain.
/**
* Library of interface functions and constants for module ejsapp.
*
* All the core Moodle functions, neeeded to allow the module to work integrated in Moodle 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();
/**
* Supported features by EJSApp
*
* @param constant $feature feature to be supported
* @return boolean true if EJSApp supports the feature, false elsewhere
*
*/
function ejsapp_supports($feature) {
switch ($feature) {
case FEATURE_MOD_INTRO:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_CONTROLS_GRADE_VISIBILITY:
return true;
default:
return null;
}
}
/**
* Given an object containing all the necessary data, (defined by the form in mod_form.php) this function will create a
* new instance and return the id number of the new instance.
*
* @param object $ejsapp An object from the form in mod_form.php
* @param object $mform
* @return int The id of the newly inserted ejsapp record
* @throws
*
*/
function ejsapp_add_instance($ejsapp, $mform = null) {
global $DB;
require_once('locallib.php');
$ejsapp->timecreated = time();
$ejsapp->id = $DB->insert_record('ejsapp', $ejsapp);
if ($mform) {
$ejsapp->appwording = $ejsapp->ejsappwording['text'];
$ejsapp->appwordingformat = $ejsapp->ejsappwording['format'];
}
$cmid = $ejsapp->coursemodule;
$context = context_module::instance($cmid);
$ejsok = update_ejsapp_files_and_tables($ejsapp, $context);
if ($ejsok) {
ejsapp_grade_item_update($ejsapp);
if ($ejsapp->is_rem_lab == 1) { // Remote lab.
if ($ejsapp->remlab_manager) {
$practiceslist = explode(';', $ejsapp->list_practices);
check_create_remlab_conf($practiceslist[$ejsapp->practiceintro]);
}
ejsapp_expsyst2pract($ejsapp);
}
}
return $ejsapp->id;
}
/**
* Given an object containing all the necessary data, (defined by the form in mod_form.php) this function will update
* an existing instance with new data.
*
* @param object $ejsapp An object from the form in mod_form.php
* @param object $mform
* @return boolean Success/Fail
* @throws
*
*/
function ejsapp_update_instance($ejsapp, $mform=null) {
global $DB;
require_once('locallib.php');
$ejsapp->timemodified = time();
$ejsapp->id = $ejsapp->instance;
if ($mform) {
$ejsapp->appwording = $ejsapp->ejsappwording['text'];
$ejsapp->appwordingformat = $ejsapp->ejsappwording['format'];
}
$moduleid = $DB->get_field('modules', 'id', array('name' => 'ejsapp'));
$cmid = $DB->get_field('course_modules', 'id', array('course' => $ejsapp->course,
'module' => $moduleid, 'instance' => $ejsapp->id));
$context = context_module::instance($cmid);
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_ejsapp', 'compressed', $ejsapp->id);
$fs->delete_area_files($context->id, 'mod_ejsapp', 'content', $ejsapp->id);
$ejsok = update_ejsapp_files_and_tables($ejsapp, $context);
if ($ejsok) {
ejsapp_grade_item_update($ejsapp);
if ($ejsapp->is_rem_lab == 1) { // Remote lab.
if ($ejsapp->remlab_manager) {
$practiceslist = explode(';', $ejsapp->list_practices);
check_create_remlab_conf($practiceslist[$ejsapp->practiceintro]);
$DB->delete_records('block_remlab_manager_exp2prc', array('ejsappid' => $ejsapp->id));
ejsapp_expsyst2pract($ejsapp);
}
} else {
if ($ejsapp->remlab_manager) {
$DB->delete_records('block_remlab_manager_exp2prc', array('ejsappid' => $ejsapp->id));
}
$bookinginstalled = $DB->get_records('modules', array('name' => 'ejsappbooking'));
if (!empty($bookinginstalled)) {
if ($DB->record_exists('ejsappbooking', array('course' => $ejsapp->course))) {
$DB->delete_records('ejsappbooking_remlab_access', array('ejsappid' => $ejsapp->id));
}
}
}
}
return $ejsapp->id;
}
/**
* Given an ID of an instance of this module, this function will permanently delete the instance and any data that
* depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
* @throws
*
*/
function ejsapp_delete_instance($id) {
global $DB, $CFG;
require_once($CFG->libdir . '/filelib.php');
if (!$ejsapp = $DB->get_record('ejsapp', array('id' => $id))) {
return false;
}
$DB->delete_records('ejsapp', array('id' => $id));
if ($ejsapp->is_rem_lab == 1) {
if ($DB->record_exists('block_remlab_manager_exp2prc', array('ejsappid' => $id))) {
$DB->delete_records('block_remlab_manager_exp2prc', array('ejsappid' => $id));
}
// EJSApp booking system.
if ($DB->record_exists('ejsappbooking', array('course' => $ejsapp->course))) {
$DB->delete_records('ejsappbooking_remlab_access', array('ejsappid' => $id));
}
}
if ($ejsapp->personalvars == 1) {
$DB->delete_records('ejsapp_personal_vars', array('ejsappid' => $id));
}
$moduleid = $DB->get_field('modules', 'id', array('name' => 'ejsapp'));
$cmid = $DB->get_field('course_modules', 'id', array('course' => $ejsapp->course, 'instance' => $id,
'module' => $moduleid));
$context = context_module::instance($cmid);
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_ejsapp', 'compressed', $id);
$fs->delete_area_files($context->id, 'mod_ejsapp', 'content', $id);
$fs->delete_area_files($context->id, 'mod_ejsapp', 'xmlfiles', $id);
$fs->delete_area_files($context->id, 'mod_ejsapp', 'recfiles', $id);
$fs->delete_area_files($context->id, 'mod_ejsapp', 'blkfiles', $id);
ejsapp_grade_item_delete($ejsapp);
return true;
}
// Grading API.
/**
* Create grade item for given $ejsapp
*
* @param object $ejsapp
* @param array|object $grades 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function ejsapp_grade_item_update($ejsapp, $grades = null) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
$params = array('itemname' => $ejsapp->name);
if ($ejsapp->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $ejsapp->grade;
$params['grademin'] = 0;
} else if ($ejsapp->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$ejsapp->grade;
} else {
$params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/ejsapp', $ejsapp->course, 'mod', 'ejsapp', $ejsapp->id, 0, $grades, $params);
}
/**
* Delete grade item for given ejsapp
*
* @category grade
* @param object $ejsapp object
* @return int
*/
function ejsapp_grade_item_delete($ejsapp) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/ejsapp', $ejsapp->course, 'mod', '$ejsapp', $ejsapp->id,
0, null, array('deleted' => 1));
}
// Activity API.
/**
* Return a small object with summary information about what a user has done with a given particular instance of this
* module. Used for user activity reports.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $ejsapp
*
* @return object $result
*
* $return->time = the time they did it
* $return->info = a short text description
* @throws
*
*/
function ejsapp_user_outline($course, $user, $mod, $ejsapp) {
global $DB;
if ($logs = $DB->get_records('logstore_standard_log', array('userid' => $user->id,
'component' => 'mod_ejsapp', 'objectid' => $ejsapp->id), 'time ASC')) {
// Standard logstore
$numviews = count($logs);
$lastlog = array_pop($logs);
$result = new stdClass();
$result->info = get_string('numviews', '', $numviews);
$result->time = $lastlog->time;
return $result;
} else if ($logs = $DB->get_records('log', array('userid' => $user->id, 'module' => 'ejsapp',
'info' => $ejsapp->name), 'time ASC')) {
// Legacy log
$numviews = count($logs);
$lastlog = array_pop($logs);
$result = new stdClass();
$result->info = get_string('numviews', '', $numviews);
$result->time = $lastlog->time;
return $result;
}
return null;
}
/**
* Print a detailed representation of what a user has done with a given particular instance of this module, for user
* activity reports.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $ejsapp
*
* @throws
*
*/
function ejsapp_user_complete($course, $user, $mod, $ejsapp) {
global $DB;
if ($logs = $DB->get_records('logstore_standard_log', array('userid' => $user->id,
'component' => 'mod_ejsapp', 'objectid' => $ejsapp->id), 'time ASC')) {
// Standard logstore
$numviews = count($logs);
$lastlog = array_pop($logs);
$strmostrecently = get_string('mostrecently');
$strnumviews = get_string('numviews', '', $numviews);
echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
} else if ($logs = $DB->get_records('log', array('userid' => $user->id, 'module' => 'ejsapp',
'info' => $ejsapp->name), 'time ASC')) {
// Legacy log
$numviews = count($logs);
$lastlog = array_pop($logs);
$strmostrecently = get_string('mostrecently');
$strnumviews = get_string('numviews', '', $numviews);
echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
} else {
print_string('neverseen', 'ejsapp');
}
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
* @param stdClass $data the data submitted from the reset course.
* @return array status array
* @throws
*
*/
function ejsapp_reset_userdata($data) {
global $DB;
$status = array();
$componentstr = get_string('modulenameplural', 'ejsapp');
$dbman = $DB->get_manager();
$moodlelog = $dbman->table_exists('logstore_standard_log');
if ($moodlelog) {
$DB->delete_records('logstore_standard_log', array('component' => 'mod_ejsapp', 'courseid' => $data->courseid));
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletedlogs', 'ejsapp'), 'error' => false);
} else {
$DB->delete_records('log', array('module' => 'mod_ejsapp', 'course' => $data->courseid));
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletedlegacylogs', 'ejsapp'), 'error' => false);
}
$select = "course = ?";
$ejsapps = $DB->get_records_select('ejsapp', $select, array($data->courseid));
foreach ($ejsapps as $ejsapp) {
$DB->delete_records('ejsapp_personal_vars', array('ejsappid' => $ejsapp->id));
$DB->delete_records('ejsapp_records', array('ejsappid' => $ejsapp->id));
ejsapp_grade_item_delete($ejsapp);
}
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletedrecords', 'ejsapp'), 'error' => false);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletedpersonalvars', 'ejsapp'), 'error' => false);
$status[] = array('component'=>$componentstr, 'item'=>get_string('deletedgrades', 'ejsapp'), 'error' => false);
return $status;
}
/**
* List of view style log actions.
* @return array
*
*/
function ejsapp_get_view_actions() {
return array('view', 'view all');
}
/**
*
* List of update style log actions
* @return array
*
*/
function ejsapp_get_post_actions() {
return array('update', 'add');
}
/**
* Given a course and a time, this module should find recent activity that has occurred in ejsapp activities and print
* it out. Return true if there was output, or false is there was none.
*
* @param object $course
* @param bool $viewfullnames
* @param int $timestart
* @return boolean false
*
*/
function ejsapp_print_recent_activity($course, $viewfullnames, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Prepares the recent activity data.
*
* This callback function is supposed to populate the passed array with custom activity records. These records are then
* rendered into HTML via ejsapp_print_recent_mod_activity().
*
* @param array $activities sequentially indexed array of objects with the 'cmid' property
* @param int $index the index in the $activities to use for the next record
* @param int $timestart append activity since this time
* @param int $courseid the id of the course we produce the report for
* @param int $cmid course module id
* @param int $userid check for a particular user's activity only, defaults to 0 (all users)
* @param int $groupid check for a particular group's activity only, defaults to 0 (all groups)
* @return void adds items into $activities and increases $index
*
*/
function ejsapp_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid = 0, $groupid = 0) {
}
/**
* Prints single activity item prepared by ejsapp_get_recent_mod_activity().
*
* @param object $activity the activity object the forum resides in
* @param int $courseid the id of the course the forum resides in
* @param bool $detail not used, but required for compatibilty with other modules
* @param int $modnames not used, but required for compatibilty with other modules
* @param bool $viewfullnames not used, but required for compatibilty with other modules
* @return void
*
*/
function ejsapp_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
}
/**
* Returns all other caps used in the module
*
* @return array
*
*/
function ejsapp_get_extra_capabilities() {
return array('moodle/role:assign', 'moodle/site:accessallgroups', 'moodle/course:viewhiddenuserfields',
'moodle/site:viewparticipants', 'moodle/course:managegroups', 'moodle/course:enrolreview',
'moodle/user:viewdetails');
}
/**
* Must return an array of users who are participants for a given instance of ejsapp. Must include every user involved
* in the instance, independent of his role (student, teacher, admin...). The returned objects must contain at least
* id property. See other modules as example.
*
* @param int $ejsappid ID of an instance of this module
* @return boolean|array false if no participants, array of objects otherwise
*
*/
function ejsapp_get_participants($ejsappid) {
return false;
}
// Navigation API.
/**
* This function extends the settings navigation block for the site.
*
* It is safe to rely on PAGE here as we will only ever be within the module context when this is called.
*
* @param settings_navigation $settings
* @param navigation_node $ejsappnode
* @return void
* @throws
*
*/
function ejsapp_extend_settings_navigation($settings, $ejsappnode) {
global $PAGE;
// We want to add these new nodes after the Edit settings node, and before the locally assigned roles node.
// Of course, both of those are controlled by capabilities.
$keys = $ejsappnode->get_children_key_list();
$beforekey = null;
$i = array_search('modedit', $keys);
if ($i === false and array_key_exists(0, $keys)) {
$beforekey = $keys[0];
} else if (array_key_exists($i + 1, $keys)) {
$beforekey = $keys[$i + 1];
}
if (has_capability('mod/ejsapp:addinstance', $PAGE->cm->context)) {
$url = new moodle_url('/mod/ejsapp/personalized_vars_values.php',
array('id' => $PAGE->cm->id, 'courseid' => $PAGE->course->id));
$node = navigation_node::create(get_string('personal_vars_button', 'ejsapp'),
$url, navigation_node::TYPE_SETTING, null, 'mod_ejsapp_personal_vars');
$ejsappnode->add_node($node, $beforekey);
$url = new moodle_url('/mod/ejsapp/rewrite_functions.php',
array('id' => $PAGE->cm->id, 'courseid' => $PAGE->course->id));
$node = navigation_node::create(get_string('rewrite_functions_button', 'ejsapp'),
$url, navigation_node::TYPE_SETTING, null, 'mod_ejsapp_rewrite_functions');
$ejsappnode->add_node($node, 'mod_ejsapp_personal_vars');
}
}
// File API.
/**
* Returns the lists of all browsable file areas within the given module context
*
* The file area 'intro' for the activity introduction field is added automatically by
* file_browser::get_file_info_context_module()
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return array of [(string)filearea] => (string)description
*
*/
function ejsapp_get_file_areas($course, $cm, $context) {
return array('compressed' => 'Applets and zip files with the virtual or remote labs',
'content' => 'Uncompressed files from applets and zip files',
'xmlfiles' => 'Text files containing all the information to define the state of a lab',
'recfiles' => 'Text files containing a script recording the interaction of a user with a lab',
'blkfiles' => 'Text files containing a blockly program or a configuration of blocks');
}
/**
* File browsing support for ejsapp file areas
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info instance or null if not found
*
*/
function ejsapp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
return null;
}
/**
* Serves the files from the ejsapp file areas
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the ejsapp's context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return null
* @throws
*
*/
function ejsapp_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, $options=null) {
global $DB;
if ($context->contextlevel != CONTEXT_MODULE && $context->contextlevel != CONTEXT_USER) {
return false;
}
require_login($course, true, $cm);
if ($filearea !== 'private' && $filearea !== 'content' && $filearea !== 'compressed' &&
$filearea !== 'xmlfiles' && $filearea !== 'recfiles' && $filearea !== 'blkfiles') {
return false;
}
$fileid = (int)array_shift($args);
if (!$submissions = $DB->get_records('files', array('contextid' => $context->id, 'itemid' => $fileid,
'filearea' => $filearea))) {
return false;
}
$relativepath = implode('/', $args);
if ($filearea == 'private') {
$fullpath = '/' . $context->id . '/user/' . $filearea . '/' . $fileid . '/' . $relativepath;
} else {
$fullpath = '/' . $context->id . '/mod_ejsapp/' . $filearea . '/' . $fileid . '/' . $relativepath;
}
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
if ($filearea = 'content' || $filearea = 'compressed') {
$lifetime = 0;
} else {
$lifetime = 604800;
}
return send_stored_file($file, $lifetime, 0, $forcedownload, $options);
}
// External API.
/**
* Returns a list of ejsapp instances in a course.
*
* @param int $courseid
* @return array $result
* @throws
*/
function get_ejsapp_instances($courseid=null) {
global $DB, $USER;
$courses = array();
if (!is_null($courseid)) {
$courses[] = $courseid;
} else {
$courserecords = $DB->get_records('course', array() );
foreach ($courserecords as $courserecord) {
$courses[] = $courserecord->id;
}
}
$ejsappinstances = array();
foreach ($courses as $course) {
$context = context_course::instance($course);
if (has_capability('mod/ejsapp:requestinformation', $context, $USER->id, true)) {
$ejsappinstances = array_merge($ejsappinstances, $DB->get_records('ejsapp', array('course' => $course)));
}
}
$result = array_values($ejsappinstances);
return $result;
}
/**
* Returns a list of .xml and .json files saved in a particular ejsapp activity for the current user.
*
* @param int $ejsappid
* @return array $statefiles
* @throws
*/
function get_ejsapp_states($ejsappid) {
global $DB, $USER;
// Get private xml state files.
$allstatefiles = $DB->get_records('files', array('userid' => $USER->id, 'mimetype' => 'application/xml',
'filearea' => 'private', 'component' => 'mod_ejsapp'));
$allstatefiles = array_merge($allstatefiles, $DB->get_records('files', array('userid' => $USER->id,
'mimetype' => 'text/xml', 'filearea' => 'private', 'component' => 'mod_ejsapp')));
// Get initial xml state files.
$allstatefiles = array_merge($allstatefiles, $DB->get_records('files', array('mimetype' => 'application/xml',
'filearea' => 'xmlfiles', 'component' => 'mod_ejsapp')));
$allstatefiles = array_merge($allstatefiles, $DB->get_records('files', array('mimetype' => 'text/xml',
'filearea' => 'xmlfiles', 'component' => 'mod_ejsapp')));
// Get private json state files.
$allstatefiles = array_merge($allstatefiles, $DB->get_records('files', array('userid' => $USER->id,
'mimetype' => 'application/json', 'filearea' => 'private', 'component' => 'mod_ejsapp')));
// Get initial json state files.
$allstatefiles = array_merge($allstatefiles, $DB->get_records('files', array('mimetype' => 'application/json',
'filearea' => 'xmlfiles', 'component' => 'mod_ejsapp')));
// Filter state files by ejsappid.
$source = 'ejsappid='.$ejsappid;
$statefiles = array();
foreach ($allstatefiles as $key => $value) {
if (($value->itemid == $ejsappid) || ($value->source == $source)) {
$stateobject = new stdClass();
$stateobject->state_name = $value->filename;
$stateobject->state_id = $value->contextid . "/" . $value->component . "/" . $value->filearea . "/" .
$value->itemid . "/" . $value->filename;
$statefiles[] = $stateobject;
}
}
return $statefiles;
}
/**
* Returns a list of .rec files saved in a particular ejsapp activity for the current user.
*
* @param int $ejsappid
* @return array $recordingfiles
* @throws
*/
function get_ejsapp_recordings($ejsappid) {
global $DB, $USER;
$recordingfiles = array();
// TODO
return $recordingfiles;
}
/**
* Returns a list of .blk files saved in a particular ejsapp activity for the current user.
*
* @param int $ejsappid
* @return array $experimentfiles
* @throws
*/
function get_ejsapp_blockly_programs($ejsappid) {
global $DB, $USER;
$experimentfiles = array();
// TODO
return $experimentfiles;
}
/**
* Returns the embedding code for an virtual lab EjsS application.
*
* @param int $ejsappid the course object
* @param array|null $datafiles
* @return string $code
* @throws
*/
function draw_ejsapp_instance($ejsappid, $datafiles = null) {
global $DB, $CFG;
if ($DB->record_exists('ejsapp', array('id' => $ejsappid))) {
$ejsapp = $DB->get_record('ejsapp', array('id' => $ejsappid));
require_once($CFG->dirroot . '/mod/ejsapp/generate_embedding_code.php');
$code = generate_embedding_code($ejsapp, null, $datafiles, null, null);
} else {
$code = get_string('ejsapp_error', 'ejsapp');
}
return $code;
}