-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocallib.php
More file actions
729 lines (590 loc) · 19.5 KB
/
locallib.php
File metadata and controls
729 lines (590 loc) · 19.5 KB
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
<?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/>.
/**
* Library of internal classes and functions for dataset editor
*
* @package local
* @subpackage dataseteditor
* @copyright 2013-2015 Daniel Seemuth
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/lib.php');
define('LOCAL_DATASETEDITOR_DEFAULT_WILDCARD_OPTIONS', 'uniform:0:0:0');
define('LOCAL_DATASETEDITOR_PREF_PREFIX', 'local_dataseteditor_');
/**
* Returns true if the dataset editor navigation should appear for
* the given course module name.
*
* @param string $modulename Course module name
* @return boolean if dataset editor navigation should appear
*/
function local_dataseteditor_applicable_module($modulename) {
$modulename = strtolower($modulename);
if ($modulename == 'quiz') {
return true;
}
return false;
}
/**
* Returns context ID for the given question category
*
* @param int $categoryid Question category ID
* @param bool $mustexist If true and the category does not exist, throw error
* @return int $contextid
*/
function local_dataseteditor_get_cat_contextid(
$categoryid,
$mustexist = true
) {
global $DB;
$tablecategories = 'question_categories';
if ($mustexist) {
$strictness = MUST_EXIST;
} else {
$strictness = IGNORE_MISSING;
}
return $DB->get_field(
$tablecategories,
'contextid',
array('id' => $categoryid),
$strictness
);
}
/**
* Returns category IDs for the given wildcard IDs
*
* @param array $wildcardids Wildcard IDs
* @return array array[$categoryid] = num_wildcards
*/
function local_dataseteditor_get_wildcard_categoryids($wildcardids) {
global $DB;
$tabledefinitions = 'question_dataset_definitions';
if (empty($wildcardids)) {
return array();
}
$ret = array();
list($whereids, $params) = $DB->get_in_or_equal($wildcardids);
$sql = 'SELECT category, COUNT(id) AS num_wc ' .
'FROM {' . $tabledefinitions . '} ' .
'WHERE id ' . $whereids . ' ' .
'GROUP BY category';
$results = $DB->get_records_sql($sql, $params);
foreach ($results as $row) {
$ret[$row->category] = $row->num_wc;
}
return $ret;
}
/**
* Returns category IDs for the given dataset item IDs
*
* @param array $itemids Dataset item IDs
* @return array array[$categoryid] = num_items
*/
function local_dataseteditor_get_dataset_item_categoryids($itemids) {
global $DB;
$tabledefinitions = 'question_dataset_definitions';
$tablevalues = 'question_dataset_items';
if (empty($itemids)) {
return array();
}
$ret = array();
list($whereids, $params) = $DB->get_in_or_equal($itemids);
$sql = 'SELECT d.category, COUNT(v.id) AS num_v ' .
'FROM {' . $tabledefinitions . '} d ' .
'INNER JOIN {' . $tablevalues . '} v ON v.definition = d.id ' .
'WHERE v.id ' . $whereids . ' ' .
'GROUP BY d.category';
$results = $DB->get_records_sql($sql, $params);
foreach ($results as $row) {
$ret[$row->category] = $row->num_v;
}
return $ret;
}
/**
* Check if all the wildcards to be within the question category.
*
* @param array $wildcardids Wildcard IDs
* @param int $categoryid Question category id
* @return bool True if all the wildcards are in the question category
* (or true if no wildcards were given)
*/
function local_dataseteditor_all_wildcards_in_cat($wildcardids, $categoryid) {
if (empty($wildcardids)) {
return true;
}
$categories = local_dataseteditor_get_wildcard_categoryids($wildcardids);
$foundcats = 0;
foreach ($categories as $id => $num) {
if ($id != $categoryid) {
return false;
}
$foundcats += $num;
}
if ($foundcats != count(array_unique($wildcardids))) {
return false;
}
return true;
}
/**
* Check if all the dataset items are within the question category.
*
* @param array $itemids Dataset item IDs
* @param int $categoryid Question category id
* @return bool True if all the dataset items are in the question category
* (or true if no dataset items were given)
*/
function local_dataseteditor_all_dataset_items_in_cat($itemids, $categoryid) {
if (empty($itemids)) {
return true;
}
$categories = local_dataseteditor_get_dataset_item_categoryids($itemids);
$foundcats = 0;
foreach ($categories as $id => $num) {
if ($id != $categoryid) {
return false;
}
$foundcats += $num;
}
if ($foundcats != count(array_unique($itemids))) {
return false;
}
return true;
}
/**
* Returns array of id => wildcard
*
* @param int $categoryid Category from which to retrieve wildcards
* @param int $vallimit Limit values to this many results
* @return array array[id] => stdClass{->id ->name ->values}
*/
function local_dataseteditor_get_wildcards($categoryid, $vallimit=4) {
global $DB;
$tabledefinitions = 'question_dataset_definitions';
$tablevalues = 'question_dataset_items';
$wildcardresults = $DB->get_records(
$tabledefinitions,
array('category' => $categoryid),
'name',
'id,name'
);
$wildcards = array();
/* Retrieve wildcard definitions. */
foreach ($wildcardresults as $row) {
$wc = new stdClass();
$wc->id = $row->id;
$wc->name = $row->name;
$wc->values = array();
$wc->num_more_values = 0; // Pass number of values not returned.
$wildcards[$wc->id] = $wc;
}
/* Retrieve sampling of wildcard values. */
$valueresults = $DB->get_records_list(
$tablevalues,
'definition',
array_keys($wildcards),
'itemnumber',
'id,definition,value'
);
foreach ($valueresults as $row) {
if (count($wildcards[$row->definition]->values) < $vallimit) {
$wildcards[$row->definition]->values[] = $row->value;
} else {
$wildcards[$row->definition]->num_more_values++;
}
}
return $wildcards;
}
/**
* Validate wildcard names. Ignore wildcards that are marked for deletion.
*
* @param array $wildcards[] = stdClass(->id ->name ->del ->orig)
* @return false|string error string if problem, or false if no errors
* @throws coding_exception
*/
function local_dataseteditor_validate_wildcards($wildcards) {
$seenwildcards = array();
foreach ($wildcards as $wc) {
if ($wc->del) {
continue;
}
if (empty($wc->name)) {
continue;
}
if (isset($seenwildcards[$wc->name])) {
return get_string('dup_wildcard_X', 'local_dataseteditor', $wc->name);
}
$seenwildcards[$wc->name] = $wc;
}
// No errors.
return false;
}
/**
* Updates database with changed wildcard names. Also deletes orphan dataset
* item values.
*
* @param array $wildcards[] = stdClass(->id ->name ->del ->orig)
* @param stdClass $defaults Default values for category, type, options,
* @param int $categoryid Require all wildcards to be in this category
* itemcount
* @return null
* @throws coding_exception
*/
function local_dataseteditor_save_wildcards(
$wildcards, $defaults, $categoryid
) {
$tabledefinitions = 'question_dataset_definitions';
$tablevalues = 'question_dataset_items';
$fields = array('category', 'name', 'type', 'options', 'itemcount');
global $DB;
/* Check all wildcard IDs against this category ID! */
$ids = array();
foreach ($wildcards as $wc) {
if ($wc->id > 0) {
$ids[] = $wc->id;
}
}
if (! local_dataseteditor_all_wildcards_in_cat($ids, $categoryid)) {
throw new coding_exception(
'Not all wildcards in category ' . $categoryid
);
}
foreach ($wildcards as $wc) {
if (empty($wc->name)) {
/* Empty name. Treat as deleted. */
$wc->del = 1;
}
if ($wc->id > 0) {
if ($wc->del) {
/* Delete this wildcard and its dataset item values. */
$DB->delete_records($tabledefinitions, array(
'id' => $wc->id,
));
$DB->delete_records($tablevalues, array(
'definition' => $wc->id,
));
} else {
/* Existing wildcard! Update only if changed. */
if ($wc->name != $wc->orig) {
$DB->set_field($tabledefinitions, 'name', $wc->name,
array('id' => $wc->id));
}
}
} else {
/* New wildcard!
* Insert into database if not marked for deletion.
*/
if ($wc->del) {
continue;
}
$newwc = new stdClass();
foreach ($fields as $field) {
if (isset($wc->$field)) {
$newwc->$field = $wc->$field;
} else if (isset($defaults->$field)) {
$newwc->$field = $defaults->$field;
} else {
throw new coding_exception('Undefined field: ' . $field);
}
}
$DB->insert_record($tabledefinitions, $newwc);
}
}
}
/**
* Returns array of itemnum => array(defnum => item)
*
* @param array $wildcardids Retrieve values matching these wildcard IDs
* @return array array[itemnum] => array(defnum => stdClass{->id ->val})
*/
function local_dataseteditor_get_dataset_items($wildcardids) {
global $DB;
$tablevalues = 'question_dataset_items';
$valueresults = $DB->get_records_list(
$tablevalues,
'definition',
$wildcardids,
'',
'id,definition,itemnumber,value'
);
$items = array();
/* Retrieve wildcard definitions. */
foreach ($valueresults as $row) {
$item = new stdClass();
$item->id = $row->id;
$item->val = $row->value;
if (! isset($items[$row->itemnumber])) {
$items[$row->itemnumber] = array();
}
$items[$row->itemnumber][$row->definition] = $item;
}
return $items;
}
/**
* Updates database with changed dataset items.
*
* @param array
* $items[itemnum] => array(defnum => stdClass{->id ->val ->orig})
* @param array $deleteitems[itemnum] = i (use keys, ignore values)
* Delete items with these itemnums
* @param int $categoryid Require all dateset items to be in this category
* @return null
* @throws coding_exception
*/
function local_dataseteditor_save_dataset_items(
$items, $deleteitems, $categoryid
) {
$tabledefinitions = 'question_dataset_definitions';
$tablevalues = 'question_dataset_items';
global $DB;
/* Check all wildcard IDs against this category ID! */
$ids = array();
foreach ($items as $def2val) {
foreach ($def2val as $defnum => $item) {
if ($item->id > 0) {
$ids[] = $item->id;
}
}
}
if (! local_dataseteditor_all_dataset_items_in_cat($ids, $categoryid)) {
throw new coding_exception(
'Not all dataset items in category ' . $categoryid
);
}
ksort($items);
/* Keep track of how many item numbers deleted so far.
* Modify subsequent item numbers by subtracting this number.
* This ensures that item numbers always start at 1 and are consecutive.
*/
$numdeleted = 0;
/* Keep track of how many items remain. */
$numdata = 0;
foreach ($items as $itemnum => $def2val) {
if (isset($deleteitems[$itemnum])) {
/* Delete dataset items with this item number and matching
* the definition IDs.
*/
foreach ($def2val as $defnum => $item) {
$DB->delete_records($tablevalues, array(
'definition' => $defnum,
'itemnumber' => $itemnum,
));
}
$numdeleted++;
continue;
}
/* Not marked for deletion! Update $itemnum as needed. */
$itemnum -= $numdeleted;
$itemhasdata = false;
foreach ($def2val as $defnum => $item) {
if ((! isset($item->val)) || ($item->val === null)) {
throw new coding_exception(
'No value defined for ' . $itemnum . ', ' . $defnum
);
}
$itemhasdata = true;
if ($item->id > 0) {
/* Existing value! Update only if changed. */
if ($item->val != $item->orig) {
$DB->set_field($tablevalues, 'value', $item->val,
array('id' => $item->id));
}
/* Update item number if any items have been deleted. */
if ($numdeleted) {
$DB->set_field($tablevalues, 'itemnumber', $itemnum,
array('id' => $item->id));
}
} else {
/* New value! Insert into database. */
$newitem = new stdClass();
$newitem->definition = $defnum;
$newitem->itemnumber = $itemnum;
$newitem->value = $item->val;
$DB->insert_record($tablevalues, $newitem);
}
}
if ($itemhasdata) {
$numdata++;
}
}
/* Update wildcards' itemcount field. */
$DB->set_field($tabledefinitions, 'itemcount', $numdata, array(
'category' => $categoryid
));
}
/**
* Overwrites wildcards and dataset with imported data
*
* @param int $categoryid Category whose data to overwrite
* @param array $wildcards[id] = name
* @param array $items[] = array(defnum => val)
* @return null
* @throws coding_exception
*/
function local_dataseteditor_overwrite_wildcard_dataset(
$categoryid, $wildcards, $items,
$requirefulldataset = true
) {
$tabledefinitions = 'question_dataset_definitions';
$tablevalues = 'question_dataset_items';
global $DB;
$curwildcards = local_dataseteditor_get_wildcards($categoryid, 0);
$curname2id = array();
foreach ($curwildcards as $wc) {
$curname2id[$wc->name] = $wc->id;
}
if (count($curwildcards) != count($curname2id)) {
throw new coding_exception('Duplicate wildcard names');
}
/* Compile list of deleted and new wildcards. */
$newname2id = array();
foreach ($wildcards as $name) {
if (array_key_exists($name, $curname2id)) {
$newname2id[$name] = $curname2id[$name];
} else {
$newname2id[$name] = null;
}
}
/* If required, make sure each dataset item has all values. */
if ($requirefulldataset) {
foreach ($items as $values) {
foreach ($wildcards as $i => $name) {
if (! isset($values[$i])) {
throw new coding_exception(
'No value defined for ' . $i . ', ' . $name
);
}
}
}
}
/* Delete old, unused wildcards. */
$deleteids = array();
foreach ($curname2id as $name => $id) {
if (! array_key_exists($name, $newname2id)) {
$deleteids[] = $id;
}
}
if (! empty($deleteids)) {
list($whereids, $params) = $DB->get_in_or_equal($deleteids);
$DB->delete_records_select($tabledefinitions, 'id ' . $whereids,
$params);
}
/* Add new wildcards. */
foreach ($newname2id as $name => $id) {
if ($id === null) {
$o = new stdClass();
$o->category = $categoryid;
$o->name = $name;
$o->type = 1;
$o->options = LOCAL_DATASETEDITOR_DEFAULT_WILDCARD_OPTIONS;
$o->itemcount = 0; /* Will be updated. */
$id = $DB->insert_record($tabledefinitions, $o);
$newname2id[$name] = $id;
}
}
/* Delete old values. */
if (! empty($curwildcards)) {
list($whereids, $params) = $DB->get_in_or_equal(
array_keys($curwildcards));
$DB->delete_records_select($tablevalues, 'definition ' . $whereids,
$params);
}
/* Insert new values. */
$itemnum = 0;
foreach ($wildcards as $i => $name) {
$wcid = $newname2id[$name];
$itemnum = 0;
foreach ($items as $values) {
$itemnum++;
$o = new stdClass();
$o->definition = $wcid;
$o->itemnumber = $itemnum;
$o->value = $values[$i];
$DB->insert_record($tablevalues, $o);
}
}
/* Update wildcards' itemcount field. */
$DB->set_field($tabledefinitions, 'itemcount', $itemnum, array(
'category' => $categoryid
));
}
/**
* Require the user to have access to the given question category.
*
* @param string $capability Require the user to have this capability
* @param int $categoryid Question category id
* @return void terminates with an error if the user does not have the given
* capability.
*/
function local_dataseteditor_require_capability_cat(
$capability, $categoryid
) {
$contextid = local_dataseteditor_get_cat_contextid($categoryid, true);
$context = context::instance_by_id($contextid);
require_capability($capability, $context);
}
/**
* Return course module from within course $courseid.
*
* @param int $courseid ID of course containing module
* @param int $cmid Course-module ID
* @return cm_info Information about that course-module
* @throws moodle_exception If the course-module does not exist
*/
function local_dataseteditor_get_cm($courseid, $cmid) {
global $DB;
$course = $DB->get_record('course',
array('id' => $courseid),
'*',
MUST_EXIST
);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($cmid);
return $cm;
}
/**
* Return user's preference for category selection in course $courseid.
*
* @param int $courseid ID of the course
* @return string Preferred category selection
*/
function local_dataseteditor_get_category_preference($courseid) {
$key = LOCAL_DATASETEDITOR_PREF_PREFIX . 'c_' . strval($courseid);
return get_user_preferences($key, '');
}
/**
* Set user's preference for category selection in course $courseid.
*
* @param int $courseid ID of the course
* @param string $category Preferred category selection
* @return bool Always true or exception
*/
function local_dataseteditor_set_category_preference($courseid, $category) {
$key = LOCAL_DATASETEDITOR_PREF_PREFIX . 'c_' . strval($courseid);
set_user_preference($key, strval($category));
return true;
}
/**
* Unset user's preference for category selection in course $courseid.
*
* @param int $courseid ID of the course
* @return bool Always true or exception
*/
function local_dataseteditor_unset_category_preference($courseid) {
$key = LOCAL_DATASETEDITOR_PREF_PREFIX . 'c_' . strval($courseid);
unset_user_preference($key);
return true;
}