forked from gbateson/moodle-mod_taskchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
2931 lines (2621 loc) · 115 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/>.
/**
* mod/taskchain/locallib.php
*
* @package mod
* @subpackage taskchain
* @copyright 2010 Gordon Bateson ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
/** Prevent direct access to this script */
defined('MOODLE_INTERNAL') || die();
/** Include required files */
require_once($CFG->dirroot.'/lib/gradelib.php');
require_once($CFG->dirroot.'/mod/taskchain/lib.php');
require_once($CFG->dirroot.'/mod/taskchain/locallib/base.php');
/**
* mod_taskchain
*
* @copyright 2010 Gordon Bateson ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
* @package mod
* @subpackage taskchain
*/
class mod_taskchain extends taskchain_base {
/** @var boolean cache of switch to show if user can start this taskchain */
public $canstart = null;
public $userid = 0; // cache $USER->userid
public $realuserid = 0; // cache $USER->realuser
public $time = 0; // the time this page was displayed
public $inpopup = 0;
public $action = ''; // 'add', 'update', 'delete', 'deleteall', 'deleteconfirmed', 'deletecancelled'
public $tab = ''; // 'info', 'preview'
public $mode = ''; // e.g. report name
public $confirmed = 0;
public $selected = null; // array of ids of selected records
public $course = null;
public $module = null;
public $coursemodule = null;
public $taskchain = null;
public $chain = null;
public $task = null;
public $condition = null;
public $chaingrade = null;
public $chainattempt = null;
public $taskscore = null;
public $taskattempt = null;
public $block = null;
public $usergrade = null;
public $lastchainattempt = null;
public $lasttaskattempt = null;
public $tasks = null;
public $conditions = null;
public $chaingrades = null;
public $chainattempts = null;
public $taskscores = null;
public $taskattempts = null;
public $taskchains = null;
public $chains = null;
public $mycourses = null;
public $mytaskchains = null;
public $pageid = '';
public $pageclass = '';
/** child objects to simulate multiple inheritance */
public $available = null;
public $can = null;
public $create = null;
public $get = null;
public $regrade = null;
public $require = null;
public $url = null;
/** fields to allow forced values for cnumber, tnumber and taskid */
public $forcecnumber = 0;
public $forcetnumber = 0;
public $forcetaskid = 0;
/** settings for allowfreeaccess */
public $maxchainattemptgrade = null;
public $chaincompleted = null;
public $availabletaskid = 0; // id of first available task
public $availabletaskids = null; // ids of all available tasks
public $countavailabletaskids = 0; // number of availabletasks
// array of arrays (by taskid)
public $cache_taskattempts = array();
public $cache_taskattemptsusort = array();
public $cache_preconditions = array();
public $cache_postconditions = array();
public $cache_available_task = array();
public $conditiontype = 0; // CONDITIONTYPE_PRE or CONDITIONTYPE_POST
public $columnlisttype = ''; // "task" or "chain"
public $columnlistid = ''; // two-digit number e.g. '01'
/** maintain statistics on deleted records */
public $deleted = null;
/** properties that should be replaced by methods: $this->get_xxx() */
/* =================================== *\
public $chainid = 0;
public $cnumber = 0;
public $taskid = 0;
public $tnumber = 0;
public $conditionid = 0;
public $chaingradeid = 0;
public $chainattemptid = 0;
public $taskscoreid = 0;
public $taskattemptid = 0;
\* =================================== */
/**
* Constructor function for this class
*/
public function __construct($dbrecord=null) {
global $CFG, $DB, $PAGE, $USER;
parent::__construct();
$this->userid = $USER->id;
if (isset($USER->realuser)) {
$this->realuserid = $USER->realuser;
}
// get $pageid and $pageclass of current Moodle page
list($pageid, $pageclass) = $this->get_pageid_pageclass();
// do TaskChain initialization if this is a TaskChain page
// i.e. don't initalize for backup, restore or upgrade
if ($pageclass=='mod-taskchain' ||
$pageclass=='mod-taskchain-edit' ||
$pageclass=='mod-taskchain-edit-form' ||
$pageclass=='course' || $pageclass=='admin') {
// get input params passed to this page
$course = optional_param('course', 0, PARAM_INT);
$courseid = optional_param('courseid', $course, PARAM_INT);
$coursemoduleid = optional_param('cm', 0, PARAM_INT);
$taskchainid = optional_param('tc', 0, PARAM_INT);
$chainid = optional_param('chainid', 0, PARAM_INT);
$taskid = optional_param('taskid', 0, PARAM_INT);
$conditionid = optional_param('conditionid', 0, PARAM_INT);
$chaingradeid = optional_param('chaingradeid', 0, PARAM_INT);
$chainattemptid = optional_param('chainattemptid', 0, PARAM_INT);
$taskscoreid = optional_param('taskscoreid', 0, PARAM_INT);
$taskattemptid = optional_param('taskattemptid', 0, PARAM_INT);
$blockid = optional_param('blockid', 0, PARAM_INT);
//get main id for this page
$set_page_context = true;
switch ($pageid) {
case 'course-mod':
$coursemoduleid = optional_param('delete', $coursemoduleid, PARAM_INT);
$coursemoduleid = optional_param('duplicate', $coursemoduleid, PARAM_INT);
break;
case 'course-modedit':
$coursemoduleid = optional_param('update', $coursemoduleid, PARAM_INT);
break;
case 'mod-taskchain-edit-columnlists':
case 'mod-taskchain-edit-chains':
case 'mod-taskchain-index':
case 'backup-backup':
$courseid = optional_param('id', $courseid, PARAM_INT);
break;
case 'mod-taskchain-edit-tasks':
case 'mod-taskchain-report':
case 'mod-taskchain-view':
case 'mod-taskchain-attempt':
case 'course-rest':
$coursemoduleid = optional_param('id', $coursemoduleid, PARAM_INT);
break;
case 'mod-taskchain-edit-task':
$taskid = optional_param('id', $taskid, PARAM_INT);
break;
case 'mod-taskchain-submit':
case 'mod-taskchain-review':
$taskattemptid = optional_param('id', $taskattemptid, PARAM_INT);
break;
case 'mod-taskchain-edit-condition':
$conditionid = optional_param('id', $conditionid, PARAM_INT);
break;
case 'mod-taskchain-mod':
$coursemoduleid = optional_param('coursemodule', $coursemoduleid, PARAM_INT);
break;
case 'mod-taskchain-edit-form-helper':
switch (optional_param('type', '', PARAM_ALPHA)) {
case 'chain' : $chainid = optional_param('id', 0, PARAM_INT); break;
case 'chainattempt' : $chainattemptid = optional_param('id', 0, PARAM_INT); break;
case 'chaingrade' : $chaingradeid = optional_param('id', 0, PARAM_INT); break;
case 'cm' : $coursemoduleid = optional_param('id', 0, PARAM_INT); break;
case 'condition' : $conditionid = optional_param('id', 0, PARAM_INT); break;
case 'coursemodule' : $coursemoduleid = optional_param('id', 0, PARAM_INT); break;
case 'task' : $taskid = optional_param('id', 0, PARAM_INT); break;
case 'taskattempt' : $taskattemptid = optional_param('id', 0, PARAM_INT); break;
case 'taskchain' : $taskchainid = optional_param('id', 0, PARAM_INT); break;
case 'taskscore' : $taskscoreid = optional_param('id', 0, PARAM_INT); break;
}
break;
case 'admin-index':
if (isset($dbrecord)) {
$taskchainid = $dbrecord->id;
}
$set_page_context = false;
break;
default:
throw new moodle_exception('error_unrecognizedpageid', 'taskchain', '', $pageid);
}
// define main select criteria
$select = array();
$allowtaskid = false;
switch (true) {
case $taskattemptid>0 : $select[] = 'tc_tsk_att.id='.$taskattemptid; break;
case $taskscoreid>0 : $select[] = 'tc_tsk_scr.id='.$taskscoreid; break;
case $chainattemptid>0 : $select[] = 'tc_chn_att.id='.$chainattemptid; $allowtaskid = true; break;
case $chaingradeid>0 : $select[] = 'tc_chn_grd.id='.$chaingradeid; $allowtaskid = true; break;
case $conditionid>0 : $select[] = 'tc_cnd.id='.$conditionid; break;
case $taskid>0 : $allowtaskid = true; break;
case $chainid>0 : $select[] = 'tc_chn.id='.$chainid; break;
case $taskchainid>0 : $select[] = 'tc.id='.$taskchainid; break;
case $coursemoduleid>0 : $select[] = 'cm.id='.$coursemoduleid; break;
case $courseid>0 : $select[] = 'c.id='.$courseid; break;
}
if ($allowtaskid && $taskid>0) {
$select[]= "tc_tsk.id=$taskid";
}
// ====================
// Define join criteria
// ====================
// the most junior, i.e furthest to the right in the following list,
// record that is required will be joined to all its parent records
// User tables:
// chain_grades, chain_attempts, task_scores, task_attempts
// Task tables:
// course, course_modules, taskchain, taskchain_chains, taskchain_tasks, taskchain_conditions
// In addition ...
// chain_grades/attempts will be joined to their corresponding chain record
// task_scores/attempts will be joined to their corresponding task record
// the $tablesnames array stores the mapping: $tablename => $table
$jointask = false;
$joinchain = false;
$tablenames = array();
switch (true) {
case $taskattemptid>0:
$tablenames['taskchain_task_attempts'] = 'tc_tsk_att';
$select[] = 'tc_tsk_att.taskid=tc_tsk_scr.taskid AND tc_tsk_att.cnumber=tc_tsk_scr.cnumber AND tc_tsk_att.userid=tc_tsk_scr.userid';
case $taskscoreid>0:
$tablenames['taskchain_task_scores'] = 'tc_tsk_scr';
$select[] = 'tc_tsk_scr.taskid=tc_tsk.id AND tc_tsk.chainid=tc_chn_att.chainid AND tc_tsk_scr.cnumber=tc_chn_att.cnumber AND tc_tsk_scr.userid=tc_chn_att.userid';
$jointask = true;
case $chainattemptid>0:
$tablenames['taskchain_chain_attempts'] = 'tc_chn_att';
$select[] = 'tc_chn_att.chainid=tc_chn.id AND tc_chn_att.userid=tc_chn_grd.userid';
case $chaingradeid>0:
$tablenames['taskchain_chain_grades'] = 'tc_chn_grd';
$select[] = 'tc_chn_grd.parenttype=tc_chn.parenttype AND tc_chn_grd.parentid=tc_chn.parentid';
case $taskchainid>0:
case $coursemoduleid>0:
$joinchain = true;
}
switch (true) {
case $conditionid>0:
$tablenames['taskchain_conditions'] = 'tc_cnd';
$select[] = 'tc_cnd.taskid=tc_tsk.id';
case $jointask:
case $taskid>0:
$tablenames['taskchain_tasks'] = 'tc_tsk';
$select[] = 'tc_tsk.chainid=tc_chn.id';
case $joinchain:
case $chainid>0:
$tablenames['taskchain_chains'] = 'tc_chn';
$select[] = 'tc_chn.parenttype=0 AND tc_chn.parentid=tc.id';
$tablenames['taskchain'] = 'tc';
$select[] = 'tc.id=cm.instance AND cm.module=m.id';
$tablenames['modules'] = 'm';
$select[] = "m.name='taskchain'";
$tablenames['course_modules'] = 'cm';
$select[] = 'cm.course=c.id';
case $courseid>0:
$tablenames['course'] = 'c';
}
// define names and aliases for tables and fields
$tables = array();
$fields = array();
foreach ($tablenames as $tablename=>$table) {
$tables[] = '{'.$tablename.'} '.$table;
if ($columns = $DB->get_columns($tablename)) {
foreach ($columns as $column) {
$field = strtolower($column->name);
$fields[] = $table.'.'.$field.' AS '.$table.'_'.$field;
}
}
// remove final "s" to get $classname
if (substr($tablename, -1)=='s') {
$classname = substr($tablename, 0, -1);
} else {
$classname = $tablename;
}
// remove initial "taskchain_" to get $propertyname
if (substr($classname, 0, 10)=='taskchain_') {
$propertyname = substr($classname, 10);
} else {
$propertyname = $classname;
}
// remove any remaining underscores, "_"
$propertyname = str_replace('_', '', $propertyname);
// create new property of the appropriate class
if ($propertyname=='course' || $propertyname=='coursemodule' | $propertyname=='module') {
$this->$propertyname = new stdClass();
} else {
$this->$propertyname = new $classname(null, array('TC' => &$this));
}
}
// check we had some sensible input
if (! $fields = implode(',', $fields)) {
throw new moodle_exception('error_nodatabaseinfo', 'taskchain');
}
if (! $tables = implode(',', $tables)) {
throw new moodle_exception('error_noinputparameters', 'taskchain');
}
if (! $select = implode(' AND ', $select)) {
throw new moodle_exception('error_noinputparameters', 'taskchain');
}
// get the information from the database
if (! $record = $DB->get_record_sql("SELECT $fields FROM $tables WHERE $select")) {
throw new moodle_exception('error_norecordsfound', 'taskchain');
}
// distribute the database information into the relevant objects
foreach(get_object_vars($record) as $field => $value) {
$pos = strrpos($field, '_');
$table = substr($field, 0, $pos);
$field = substr($field, $pos + 1);
switch ($table) {
case 'tc_tsk_att': $this->taskattempt->$field = $value; break;
case 'tc_tsk_scr': $this->taskscore->$field = $value; break;
case 'tc_chn_att': $this->chainattempt->$field = $value; break;
case 'tc_chn_grd': $this->chaingrade->$field = $value; break;
case 'tc_cnd': $this->condition->$field = $value; break;
case 'tc_tsk': $this->task->$field = $value; break;
case 'tc_chn': $this->chain->$field = $value; break;
case 'tc': $this->taskchain->$field = $value; break;
case 'cm': $this->coursemodule->$field = $value; break;
case 'm': $this->module->$field = $value; break;
case 'c': $this->course->$field = $value; break;
}
}
// get course context
$this->course->context = self::context(CONTEXT_COURSE, $this->course->id);
// mimic get_coursemodule_from_id() and get_coursemodule_from_instance()
if ($this->coursemodule) {
$this->coursemodule->name = $this->taskchain->name;
$this->coursemodule->modname = $this->module->name;
$this->coursemodule->context = self::context(CONTEXT_MODULE, $this->coursemodule->id);
if ($set_page_context) {
$PAGE->set_context($this->coursemodule->context);
}
// prevent "Cannot find grade item" error in "lib/completionlib.php"
if ($this->chain->gradelimit==0 && $this->chain->gradeweighting==0) {
$this->coursemodule->completiongradeitemnumber = null;
}
} else {
if ($set_page_context) {
$PAGE->set_context($this->course->context);
}
}
// the main objects have now been set up - yay !
// reclaim some memory
unset($record, $tablenames, $tablename, $tables, $table, $fields, $field, $select, $allowtaskid, $jointask, $joinchain);
// we should at least have a course record by now
if (empty($this->course->id)) {
throw new moodle_exception('error_nocourseid', 'taskchain');
}
// require_login must come before inclusion of script libraries
// so that correct language is set for calls to get_string() in the
// included libraries
if (substr($pageclass, 0, 13)=='mod-taskchain') {
require_login($this->course->id, true, $this->coursemodule);
}
// create secondary objects
$this->available = new taskchain_available(null, array('TC' => &$this));
$this->can = new taskchain_can(null, array('TC' => &$this));
$this->create = new taskchain_create(null, array('TC' => &$this));
$this->get = new taskchain_get(null, array('TC' => &$this));
$this->regrade = new taskchain_regrade(null, array('TC' => &$this));
$this->require = new taskchain_require(null, array('TC' => &$this));
$this->url = new taskchain_url(null, array('TC' => &$this));
// check capabilities ("true" means "require")
switch ($pageid) {
case 'mod-taskchain-view':
$this->can->attempt() || $this->can->view(true);
break;
case 'mod-taskchain-attempt':
$this->can->attempt(true);
break;
case 'mod-taskchain-report':
$this->can->viewreports() || $this->can->reviewmyattempts(true);
break;
case 'mod-taskchain-edit-condition':
case 'mod-taskchain-edit-task':
case 'mod-taskchain-edit-tasks':
case 'course-modedit':
$this->can->manage(true);
break;
case 'mod-taskchain-index':
$this->can->view(true);
break;
case 'mod-taskchain-edit-columnlists':
case 'mod-taskchain-edit-form-helper':
$this->can->manage() || $this->can->manageactivities(true);
break;
}
// set tab and other page settings
switch ($pageid) {
case 'mod-taskchain-edit-tasks' : $this->tab = 'edit' ; break;
case 'mod-taskchain-report' : $this->tab = 'report'; break;
case 'mod-taskchain-attempt' : $this->tab = 'attempt'; break;
case 'mod-taskchain-submit' : $this->tab = 'submit'; break;
default: $this->tab = optional_param('tab', 'info', PARAM_ALPHA);
}
$this->mode = optional_param('mode', '', PARAM_ALPHA);
$this->action = optional_param('action', '', PARAM_ALPHA);
$this->inpopup = optional_param('inpopup', 0, PARAM_INT);
$this->confirmed = optional_param('confirmed', 0, PARAM_INT);
$this->selected = self::optional_param_array('selected', 0, PARAM_INT);
// set conditiontype
$type = ($this->condition ? $this->condition->conditiontype : 0);
$this->conditiontype = optional_param('conditiontype', $type, PARAM_INT);
// set columnlisttype and columnlistid
switch ($pageid) {
case 'mod-taskchain-edit-chains': $type = 'chains'; break;
case 'mod-taskchain-edit-tasks': $type = 'tasks'; break;
default: $type = '';
}
$type = optional_param('columnlisttype', $type, PARAM_ALPHA);
if ($type) {
$this->columnlisttype = $type;
$this->columnlistid = get_user_preferences('taskchain_'.$type.'_columnlistid', 'default');
$this->columnlistid = optional_param('columnlistid', $this->columnlistid, PARAM_ALPHA);
}
// set action
if ($this->action=='' && ($pageclass=='mod-taskchain' || $pageclass=='mod-taskchain-edit')) {
$actions = array('add',
'update',
'submit',
'cancel',
'delete',
'deleteall',
'deletecancelled',
'deleteconfirmed');
foreach ($actions as $action) {
if (optional_param($action.'button', '', PARAM_RAW)) {
$this->action = $action;
break;
}
}
}
// check sesskey, if required
if ($this->action) {
require_sesskey();
}
$this->force_cnumber(optional_param('cnumber', 0, PARAM_INT));
$this->force_tnumber(optional_param('tnumber', 0, PARAM_INT));
$this->force_taskid(optional_param('taskid', 0, PARAM_INT));
// store the time this page was created
$this->time = time();
} // end if $pageid == mod-taskchain
}
////////////////////////////////////////////////////////////////////////////////
// Magic methods //
////////////////////////////////////////////////////////////////////////////////
/**
* __call
*
* here is one way to implement inheritance from multiple classes
* this allows us to separate the methods into several classes
* http://stackoverflow.com/questions/356128/can-i-extend-a-class-using-more-than-1-class-in-php
*
* taskchain->available_xxx() will call taskchain->available->xxx()
* taskchain->can_xxx() will call taskchain->can->xxx()
* taskchain->create_xxx() will call taskchain->create->xxx()
* taskchain->get_xxx() will call taskchain->get->xxx()
* taskchain->regrade_xxx() will call taskchain->regrade->xxx()
* taskchain->require_xxx() will call taskchain->require->xxx()
*
* @param string $name
* @param array $params
* @todo Finish documenting this function
*/
public function __call($name, $params) {
switch (true) {
case substr($name, 0, 10)=='available_' : $callback = array($this->available, substr($name, 10)); break;
case substr($name, 0, 4)=='can_' : $callback = array($this->can, substr($name, 4)); break;
case substr($name, 0, 7)=='create_' : $callback = array($this->create, substr($name, 7)); break;
case substr($name, 0, 4)=='get_' : $callback = array($this->get, substr($name, 4)); break;
case substr($name, 0, 8)=='regrade_' : $callback = array($this->regrade, substr($name, 8)); break;
case substr($name, 0, 8)=='require_' : $callback = array($this->require, substr($name, 8)); break;
case substr($name, 0, 4)=='url_' : $callback = array($this->url, substr($name, 4)); break;
default: return false; // shouldn't happen !!
}
return call_user_func_array($callback, $params);
}
////////////////////////////////////////////////////////////////////////////////
// Static methods //
////////////////////////////////////////////////////////////////////////////////
// - set_user_editing()
// the following methods are passed to taskchain_available
// class in "mod/taskchain/locallib/available.php"
// - available_navigations_list()
// - available_feedbacks_list()
// - available_mediafilters_list()
// - available_outputformats_list($sourcetype)
// - available_attemptlimits_list()
// - available_allowresumes_list()
// - available_grademethods_list()
// - available_attemptgrademethods_list($type='grade')
// - available_statuses_list()
// - available_namesources_list()
// - available_titles_list()
// - available_addtypes_list()
// - available_gradeweightings_list()
// - available_gradelimits_list()
// - available_studentfeedbacks_list()
// - get_classes($plugintype, $classfilename='class.php', $prefix='', $suffix='')
// - get_sourcetype($sourcefile)
// - get_js_module(array $requires = null, array $strings = null)
// - get_version_info($info)
// - filearea_options()
// - text_editor_options($context)
// - text_page_types()
// - filearea_types()
// - text_page_options($type)
// - window_options($type='')
// - user_preferences_fieldnames_chain()
// - user_preferences_fieldnames_task()
// - get_question_text($question)
// - string_ids($field_value, $max_field_length=255)
// - string_id($str)
// - format_status($status)
// - format_time($time, $format=null, $notime=' ')
// - format_score($record, $default=' ')
/**
* set_user_editing
*
* @uses $USER
* @todo Finish documenting this function
*/
static public function set_user_editing() {
global $PAGE, $USER;
if ($editing = $PAGE->user_allowed_editing()) {
$editing = (isset($USER->editing) && $USER->editing);
$editing = optional_param('editmode', $editing, PARAM_BOOL);
}
$USER->editing = $editing;
}
/**
* This function will "include" all the files matching $classfilename for a given a plugin type
* (e.g. taskchainsource), and return a list of classes that were included
*
* @uses $CFG
* @param string $plugintype one of the plugintypes specified in mod/taskchain/db/subplugins.php
* @param xxx $classfilename (optional, default='class.php')
* @param xxx $prefix (optional, default='')
* @param xxx $suffix (optional, default='')
* @return xxx
* @todo Finish documenting this function
*/
static public function get_classes($plugintype, $classfilename='class.php', $prefix='', $suffix='') {
global $CFG;
// initialize array to hold class names
$classes = array();
// get list of all subplugins
$subplugins = array();
include($CFG->dirroot.'/mod/taskchain/db/subplugins.php');
// extract the $plugintype we are interested in
$types = array();
if (isset($subplugins[$plugintype])) {
$types[$plugintype] = $subplugins[$plugintype];
}
unset($subplugins);
// we are not interested in these directories (or any beginning with ".")
$ignored = array('CVS', '_vti_cnf', 'simpletest', 'db', 'yui', 'phpchain');
// get all the subplugins for this $plugintype
reset($types);
while ($type = key($types)) {
$dir = current($types);
$fulldir = $CFG->dirroot.'/'.$dir;
if (is_dir($fulldir) && file_exists($fulldir.'/'.$classfilename)) {
// include the class
require_once($fulldir.'/'.$classfilename);
// extract class name, e.g. taskchain_source_hp_6_jcloze_xml
// from $subdir, e.g. mod/taskchain/file/h6/6/jcloze/xml
// by removing leading "mod/" and converting all "/" to "_"
$classes[] = $prefix.str_replace('/', '_', substr($dir, 4)).$suffix;
// get subplugins in this $dir
$items = new DirectoryIterator($fulldir);
foreach ($items as $item) {
if (substr($item, 0, 1)=='.' || in_array($item, $ignored)) {
continue;
}
if ($item->isDir()) {
$types[$type.$item] = $dir.'/'.$item;
}
}
}
next($types);
}
sort($classes);
return $classes;
}
/**
* Returns a js module object for the TaskChain module
*
* @param array $requires
* e.g. array('base', 'dom', 'event-delegate', 'event-key')
* @return array $strings
* e.g. array(
* array('timesup', 'task'),
* array('functiondisabledbysecuremode', 'task'),
* array('flagged', 'question')
* )
*/
static public function get_js_module(array $requires=null, array $strings=null) {
return array('name' => 'mod_taskchain',
'fullpath' => '/mod/taskchain/module.js',
'requires' => $requires,
'strings' => $strings);
}
/**
* get_version_info
*
* @uses $CFG
* @param xxx $info
* @return xxx
* @todo Finish documenting this function
*/
static public function get_version_info($info) {
global $CFG;
static $plugin = null;
if (is_null($plugin)) {
$plugin = new stdClass();
require($CFG->dirroot.'/mod/taskchain/version.php');
}
if (isset($plugin->$info)) {
return $plugin->$info;
} else {
return "no $info found";
}
}
/**
* load_mediafilter_filter
*
* @param xxx $classname
* @todo Finish documenting this function
*/
static public function load_mediafilter_filter($classname) {
global $CFG;
$path = $CFG->dirroot.'/mod/taskchain/mediafilter/'.$classname.'/class.php';
// check the filter exists
if (! file_exists($path)) {
debugging('taskchain mediafilter class is not accessible: '.$classname, DEBUG_DEVELOPER);
return false;
}
return require_once($path);
}
/**
* filearea_options
*
* @uses $CFG
* @return xxx
* @todo Finish documenting this function
*/
static public function filearea_options($context=null) {
global $CFG;
require_once($CFG->dirroot.'/repository/lib.php');
$types = 0;
if (defined('FILE_INTERNAL')) {
$types = $types | FILE_INTERNAL; // Moodle >= 2.0
}
if (defined('FILE_EXTERNAL')) {
$types = $types | FILE_EXTERNAL; // Moodle >= 2.0
}
if (defined('FILE_REFERENCE')) {
$types = $types | FILE_REFERENCE; // Moodle >= 2.3
}
return array('context' => $context,
'maxbytes' => 0,
'maxfiles' => EDITOR_UNLIMITED_FILES, // = -1
'noclean' => 1,
'return_types' => $types,
'subdirs' => 1,
'trusttext' => 0);
}
/**
* text_editor_options
*
* @param xxx $context
* @return xxx
* @todo Finish documenting this function
*/
static public function text_editor_options($context) {
return array('subdirs' => 1,
'maxbytes' => 0,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'changeformat' => 1,
'context' => $context,
'noclean' => 1,
'trusttext' => 0);
}
/**
* text_page_types
*
* @return xxx
* @todo Finish documenting this function
*/
static public function text_page_types() {
return array('entry', 'exit');
}
/**
* filearea_types
*
* @return xxx
* @todo Finish documenting this function
*/
static public function filearea_types() {
return array('source', 'config');
}
/**
* text_page_options
*
* @param xxx $type
* @param xxx $subtype (optional, default='')
* @return xxx
* @todo Finish documenting this function
*/
static public function text_page_options($type, $subtype='') {
$options = array();
switch (true) {
case ($type=='entry'):
$options['title'] = self::ENTRYOPTIONS_TITLE;
$options['grading'] = self::ENTRYOPTIONS_GRADING;
$options['dates'] = self::ENTRYOPTIONS_DATES;
$options['attempts'] = self::ENTRYOPTIONS_ATTEMPTS;
break;
case ($type=='exit'):
if ($subtype=='' || $subtype=='feedback') {
$options['title'] = self::ENTRYOPTIONS_TITLE;
$options['encouragement'] = self::EXITOPTIONS_ENCOURAGEMENT;
$options['attemptscore'] = self::EXITOPTIONS_ATTEMPTSCORE;
$options['taskchaingrade'] = self::EXITOPTIONS_TASKCHAINGRADE;
}
if ($subtype=='' || $subtype=='links') {
$options['retry'] = self::EXITOPTIONS_RETRY;
$options['index'] = self::EXITOPTIONS_INDEX;
$options['course'] = self::EXITOPTIONS_COURSE;
$options['grades'] = self::EXITOPTIONS_GRADES;
}
break;
}
return $options;
}
/**
* window_options
*
* @param string $type 'yesno', 'moodle' or 'numeric'
* @return xxx
* @todo Finish documenting this function
*/
static public function window_options($type='') {
$options = array();
if ($type=='' || $type=='moodle') {
array_push($options, 'moodleheader','moodlenavbar','moodlefooter','moodlebutton');
}
if ($type=='' || $type=='yesno') {
array_push($options, 'resizable', 'scrollbars', 'directories', 'location', 'menubar', 'toolbar', 'status');
}
if ($type=='' || $type=='numeric') {
array_push($options, 'width', 'height');
}
return $options;
}
/**
* user_preferences_fieldnames_chain
*
* @return array of user_preferences for a TaskChain chain
* @todo Finish documenting this function
*/
static public function user_preferences_fieldnames_chain() {
return array(
// fields used only when adding a new TaskChain
'namesource','entrytextsource','exittextsource','taskchain',
// source/config files
'sourcefile','sourcelocation','configfile','configlocation',
// entry/exit pages
'entrypage','entryformat','entryoptions',
'exitpage','exitformat','exitoptions',
'entrycm','entrygrade','exitcm','exitgrade',
// display
'outputformat','navigation','title','stopbutton','stoptext',
'usefilters','useglossary','usemediafilter','studentfeedback','studentfeedbackurl',
// access restrictions
'timeopen','timeclose','timelimit','delay1','delay2','delay3',
'password','subnet','reviewoptions','attemptlimit',
// grading and reporting
'grademethod','gradeweighting','clickreporting','discarddetails'
);
}
/**
* user_preferences_fieldnames_task
*
* @return array of user_preferences for a TaskChain task
* @todo Finish documenting this function
*/
static public function user_preferences_fieldnames_task() {
return array(
// adding a task
'namesource','addtype',
// adding / editing a task
'sourcelocation','sourcefile','configfile','configlocation',
// display
'outputformat','navigation','title','stopbutton','stoptext',
'usefilters','useglossary','usemediafilter','studentfeedback','studentfeedbackurl',
// access restrictions
'timeopen','timeclose','timelimit','delay1','delay2','delay3',
'password','subnet','allowresume','reviewoptions','attemptlimit',
// scoring, storing and reports
'scoremethod','scoreignore','scorelimit','scoreweighting','clickreporting','discarddetails',
// conditions
'preconditions','postconditions'
);
}
/**
* get_question_text
*
* @uses $DB
* @param xxx $question
* @return xxx
* @todo Finish documenting this function
*/
static public function get_question_text($question) {
global $DB;
if (empty($question->text)) {
// JMatch, JMix and JQuiz
return $question->name;
} else {
// JCloze and JCross
return $DB->get_field('taskchain_strings', 'string', array('id' => $question->text));
}
}
/**
* string_ids
*
* @param xxx $field_value
* @param xxx $max_field_length (optional, default=255)
* @return xxx
* @todo Finish documenting this function
*/
static public function string_ids($field_value, $max_field_length=255) {
$ids = array();
$strings = explode(',', $field_value);
foreach($strings as $str) {
if ($id = self::string_id($str)) {
$ids[] = $id;
}
}
$ids = implode(',', $ids);
// we have to make sure that the list of $ids is no longer
// than the maximum allowable length for this field
if (strlen($ids) > $max_field_length) {
// truncate $ids just before last comma in allowable field length
// Note: largest possible id is something like 9223372036854775808
// so we must leave space for that in the $ids string
$ids = substr($ids, 0, $max_field_length - 20);
$ids = substr($ids, 0, strrpos($ids, ','));
// create single $str(ing) containing all $strings not included in $ids
$str = implode(',', array_slice($strings, substr_count($ids, ',') + 1));
// append the id of the string containing all the strings not yet in $ids
if ($id = self::string_id($str)) {
$ids .= ','.$id;
}
}