2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Competency data generator.
20 * @package core_competency
22 * @copyright 2015 Frédéric Massart - FMCorz.net
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 use core_competency\competency
;
27 use core_competency\competency_framework
;
28 use core_competency\course_competency
;
29 use core_competency\course_module_competency
;
30 use core_competency\evidence
;
31 use core_competency\external
;
32 use core_competency\plan
;
33 use core_competency\plan_competency
;
34 use core_competency\related_competency
;
35 use core_competency\template
;
36 use core_competency\template_cohort
;
37 use core_competency\template_competency
;
38 use core_competency\user_competency
;
39 use core_competency\user_competency_course
;
40 use core_competency\user_competency_plan
;
41 use core_competency\user_evidence
;
42 use core_competency\user_evidence_competency
;
45 defined('MOODLE_INTERNAL') ||
die();
48 require_once($CFG->libdir
. '/grade/grade_scale.php');
51 * Competency data generator class.
53 * @package core_competency
55 * @copyright 2015 Frédéric Massart - FMCorz.net
56 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
58 class core_competency_generator
extends component_generator_base
{
60 /** @var int Number of created competencies. */
61 protected $competencycount = 0;
63 /** @var int Number of created frameworks. */
64 protected $frameworkcount = 0;
66 /** @var int Number of created plans. */
67 protected $plancount = 0;
69 /** @var int Number of created templates. */
70 protected $templatecount = 0;
72 /** @var int Number of created user_evidence. */
73 protected $userevidencecount = 0;
75 /** @var stdClass Scale that we might need. */
81 * Do not call directly.
85 public function reset() {
86 $this->competencycount
= 0;
87 $this->frameworkcount
= 0;
92 * Create a new competency.
94 * @param array|stdClass $record
97 public function create_competency($record = null) {
98 $this->competencycount++
;
99 $i = $this->competencycount
;
100 $record = (object) $record;
102 if (!isset($record->competencyframeworkid
)) {
103 throw new coding_exception('The competencyframeworkid value is required.');
105 if (!isset($record->shortname
)) {
106 $record->shortname
= "Competency shortname $i";
108 if (!isset($record->idnumber
)) {
109 $record->idnumber
= "cmp{$i}";
111 if (!isset($record->description
)) {
112 $record->description
= "Competency $i description ";
114 if (!isset($record->descriptionformat
)) {
115 $record->descriptionformat
= FORMAT_HTML
;
117 if (!isset($record->scaleconfiguration
) && isset($record->scaleid
)) {
118 $record->scaleconfiguration
= json_encode($this->make_default_scale_configuration($record->scaleid
));
120 if (isset($record->scaleconfiguration
)
121 && (is_array($record->scaleconfiguration
) ||
is_object($record->scaleconfiguration
))) {
122 // Conveniently encode the config.
123 $record->scaleconfiguration
= json_encode($record->scaleconfiguration
);
126 $competency = new competency(0, $record);
127 $competency->create();
133 * Create a new framework.
135 * @param array|stdClass $record
136 * @return competency_framework
138 public function create_framework($record = null) {
139 $generator = phpunit_util
::get_data_generator();
140 $this->frameworkcount++
;
141 $i = $this->frameworkcount
;
142 $record = (object) $record;
144 if (!isset($record->shortname
)) {
145 $record->shortname
= "Framework shortname $i";
147 if (!isset($record->idnumber
)) {
148 $record->idnumber
= "frm{$i}";
150 if (!isset($record->description
)) {
151 $record->description
= "Framework $i description ";
153 if (!isset($record->descriptionformat
)) {
154 $record->descriptionformat
= FORMAT_HTML
;
156 if (!isset($record->visible
)) {
157 $record->visible
= 1;
159 if (!isset($record->scaleid
)) {
160 if (isset($record->scaleconfiguration
)) {
161 throw new coding_exception('Scale configuration must be provided with a scale.');
164 $this->scale
= $generator->create_scale(array('scale' => 'A,B,C,D'));
166 $record->scaleid
= $this->scale
->id
;
168 if (!isset($record->scaleconfiguration
)) {
169 $record->scaleconfiguration
= json_encode($this->make_default_scale_configuration($record->scaleid
));
171 if (is_array($record->scaleconfiguration
) ||
is_object($record->scaleconfiguration
)) {
172 // Conveniently encode the config.
173 $record->scaleconfiguration
= json_encode($record->scaleconfiguration
);
175 if (!isset($record->contextid
)) {
176 $record->contextid
= context_system
::instance()->id
;
179 $framework = new competency_framework(0, $record);
180 $framework->create();
186 * Create a related competency.
188 * @param array|stdClass $record
189 * @return related_competency
191 public function create_related_competency($record = null) {
192 $record = (object) $record;
194 if (!isset($record->competencyid
)) {
195 throw new coding_exception('Property competencyid is required.');
197 if (!isset($record->relatedcompetencyid
)) {
198 throw new coding_exception('Property relatedcompetencyid is required.');
201 $relation = related_competency
::get_relation($record->competencyid
, $record->relatedcompetencyid
);
202 if ($relation->get('id')) {
203 throw new coding_exception('Relation already exists');
213 * @param array|stdClass $record
216 public function create_template($record = null) {
217 $this->templatecount++
;
218 $i = $this->templatecount
;
219 $record = (object) $record;
221 if (!isset($record->shortname
)) {
222 $record->shortname
= "Template shortname $i";
224 if (!isset($record->description
)) {
225 $record->description
= "Template $i description ";
227 if (!isset($record->contextid
)) {
228 $record->contextid
= context_system
::instance()->id
;
231 $template = new template(0, $record);
238 * Create a template competency.
240 * @param array|stdClass $record
241 * @return template_competency
243 public function create_template_competency($record = null) {
244 $record = (object) $record;
246 if (!isset($record->competencyid
)) {
247 throw new coding_exception('Property competencyid is required.');
249 if (!isset($record->templateid
)) {
250 throw new coding_exception('Property templateid is required.');
253 $relation = new template_competency(0, $record);
260 * Create a new user competency.
262 * @param array|stdClass $record
263 * @return user_competency
265 public function create_user_competency($record = null) {
266 $record = (object) $record;
268 if (!isset($record->userid
)) {
269 throw new coding_exception('The userid value is required.');
271 if (!isset($record->competencyid
)) {
272 throw new coding_exception('The competencyid value is required.');
275 $usercompetency = new user_competency(0, $record);
276 $usercompetency->create();
278 return $usercompetency;
284 * @param array|stdClass $record
287 public function create_plan($record = null) {
289 $i = $this->plancount
;
290 $record = (object) $record;
292 if (!isset($record->name
)) {
293 $record->name
= "Plan shortname $i";
295 if (!isset($record->description
)) {
296 $record->description
= "Plan $i description";
298 if (!isset($record->descriptionformat
)) {
299 $record->descriptionformat
= FORMAT_HTML
;
301 if (!isset($record->userid
)) {
302 throw new coding_exception('The userid value is required.');
305 $plan = new plan(0, $record);
312 * Create a new user competency course.
314 * @param array|stdClass $record
315 * @return user_competency_course
317 public function create_user_competency_course($record = null) {
318 $record = (object) $record;
320 if (!isset($record->userid
)) {
321 throw new coding_exception('The userid value is required.');
323 if (!isset($record->competencyid
)) {
324 throw new coding_exception('The competencyid value is required.');
327 if (!isset($record->courseid
)) {
328 throw new coding_exception('The courseid value is required.');
331 $usercompetencycourse = new user_competency_course(0, $record);
332 $usercompetencycourse->create();
334 return $usercompetencycourse;
338 * Create a new user competency plan.
340 * @param array|stdClass $record
341 * @return user_competency_plan
343 public function create_user_competency_plan($record = null) {
344 $record = (object) $record;
346 if (!isset($record->userid
)) {
347 throw new coding_exception('The userid value is required.');
349 if (!isset($record->competencyid
)) {
350 throw new coding_exception('The competencyid value is required.');
353 if (!isset($record->planid
)) {
354 throw new coding_exception('The planid value is required.');
357 if (!isset($record->sortorder
)) {
358 $record->sortorder
= 0;
361 $usercompetencyplan = new user_competency_plan(0, $record);
362 $usercompetencyplan->create();
364 return $usercompetencyplan;
368 * Create a new plan competency.
370 * @param array|stdClass $record
371 * @return plan_competency
373 public function create_plan_competency($record = null) {
374 $record = (object) $record;
376 if (!isset($record->planid
)) {
377 throw new coding_exception('The planid value is required.');
379 if (!isset($record->competencyid
)) {
380 throw new coding_exception('The competencyid value is required.');
383 $plancompetency = new plan_competency(0, $record);
384 $plancompetency->create();
386 return $plancompetency;
390 * Create a new template cohort.
392 * @param array|stdClass $record
393 * @return template_cohort
395 public function create_template_cohort($record = null) {
396 $record = (object) $record;
398 if (!isset($record->templateid
)) {
399 throw new coding_exception('The templateid value is required.');
401 if (!isset($record->cohortid
)) {
402 throw new coding_exception('The cohortid value is required.');
405 $tplcohort = new template_cohort(0, $record);
406 $tplcohort->create();
412 * Create a new evidence.
414 * @param array|stdClass $record
417 public function create_evidence($record = null) {
418 $record = (object) $record;
420 if (!isset($record->usercompetencyid
)) {
421 throw new coding_exception('The usercompetencyid value is required.');
423 if (!isset($record->action
) && !isset($record->grade
)) {
424 $record->action
= evidence
::ACTION_LOG
;
426 if (!isset($record->action
)) {
427 throw new coding_exception('The action value is required with a grade.');
430 if (!isset($record->contextid
)) {
431 $record->contextid
= context_system
::instance()->id
;
433 if (!isset($record->descidentifier
)) {
434 $record->descidentifier
= 'invalidevidencedesc';
436 if (!isset($record->desccomponent
)) {
437 $record->desccomponent
= 'core_competency';
439 $evidence = new evidence(0, $record);
446 * Create a new course competency.
448 * @param array|stdClass $record
449 * @return user_competency
451 public function create_course_competency($record = null) {
452 $record = (object) $record;
454 if (!isset($record->courseid
)) {
455 throw new coding_exception('The courseid value is required.');
457 if (!isset($record->competencyid
)) {
458 throw new coding_exception('The competencyid value is required.');
461 $cc = new course_competency(0, $record);
468 * Create a new course module competency.
470 * @param array|stdClass $record
471 * @return course_module_competency
473 public function create_course_module_competency($record = null) {
474 $record = (object) $record;
476 if (!isset($record->cmid
)) {
477 throw new coding_exception('The cmid value is required.');
479 if (!isset($record->competencyid
)) {
480 throw new coding_exception('The competencyid value is required.');
483 $cc = new course_module_competency(0, $record);
490 * Create a new user_evidence.
492 * @param array|stdClass $record
495 public function create_user_evidence($record = null) {
496 $this->userevidencecount++
;
497 $i = $this->userevidencecount
;
498 $record = (object) $record;
500 if (!isset($record->userid
)) {
501 throw new coding_exception('The userid value is required.');
503 if (!isset($record->name
)) {
504 $record->name
= "Evidence $i name";
506 if (!isset($record->description
)) {
507 $record->description
= "Evidence $i description";
509 if (!isset($record->descriptionformat
)) {
510 $record->descriptionformat
= FORMAT_HTML
;
513 $ue = new user_evidence(0, $record);
520 * Create a new user_evidence_comp.
522 * @param array|stdClass $record
525 public function create_user_evidence_competency($record = null) {
526 $record = (object) $record;
528 if (!isset($record->userevidenceid
)) {
529 throw new coding_exception('The userevidenceid value is required.');
531 if (!isset($record->competencyid
)) {
532 throw new coding_exception('The competencyid value is required.');
535 $uec = new user_evidence_competency(0, $record);
542 * Make a default scale configuration.
544 * The last and second-last item will be flagged proficient. The
545 * second-last item will be flagged as default.
547 * @param int $scaleid The scale ID.
548 * @return array Configuration as array.
550 protected function make_default_scale_configuration($scaleid) {
551 $scale = grade_scale
::fetch(array('id' => $scaleid));
552 $values = $scale->load_items();
554 foreach ($values as $key => $value) {
555 // Add a key (make the first value 1).
556 $values[$key] = array('id' => $key +
1, 'name' => $value);
559 if (count($values) < 2) {
560 throw new coding_exception('Please provide the scale configuration for one-item scales.');
563 $scaleconfig = array();
565 // Last item is proficient.
566 $item = array_pop($values);
567 array_unshift($scaleconfig, array(
572 // Second-last item is default and proficient.
573 $item = array_pop($values);
574 array_unshift($scaleconfig, array(
581 array_unshift($scaleconfig, array('scaleid' => $scaleid));