1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
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.
|
|
|
8 |
//
|
|
|
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
use core_competency\competency;
|
|
|
18 |
use core_competency\competency_framework;
|
|
|
19 |
use core_competency\plan;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Behat data generator for core_competency.
|
|
|
23 |
*
|
|
|
24 |
* @package core_competency
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright 2022 Noel De Martin
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class behat_core_competency_generator extends behat_generator_base {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Get a list of the entities that Behat can create using the generator step.
|
|
|
33 |
*
|
|
|
34 |
* @return array
|
|
|
35 |
*/
|
|
|
36 |
protected function get_creatable_entities(): array {
|
|
|
37 |
return [
|
|
|
38 |
'competencies' => [
|
|
|
39 |
'singular' => 'competency',
|
|
|
40 |
'datagenerator' => 'competency',
|
|
|
41 |
'required' => ['shortname', 'competencyframework'],
|
|
|
42 |
'switchids' => ['competencyframework' => 'competencyframeworkid'],
|
|
|
43 |
],
|
|
|
44 |
'course_competencies' => [
|
|
|
45 |
'singular' => 'course_competency',
|
|
|
46 |
'datagenerator' => 'course_competency',
|
|
|
47 |
'required' => ['course', 'competency'],
|
|
|
48 |
'switchids' => ['course' => 'courseid', 'competency' => 'competencyid'],
|
|
|
49 |
],
|
|
|
50 |
'frameworks' => [
|
|
|
51 |
'singular' => 'framework',
|
|
|
52 |
'datagenerator' => 'framework',
|
|
|
53 |
'required' => ['shortname'],
|
|
|
54 |
'switchids' => ['scale' => 'scaleid'],
|
|
|
55 |
],
|
|
|
56 |
'plans' => [
|
|
|
57 |
'singular' => 'plan',
|
|
|
58 |
'datagenerator' => 'plan',
|
|
|
59 |
'required' => ['name'],
|
1441 |
ariadna |
60 |
'switchids' => ['user' => 'userid', 'status' => 'status'],
|
1 |
efrain |
61 |
],
|
1441 |
ariadna |
62 |
'plan_competencies' => [
|
|
|
63 |
'singular' => 'plan_competency',
|
|
|
64 |
'datagenerator' => 'plan_competency',
|
|
|
65 |
'required' => ['plan', 'competency'],
|
|
|
66 |
'switchids' => ['competency' => 'competencyid', 'plan' => 'planid'],
|
|
|
67 |
],
|
1 |
efrain |
68 |
'related_competencies' => [
|
|
|
69 |
'singular' => 'related_competency',
|
|
|
70 |
'datagenerator' => 'related_competency',
|
|
|
71 |
'required' => ['competency', 'relatedcompetency'],
|
|
|
72 |
'switchids' => ['competency' => 'competencyid', 'relatedcompetency' => 'relatedcompetencyid'],
|
|
|
73 |
],
|
|
|
74 |
'user_competency' => [
|
|
|
75 |
'singular' => 'user_competency',
|
|
|
76 |
'datagenerator' => 'user_competency',
|
|
|
77 |
'required' => ['competency', 'user'],
|
|
|
78 |
'switchids' => ['competency' => 'competencyid', 'user' => 'userid'],
|
|
|
79 |
],
|
|
|
80 |
'user_competency_courses' => [
|
|
|
81 |
'singular' => 'user_competency_course',
|
|
|
82 |
'datagenerator' => 'user_competency_course',
|
|
|
83 |
'required' => ['course', 'competency', 'user'],
|
|
|
84 |
'switchids' => ['course' => 'courseid', 'competency' => 'competencyid', 'user' => 'userid'],
|
|
|
85 |
],
|
|
|
86 |
'user_competency_plans' => [
|
|
|
87 |
'singular' => 'user_competency_plan',
|
|
|
88 |
'datagenerator' => 'user_competency_plan',
|
|
|
89 |
'required' => ['plan', 'competency', 'user'],
|
|
|
90 |
'switchids' => ['plan' => 'planid', 'competency' => 'competencyid', 'user' => 'userid'],
|
|
|
91 |
],
|
1441 |
ariadna |
92 |
'user_evidence' => [
|
|
|
93 |
'singular' => 'user_evidence',
|
|
|
94 |
'datagenerator' => 'user_evidence',
|
|
|
95 |
'required' => ['user', 'name'],
|
|
|
96 |
'switchids' => ['user' => 'userid'],
|
|
|
97 |
],
|
|
|
98 |
'user_evidence_competency' => [
|
|
|
99 |
'singular' => 'user_evidence_competency',
|
|
|
100 |
'datagenerator' => 'user_evidence_competency',
|
|
|
101 |
'required' => ['userevidence', 'competency'],
|
|
|
102 |
'switchids' => ['userevidence' => 'userevidenceid', 'competency' => 'competencyid'],
|
|
|
103 |
],
|
|
|
104 |
'templates' => [
|
|
|
105 |
'singular' => 'template',
|
|
|
106 |
'datagenerator' => 'template',
|
|
|
107 |
'required' => ['shortname'],
|
|
|
108 |
'switchids' => ['context' => 'contextid'],
|
|
|
109 |
],
|
|
|
110 |
'template_competencies' => [
|
|
|
111 |
'singular' => 'template_competency',
|
|
|
112 |
'datagenerator' => 'template_competency',
|
|
|
113 |
'required' => ['template', 'competency'],
|
|
|
114 |
'switchids' => ['template' => 'templateid', 'competency' => 'competencyid'],
|
|
|
115 |
],
|
1 |
efrain |
116 |
];
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
1441 |
ariadna |
120 |
* Get the competency framework id using an idnumber.
|
1 |
efrain |
121 |
*
|
|
|
122 |
* @param string $idnumber
|
1441 |
ariadna |
123 |
* @return int The competency framework id
|
1 |
efrain |
124 |
*/
|
|
|
125 |
protected function get_competencyframework_id(string $idnumber): int {
|
|
|
126 |
global $DB;
|
|
|
127 |
|
|
|
128 |
if (!$id = $DB->get_field('competency_framework', 'id', ['idnumber' => $idnumber])) {
|
|
|
129 |
throw new Exception('The specified competency framework with idnumber "' . $idnumber . '" could not be found.');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
return $id;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
1441 |
ariadna |
136 |
* Get the competency id using an idnumber.
|
1 |
efrain |
137 |
*
|
|
|
138 |
* @param string $idnumber
|
1441 |
ariadna |
139 |
* @return int The competency id
|
1 |
efrain |
140 |
*/
|
|
|
141 |
protected function get_competency_id(string $idnumber): int {
|
|
|
142 |
global $DB;
|
|
|
143 |
|
|
|
144 |
if (!$id = $DB->get_field('competency', 'id', ['idnumber' => $idnumber])) {
|
|
|
145 |
throw new Exception('The specified competency with idnumber "' . $idnumber . '" could not be found.');
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
return $id;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Get the learning plan id using a name.
|
|
|
153 |
*
|
|
|
154 |
* @param string $name
|
|
|
155 |
* @return int The learning plan id
|
|
|
156 |
*/
|
|
|
157 |
protected function get_plan_id(string $name): int {
|
|
|
158 |
global $DB;
|
|
|
159 |
|
|
|
160 |
if (!$id = $DB->get_field('competency_plan', 'id', ['name' => $name])) {
|
|
|
161 |
throw new Exception('The specified learning plan with name "' . $name . '" could not be found.');
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
return $id;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
1441 |
ariadna |
168 |
* Get the related competency id using an idnumber.
|
1 |
efrain |
169 |
*
|
|
|
170 |
* @param string $idnumber
|
1441 |
ariadna |
171 |
* @return int The related competency id
|
1 |
efrain |
172 |
*/
|
|
|
173 |
protected function get_relatedcompetency_id(string $idnumber): int {
|
|
|
174 |
return $this->get_competency_id($idnumber);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
1441 |
ariadna |
178 |
* Get the template id by shortname.
|
|
|
179 |
*
|
|
|
180 |
* @param string $shortname The template name.
|
|
|
181 |
* @return int
|
|
|
182 |
*/
|
|
|
183 |
protected function get_template_id(string $shortname): int {
|
|
|
184 |
global $DB;
|
|
|
185 |
|
|
|
186 |
if (!$id = $DB->get_field('competency_template', 'id', ['shortname' => $shortname])) {
|
|
|
187 |
throw new Exception('The specified template with name "' . $shortname . '" could not be found.');
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
return $id;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
1 |
efrain |
194 |
* Add a plan.
|
|
|
195 |
*
|
|
|
196 |
* @param array $data Plan data.
|
|
|
197 |
*/
|
|
|
198 |
public function process_plan(array $data): void {
|
|
|
199 |
$generator = $this->get_data_generator();
|
|
|
200 |
$competencyids = $data['competencyids'] ?? [];
|
|
|
201 |
|
|
|
202 |
unset($data['competencyids']);
|
|
|
203 |
|
|
|
204 |
$plan = $generator->create_plan($data);
|
|
|
205 |
|
|
|
206 |
foreach ($competencyids as $competencyid) {
|
|
|
207 |
$generator->create_plan_competency([
|
|
|
208 |
'planid' => $plan->get('id'),
|
|
|
209 |
'competencyid' => $competencyid,
|
|
|
210 |
]);
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Preprocess user competency data.
|
|
|
216 |
*
|
|
|
217 |
* @param array $data Raw data.
|
|
|
218 |
* @return array Processed data.
|
|
|
219 |
*/
|
|
|
220 |
protected function preprocess_user_competency(array $data): array {
|
|
|
221 |
$this->prepare_grading($data);
|
|
|
222 |
|
|
|
223 |
return $data;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Preprocess user course competency data.
|
|
|
228 |
*
|
|
|
229 |
* @param array $data Raw data.
|
|
|
230 |
* @return array Processed data.
|
|
|
231 |
*/
|
|
|
232 |
protected function preprocess_user_competency_course(array $data): array {
|
|
|
233 |
$this->prepare_grading($data);
|
|
|
234 |
|
|
|
235 |
return $data;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Preprocess user learning plan competency data.
|
|
|
240 |
*
|
|
|
241 |
* @param array $data Raw data.
|
|
|
242 |
* @return array Processed data.
|
|
|
243 |
*/
|
|
|
244 |
protected function preprocess_user_competency_plan(array $data): array {
|
|
|
245 |
$this->prepare_grading($data);
|
|
|
246 |
|
|
|
247 |
return $data;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Preprocess plan data.
|
|
|
252 |
*
|
|
|
253 |
* @param array $data Raw data.
|
|
|
254 |
* @return array Processed data.
|
|
|
255 |
*/
|
|
|
256 |
protected function preprocess_plan(array $data): array {
|
1441 |
ariadna |
257 |
global $DB, $USER;
|
|
|
258 |
|
1 |
efrain |
259 |
if (isset($data['competencies'])) {
|
1441 |
ariadna |
260 |
$competencies = array_map('trim', str_getcsv($data['competencies'], escape: '\\'));
|
1 |
efrain |
261 |
$data['competencyids'] = array_map([$this, 'get_competency_id'], $competencies);
|
|
|
262 |
|
|
|
263 |
unset($data['competencies']);
|
|
|
264 |
}
|
|
|
265 |
|
1441 |
ariadna |
266 |
if (isset($data['reviewer'])) {
|
|
|
267 |
if (is_number($data['reviewer'])) {
|
|
|
268 |
$data['reviewerid'] = $data['reviewer'];
|
|
|
269 |
} else {
|
|
|
270 |
if (!$userid = $DB->get_field('user', 'id', ['username' => $data['reviewer']])) {
|
|
|
271 |
throw new Exception('The specified user "' . $data['reviewer'] . '" could not be found.');
|
|
|
272 |
}
|
|
|
273 |
$data['reviewerid'] = $userid;
|
|
|
274 |
}
|
|
|
275 |
unset($data['reviewer']);
|
|
|
276 |
}
|
1 |
efrain |
277 |
|
|
|
278 |
return $data + [
|
|
|
279 |
'userid' => $USER->id,
|
|
|
280 |
];
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Prepare grading attributes for record data.
|
|
|
285 |
*
|
|
|
286 |
* @param array $data Record data.
|
|
|
287 |
*/
|
|
|
288 |
protected function prepare_grading(array &$data): void {
|
|
|
289 |
if (!isset($data['grade'])) {
|
|
|
290 |
return;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
global $DB;
|
|
|
294 |
|
|
|
295 |
$competency = competency::get_record(['id' => $data['competencyid']]);
|
|
|
296 |
$competencyframework = competency_framework::get_record(['id' => $competency->get('competencyframeworkid')]);
|
|
|
297 |
$scale = $DB->get_field('scale', 'scale', ['id' => $competencyframework->get('scaleid')]);
|
|
|
298 |
$grades = array_map('trim', explode(',', $scale));
|
|
|
299 |
$grade = array_search($data['grade'], $grades);
|
|
|
300 |
|
|
|
301 |
if ($grade === false) {
|
|
|
302 |
throw new Exception('The grade "'.$data['grade'].'" was not found in the "'.
|
|
|
303 |
$competencyframework->get('shortname').'" competency framework.');
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
$data['proficiency'] = true;
|
|
|
307 |
$data['grade'] = $grade + 1;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Get the module data generator.
|
|
|
312 |
*
|
|
|
313 |
* @return core_competency_generator Competency data generator.
|
|
|
314 |
*/
|
|
|
315 |
protected function get_data_generator(): core_competency_generator {
|
|
|
316 |
return $this->componentdatagenerator;
|
|
|
317 |
}
|
1441 |
ariadna |
318 |
|
|
|
319 |
/**
|
|
|
320 |
* Get the user evidence id using a name.
|
|
|
321 |
*
|
|
|
322 |
* @param string $name User evidence name.
|
|
|
323 |
* @return int The user evidence id
|
|
|
324 |
*/
|
|
|
325 |
protected function get_userevidence_id(string $name): int {
|
|
|
326 |
global $DB;
|
|
|
327 |
|
|
|
328 |
if (!$id = $DB->get_field('competency_userevidence', 'id', ['name' => $name])) {
|
|
|
329 |
throw new Exception('The specified user evidence with name "' . $name . '" could not be found.');
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
return $id;
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
/**
|
|
|
336 |
* Get the template competency id using a name.
|
|
|
337 |
*
|
|
|
338 |
* @param string $name Template competency name.
|
|
|
339 |
* @return int The template competency id
|
|
|
340 |
*/
|
|
|
341 |
protected function get_templatecompetency_id(string $name): int {
|
|
|
342 |
global $DB;
|
|
|
343 |
|
|
|
344 |
if (!$id = $DB->get_field('competency_template', 'id', ['name' => $name])) {
|
|
|
345 |
throw new Exception('The specified template competency with name "' . $name . '" could not be found.');
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
return $id;
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
/**
|
|
|
352 |
* Get the context id using a contextid.
|
|
|
353 |
*
|
|
|
354 |
* @param string $contextid Context id.
|
|
|
355 |
* @return int The context id
|
|
|
356 |
*/
|
|
|
357 |
protected function get_context_id(string $contextid): int {
|
|
|
358 |
global $DB;
|
|
|
359 |
|
|
|
360 |
if (!$id = $DB->get_field('context', 'id', ['id' => $contextid])) {
|
|
|
361 |
throw new Exception('The specified context with id "' . $contextid . '" could not be found.');
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
return $id;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* Get the status id by status name.
|
|
|
369 |
*
|
|
|
370 |
* @param string $name Status name.
|
|
|
371 |
* @return int
|
|
|
372 |
*/
|
|
|
373 |
protected function get_status_id(string $name): int {
|
|
|
374 |
|
|
|
375 |
switch ($name) {
|
|
|
376 |
case 'draft':
|
|
|
377 |
$status = plan::STATUS_DRAFT;
|
|
|
378 |
break;
|
|
|
379 |
case 'in review':
|
|
|
380 |
$status = plan::STATUS_IN_REVIEW;
|
|
|
381 |
break;
|
|
|
382 |
case 'waiting for review':
|
|
|
383 |
$status = plan::STATUS_WAITING_FOR_REVIEW;
|
|
|
384 |
break;
|
|
|
385 |
case 'complete':
|
|
|
386 |
$status = plan::STATUS_COMPLETE;
|
|
|
387 |
break;
|
|
|
388 |
default:
|
|
|
389 |
$status = plan::STATUS_ACTIVE;
|
|
|
390 |
break;
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
return $status;
|
|
|
394 |
}
|
1 |
efrain |
395 |
}
|