-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
3411 lines (3029 loc) · 120 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 X5Moodle (X5GON Activity plugin for Moodle)
// 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 <https://www.gnu.org/licenses/>.
/**
* X5GON Europeen project: AI based Recommendation System for Open Educative Resources
* @package mod_xfgon
* @copyright 2018-2020 X5GON-Univ_Nantes_Team (https://chaireunescorel.ls2n.fr/, https://www.univ-nantes.fr/)
* @license BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
*/
/** Make sure this isn't being directly accessed */
defined('MOODLE_INTERNAL') || die();
/** Include the files that are required by this module */
require_once($CFG->dirroot.'/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/xfgon/lib.php');
require_once($CFG->libdir . '/filelib.php');
/** This page */
define('xfgon_THISPAGE', 0);
/** Next page -> any page not seen before */
define("xfgon_UNSEENPAGE", 1);
/** Next page -> any page not answered correctly */
define("xfgon_UNANSWEREDPAGE", 2);
/** Jump to Next Page */
define("xfgon_NEXTPAGE", -1);
/** End of Lesson */
define("xfgon_EOL", -9);
/** Jump to an unseen page within a branch and end of branch or end of xfgon */
define("xfgon_UNSEENBRANCHPAGE", -50);
/** Jump to Previous Page */
define("xfgon_PREVIOUSPAGE", -40);
/** Jump to a random page within a branch and end of branch or end of xfgon */
define("xfgon_RANDOMPAGE", -60);
/** Jump to a random Branch */
define("xfgon_RANDOMBRANCH", -70);
/** Cluster Jump */
define("xfgon_CLUSTERJUMP", -80);
/** Undefined */
define("xfgon_UNDEFINED", -99);
/** xfgon_MAX_EVENT_LENGTH = 432000 ; 5 days maximum */
define("xfgon_MAX_EVENT_LENGTH", "432000");
/** Answer format is HTML */
define("xfgon_ANSWER_HTML", "HTML");
//////////////////////////////////////////////////////////////////////////////////////
/// Any other xfgon functions go here. Each of them must have a name that
/// starts with xfgon_
/**
* Calculates a user's grade for a xfgon.
*
* @param object $xfgon The xfgon that the user is taking.
* @param int $retries The attempt number.
* @param int $userid Id of the user (optional, default current user).
* @return object { nquestions => number of questions answered
attempts => number of question attempts
total => max points possible
earned => points earned by student
grade => calculated percentage grade
nmanual => number of manually graded questions
manualpoints => point value for manually graded questions }
*/
function xfgon_grade($xfgon, $ntries, $userid = 0) {
global $USER, $DB;
if (empty($userid)) {
$userid = $USER->id;
}
// Zero out everything
$ncorrect = 0;
$nviewed = 0;
$score = 0;
$nmanual = 0;
$manualpoints = 0;
$thegrade = 0;
$nquestions = 0;
$total = 0;
$earned = 0;
$params = array ("xfgonid" => $xfgon->id, "userid" => $userid, "retry" => $ntries);
if ($total) { // not zero
$thegrade = round(100 * $earned / $total, 5);
}
// Build the grade information object
$gradeinfo = new stdClass;
$gradeinfo->nquestions = $nquestions;
$gradeinfo->attempts = $nviewed;
$gradeinfo->total = $total;
$gradeinfo->earned = $earned;
$gradeinfo->grade = $thegrade;
$gradeinfo->nmanual = $nmanual;
$gradeinfo->manualpoints = $manualpoints;
return $gradeinfo;
}
/**
* Determines if a user can view the left menu. The determining factor
* is whether a user has a grade greater than or equal to the xfgon setting
* of displayleftif
*
* @param object $xfgon Lesson object of the current xfgon
* @return boolean 0 if the user cannot see, or $xfgon->displayleft to keep displayleft unchanged
**/
function xfgon_displayleftif($xfgon) {
global $CFG, $USER, $DB;
if (!empty($xfgon->displayleftif)) {
// get the current user's max grade for this xfgon
$params = array ("userid" => $USER->id, "xfgonid" => $xfgon->id);
}
// if we get to here, keep the original state of displayleft xfgon setting
return $xfgon->displayleft;
}
/**
*
* @param $cm
* @param $xfgon
* @param $page
* @return unknown_type
*/
function xfgon_add_fake_blocks($page, $cm, $xfgon, $timer = null) {
$bc = xfgon_menu_block_contents($cm->id, $xfgon);
if (!empty($bc)) {
$regions = $page->blocks->get_regions();
$firstregion = reset($regions);
$page->blocks->add_fake_block($bc, $firstregion);
}
$bc = xfgon_mediafile_block_contents($cm->id, $xfgon);
if (!empty($bc)) {
$page->blocks->add_fake_block($bc, $page->blocks->get_default_region());
}
if (!empty($timer)) {
$bc = xfgon_clock_block_contents($cm->id, $xfgon, $timer, $page);
if (!empty($bc)) {
$page->blocks->add_fake_block($bc, $page->blocks->get_default_region());
}
}
}
/**
* If there is a media file associated with this
* xfgon, return a block_contents that displays it.
*
* @param int $cmid Course Module ID for this xfgon
* @param object $xfgon Full xfgon record object
* @return block_contents
**/
function xfgon_mediafile_block_contents($cmid, $xfgon) {
global $OUTPUT;
if (empty($xfgon->mediafile)) {
return null;
}
$options = array();
$options['menubar'] = 0;
$options['location'] = 0;
$options['left'] = 5;
$options['top'] = 5;
$options['scrollbars'] = 1;
$options['resizable'] = 1;
$options['width'] = $xfgon->mediawidth;
$options['height'] = $xfgon->mediaheight;
$link = new moodle_url('/mod/xfgon/mediafile.php?id='.$cmid);
$action = new popup_action('click', $link, 'xfgonmediafile', $options);
$content = $OUTPUT->action_link($link, get_string('mediafilepopup', 'xfgon'), $action, array('title'=>get_string('mediafilepopup', 'xfgon')));
$bc = new block_contents();
$bc->title = get_string('linkedmedia', 'xfgon');
$bc->attributes['class'] = 'mediafile block';
$bc->content = $content;
return $bc;
}
/**
* If a timed xfgon and not a teacher, then
* return a block_contents containing the clock.
*
* @param int $cmid Course Module ID for this xfgon
* @param object $xfgon Full xfgon record object
* @param object $timer Full timer record object
* @return block_contents
**/
function xfgon_clock_block_contents($cmid, $xfgon, $timer, $page) {
// Display for timed xfgons and for students only
$context = context_module::instance($cmid);
if ($xfgon->timelimit == 0 || has_capability('mod/xfgon:manage', $context)) {
return null;
}
$content = '<div id="xfgon-timer">';
$content .= $xfgon->time_remaining($timer->starttime);
$content .= '</div>';
$clocksettings = array('starttime' => $timer->starttime, 'servertime' => time(), 'testlength' => $xfgon->timelimit);
$page->requires->data_for_js('clocksettings', $clocksettings, true);
$page->requires->strings_for_js(array('timeisup'), 'xfgon');
$page->requires->js('/mod/xfgon/timer.js');
$page->requires->js_init_call('show_clock');
$bc = new block_contents();
$bc->title = get_string('timeremaining', 'xfgon');
$bc->attributes['class'] = 'clock block';
$bc->content = $content;
return $bc;
}
/**
* If left menu is turned on, then this will
* print the menu in a block
*
* @param int $cmid Course Module ID for this xfgon
* @param xfgon $xfgon Full xfgon record object
* @return void
**/
function xfgon_menu_block_contents($cmid, $xfgon) {
global $CFG, $DB;
if (!$xfgon->displayleft) {
return null;
}
$pages = $xfgon->load_all_pages();
foreach ($pages as $page) {
if ((int)$page->prevpageid === 0) {
$pageid = $page->id;
break;
}
}
$currentpageid = optional_param('pageid', $pageid, PARAM_INT);
if (!$pageid || !$pages) {
return null;
}
$content = '<a href="#maincontent" class="accesshide">' .
get_string('skip', 'xfgon') .
"</a>\n<div class=\"menuwrapper\">\n<ul>\n";
while ($pageid != 0) {
$page = $pages[$pageid];
// Only process branch tables with display turned on
if ($page->displayinmenublock && $page->display) {
if ($page->id == $currentpageid) {
$content .= '<li class="selected">'.format_string($page->title,true)."</li>\n";
} else {
$content .= "<li class=\"notselected\"><a href=\"$CFG->wwwroot/mod/xfgon/view.php?id=$cmid&pageid=$page->id\">".format_string($page->title,true)."</a></li>\n";
}
}
$pageid = $page->nextpageid;
}
$content .= "</ul>\n</div>\n";
$bc = new block_contents();
$bc->title = get_string('xfgonmenu', 'xfgon');
$bc->attributes['class'] = 'menu block';
$bc->content = $content;
return $bc;
}
/**
* Adds header buttons to the page for the xfgon
*
* @param object $cm
* @param object $context
* @param bool $extraeditbuttons
* @param int $xfgonpageid
*/
function xfgon_add_header_buttons($cm, $context, $extraeditbuttons=false, $xfgonpageid=null) {
global $CFG, $PAGE, $OUTPUT;
if (has_capability('mod/xfgon:edit', $context) && $extraeditbuttons) {
if ($xfgonpageid === null) {
print_error('invalidpageid', 'xfgon');
}
if (!empty($xfgonpageid) && $xfgonpageid != xfgon_EOL) {
$url = new moodle_url('/mod/xfgon/editpage.php', array(
'id' => $cm->id,
'pageid' => $xfgonpageid,
'edit' => 1,
'returnto' => $PAGE->url->out(false)
));
$PAGE->set_button($OUTPUT->single_button($url, get_string('editpagecontent', 'xfgon')));
}
}
}
/**
* This is a function used to detect media types and generate html code.
*
* @global object $CFG
* @global object $PAGE
* @param object $xfgon
* @param object $context
* @return string $code the html code of media
*/
function xfgon_get_media_html($xfgon, $context) {
global $CFG, $PAGE, $OUTPUT;
require_once("$CFG->libdir/resourcelib.php");
// get the media file link
if (strpos($xfgon->mediafile, '://') !== false) {
$url = new moodle_url($xfgon->mediafile);
} else {
// the timemodified is used to prevent caching problems, instead of '/' we should better read from files table and use sortorder
$url = moodle_url::make_pluginfile_url($context->id, 'mod_xfgon', 'mediafile', $xfgon->timemodified, '/', ltrim($xfgon->mediafile, '/'));
}
$title = $xfgon->mediafile;
$clicktoopen = html_writer::link($url, get_string('download'));
$mimetype = resourcelib_guess_url_mimetype($url);
$extension = resourcelib_get_extension($url->out(false));
$mediamanager = core_media_manager::instance($PAGE);
$embedoptions = array(
core_media_manager::OPTION_TRUSTED => true,
core_media_manager::OPTION_BLOCK => true
);
// find the correct type and print it out
if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
$code = resourcelib_embed_image($url, $title);
} else if ($mediamanager->can_embed_url($url, $embedoptions)) {
// Media (audio/video) file.
$code = $mediamanager->embed_url($url, $title, 0, 0, $embedoptions);
} else {
// anything else - just try object tag enlarged as much as possible
$code = resourcelib_embed_general($url, $title, $clicktoopen, $mimetype);
}
return $code;
}
/**
* Logic to happen when a/some group(s) has/have been deleted in a course.
*
* @param int $courseid The course ID.
* @param int $groupid The group id if it is known
* @return void
*/
function xfgon_process_group_deleted_in_course($courseid, $groupid = null) {
global $DB;
$params = array('courseid' => $courseid);
}
/**
* Return user's deadline for all xfgons in a course, hereby taking into account group and user overrides.
*
* @param int $courseid the course id.
* @return object An object with of all xfgonsids and close unixdates in this course,
* taking into account the most lenient overrides, if existing and 0 if no close date is set.
*/
function xfgon_get_user_deadline($courseid) {
global $DB, $USER;
// For teacher and manager/admins return xfgon's deadline.
if (has_capability('moodle/course:update', context_course::instance($courseid))) {
$sql = "SELECT xfgon.id, xfgon.deadline AS userdeadline
FROM {xfgon} xfgon
WHERE xfgon.course = :courseid";
$results = $DB->get_records_sql($sql, array('courseid' => $courseid));
return $results;
}
$sql = "SELECT a.id,
COALESCE(v.userclose, v.groupclose, a.deadline, 0) AS userdeadline
FROM (
SELECT xfgon.id as xfgonid,
MAX(leo.deadline) AS userclose, MAX(qgo.deadline) AS groupclose
FROM {xfgon} xfgon
-- LEFT JOIN {xfgon_overrides} leo on xfgon.id = leo.xfgonid AND leo.userid = :userid
-- LEFT JOIN {groups_members} gm ON gm.userid = :useringroupid
-- LEFT JOIN {xfgon_overrides} qgo on xfgon.id = qgo.xfgonid AND qgo.groupid = gm.groupid
WHERE xfgon.course = :courseid
GROUP BY xfgon.id
) v
JOIN {xfgon} a ON a.id = v.xfgonid";
$results = $DB->get_records_sql($sql, array('userid' => $USER->id, 'useringroupid' => $USER->id, 'courseid' => $courseid));
return $results;
}
/**
* Abstract class that page type's MUST inherit from.
*
* This is the abstract class that ALL add page type forms must extend.
* You will notice that all but two of the methods this class contains are final.
* Essentially the only thing that extending classes can do is extend custom_definition.
* OR if it has a special requirement on creation it can extend construction_override
*
* @abstract
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class xfgon_add_page_form_base extends moodleform {
/**
* This is the classic define that is used to identify this pagetype.
* Will be one of xfgon_*
* @var int
*/
public $qtype;
/**
* The simple string that describes the page type e.g. truefalse, multichoice
* @var string
*/
public $qtypestring;
/**
* An array of options used in the htmleditor
* @var array
*/
protected $editoroptions = array();
/**
* True if this is a standard page of false if it does something special.
* Questions are standard pages, branch tables are not
* @var bool
*/
protected $standard = true;
/**
* Answer format supported by question type.
*/
protected $answerformat = '';
/**
* Response format supported by question type.
*/
protected $responseformat = '';
/**
* Each page type can and should override this to add any custom elements to
* the basic form that they want
*/
public function custom_definition() {}
/**
* Returns answer format used by question type.
*/
public function get_answer_format() {
return $this->answerformat;
}
/**
* Returns response format used by question type.
*/
public function get_response_format() {
return $this->responseformat;
}
/**
* Used to determine if this is a standard page or a special page
* @return bool
*/
public final function is_standard() {
return (bool)$this->standard;
}
/**
* Add the required basic elements to the form.
*
* This method adds the basic elements to the form including title and contents
* and then calls custom_definition();
*/
public final function definition() {
$mform = $this->_form;
$editoroptions = $this->_customdata['editoroptions'];
if ($this->qtypestring != 'selectaqtype') {
if ($this->_customdata['edit']) {
$mform->addElement('header', 'qtypeheading', get_string('edit'. $this->qtypestring, 'xfgon'));
} else {
$mform->addElement('header', 'qtypeheading', get_string('add'. $this->qtypestring, 'xfgon'));
}
}
if (!empty($this->_customdata['returnto'])) {
$mform->addElement('hidden', 'returnto', $this->_customdata['returnto']);
$mform->setType('returnto', PARAM_URL);
}
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'pageid');
$mform->setType('pageid', PARAM_INT);
if ($this->standard === true) {
$mform->addElement('hidden', 'qtype');
$mform->setType('qtype', PARAM_INT);
$mform->addElement('text', 'title', get_string('pagetitle', 'xfgon'), array('size'=>70));
$mform->setType('title', PARAM_TEXT);
$mform->addRule('title', get_string('required'), 'required', null, 'client');
$this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$this->_customdata['maxbytes']);
$mform->addElement('editor', 'contents_editor', get_string('pagecontents', 'xfgon'), null, $this->editoroptions);
$mform->setType('contents_editor', PARAM_RAW);
$mform->addRule('contents_editor', get_string('required'), 'required', null, 'client');
}
$this->custom_definition();
if ($this->_customdata['edit'] === true) {
$mform->addElement('hidden', 'edit', 1);
$mform->setType('edit', PARAM_BOOL);
$this->add_action_buttons(get_string('cancel'), get_string('savepage', 'xfgon'));
} else if ($this->qtype === 'questiontype') {
$this->add_action_buttons(get_string('cancel'), get_string('addaquestionpage', 'xfgon'));
} else {
$this->add_action_buttons(get_string('cancel'), get_string('savepage', 'xfgon'));
}
}
/**
* Convenience function: Adds a jumpto select element
*
* @param string $name
* @param string|null $label
* @param int $selected The page to select by default
*/
protected final function add_jumpto($name, $label=null, $selected=xfgon_NEXTPAGE) {
$title = get_string("jump", "xfgon");
if ($label === null) {
$label = $title;
}
if (is_int($name)) {
$name = "jumpto[$name]";
}
$this->_form->addElement('select', $name, $label, $this->_customdata['jumpto']);
$this->_form->setDefault($name, $selected);
$this->_form->addHelpButton($name, 'jumps', 'xfgon');
}
/**
* Convenience function: Adds a score input element
*
* @param string $name
* @param string|null $label
* @param mixed $value The default value
*/
protected final function add_score($name, $label=null, $value=null) {
if ($label === null) {
$label = get_string("score", "xfgon");
}
if (is_int($name)) {
$name = "score[$name]";
}
$this->_form->addElement('text', $name, $label, array('size'=>5));
$this->_form->setType($name, PARAM_INT);
if ($value !== null) {
$this->_form->setDefault($name, $value);
}
$this->_form->addHelpButton($name, 'score', 'xfgon');
// Score is only used for custom scoring. Disable the element when not in use to stop some confusion.
if (!$this->_customdata['xfgon']->custom) {
$this->_form->freeze($name);
}
}
/**
* Convenience function: Adds an answer editor
*
* @param int $count The count of the element to add
* @param string $label, null means default
* @param bool $required
* @param string $format
* @return void
*/
protected final function add_answer($count, $label = null, $required = false, $format= '') {
if ($label === null) {
$label = get_string('answer', 'xfgon');
}
if ($format == xfgon_ANSWER_HTML) {
$this->_form->addElement('editor', 'answer_editor['.$count.']', $label,
array('rows' => '4', 'columns' => '80'),
array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
$this->_form->setType('answer_editor['.$count.']', PARAM_RAW);
$this->_form->setDefault('answer_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
} else {
$this->_form->addElement('text', 'answer_editor['.$count.']', $label,
array('size' => '50', 'maxlength' => '200'));
$this->_form->setType('answer_editor['.$count.']', PARAM_TEXT);
}
if ($required) {
$this->_form->addRule('answer_editor['.$count.']', get_string('required'), 'required', null, 'client');
}
}
/**
* Convenience function: Adds an response editor
*
* @param int $count The count of the element to add
* @param string $label, null means default
* @param bool $required
* @return void
*/
protected final function add_response($count, $label = null, $required = false) {
if ($label === null) {
$label = get_string('response', 'xfgon');
}
$this->_form->addElement('editor', 'response_editor['.$count.']', $label,
array('rows' => '4', 'columns' => '80'),
array('noclean' => true, 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $this->_customdata['maxbytes']));
$this->_form->setType('response_editor['.$count.']', PARAM_RAW);
$this->_form->setDefault('response_editor['.$count.']', array('text' => '', 'format' => FORMAT_HTML));
if ($required) {
$this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
}
}
/**
* A function that gets called upon init of this object by the calling script.
*
* This can be used to process an immediate action if required. Currently it
* is only used in special cases by non-standard page types.
*
* @return bool
*/
public function construction_override($pageid, xfgon $xfgon) {
return true;
}
}
/**
* Class representation of a xfgon
*
* This class is used the interact with, and manage a xfgon once instantiated.
* If you need to fetch a xfgon object you can do so by calling
*
* <code>
* xfgon::load($xfgonid);
* // or
* $xfgonrecord = $DB->get_record('xfgon', $xfgonid);
* $xfgon = new xfgon($xfgonrecord);
* </code>
*
* The class itself extends xfgon_base as all classes within the xfgon module should
*
* These properties are from the database
* @property int $id The id of this xfgon
* @property int $course The ID of the course this xfgon belongs to
* @property string $name The name of this xfgon
* @property int $practice Flag to toggle this as a practice xfgon
* @property int $modattempts Toggle to allow the user to go back and review answers
* @property int $usepassword Toggle the use of a password for entry
* @property string $password The password to require users to enter
* @property int $dependency ID of another xfgon this xfgon is dependent on
* @property string $conditions Conditions of the xfgon dependency
* @property int $grade The maximum grade a user can achieve (%)
* @property int $custom Toggle custom scoring on or off
* @property int $ongoing Toggle display of an ongoing score
* @property int $usemaxgrade How retakes are handled (max=1, mean=0)
* @property int $maxanswers The max number of answers or branches
* @property int $maxattempts The maximum number of attempts a user can record
* @property int $review Toggle use or wrong answer review button
* @property int $nextpagedefault Override the default next page
* @property int $feedback Toggles display of default feedback
* @property int $minquestions Sets a minimum value of pages seen when calculating grades
* @property int $maxpages Maximum number of pages this xfgon can contain
* @property int $retake Flag to allow users to retake a xfgon
* @property int $activitylink Relate this xfgon to another xfgon
* @property string $mediafile File to pop up to or webpage to display
* @property int $mediaheight Sets the height of the media file popup
* @property int $mediawidth Sets the width of the media file popup
* @property int $mediaclose Toggle display of a media close button
* @property int $slideshow Flag for whether branch pages should be shown as slideshows
* @property int $width Width of slideshow
* @property int $height Height of slideshow
* @property string $bgcolor Background colour of slideshow
* @property int $displayleft Display a left menu
* @property int $displayleftif Sets the condition on which the left menu is displayed
* @property int $progressbar Flag to toggle display of a xfgon progress bar
* @property int $available Timestamp of when this xfgon becomes available
* @property int $deadline Timestamp of when this xfgon is no longer available
* @property int $timemodified Timestamp when xfgon was last modified
* @property int $allowofflineattempts Whether to allow the xfgon to be attempted offline in the mobile app
*
* These properties are calculated
* @property int $firstpageid Id of the first page of this xfgon (prevpageid=0)
* @property int $lastpageid Id of the last page of this xfgon (nextpageid=0)
*
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class xfgon extends xfgon_base {
/**
* The id of the first page (where prevpageid = 0) gets set and retrieved by
* {@see get_firstpageid()} by directly calling <code>$xfgon->firstpageid;</code>
* @var int
*/
protected $firstpageid = null;
/**
* The id of the last page (where nextpageid = 0) gets set and retrieved by
* {@see get_lastpageid()} by directly calling <code>$xfgon->lastpageid;</code>
* @var int
*/
protected $lastpageid = null;
/**
* An array used to cache the pages associated with this xfgon after the first
* time they have been loaded.
* A note to developers: If you are going to be working with MORE than one or
* two pages from a xfgon you should probably call {@see $xfgon->load_all_pages()}
* in order to save excess database queries.
* @var array An array of xfgon_page objects
*/
protected $pages = array();
/**
* Flag that gets set to true once all of the pages associated with the xfgon
* have been loaded.
* @var bool
*/
protected $loadedallpages = false;
/**
* Course module object gets set and retrieved by directly calling <code>$xfgon->cm;</code>
* @see get_cm()
* @var stdClass
*/
protected $cm = null;
/**
* Course object gets set and retrieved by directly calling <code>$xfgon->courserecord;</code>
* @see get_courserecord()
* @var stdClass
*/
protected $courserecord = null;
/**
* Context object gets set and retrieved by directly calling <code>$xfgon->context;</code>
* @see get_context()
* @var stdClass
*/
protected $context = null;
/**
* Constructor method
*
* @param object $properties
* @param stdClass $cm course module object
* @param stdClass $course course object
* @since Moodle 3.3
*/
public function __construct($properties, $cm = null, $course = null) {
parent::__construct($properties);
$this->cm = $cm;
$this->courserecord = $course;
}
/**
* Simply generates a xfgon object given an array/object of properties
* Overrides {@see xfgon_base->create()}
* @static
* @param object|array $properties
* @return xfgon
*/
public static function create($properties) {
return new xfgon($properties);
}
/**
* Generates a xfgon object from the database given its id
* @static
* @param int $xfgonid
* @return xfgon
*/
public static function load($xfgonid) {
global $DB;
if (!$xfgon = $DB->get_record('xfgon', array('id' => $xfgonid))) {
print_error('invalidcoursemodule');
}
return new xfgon($xfgon);
}
/**
* Deletes this xfgon from the database
*/
public function delete() {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->dirroot.'/calendar/lib.php');
$cm = get_coursemodule_from_instance('xfgon', $this->properties->id, $this->properties->course);
$context = context_module::instance($cm->id);
$this->delete_all_overrides();
grade_update('mod/xfgon', $this->properties->course, 'mod', 'xfgon', $this->properties->id, 0, null, array('deleted'=>1));
// We must delete the module record after we delete the grade item.
$DB->delete_records("xfgon", array("id"=>$this->properties->id));
if ($events = $DB->get_records('event', array("modulename"=>'xfgon', "instance"=>$this->properties->id))) {
$coursecontext = context_course::instance($cm->course);
foreach($events as $event) {
$event->context = $coursecontext;
$event = calendar_event::load($event);
$event->delete();
}
}
// Delete files associated with this module.
$fs = get_file_storage();
$fs->delete_area_files($context->id);
return true;
}
/**
* Deletes a xfgon override from the database and clears any corresponding calendar events
*
* @param int $overrideid The id of the override being deleted
* @return bool true on success
*/
public function delete_override($overrideid) {
global $CFG, $DB;
require_once($CFG->dirroot . '/calendar/lib.php');
$cm = get_coursemodule_from_instance('xfgon', $this->properties->id, $this->properties->course);
// Delete the events.
$conds = array('modulename' => 'xfgon',
'instance' => $this->properties->id);
$events = $DB->get_records('event', $conds);
foreach ($events as $event) {
$eventold = calendar_event::load($event);
$eventold->delete();
}
// Set the common parameters for one of the events we will be triggering.
$params = array(
'objectid' => $override->id,
'context' => context_module::instance($cm->id),
'other' => array(
'xfgonid' => $override->xfgonid
)
);
// Trigger the override deleted event.
$event->trigger();
return true;
}
/**
* Deletes all xfgon overrides from the database and clears any corresponding calendar events
*/
public function delete_all_overrides() {
global $DB;
}
/**
* Checks user enrollment in the current course.
*
* @param int $userid
* @return null|stdClass user record
*/
public function is_participant($userid) {
return is_enrolled($this->get_context(), $userid, 'mod/xfgon:view', $this->show_only_active_users());
}
/**
* Check is only active users in course should be shown.
*
* @return bool true if only active users should be shown.
*/
public function show_only_active_users() {
return !has_capability('moodle/course:viewsuspendedusers', $this->get_context());
}
/**
* Updates the xfgon properties with override information for a user.
*
* Algorithm: For each xfgon setting, if there is a matching user-specific override,
* then use that otherwise, if there are group-specific overrides, return the most
* lenient combination of them. If neither applies, leave the quiz setting unchanged.
*
* Special case: if there is more than one password that applies to the user, then
* xfgon->extrapasswords will contain an array of strings giving the remaining
* passwords.
*
* @param int $userid The userid.
*/
public function update_effective_access($userid) {
global $DB;
// To be improved...
}
/**
* Fetches messages from the session that may have been set in previous page
* actions.
*
* <code>
* // Do not call this method directly instead use
* $xfgon->messages;
* </code>
*
* @return array
*/
protected function get_messages() {
global $SESSION;
$messages = array();
if (!empty($SESSION->xfgon_messages) && is_array($SESSION->xfgon_messages) && array_key_exists($this->properties->id, $SESSION->xfgon_messages)) {
$messages = $SESSION->xfgon_messages[$this->properties->id];
unset($SESSION->xfgon_messages[$this->properties->id]);
}
return $messages;
}
/**
* Get all of the attempts for the current user.
*
* @param int $retries
* @param bool $correct Optional: only fetch correct attempts
* @param int $pageid Optional: only fetch attempts at the given page
* @param int $userid Optional: defaults to the current user if not set
* @return array|false
*/
public function get_attempts($retries, $correct=false, $pageid=null, $userid=null) {
global $USER, $DB;
// To be improved...
}
/**
* Get a list of content pages (formerly known as branch tables) viewed in the xfgon for the given user during an attempt.
*
* @param int $xfgonattempt the xfgon attempt number (also known as retries)
* @param int $userid the user id to retrieve the data from
* @param string $sort an order to sort the results in (a valid SQL ORDER BY parameter)
* @param string $fields a comma separated list of fields to return
* @return array of pages
* @since Moodle 3.3
*/
public function get_content_pages_viewed($xfgonattempt, $userid = null, $sort = '', $fields = '*') {
global $USER, $DB;
// To be improved...
}
/**
* Returns the first page for the xfgon or false if there isn't one.
*
* This method should be called via the magic method __get();
* <code>
* $firstpage = $xfgon->firstpage;