mirrored from git://git.moodle.org/moodle.git
/
api.php
5251 lines (4482 loc) · 200 KB
/
api.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/>.
/**
* Class for loading/storing competency frameworks from the DB.
*
* @package core_competency
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_competency;
defined('MOODLE_INTERNAL') || die();
use stdClass;
use cm_info;
use context;
use context_helper;
use context_system;
use context_course;
use context_module;
use context_user;
use coding_exception;
use require_login_exception;
use moodle_exception;
use moodle_url;
use required_capability_exception;
/**
* Class for doing things with competency frameworks.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {
/**
* Returns whether competencies are enabled.
*
* This method should never do more than checking the config setting, the reason
* being that some other code could be checking the config value directly
* to avoid having to load this entire file into memory.
*
* @return boolean True when enabled.
*/
public static function is_enabled() {
return get_config('core_competency', 'enabled');
}
/**
* Throws an exception if competencies are not enabled.
*
* @return void
* @throws moodle_exception
*/
public static function require_enabled() {
if (!static::is_enabled()) {
throw new moodle_exception('competenciesarenotenabled', 'core_competency');
}
}
/**
* Checks whether a scale is used anywhere in the plugin.
*
* This public API has two exceptions:
* - It MUST NOT perform any capability checks.
* - It MUST ignore whether competencies are enabled or not ({@link self::is_enabled()}).
*
* @param int $scaleid The scale ID.
* @return bool
*/
public static function is_scale_used_anywhere($scaleid) {
global $DB;
$sql = "SELECT s.id
FROM {scale} s
LEFT JOIN {" . competency_framework::TABLE ."} f
ON f.scaleid = :scaleid1
LEFT JOIN {" . competency::TABLE ."} c
ON c.scaleid = :scaleid2
WHERE f.id IS NOT NULL
OR c.id IS NOT NULL";
return $DB->record_exists_sql($sql, ['scaleid1' => $scaleid, 'scaleid2' => $scaleid]);
}
/**
* Validate if current user have acces to the course_module if hidden.
*
* @param mixed $cmmixed The cm_info class, course module record or its ID.
* @param bool $throwexception Throw an exception or not.
* @return bool
*/
protected static function validate_course_module($cmmixed, $throwexception = true) {
$cm = $cmmixed;
if (!is_object($cm)) {
$cmrecord = get_coursemodule_from_id(null, $cmmixed);
$modinfo = get_fast_modinfo($cmrecord->course);
$cm = $modinfo->get_cm($cmmixed);
} else if (!$cm instanceof cm_info) {
// Assume we got a course module record.
$modinfo = get_fast_modinfo($cm->course);
$cm = $modinfo->get_cm($cm->id);
}
if (!$cm->uservisible) {
if ($throwexception) {
throw new require_login_exception('Course module is hidden');
} else {
return false;
}
}
return true;
}
/**
* Validate if current user have acces to the course if hidden.
*
* @param mixed $courseorid The course or it ID.
* @param bool $throwexception Throw an exception or not.
* @return bool
*/
protected static function validate_course($courseorid, $throwexception = true) {
$course = $courseorid;
if (!is_object($course)) {
$course = get_course($course);
}
$coursecontext = context_course::instance($course->id);
if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
if ($throwexception) {
throw new require_login_exception('Course is hidden');
} else {
return false;
}
}
return true;
}
/**
* Create a competency from a record containing all the data for the class.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param stdClass $record Record containing all the data for an instance of the class.
* @return competency
*/
public static function create_competency(stdClass $record) {
static::require_enabled();
$competency = new competency(0, $record);
// First we do a permissions check.
require_capability('moodle/competency:competencymanage', $competency->get_context());
// Reset the sortorder, use reorder instead.
$competency->set('sortorder', 0);
$competency->create();
\core\event\competency_created::create_from_competency($competency)->trigger();
// Reset the rule of the parent.
$parent = $competency->get_parent();
if ($parent) {
$parent->reset_rule();
$parent->update();
}
return $competency;
}
/**
* Delete a competency by id.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param int $id The record to delete. This will delete alot of related data - you better be sure.
* @return boolean
*/
public static function delete_competency($id) {
global $DB;
static::require_enabled();
$competency = new competency($id);
// First we do a permissions check.
require_capability('moodle/competency:competencymanage', $competency->get_context());
$events = array();
$competencyids = array(intval($competency->get('id')));
$contextid = $competency->get_context()->id;
$competencyids = array_merge(competency::get_descendants_ids($competency), $competencyids);
if (!competency::can_all_be_deleted($competencyids)) {
return false;
}
$transaction = $DB->start_delegated_transaction();
try {
// Reset the rule of the parent.
$parent = $competency->get_parent();
if ($parent) {
$parent->reset_rule();
$parent->update();
}
// Delete the competency separately so the after_delete event can be triggered.
$competency->delete();
// Delete the competencies.
competency::delete_multiple($competencyids);
// Delete the competencies relation.
related_competency::delete_multiple_relations($competencyids);
// Delete competency evidences.
user_evidence_competency::delete_by_competencyids($competencyids);
// Register the competencies deleted events.
$events = \core\event\competency_deleted::create_multiple_from_competencyids($competencyids, $contextid);
} catch (\Exception $e) {
$transaction->rollback($e);
}
$transaction->allow_commit();
// Trigger events.
foreach ($events as $event) {
$event->trigger();
}
return true;
}
/**
* Reorder this competency.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param int $id The id of the competency to move.
* @return boolean
*/
public static function move_down_competency($id) {
static::require_enabled();
$current = new competency($id);
// First we do a permissions check.
require_capability('moodle/competency:competencymanage', $current->get_context());
$max = self::count_competencies(array('parentid' => $current->get('parentid'),
'competencyframeworkid' => $current->get('competencyframeworkid')));
if ($max > 0) {
$max--;
}
$sortorder = $current->get('sortorder');
if ($sortorder >= $max) {
return false;
}
$sortorder = $sortorder + 1;
$current->set('sortorder', $sortorder);
$filters = array('parentid' => $current->get('parentid'),
'competencyframeworkid' => $current->get('competencyframeworkid'),
'sortorder' => $sortorder);
$children = self::list_competencies($filters, 'id');
foreach ($children as $needtoswap) {
$needtoswap->set('sortorder', $sortorder - 1);
$needtoswap->update();
}
// OK - all set.
$result = $current->update();
return $result;
}
/**
* Reorder this competency.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param int $id The id of the competency to move.
* @return boolean
*/
public static function move_up_competency($id) {
static::require_enabled();
$current = new competency($id);
// First we do a permissions check.
require_capability('moodle/competency:competencymanage', $current->get_context());
$sortorder = $current->get('sortorder');
if ($sortorder == 0) {
return false;
}
$sortorder = $sortorder - 1;
$current->set('sortorder', $sortorder);
$filters = array('parentid' => $current->get('parentid'),
'competencyframeworkid' => $current->get('competencyframeworkid'),
'sortorder' => $sortorder);
$children = self::list_competencies($filters, 'id');
foreach ($children as $needtoswap) {
$needtoswap->set('sortorder', $sortorder + 1);
$needtoswap->update();
}
// OK - all set.
$result = $current->update();
return $result;
}
/**
* Move this competency so it sits in a new parent.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param int $id The id of the competency to move.
* @param int $newparentid The new parent id for the competency.
* @return boolean
*/
public static function set_parent_competency($id, $newparentid) {
global $DB;
static::require_enabled();
$current = new competency($id);
// First we do a permissions check.
require_capability('moodle/competency:competencymanage', $current->get_context());
if ($id == $newparentid) {
throw new coding_exception('Can not set a competency as a parent of itself.');
} if ($newparentid == $current->get('parentid')) {
throw new coding_exception('Can not move a competency to the same location.');
}
// Some great variable assignment right here.
$currentparent = $current->get_parent();
$parent = !empty($newparentid) ? new competency($newparentid) : null;
$parentpath = !empty($parent) ? $parent->get('path') : '/0/';
// We're going to change quite a few things.
$transaction = $DB->start_delegated_transaction();
// If we are moving a node to a child of itself:
// - promote all the child nodes by one level.
// - remove the rule on self.
// - re-read the parent.
$newparents = explode('/', $parentpath);
if (in_array($current->get('id'), $newparents)) {
$children = competency::get_records(array('parentid' => $current->get('id')), 'id');
foreach ($children as $child) {
$child->set('parentid', $current->get('parentid'));
$child->update();
}
// Reset the rule on self as our children have changed.
$current->reset_rule();
// The destination parent is one of our descendants, we need to re-fetch its values (path, parentid).
$parent->read();
}
// Reset the rules of initial parent and destination.
if (!empty($currentparent)) {
$currentparent->reset_rule();
$currentparent->update();
}
if (!empty($parent)) {
$parent->reset_rule();
$parent->update();
}
// Do the actual move.
$current->set('parentid', $newparentid);
$result = $current->update();
// All right, let's commit this.
$transaction->allow_commit();
return $result;
}
/**
* Update the details for a competency.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param stdClass $record The new details for the competency.
* Note - must contain an id that points to the competency to update.
*
* @return boolean
*/
public static function update_competency($record) {
static::require_enabled();
$competency = new competency($record->id);
// First we do a permissions check.
require_capability('moodle/competency:competencymanage', $competency->get_context());
// Some things should not be changed in an update - they should use a more specific method.
$record->sortorder = $competency->get('sortorder');
$record->parentid = $competency->get('parentid');
$record->competencyframeworkid = $competency->get('competencyframeworkid');
$competency->from_record($record);
require_capability('moodle/competency:competencymanage', $competency->get_context());
// OK - all set.
$result = $competency->update();
// Trigger the update event.
\core\event\competency_updated::create_from_competency($competency)->trigger();
return $result;
}
/**
* Read a the details for a single competency and return a record.
*
* Requires moodle/competency:competencyview capability at the system context.
*
* @param int $id The id of the competency to read.
* @param bool $includerelated Include related tags or not.
* @return stdClass
*/
public static function read_competency($id, $includerelated = false) {
static::require_enabled();
$competency = new competency($id);
// First we do a permissions check.
$context = $competency->get_context();
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'), $context)) {
throw new required_capability_exception($context, 'moodle/competency:competencyview', 'nopermissions', '');
}
// OK - all set.
if ($includerelated) {
$relatedcompetency = new related_competency();
if ($related = $relatedcompetency->list_relations($id)) {
$competency->relatedcompetencies = $related;
}
}
return $competency;
}
/**
* Perform a text search based and return all results and their parents.
*
* Requires moodle/competency:competencyview capability at the framework context.
*
* @param string $textsearch A string to search for.
* @param int $competencyframeworkid The id of the framework to limit the search.
* @return array of competencies
*/
public static function search_competencies($textsearch, $competencyframeworkid) {
static::require_enabled();
$framework = new competency_framework($competencyframeworkid);
// First we do a permissions check.
$context = $framework->get_context();
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'), $context)) {
throw new required_capability_exception($context, 'moodle/competency:competencyview', 'nopermissions', '');
}
// OK - all set.
$competencies = competency::search($textsearch, $competencyframeworkid);
return $competencies;
}
/**
* Perform a search based on the provided filters and return a paginated list of records.
*
* Requires moodle/competency:competencyview capability at some context.
*
* @param array $filters A list of filters to apply to the list.
* @param string $sort The column to sort on
* @param string $order ('ASC' or 'DESC')
* @param int $skip Number of records to skip (pagination)
* @param int $limit Max of records to return (pagination)
* @return array of competencies
*/
public static function list_competencies($filters, $sort = '', $order = 'ASC', $skip = 0, $limit = 0) {
static::require_enabled();
if (!isset($filters['competencyframeworkid'])) {
$context = context_system::instance();
} else {
$framework = new competency_framework($filters['competencyframeworkid']);
$context = $framework->get_context();
}
// First we do a permissions check.
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'), $context)) {
throw new required_capability_exception($context, 'moodle/competency:competencyview', 'nopermissions', '');
}
// OK - all set.
return competency::get_records($filters, $sort, $order, $skip, $limit);
}
/**
* Perform a search based on the provided filters and return a paginated list of records.
*
* Requires moodle/competency:competencyview capability at some context.
*
* @param array $filters A list of filters to apply to the list.
* @return int
*/
public static function count_competencies($filters) {
static::require_enabled();
if (!isset($filters['competencyframeworkid'])) {
$context = context_system::instance();
} else {
$framework = new competency_framework($filters['competencyframeworkid']);
$context = $framework->get_context();
}
// First we do a permissions check.
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'), $context)) {
throw new required_capability_exception($context, 'moodle/competency:competencyview', 'nopermissions', '');
}
// OK - all set.
return competency::count_records($filters);
}
/**
* Create a competency framework from a record containing all the data for the class.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param stdClass $record Record containing all the data for an instance of the class.
* @return competency_framework
*/
public static function create_framework(stdClass $record) {
static::require_enabled();
$framework = new competency_framework(0, $record);
require_capability('moodle/competency:competencymanage', $framework->get_context());
// Account for different formats of taxonomies.
if (isset($record->taxonomies)) {
$framework->set('taxonomies', $record->taxonomies);
}
$framework = $framework->create();
// Trigger a competency framework created event.
\core\event\competency_framework_created::create_from_framework($framework)->trigger();
return $framework;
}
/**
* Duplicate a competency framework by id.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param int $id The record to duplicate. All competencies associated and related will be duplicated.
* @return competency_framework the framework duplicated
*/
public static function duplicate_framework($id) {
global $DB;
static::require_enabled();
$framework = new competency_framework($id);
require_capability('moodle/competency:competencymanage', $framework->get_context());
// Starting transaction.
$transaction = $DB->start_delegated_transaction();
try {
// Get a uniq idnumber based on the origin framework.
$idnumber = competency_framework::get_unused_idnumber($framework->get('idnumber'));
$framework->set('idnumber', $idnumber);
// Adding the suffix copy to the shortname.
$framework->set('shortname', get_string('duplicateditemname', 'core_competency', $framework->get('shortname')));
$framework->set('id', 0);
$framework = $framework->create();
// Array that match the old competencies ids with the new one to use when copying related competencies.
$frameworkcompetency = competency::get_framework_tree($id);
$matchids = self::duplicate_competency_tree($framework->get('id'), $frameworkcompetency, 0, 0);
// Copy the related competencies.
$relcomps = related_competency::get_multiple_relations(array_keys($matchids));
foreach ($relcomps as $relcomp) {
$compid = $relcomp->get('competencyid');
$relcompid = $relcomp->get('relatedcompetencyid');
if (isset($matchids[$compid]) && isset($matchids[$relcompid])) {
$newcompid = $matchids[$compid]->get('id');
$newrelcompid = $matchids[$relcompid]->get('id');
if ($newcompid < $newrelcompid) {
$relcomp->set('competencyid', $newcompid);
$relcomp->set('relatedcompetencyid', $newrelcompid);
} else {
$relcomp->set('competencyid', $newrelcompid);
$relcomp->set('relatedcompetencyid', $newcompid);
}
$relcomp->set('id', 0);
$relcomp->create();
} else {
// Debugging message when there is no match found.
debugging('related competency id not found');
}
}
// Setting rules on duplicated competencies.
self::migrate_competency_tree_rules($frameworkcompetency, $matchids);
$transaction->allow_commit();
} catch (\Exception $e) {
$transaction->rollback($e);
}
// Trigger a competency framework created event.
\core\event\competency_framework_created::create_from_framework($framework)->trigger();
return $framework;
}
/**
* Delete a competency framework by id.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param int $id The record to delete. This will delete alot of related data - you better be sure.
* @return boolean
*/
public static function delete_framework($id) {
global $DB;
static::require_enabled();
$framework = new competency_framework($id);
require_capability('moodle/competency:competencymanage', $framework->get_context());
$events = array();
$competenciesid = competency::get_ids_by_frameworkid($id);
$contextid = $framework->get('contextid');
if (!competency::can_all_be_deleted($competenciesid)) {
return false;
}
$transaction = $DB->start_delegated_transaction();
try {
if (!empty($competenciesid)) {
// Delete competencies.
competency::delete_by_frameworkid($id);
// Delete the related competencies.
related_competency::delete_multiple_relations($competenciesid);
// Delete the evidences for competencies.
user_evidence_competency::delete_by_competencyids($competenciesid);
}
// Create a competency framework deleted event.
$event = \core\event\competency_framework_deleted::create_from_framework($framework);
$result = $framework->delete();
// Register the deleted events competencies.
$events = \core\event\competency_deleted::create_multiple_from_competencyids($competenciesid, $contextid);
} catch (\Exception $e) {
$transaction->rollback($e);
}
// Commit the transaction.
$transaction->allow_commit();
// If all operations are successfull then trigger the delete event.
$event->trigger();
// Trigger deleted event competencies.
foreach ($events as $event) {
$event->trigger();
}
return $result;
}
/**
* Update the details for a competency framework.
*
* Requires moodle/competency:competencymanage capability at the system context.
*
* @param stdClass $record The new details for the framework. Note - must contain an id that points to the framework to update.
* @return boolean
*/
public static function update_framework($record) {
static::require_enabled();
$framework = new competency_framework($record->id);
// Check the permissions before update.
require_capability('moodle/competency:competencymanage', $framework->get_context());
// Account for different formats of taxonomies.
$framework->from_record($record);
if (isset($record->taxonomies)) {
$framework->set('taxonomies', $record->taxonomies);
}
// Trigger a competency framework updated event.
\core\event\competency_framework_updated::create_from_framework($framework)->trigger();
return $framework->update();
}
/**
* Read a the details for a single competency framework and return a record.
*
* Requires moodle/competency:competencyview capability at the system context.
*
* @param int $id The id of the framework to read.
* @return competency_framework
*/
public static function read_framework($id) {
static::require_enabled();
$framework = new competency_framework($id);
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
$framework->get_context())) {
throw new required_capability_exception($framework->get_context(), 'moodle/competency:competencyview',
'nopermissions', '');
}
return $framework;
}
/**
* Logg the competency framework viewed event.
*
* @param competency_framework|int $frameworkorid The competency_framework object or competency framework id
* @return bool
*/
public static function competency_framework_viewed($frameworkorid) {
static::require_enabled();
$framework = $frameworkorid;
if (!is_object($framework)) {
$framework = new competency_framework($framework);
}
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
$framework->get_context())) {
throw new required_capability_exception($framework->get_context(), 'moodle/competency:competencyview',
'nopermissions', '');
}
\core\event\competency_framework_viewed::create_from_framework($framework)->trigger();
return true;
}
/**
* Logg the competency viewed event.
*
* @param competency|int $competencyorid The competency object or competency id
* @return bool
*/
public static function competency_viewed($competencyorid) {
static::require_enabled();
$competency = $competencyorid;
if (!is_object($competency)) {
$competency = new competency($competency);
}
if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'),
$competency->get_context())) {
throw new required_capability_exception($competency->get_context(), 'moodle/competency:competencyview',
'nopermissions', '');
}
\core\event\competency_viewed::create_from_competency($competency)->trigger();
return true;
}
/**
* Perform a search based on the provided filters and return a paginated list of records.
*
* Requires moodle/competency:competencyview capability at the system context.
*
* @param string $sort The column to sort on
* @param string $order ('ASC' or 'DESC')
* @param int $skip Number of records to skip (pagination)
* @param int $limit Max of records to return (pagination)
* @param context $context The parent context of the frameworks.
* @param string $includes Defines what other contexts to fetch frameworks from.
* Accepted values are:
* - children: All descendants
* - parents: All parents, grand parents, etc...
* - self: Context passed only.
* @param bool $onlyvisible If true return only visible frameworks
* @param string $query A string to use to filter down the frameworks.
* @return array of competency_framework
*/
public static function list_frameworks($sort, $order, $skip, $limit, $context, $includes = 'children',
$onlyvisible = false, $query = '') {
global $DB;
static::require_enabled();
// Get all the relevant contexts.
$contexts = self::get_related_contexts($context, $includes,
array('moodle/competency:competencyview', 'moodle/competency:competencymanage'));
if (empty($contexts)) {
throw new required_capability_exception($context, 'moodle/competency:competencyview', 'nopermissions', '');
}
// OK - all set.
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($contexts), SQL_PARAMS_NAMED);
$select = "contextid $insql";
if ($onlyvisible) {
$select .= " AND visible = :visible";
$inparams['visible'] = 1;
}
if (!empty($query) || is_numeric($query)) {
$sqlnamelike = $DB->sql_like('shortname', ':namelike', false);
$sqlidnlike = $DB->sql_like('idnumber', ':idnlike', false);
$select .= " AND ($sqlnamelike OR $sqlidnlike) ";
$inparams['namelike'] = '%' . $DB->sql_like_escape($query) . '%';
$inparams['idnlike'] = '%' . $DB->sql_like_escape($query) . '%';
}
return competency_framework::get_records_select($select, $inparams, $sort . ' ' . $order, '*', $skip, $limit);
}
/**
* Perform a search based on the provided filters and return a paginated list of records.
*
* Requires moodle/competency:competencyview capability at the system context.
*
* @param context $context The parent context of the frameworks.
* @param string $includes Defines what other contexts to fetch frameworks from.
* Accepted values are:
* - children: All descendants
* - parents: All parents, grand parents, etc...
* - self: Context passed only.
* @return int
*/
public static function count_frameworks($context, $includes) {
global $DB;
static::require_enabled();
// Get all the relevant contexts.
$contexts = self::get_related_contexts($context, $includes,
array('moodle/competency:competencyview', 'moodle/competency:competencymanage'));
if (empty($contexts)) {
throw new required_capability_exception($context, 'moodle/competency:competencyview', 'nopermissions', '');
}
// OK - all set.
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($contexts), SQL_PARAMS_NAMED);
return competency_framework::count_records_select("contextid $insql", $inparams);
}
/**
* Fetches all the relevant contexts.
*
* Note: This currently only supports system, category and user contexts. However user contexts
* behave a bit differently and will fallback on the system context. This is what makes the most
* sense because a user context does not have descendants, and only has system as a parent.
*
* @param context $context The context to start from.
* @param string $includes Defines what other contexts to find.
* Accepted values are:
* - children: All descendants
* - parents: All parents, grand parents, etc...
* - self: Context passed only.
* @param array $hasanycapability Array of capabilities passed to {@link has_any_capability()} in each context.
* @return context[] An array of contexts where keys are context IDs.
*/
public static function get_related_contexts($context, $includes, array $hasanycapability = null) {
global $DB;
static::require_enabled();
if (!in_array($includes, array('children', 'parents', 'self'))) {
throw new coding_exception('Invalid parameter value for \'includes\'.');
}
// If context user swap it for the context_system.
if ($context->contextlevel == CONTEXT_USER) {
$context = context_system::instance();
}
$contexts = array($context->id => $context);
if ($includes == 'children') {
$params = array('coursecatlevel' => CONTEXT_COURSECAT, 'path' => $context->path . '/%');
$pathlike = $DB->sql_like('path', ':path');
$sql = "contextlevel = :coursecatlevel AND $pathlike";
$rs = $DB->get_recordset_select('context', $sql, $params);
foreach ($rs as $record) {
$ctxid = $record->id;
context_helper::preload_from_record($record);
$contexts[$ctxid] = context::instance_by_id($ctxid);
}
$rs->close();
} else if ($includes == 'parents') {
$children = $context->get_parent_contexts();
foreach ($children as $ctx) {
$contexts[$ctx->id] = $ctx;
}
}
// Filter according to the capabilities required.
if (!empty($hasanycapability)) {
foreach ($contexts as $key => $ctx) {
if (!has_any_capability($hasanycapability, $ctx)) {
unset($contexts[$key]);
}
}
}
return $contexts;
}
/**
* Count all the courses using a competency.
*
* @param int $competencyid The id of the competency to check.
* @return int
*/
public static function count_courses_using_competency($competencyid) {
static::require_enabled();
// OK - all set.
$courses = course_competency::list_courses_min($competencyid);
$count = 0;
// Now check permissions on each course.
foreach ($courses as $course) {
if (!self::validate_course($course, false)) {
continue;
}
$context = context_course::instance($course->id);
$capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
if (!has_any_capability($capabilities, $context)) {
continue;
}
$count++;
}
return $count;
}
/**
* List all the courses modules using a competency in a course.
*
* @param int $competencyid The id of the competency to check.
* @param int $courseid The id of the course to check.
* @return array[int] Array of course modules ids.
*/
public static function list_course_modules_using_competency($competencyid, $courseid) {
static::require_enabled();
$result = array();
self::validate_course($courseid);
$coursecontext = context_course::instance($courseid);
// We will not check each module - course permissions should be enough.
$capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
if (!has_any_capability($capabilities, $coursecontext)) {
throw new required_capability_exception($coursecontext, 'moodle/competency:coursecompetencyview', 'nopermissions', '');
}
$cmlist = course_module_competency::list_course_modules($competencyid, $courseid);
foreach ($cmlist as $cmid) {
if (self::validate_course_module($cmid, false)) {
array_push($result, $cmid);
}
}
return $result;
}
/**
* List all the competencies linked to a course module.
*
* @param mixed $cmorid The course module, or its ID.
* @return array[competency] Array of competency records.
*/
public static function list_course_module_competencies_in_course_module($cmorid) {
static::require_enabled();
$cm = $cmorid;
if (!is_object($cmorid)) {
$cm = get_coursemodule_from_id('', $cmorid, 0, true, MUST_EXIST);
}