-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
1269 lines (1058 loc) · 40.3 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 jclic
*
* All the jclic specific functions, needed to implement the module
* logic, should go here. Never include this file from your lib.php!
*
* @package mod
* @subpackage jclic
* @copyright 2011 Departament d'Ensenyament de la Generalitat de Catalunya
* @author Sara Arjona Téllez <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once "$CFG->libdir/filelib.php";
/**
* Standard base class for mod_jclic
*
* @package mod_jclic
* @copyright 2014 Pau Ferrer Ocaña
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class jclic {
/** @var stdClass the jclic record that contains the global settings for this jclic instance */
private $instance;
/** @var stdClass the course this jclic instance belongs to */
private $course;
/** @var context the context of the course module for this jclic instance
* (or just the course if we are creating a new one)
*/
private $context;
/** @var stdClass the course module for this jclic instance */
private $coursemodule;
/**
* Constructor for the base jclic class.
*
* @param mixed $coursemodulecontext context|null the course module context
* (or the course context if the coursemodule has not been
* created yet).
* @param mixed $coursemodule the current course module if it was already loaded,
* otherwise this class will load one from the context as required.
* @param mixed $course the current course if it was already loaded,
* otherwise this class will load one from the context as required.
* @param mixed $instance the current instance
* @throws coding_exception
* @throws dml_exception
*/
public function __construct($coursemodulecontext, $coursemodule = null, $course = null, $instance = null) {
global $DB;
$this->context = $coursemodulecontext;
if ($coursemodule) {
$this->coursemodule = $coursemodule;
} else if ($this->context && $this->context->contextlevel === CONTEXT_MODULE) {
// Get coursemodule instance ID from $instance instead of $coursemodulecontext (actually filled with course data by
// 'jclic_display').
$this->coursemodule = get_coursemodule_from_instance(
'jclic',
$instance ? $instance->id : $this->context->instanceid
);
}
if ($course) {
$this->course = $course;
} else if ($this->coursemodule) {
$params = ['id' => $this->coursemodule->course];
$this->course = $DB->get_record('course', $params);
}
if ($this->coursemodule) {
$params = ['id' => $this->coursemodule->instance];
$this->instance = $DB->get_record('jclic', $params);
}
if ($instance && empty($this->instance)) {
$this->set_instance($instance);
}
}
/**
* Set the submitted form data.
*
* @param stdClass $data The form data (instance)
* @throws coding_exception
* @throws dml_exception
*/
public function set_instance(stdClass $data) {
global $DB;
$this->instance = $data;
// Cache some info.
$this->course = $DB->get_record('course', ['id' => $data->course], '*', MUST_EXIST);
$this->coursemodule = get_coursemodule_from_instance('jclic', $data->id, 0, false, MUST_EXIST);
$this->context = context_module::instance($this->coursemodule->id);
}
/**
* Get the settings for the current instance of this jclic
*
* @throws coding_exception
* @throws dml_exception
* @return stdClass The settings
*/
public function get_instance() {
global $DB;
if ($this->instance) {
return $this->instance;
}
if ($this->get_course_module()) {
$params = ['id' => $this->get_course_module()->instance];
$this->instance = $DB->get_record('jclic', $params, '*', MUST_EXIST);
}
if (!$this->instance) {
throw new coding_exception('Improper use of the jclic class. ' .
'Cannot load the jclic record.');
}
return $this->instance;
}
/**
* Get the current course module.
*
* @throws coding_exception
* @return mixed stdClass|null The course module
*/
public function get_course_module() {
if ($this->coursemodule) {
return $this->coursemodule;
}
if (!$this->context) {
return null;
}
if ($this->context->contextlevel === CONTEXT_MODULE) {
$this->coursemodule = get_coursemodule_from_id(
'jclic',
$this->context->instanceid,
0,
false,
MUST_EXIST
);
return $this->coursemodule;
}
return null;
}
/**
* Add this instance to the database.
*
* @param stdClass $formdata The data submitted from the form
* @throws coding_exception
* @throws dml_exception
* @return mixed false if an error occurs or the int id of the new instance
*/
public function add_instance(stdClass $formdata) {
global $DB;
// Add the database record.
$update = new stdClass();
$update->name = $formdata->name;
$update->timemodified = time();
$update->timecreated = $update->timemodified;
$update->course = $formdata->course;
$update->intro = $formdata->intro;
$update->introformat = $formdata->introformat;
if (isset($formdata->filetype) && $formdata->filetype === JCLIC_FILE_TYPE_LOCAL) {
$update->url = $formdata->jclicfile;
} else {
$formdata->filetype = JCLIC_FILE_TYPE_EXTERNAL;
$update->url = $formdata->url;
}
$update->skin = empty($formdata->skin) ? 'default' : $formdata->skin;
$update->avaluation = $formdata->avaluation;
$update->grade = $formdata->grade;
if ($update->grade >= 0) {
$update->maxgrade = $update->grade;
}
$update->lang = $formdata->lang;
$update->exiturl = $formdata->exiturl;
$update->timeavailable = $formdata->timeavailable;
$update->timedue = $formdata->timedue;
if (!empty($formdata->maxattempts)) {
$update->maxattempts = $formdata->maxattempts;
}
// Store the JClic and verify.
$returnid = $DB->insert_record('jclic', $update);
// We need to use context now, so we need to make sure all needed info is already in db.
$DB->set_field('course_modules', 'instance', $returnid, ['id' => $formdata->coursemodule]);
$instance = $DB->get_record('jclic', ['id' => $returnid], '*', MUST_EXIST);
$this->set_instance($instance);
if ($formdata->filetype === JCLIC_FILE_TYPE_LOCAL) {
$update = new StdClass();
$update->id = $returnid;
$update->url = $this->set_mainfile();
$DB->update_record('jclic', $update);
}
// In the case of upgrades the coursemodule has not been set,
// so we need to wait before calling these two.
$this->update_file($formdata->filetype);
$this->update_calendar($formdata->coursemodule);
$this->update_gradebook(false, $formdata->coursemodule);
return $returnid;
}
/**
* Update this instance in the database.
*
* @param stdClass $formdata - the data submitted from the form
* @throws coding_exception
* @throws dml_exception
* @return bool false if an error occurs
*/
public function update_instance($formdata) {
global $DB;
$update = new stdClass();
$update->id = $formdata->instance;
$update->name = $formdata->name;
$update->timemodified = time();
$update->course = $formdata->course;
$update->intro = $formdata->intro;
$update->introformat = $formdata->introformat;
if (isset($formdata->filetype) && $formdata->filetype === JCLIC_FILE_TYPE_LOCAL) {
$update->url = $formdata->jclicfile;
} else {
$formdata->filetype = JCLIC_FILE_TYPE_EXTERNAL;
$update->url = $formdata->url;
}
$update->skin = empty($formdata->skin) ? 'default' : $formdata->skin;
$update->width = empty($formdata->width) ? '800' : $formdata->width;
$update->height = empty($formdata->height) ? '600' : $formdata->height;
$update->avaluation = $formdata->avaluation;
$update->grade = $formdata->grade;
if ($update->grade >= 0) {
$update->maxgrade = $update->grade;
}
$update->lang = $formdata->lang;
$update->exiturl = $formdata->exiturl;
$update->timeavailable = $formdata->timeavailable;
$update->timedue = $formdata->timedue;
if (!empty($formdata->maxattempts)) {
$update->maxattempts = $formdata->maxattempts;
}
// Store the JClic and verify.
$result = $DB->update_record('jclic', $update);
$instance = $DB->get_record('jclic', array('id' => $update->id), '*', MUST_EXIST);
$this->set_instance($instance);
$this->update_file($formdata->filetype, $formdata->jclicfile);
$this->update_calendar($this->coursemodule->id);
$this->update_gradebook(false, $this->coursemodule->id);
return $result;
}
function update_file($filetype) {
global $DB;
// We need to use context now, so we need to make sure all needed info is already in db.
$cmid = $this->coursemodule->id;
$DB->set_field('course_modules', 'instance', $this->instance->id, ['id' => $cmid]);
$context = context_module::instance($cmid);
$fs = get_file_storage();
// Erase content files to force regeneration and package to reset old files.
$fs->delete_area_files($context->id, 'mod_jclic', 'content');
$fs->delete_area_files($context->id, 'mod_jclic', 'package');
if ($filetype === JCLIC_FILE_TYPE_LOCAL) {
$this->save_package();
} else {
$fs->delete_area_files($context->id, 'mod_jclic', $this->get_filearea());
}
// Remove activity type to calculate again it (because file/URL can be different).
$this->get_instance()->type = -1;
if (is_numeric($this->get_instance()->url)) {
$this->get_instance()->url = $this->set_mainfile();
}
$DB->update_record('jclic', $this->get_instance());
}
/**
* Delete this instance from the database.
*
* @throws dml_exception
* @return bool false if an error occurs
*/
public function delete_instance() {
global $DB;
$result = true;
$id = $this->instance->id;
// Delete files associated with this jclic.
$fs = get_file_storage();
if (!$fs->delete_area_files($this->context->id)) {
$result = false;
}
// Delete any dependent records.
$rs = $DB->get_records('jclic_sessions', ['jclicid' => $id]);
foreach ($rs as $session) {
$DB->delete_records('jclic_activities', ['session_id' => $session->session_id]);
}
$DB->delete_records('jclic_sessions', ['jclicid' => $id]);
// Delete items from the gradebook.
if (!$this->delete_grades()) {
$result = false;
}
// Delete the instance.
$DB->delete_records('jclic', ['id' => $id]);
return $result;
}
/**
* Delete all grades from the gradebook for this jclic.
*
* @return bool
*/
protected function delete_grades() {
global $CFG;
require_once $CFG->libdir . '/gradelib.php';
$result = grade_update('mod/jclic',
$this->course->id,
'mod',
'jclic',
$this->instance->id,
0,
null,
['deleted' => 1]);
return $result === GRADE_UPDATE_OK;
}
/**
* Update the gradebook information for this jclic.
*
* @param bool $reset If true, will reset all grades in the gradbook for this jclic
* @param int $coursemoduleid This is required because it might not exist in the database yet
* @throws coding_exception
* @return bool
*/
public function update_gradebook($reset, $coursemoduleid) {
global $CFG;
require_once $CFG->dirroot . '/mod/jclic/lib.php';
$jclic = clone $this->instance;
$jclic->cmidnumber = $coursemoduleid;
$param = null;
if ($reset) {
$param = 'reset';
}
return jclic_grade_item_update($jclic, $param);
}
/**
* Update the calendar entries for this jclic.
*
* @param int $coursemoduleid - Required to pass this in because it might
* not exist in the database yet.
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
* @return bool
*/
public function update_calendar($coursemoduleid) {
global $DB, $CFG;
require_once $CFG->dirroot . '/calendar/lib.php';
// Special case for add_instance as the coursemodule has not been set yet.
$instance = $this->instance;
if ($instance->timedue) {
$event = new stdClass();
$params = ['modulename' => 'jclic', 'instance' => $instance->id];
$event->id = $DB->get_field('event', 'id', $params);
$event->name = $instance->name;
$event->timestart = $instance->timedue;
$event->description = format_module_intro('jclic', $instance, $this->coursemodule->id);
if ($event->id) {
$calendarevent = calendar_event::load($event->id);
$calendarevent->update($event);
} else {
unset($event->id);
$event->courseid = $instance->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'jclic';
$event->instance = $instance->id;
$event->eventtype = 'due';
$event->timeduration = 0;
calendar_event::create($event);
}
} else {
$DB->delete_records('event', ['modulename' => 'jclic', 'instance' => $instance->id]);
}
}
public function get_filetype() {
if (empty($this->instance->filetype)) {
if (jclic_is_valid_external_url($this->instance->url)) {
$this->instance->filetype = JCLIC_FILE_TYPE_EXTERNAL;
} else {
$this->instance->filetype = JCLIC_FILE_TYPE_LOCAL;
}
}
return $this->instance->filetype;
}
public function set_mainfile() {
$fs = get_file_storage();
$fileid = $this->instance->url;
if ($fileid) {
file_save_draft_area_files(
$fileid,
$this->context->id,
'mod_jclic',
$this->get_filearea(),
0,
self::get_filemanager_options()
);
}
$files = $fs->get_area_files(
$this->context->id,
'mod_jclic',
$this->get_filearea(),
0,
'sortorder',
false
);
if (count($files) === 1) {
// Only one file attached, set it as main file automatically.
$file = reset($files);
file_set_sortorder(
$this->context->id,
'mod_jclic',
$this->get_filearea(),
0,
$file->get_filepath(),
$file->get_filename(),
1
);
return $file->get_filename();
}
return null;
}
function save_package() {
$fs = get_file_storage();
$cmid = $this->coursemodule->id;
$draftitemid = $this->instance->url;
$context = context_module::instance($cmid);
if ($draftitemid) {
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_jclic',
$this->get_filearea(),
0,
jclic::get_filemanager_options()
);
}
if ($this->get_filearea() === 'package') {
$this->extract_package(true);
}
}
/**
* Extracts JClic package, sets up all variables.
*
* @param bool $force_extract force full update if true
* @throws coding_exception
* @return false
*/
function extract_package($force_extract = false) {
if ($this->get_filearea() === 'package') {
$fs = get_file_storage();
$cmid = $this->coursemodule->id;
$context = $this->context;
$files = $fs->get_area_files(
$context->id,
'mod_jclic',
'package',
0,
'sortorder',
false
);
if (count($files) === 1) {
// Only one file attached, set it as main file automatically.
$package = reset($files);
file_set_sortorder(
$context->id,
'mod_jclic',
'package',
0,
$package->get_filepath(),
$package->get_filename(),
1
);
if ($force_extract) {
// Now extract files.
$fs->delete_area_files($context->id, 'mod_jclic', 'content');
$packer = get_file_packer('application/zip');
$package->extract_to_storage($packer, $context->id, 'mod_jclic', 'content', 0, '/');
}
}
}
return false;
}
public function is_html5($fs = false) {
global $DB;
if ($this->get_instance()->type === -1) {
// Activity type is not defined so it's necessary to calculate it.
if (str_ends_with(trim($this->get_instance()->url), '.jclic.zip')) {
$this->get_instance()->type = JCLIC_ACTIVITY_TYPE_JAVA;
} else {
$this->get_instance()->type = JCLIC_ACTIVITY_TYPE_HTML5;
}
if ($this->instance->type !== -1) {
// Save value in database (to avoid calculate again).
$DB->update_record('jclic', $this->get_instance());
}
}
return ($this->instance->type === JCLIC_ACTIVITY_TYPE_HTML5);
}
public static function get_filemanager_options() {
return [
'return_types' => 3, // 3 == FILE_EXTERNAL & FILE_INTERNAL. These two constant names are defined in repository/lib.php
'accepted_types' => 'archive',
'maxbytes' => 0,
'subdirs' => 0,
'maxfiles' => 1,
];
}
/**
* Get an array with the languages
*
* @return array The array with each language.
*/
public static function get_languages() {
return get_string_manager()->get_list_of_translations();
}
/**
* Get an array with the skins
*
* @return array The array with each skin.
*/
public static function get_skins() {
return [
'@default.xml' => 'default',
'@blue.xml' => 'blue',
'@orange.xml' => 'orange',
'@green.xml' => 'green',
'@simple.xml' => 'simple',
'@mini.xml' => 'mini',
];
}
/**
* Get an array with the file types
*
* @return array The array with each file type
*/
public static function get_file_types() {
return [
JCLIC_FILE_TYPE_LOCAL => get_string('filetypelocal', 'jclic'),
JCLIC_FILE_TYPE_EXTERNAL => get_string('filetypeexternal', 'jclic'),
];
}
public function get_context() {
return $this->context;
}
/**
* For Java type, filearea is content and for HTML5 is package
*
* @throws coding_exception
* @throws dml_exception
* @return string Filearea name
*/
function get_filearea() {
return jclic_get_filearea($this->get_instance()->url);
}
}
class jclic_package_file_info extends file_info_stored {
public function get_parent() {
if ($this->lf->get_filepath() === '/' && $this->lf->get_filename() === '.') {
return $this->browser->get_file_info($this->context);
}
return parent::get_parent();
}
public function get_visible_name() {
if ($this->lf->get_filepath() === '/' && $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
}
/**
* Display the jclic intro
*
*/
function jclic_view_intro($jclic, $cm) {
global $OUTPUT;
echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
echo format_module_intro('jclic', $jclic, $cm->id);
echo $OUTPUT->box_end();
}
/**
* Display the jclic dates
*
* Prints the jclic start and end dates in a box.
*/
function jclic_view_dates($jclic, $cm) {
global $OUTPUT;
if (!$jclic->timeavailable && !$jclic->timedue) {
return;
}
echo $OUTPUT->box_start('generalbox boxaligncenter jclicdates', 'dates');
if ($jclic->timeavailable) {
echo '<div class="title-time">' . get_string('availabledate', 'jclic') . ': </div>';
echo '<div class="data-time">' . userdate($jclic->timeavailable) . '</div>';
}
if ($jclic->timedue) {
echo '<div class="title-time">' . get_string('duedate', 'jclic') . ': </div>';
echo '<div class="data-time">' . userdate($jclic->timedue) . '</div>';
}
echo $OUTPUT->box_end();
}
/**
* Display the jclic applet
*
*/
function jclic_view_activity($jclic, $context, $ispreview = false) {
global $OUTPUT, $USER;
$timenow = time();
$isopen = (empty($jclic->timeavailable) || $jclic->timeavailable < $timenow);
if (!$isopen) {
echo $OUTPUT->box(
get_string('notopenyet', 'jclic', userdate($jclic->timeavailable)),
'generalbox boxaligncenter jclicdates'
);
if (!$ispreview) {
return;
}
}
$isclosed = (!empty($jclic->timedue) && $jclic->timedue < $timenow);
if ($isclosed) {
echo $OUTPUT->box(
get_string('expired', 'jclic', userdate($jclic->timedue)),
'generalbox boxaligncenter jclicdates'
);
if (!$ispreview) {
return;
}
}
$sessions = jclic_get_sessions($jclic->id, $USER->id);
$attempts = count($sessions);
if (!$ispreview && ($attempts > 0 || $isopen)) {
echo '<br><a href="#" onclick="window.open(\'action/student_results.php?id=' . $context->instanceid . '\',\'JClic\',\'navigation=0,toolbar=0,resizable=1,scrollbars=1,width=900,height=600\');" >' . get_string('show_my_results', 'jclic') . '</a>';
}
$config = get_config('jclic');
if ($jclic->maxattempts < 0 || $attempts < $jclic->maxattempts) {
jclic_display($jclic, $context, $config, $ispreview);
} else {
echo $OUTPUT->box(get_string('msg_noattempts', 'jclic'), 'generalbox boxaligncenter');
}
jclic_view_dates($jclic, $context, $timenow);
}
function jclic_display($jclic_instance, $context, $config, $ispreview) {
$jclic = new jclic($context, null, null, $jclic_instance);
if ($jclic->is_html5()) {
jclic_display_html5($jclic, $context, $config, $ispreview);
} else {
jclic_display_applet($jclic, $context, $config);
}
}
function jclic_display_applet($jclic, $context, $config) {
global $PAGE, $CFG, $USER;
$jclic_instance = $jclic->get_instance();
echo '<div id="jclic_applet" style="text-align:center;padding-top:10px;">';
echo '</div>';
if (!empty($config->pluginjs)) {
echo '<script type="text/javascript" src="' . $config->pluginjs . '"></script>';
} else {
$PAGE->requires->js('/mod/jclic/jclicplugin.js');
}
$PAGE->requires->js('/mod/jclic/jclic.js');
$params = get_object_vars($jclic_instance);
$params['jclic_url'] = jclic_get_url($jclic, $context);
$params['jclic_path'] = jclic_get_server();
$params['jclic_service'] = jclic_get_path() . '/mod/jclic/action/beans.php';
$params['jclic_user'] = $USER->id;
$params['jclic_jarbase'] = $config->jarbase;
$params['jclic_lap'] = $config->lap;
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] === 443
|| str_starts_with($CFG->wwwroot, 'https')) {
$params['jclic_protocol'] = 'https';
} else {
$params['jclic_protocol'] = 'http';
}
$PAGE->requires->js_init_call('M.mod_jclic.initApplet', [$params]);
}
function jclic_display_html5($jclic, $context, $config, $ispreview = false) {
global $PAGE, $CFG, $USER;
$jclic_instance = $jclic->get_instance();
echo '<div id="jclic_html5">';
echo '</div>';
if (!empty($config->jclicjs)) {
echo '<script type="text/javascript" src="' . $config->jclicjs . '"></script>';
} else {
echo '<script type="text/javascript" src="https://clic.xtec.cat/dist/jclic.js/jclic.min.js"></script>';
}
$PAGE->requires->js('/mod/jclic/jclic.js');
$params = get_object_vars($jclic_instance);
$params['jclic_ispreview'] = $ispreview;
$params['jclic_url'] = jclic_get_url($jclic, $context);
$params['jclic_path'] = jclic_get_server();
$params['jclic_service'] = jclic_get_path() . '/mod/jclic/action/beans.php';
$params['jclic_user'] = $USER->id;
$params['jclic_jarbase'] = $config->jarbase;
$params['jclic_lap'] = $config->lap;
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] === 443
|| str_starts_with($CFG->wwwroot, 'https')) {
$params['jclic_protocol'] = 'https';
} else {
$params['jclic_protocol'] = 'http';
}
$PAGE->requires->js_init_call('M.mod_jclic.initHTML5', [$params]);
}
function jclic_download_file_content($jclic, $context) {
$tofile = tempnam(sys_get_temp_dir(), 'jclic_');
$url = jclic_get_url($jclic, $context);
// Prepare the parameters for the download_file_content() function.
$headers = null;
$postdata = null;
$fullresponse = false;
$timeout = 300;
$connecttimeout = 20;
$skipcertverify = true;
$tofile = $tofile;
$calctimeout = false;
download_file_content($url, $headers, $postdata, $fullresponse, $timeout,
$connecttimeout, $skipcertverify, $tofile, $calctimeout);
return $tofile;
}
function jclic_get_url($jclic, $context, $jclicfile = false) {
global $DB;
if ($jclic instanceof jclic) {
$jclic_instance = $jclic->get_instance();
} else {
$jclic_instance = $jclic;
}
if (jclic_is_valid_external_url($jclic_instance->url)) {
$url = $jclic_instance->url;
} else {
$fs = get_file_storage();
if ($jclic->is_html5()) {
// Get project.son file (to avoid to unzip it)
$filename = 'project.json';
if ($rs = $DB->get_record_sql("SELECT filename FROM {files} WHERE component='mod_jclic' AND filearea='content' AND contextid='" . $jclic->get_context()->id . "' AND filename='" . $filename . "'")) {
$file = $fs->get_file(
$jclic->get_context()->id,
'mod_jclic',
'content',
0,
'/',
$rs->filename
);
} else {
// If project.json doesn't exist, get .jclic file (to avoid to unzip it)
$filename = substr($jclic_instance->url, 0, count($jclic_instance->url) - 11) . '.jclic';
if ($rs = $DB->get_record_sql("SELECT filename FROM {files} WHERE component='mod_jclic' AND filearea='content' AND contextid='" . $jclic->get_context()->id . "' AND filename='" . $filename . "'")) {
$file = $fs->get_file(
$jclic->get_context()->id,
'mod_jclic',
'content',
0,
'/',
$rs->filename
);
}
}
} else {
// Get JClic file
$files = $fs->get_area_files(
$context->id,
'mod_jclic',
$jclic->get_filearea(),
0,
'sortorder DESC, id ASC',
false
);
if (count($files) < 1) {
//resource_print_filenotfound($resource, $cm, $course);
die;
}
$file = reset($files);
unset($files);
}
$url = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
);
$url = (string)$url;
}
return $url;
}
/**
* Get moodle server
*
* @return string myserver.com:port
*/
function jclic_get_server() {
global $CFG, $OUTPUT;
if (!empty($CFG->wwwroot)) {
$url = parse_url($CFG->wwwroot);
}
if (!empty($url['host'])) {
$hostname = $url['host'];
} else if (!empty($_SERVER['SERVER_NAME'])) {
$hostname = $_SERVER['SERVER_NAME'];
} else if (!empty($_ENV['SERVER_NAME'])) {
$hostname = $_ENV['SERVER_NAME'];
} else if (!empty($_SERVER['HTTP_HOST'])) {
$hostname = $_SERVER['HTTP_HOST'];
} else if (!empty($_ENV['HTTP_HOST'])) {
$hostname = $_ENV['HTTP_HOST'];
} else {
$OUTPUT->notification('Warning: could not find the name of this server!');
return false;
}
if (!empty($url['port'])) {
$hostname .= ':' . $url['port'];
} else if (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] !== 80 && $_SERVER['SERVER_PORT'] !== 443) {
$hostname .= ':' . $_SERVER['SERVER_PORT'];
}
return $hostname;
}
/**
* Get moodle path
*
* @return string /path_to_moodle
*/
function jclic_get_path() {
global $CFG;
$path = '/';