MDL-49461 tool_lp: Competencies can be marked with course completion
[moodle.git] / admin / tool / lp / tests / api_test.php
CommitLineData
f610a957
FM
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/**
18 * API tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28use tool_lp\api;
29
30/**
31 * API tests.
32 *
33 * @package tool_lp
34 * @copyright 2015 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_api_testcase extends advanced_testcase {
38
39 public function test_get_framework_related_contexts() {
40 $this->resetAfterTest(true);
41 $dg = $this->getDataGenerator();
42 $cat1 = $dg->create_category();
43 $cat2 = $dg->create_category(array('parent' => $cat1->id));
44 $cat3 = $dg->create_category(array('parent' => $cat2->id));
f0da26a4 45 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
f610a957
FM
46
47 $cat1ctx = context_coursecat::instance($cat1->id);
48 $cat2ctx = context_coursecat::instance($cat2->id);
49 $cat3ctx = context_coursecat::instance($cat3->id);
50 $sysctx = context_system::instance();
51
52 $expected = array($cat1ctx->id => $cat1ctx);
f0da26a4 53 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'self'));
f610a957
FM
54
55 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
f0da26a4 56 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children'));
f610a957
FM
57
58 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
f0da26a4 59 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents'));
f610a957
FM
60 }
61
62 public function test_get_framework_related_contexts_with_capabilities() {
63 $this->resetAfterTest(true);
64 $dg = $this->getDataGenerator();
65 $user = $dg->create_user();
66 $cat1 = $dg->create_category();
67 $cat2 = $dg->create_category(array('parent' => $cat1->id));
68 $cat3 = $dg->create_category(array('parent' => $cat2->id));
f0da26a4 69 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
f610a957
FM
70
71 $cat1ctx = context_coursecat::instance($cat1->id);
72 $cat2ctx = context_coursecat::instance($cat2->id);
73 $cat3ctx = context_coursecat::instance($cat3->id);
74 $sysctx = context_system::instance();
75
76 $roleallow = create_role('Allow', 'allow', 'Allow read');
77 assign_capability('tool/lp:competencyread', CAP_ALLOW, $roleallow, $sysctx->id);
78 role_assign($roleallow, $user->id, $sysctx->id);
79
80 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
81 assign_capability('tool/lp:competencyread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
82 role_assign($roleprevent, $user->id, $cat2ctx->id);
83
84 accesslib_clear_all_caches_for_unit_testing();
85 $this->setUser($user);
86 $this->assertFalse(has_capability('tool/lp:competencyread', $cat2ctx));
87
88 $requiredcap = array('tool/lp:competencyread');
89
90 $expected = array();
f0da26a4 91 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'self', $requiredcap));
f610a957
FM
92
93 $expected = array($cat1ctx->id => $cat1ctx);
f0da26a4 94 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children', $requiredcap));
f610a957
FM
95
96 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
f0da26a4 97 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents', $requiredcap));
f610a957 98 }
f0da26a4
FM
99
100 public function test_get_template_related_contexts() {
101 $this->resetAfterTest(true);
102 $dg = $this->getDataGenerator();
103 $cat1 = $dg->create_category();
104 $cat2 = $dg->create_category(array('parent' => $cat1->id));
105 $cat3 = $dg->create_category(array('parent' => $cat2->id));
106 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
107
108 $cat1ctx = context_coursecat::instance($cat1->id);
109 $cat2ctx = context_coursecat::instance($cat2->id);
110 $cat3ctx = context_coursecat::instance($cat3->id);
111 $sysctx = context_system::instance();
112
113 $expected = array($cat1ctx->id => $cat1ctx);
114 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'self'));
115
116 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
117 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children'));
118
119 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
120 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents'));
121 }
122
123 public function test_get_template_related_contexts_with_capabilities() {
124 $this->resetAfterTest(true);
125 $dg = $this->getDataGenerator();
126 $user = $dg->create_user();
127 $cat1 = $dg->create_category();
128 $cat2 = $dg->create_category(array('parent' => $cat1->id));
129 $cat3 = $dg->create_category(array('parent' => $cat2->id));
130 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
131
132 $cat1ctx = context_coursecat::instance($cat1->id);
133 $cat2ctx = context_coursecat::instance($cat2->id);
134 $cat3ctx = context_coursecat::instance($cat3->id);
135 $sysctx = context_system::instance();
136
137 $roleallow = create_role('Allow', 'allow', 'Allow read');
138 assign_capability('tool/lp:templateread', CAP_ALLOW, $roleallow, $sysctx->id);
139 role_assign($roleallow, $user->id, $sysctx->id);
140
141 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
142 assign_capability('tool/lp:templateread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
143 role_assign($roleprevent, $user->id, $cat2ctx->id);
144
145 accesslib_clear_all_caches_for_unit_testing();
146 $this->setUser($user);
147 $this->assertFalse(has_capability('tool/lp:templateread', $cat2ctx));
148
149 $requiredcap = array('tool/lp:templateread');
150
151 $expected = array();
152 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'self', $requiredcap));
153
154 $expected = array($cat1ctx->id => $cat1ctx);
155 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children', $requiredcap));
156
157 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
158 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents', $requiredcap));
159 }
160
ec324dc6
FM
161 /**
162 * Test updating a template.
163 */
164 public function test_update_template() {
165 $cat = $this->getDataGenerator()->create_category();
166 $this->resetAfterTest(true);
167 $this->setAdminUser();
168
169 $syscontext = context_system::instance();
170 $template = api::create_template((object) array('shortname' => 'testing', 'contextid' => $syscontext->id));
171
172 $this->assertEquals('testing', $template->get_shortname());
173 $this->assertEquals($syscontext->id, $template->get_contextid());
174
175 // Simple update.
176 api::update_template((object) array('id' => $template->get_id(), 'shortname' => 'success'));
177 $template = api::read_template($template->get_id());
178 $this->assertEquals('success', $template->get_shortname());
179
180 // Trying to change the context.
181 $this->setExpectedException('coding_exception');
182 api::update_template((object) array('id' => $template->get_id(), 'contextid' => context_coursecat::instance($cat->id)));
183 }
184
b3979696
IT
185 /**
186 * Test listing framework with order param.
187 */
188 public function test_list_frameworks() {
189 $this->resetAfterTest(true);
190 $this->setAdminUser();
bf61cd0c 191 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
b3979696
IT
192
193 // Create a list of frameworks.
bf61cd0c
FM
194 $framework1 = $lpg->create_framework(array(
195 'shortname' => 'shortname_a',
196 'idnumber' => 'idnumber_c',
197 'description' => 'description',
198 'descriptionformat' => FORMAT_HTML,
199 'visible' => true,
200 'contextid' => context_system::instance()->id
201 ));
202
203 $framework2 = $lpg->create_framework(array(
204 'shortname' => 'shortname_b',
205 'idnumber' => 'idnumber_a',
206 'description' => 'description',
207 'descriptionformat' => FORMAT_HTML,
208 'visible' => true,
209 'contextid' => context_system::instance()->id
210 ));
211
212 $framework3 = $lpg->create_framework(array(
213 'shortname' => 'shortname_c',
214 'idnumber' => 'idnumber_b',
215 'description' => 'description',
216 'descriptionformat' => FORMAT_HTML,
217 'visible' => true,
218 'contextid' => context_system::instance()->id
219 ));
b3979696
IT
220
221 // Get frameworks list order by shortname desc.
222 $result = api::list_frameworks('shortname', 'DESC', null, 3, context_system::instance());
223
1896274f
FM
224 $f = (object) array_shift($result);
225 $this->assertEquals($framework3->get_id(), $f->get_id());
226 $f = (object) array_shift($result);
227 $this->assertEquals($framework2->get_id(), $f->get_id());
228 $f = (object) array_shift($result);
229 $this->assertEquals($framework1->get_id(), $f->get_id());
b3979696
IT
230
231 // Get frameworks list order by idnumber asc.
232 $result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance());
233
1896274f
FM
234 $f = (object) array_shift($result);
235 $this->assertEquals($framework2->get_id(), $f->get_id());
236 $f = (object) array_shift($result);
237 $this->assertEquals($framework3->get_id(), $f->get_id());
238 $f = (object) array_shift($result);
239 $this->assertEquals($framework1->get_id(), $f->get_id());
b3979696
IT
240 }
241
c61db7bb
IT
242 /**
243 * Test duplicate a framework.
244 */
245 public function test_duplicate_framework() {
246 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
247 $this->resetAfterTest(true);
248 $this->setAdminUser();
249
250 $syscontext = context_system::instance();
251 $params = array(
252 'shortname' => 'shortname_a',
253 'idnumber' => 'idnumber_c',
254 'description' => 'description',
255 'descriptionformat' => FORMAT_HTML,
256 'visible' => true,
257 'contextid' => $syscontext->id
258 );
259 $framework = $lpg->create_framework($params);
260 $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
261 $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
262 $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
263 $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
264 $competencyidnumbers = array($competency1->get_idnumber(),
265 $competency2->get_idnumber(),
266 $competency3->get_idnumber(),
267 $competency4->get_idnumber()
268 );
269
270 api::add_related_competency($competency1->get_id(), $competency2->get_id());
271 api::add_related_competency($competency3->get_id(), $competency4->get_id());
272
273 $frameworkduplicated1 = api::duplicate_framework($framework->get_id());
274 $frameworkduplicated2 = api::duplicate_framework($framework->get_id());
275
276 $this->assertEquals($framework->get_idnumber().'_1', $frameworkduplicated1->get_idnumber());
277 $this->assertEquals($framework->get_idnumber().'_2', $frameworkduplicated2->get_idnumber());
278
279 $competenciesfr1 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated1->get_id()));
280 $competenciesfr2 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated2->get_id()));
281
282 $competencyidsfr1 = array();
283 $competencyidsfr2 = array();
284
285 foreach ($competenciesfr1 as $cmp) {
286 $competencyidsfr1[] = $cmp->get_idnumber();
287 }
288 foreach ($competenciesfr2 as $cmp) {
289 $competencyidsfr2[] = $cmp->get_idnumber();
290 }
291
292 $this->assertEmpty(array_diff($competencyidsfr1, $competencyidnumbers));
293 $this->assertEmpty(array_diff($competencyidsfr2, $competencyidnumbers));
294 $this->assertCount(4, $competenciesfr1);
295 $this->assertCount(4, $competenciesfr2);
296
297 // Test the related competencies.
298 reset($competenciesfr1);
299 $compduplicated1 = current($competenciesfr1);
300 $relatedcompetencies = $compduplicated1->get_related_competencies();
301 $comprelated = current($relatedcompetencies);
302 $this->assertEquals($comprelated->get_idnumber(), $competency2->get_idnumber());
303 }
304
58405003
IT
305 /**
306 * Test update plan.
307 */
308 public function test_update_plan() {
309 $this->resetAfterTest(true);
310 $dg = $this->getDataGenerator();
311 $usermanageowndraft = $dg->create_user();
312 $usermanageown = $dg->create_user();
313 $usermanagedraft = $dg->create_user();
314 $usermanage = $dg->create_user();
315
316 $syscontext = context_system::instance();
317
318 // Creating specific roles.
319 $manageowndraftrole = $dg->create_role(array(
320 'name' => 'User manage own draft',
321 'shortname' => 'manage-own-draft'
322 ));
323 $manageownrole = $dg->create_role(array(
324 'name' => 'User manage own',
325 'shortname' => 'manage-own'
326 ));
327 $managedraftrole = $dg->create_role(array(
328 'name' => 'User manage draft',
329 'shortname' => 'manage-draft'
330 ));
331 $managerole = $dg->create_role(array(
332 'name' => 'User manage',
333 'shortname' => 'manage'
334 ));
335
336 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
337 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
338
339 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
340 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
341
342 assign_capability('tool/lp:planmanagedraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
343 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
344
345 assign_capability('tool/lp:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
346 assign_capability('tool/lp:planview', CAP_ALLOW, $managerole, $syscontext->id);
347
348 $dg->role_assign($manageowndraftrole, $usermanageowndraft->id, $syscontext->id);
349 $dg->role_assign($manageownrole, $usermanageown->id, $syscontext->id);
350 $dg->role_assign($managedraftrole, $usermanagedraft->id, $syscontext->id);
351 $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
352
353 // Create first learning plan with user create draft.
354 $this->setUser($usermanageowndraft);
355 $plan = array (
356 'name' => 'plan own draft',
357 'description' => 'plan own draft',
358 'userid' => $usermanageowndraft->id
359 );
360 $plan = api::create_plan((object)$plan);
361 $record = $plan->to_record();
362 $record->name = 'plan own draft modified';
363
364 // Check if user create draft can edit the plan name.
365 $plan = api::update_plan($record);
366 $this->assertInstanceOf('\tool_lp\plan', $plan);
367
368 // Thrown exception when manageowndraft user try to change the status.
369 $record->status = \tool_lp\plan::STATUS_ACTIVE;
370 try {
371 $plan = api::update_plan($record);
372 $this->fail('User with manage own draft capability cannot edit the plan status.');
373 } catch (required_capability_exception $e) {
374 $this->assertTrue(true);
375 }
376
377 // Test when user with manage own plan capability try to edit other user plan.
378 $record->status = \tool_lp\plan::STATUS_DRAFT;
379 $record->name = 'plan create draft modified 2';
380 $this->setUser($usermanageown);
381 try {
382 $plan = api::update_plan($record);
383 $this->fail('User with manage own plan capability can only edit his own plan.');
384 } catch (required_capability_exception $e) {
385 $this->assertTrue(true);
386 }
387
388 // User with manage plan capability cannot edit the other user plans with status draft.
389 $this->setUser($usermanage);
390 $record->status = \tool_lp\plan::STATUS_COMPLETE;
391 try {
392 $plan = api::update_plan($record);
393 $this->fail('User with manage plan capability cannot edit the other user plans with status draft');
394 } catch (required_capability_exception $e) {
395 $this->assertTrue(true);
396 }
397
398 // User with manage draft capability can edit other user's learning plan if the status is draft.
399 $this->setUser($usermanagedraft);
400 $record->status = \tool_lp\plan::STATUS_DRAFT;
401 $record->name = 'plan manage draft modified 3';
402 $plan = api::update_plan($record);
403 $this->assertInstanceOf('\tool_lp\plan', $plan);
404
405 // User with manage plan capability can create/edit learning plan if status is active/complete.
406 $this->setUser($usermanage);
407 $plan = array (
408 'name' => 'plan create',
409 'description' => 'plan create',
410 'userid' => $usermanage->id,
411 'status' => \tool_lp\plan::STATUS_ACTIVE
412 );
413 $plan = api::create_plan((object)$plan);
5bfab685
FM
414
415 // Silently transition to complete status to avoid errors about transitioning to complete.
416 $plan->set_status(\tool_lp\plan::STATUS_COMPLETE);
417 $plan->update();
418
58405003
IT
419 $record = $plan->to_record();
420 $record->name = 'plan create own modified';
d805cc37
IT
421 try {
422 api::update_plan($record);
423 $this->fail('Completed plan can not be edited');
424 } catch (coding_exception $e) {
425 $this->assertTrue(true);
426 }
58405003
IT
427 }
428
2388d46d
FM
429 public function test_create_plan_from_template() {
430 $this->resetAfterTest(true);
431 $this->setAdminUser();
432
433 $u1 = $this->getDataGenerator()->create_user();
434 $tpl = $this->getDataGenerator()->get_plugin_generator('tool_lp')->create_template();
435
436 // Creating a new plan.
437 $plan = api::create_plan_from_template($tpl, $u1->id);
438 $record = $plan->to_record();
439 $this->assertInstanceOf('\tool_lp\plan', $plan);
440 $this->assertTrue(\tool_lp\plan::record_exists($plan->get_id()));
441 $this->assertEquals($tpl->get_id(), $plan->get_templateid());
442 $this->assertEquals($u1->id, $plan->get_userid());
443 $this->assertTrue($plan->is_based_on_template());
444
445 // Creating a plan that already exists.
446 $plan = api::create_plan_from_template($tpl, $u1->id);
447 $this->assertFalse($plan);
448
449 // Check that api::create_plan cannot be used.
450 $this->setExpectedException('coding_exception');
451 unset($record->id);
452 $plan = api::create_plan($record);
453 }
454
4de877eb
FM
455 public function test_update_plan_based_on_template() {
456 $this->resetAfterTest(true);
457 $dg = $this->getDataGenerator();
458 $lpg = $dg->get_plugin_generator('tool_lp');
459 $u1 = $dg->create_user();
460 $u2 = $dg->create_user();
461
462 $this->setAdminUser();
463 $tpl1 = $lpg->create_template();
464 $tpl2 = $lpg->create_template();
465 $up1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id()));
466 $up2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => null));
467
468 try {
469 // Trying to remove the template dependency.
470 $record = $up1->to_record();
471 $record->templateid = null;
472 api::update_plan($record);
473 $this->fail('A plan cannot be unlinked using api::update_plan()');
474 } catch (coding_exception $e) {
475 }
476
477 try {
478 // Trying to switch to another template.
479 $record = $up1->to_record();
480 $record->templateid = $tpl2->get_id();
481 api::update_plan($record);
482 $this->fail('A plan cannot be moved to another template.');
483 } catch (coding_exception $e) {
484 }
485
486 try {
487 // Trying to switch to using a template.
488 $record = $up2->to_record();
489 $record->templateid = $tpl1->get_id();
490 api::update_plan($record);
491 $this->fail('A plan cannot be update to use a template.');
492 } catch (coding_exception $e) {
493 }
494 }
495
6d2c2e86
FM
496 public function test_unlink_plan_from_template() {
497 $this->resetAfterTest(true);
498 $dg = $this->getDataGenerator();
499 $lpg = $dg->get_plugin_generator('tool_lp');
500 $u1 = $dg->create_user();
501 $u2 = $dg->create_user();
502
503 $this->setAdminUser();
504 $f1 = $lpg->create_framework();
505 $f2 = $lpg->create_framework();
506 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
507 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
508 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
509
510 $tpl1 = $lpg->create_template();
511 $tpl2 = $lpg->create_template();
512
513 $tplc1a = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1a->get_id(),
514 'sortorder' => 9));
515 $tplc1b = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1b->get_id(),
516 'sortorder' => 8));
517 $tplc2a = $lpg->create_template_competency(array('templateid' => $tpl2->get_id(), 'competencyid' => $c2a->get_id()));
518
519 $plan1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id()));
520 $plan2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => $tpl2->get_id()));
521
522 // Check that we have what we expect at this stage.
523 $this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
524 $this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
525 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
526 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan2->get_id())));
527 $this->assertTrue($plan1->is_based_on_template());
528 $this->assertTrue($plan2->is_based_on_template());
529
530 // Let's do this!
531 $tpl1comps = \tool_lp\template_competency::list_competencies($tpl1->get_id(), true);
532 $tpl2comps = \tool_lp\template_competency::list_competencies($tpl2->get_id(), true);
533
534 api::unlink_plan_from_template($plan1);
535
536 $plan1->read();
537 $plan2->read();
538 $this->assertCount(2, $tpl1comps);
539 $this->assertCount(1, $tpl2comps);
540 $this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
541 $this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
542 $this->assertEquals(2, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
543 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan2->get_id())));
544 $this->assertFalse($plan1->is_based_on_template());
545 $this->assertEquals($tpl1->get_id(), $plan1->get_origtemplateid());
546 $this->assertTrue($plan2->is_based_on_template());
547 $this->assertEquals(null, $plan2->get_origtemplateid());
548
549 // Even the order remains.
550 $plan1comps = \tool_lp\plan_competency::list_competencies($plan1->get_id());
551 $before = reset($tpl1comps);
552 $after = reset($plan1comps);
553 $this->assertEquals($before->get_id(), $after->get_id());
554 $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
555 $before = next($tpl1comps);
556 $after = next($plan1comps);
557 $this->assertEquals($before->get_id(), $after->get_id());
558 $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
559 }
560
c7999f6d
FM
561 public function test_update_template_updates_plans() {
562 $this->resetAfterTest(true);
563 $this->setAdminUser();
564
565 $dg = $this->getDataGenerator();
566 $u1 = $dg->create_user();
567 $u2 = $dg->create_user();
568 $lpg = $dg->get_plugin_generator('tool_lp');
569 $tpl1 = $lpg->create_template();
570 $tpl2 = $lpg->create_template();
571
572
573 // Create plans with data not matching templates.
574 $time = time();
575 $plan1 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u1->id,
576 'name' => 'Not good name', 'duedate' => $time + 3600, 'description' => 'Ahah', 'descriptionformat' => FORMAT_MARKDOWN));
577 $plan2 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u2->id,
578 'name' => 'Not right name', 'duedate' => $time + 3601, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
579 $plan3 = $lpg->create_plan(array('templateid' => $tpl2->get_id(), 'userid' => $u1->id,
580 'name' => 'Not sweet name', 'duedate' => $time + 3602, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
581
582 // Prepare our expectations.
583 $plan1->read();
584 $plan2->read();
585 $plan3->read();
586
587 $this->assertEquals($tpl1->get_id(), $plan1->get_templateid());
588 $this->assertEquals($tpl1->get_id(), $plan2->get_templateid());
589 $this->assertEquals($tpl2->get_id(), $plan3->get_templateid());
590 $this->assertNotEquals($tpl1->get_shortname(), $plan1->get_name());
591 $this->assertNotEquals($tpl1->get_shortname(), $plan2->get_name());
592 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
593 $this->assertNotEquals($tpl1->get_description(), $plan1->get_description());
594 $this->assertNotEquals($tpl1->get_description(), $plan2->get_description());
595 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
596 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
597 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
598 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
599 $this->assertNotEquals($tpl1->get_duedate(), $plan1->get_duedate());
600 $this->assertNotEquals($tpl1->get_duedate(), $plan2->get_duedate());
601 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
602
603 // Update the template without changing critical fields does not update the plans.
604 $data = $tpl1->to_record();
605 $data->visible = 0;
606 api::update_template($data);
607 $this->assertNotEquals($tpl1->get_shortname(), $plan1->get_name());
608 $this->assertNotEquals($tpl1->get_shortname(), $plan2->get_name());
609 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
610 $this->assertNotEquals($tpl1->get_description(), $plan1->get_description());
611 $this->assertNotEquals($tpl1->get_description(), $plan2->get_description());
612 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
613 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
614 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
615 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
616 $this->assertNotEquals($tpl1->get_duedate(), $plan1->get_duedate());
617 $this->assertNotEquals($tpl1->get_duedate(), $plan2->get_duedate());
618 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
619
620 // Now really update the template.
621 $data = $tpl1->to_record();
622 $data->shortname = 'Awesome!';
623 $data->description = 'This is too awesome!';
624 $data->descriptionformat = FORMAT_HTML;
625 $data->duedate = $time + 7200;
626 api::update_template($data);
627 $tpl1->read();
628
629 // Now confirm that the right plans were updated.
630 $plan1->read();
631 $plan2->read();
632 $plan3->read();
633
634 $this->assertEquals($tpl1->get_id(), $plan1->get_templateid());
635 $this->assertEquals($tpl1->get_id(), $plan2->get_templateid());
636 $this->assertEquals($tpl2->get_id(), $plan3->get_templateid());
637
638 $this->assertEquals($tpl1->get_shortname(), $plan1->get_name());
639 $this->assertEquals($tpl1->get_shortname(), $plan2->get_name());
640 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
641 $this->assertEquals($tpl1->get_description(), $plan1->get_description());
642 $this->assertEquals($tpl1->get_description(), $plan2->get_description());
643 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
644 $this->assertEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
645 $this->assertEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
646 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
647 $this->assertEquals($tpl1->get_duedate(), $plan1->get_duedate());
648 $this->assertEquals($tpl1->get_duedate(), $plan2->get_duedate());
649 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
650 }
651
5bfab685
FM
652 /**
653 * Test that the method to complete a plan.
654 */
655 public function test_complete_plan() {
656 global $DB;
657
658 $this->resetAfterTest(true);
659 $this->setAdminUser();
660 $dg = $this->getDataGenerator();
661 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
662 $user = $dg->create_user();
663
664 // Create a framework and assign competencies.
665 $framework = $lpg->create_framework();
666 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
667 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
668 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
669
670 // Create two plans and assign competencies.
671 $plan = $lpg->create_plan(array('userid' => $user->id));
672 $otherplan = $lpg->create_plan(array('userid' => $user->id));
673
674 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
675 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
676 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
677 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
678
679 $uclist = array(
680 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
681 'proficiency' => true, 'grade' => 1 )),
682 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
683 'proficiency' => false, 'grade' => 2 ))
684 );
685
686 $this->assertEquals(2, \tool_lp\user_competency::count_records());
687 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
688
689 // Change status of the plan to complete.
690 api::complete_plan($plan);
691
692 // Check that user competencies are now in user_competency_plan objects and still in user_competency.
693 $this->assertEquals(2, \tool_lp\user_competency::count_records());
694 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
695
696 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
697
698 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
699 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
700 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
701 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
702 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
703
704 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
705 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
706 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
707 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
708 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
709
710 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
711 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
712 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
713 $this->assertNull($usercompetenciesplan[2]->get_grade());
714 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
715
716 // Completing a plan that is completed throws an exception.
717 $this->setExpectedException('coding_exception');
718 api::complete_plan($plan);
719 }
720
192569ed
JPG
721 /**
722 * Test update plan and the managing of archived user competencies.
723 */
724 public function test_update_plan_manage_archived_competencies() {
725 global $DB;
726
727 $this->resetAfterTest(true);
728 $dg = $this->getDataGenerator();
729 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
730
731 $syscontext = context_system::instance();
732
733 // Create users and roles for the test.
734 $user = $dg->create_user();
735 $manageownrole = $dg->create_role(array(
736 'name' => 'User manage own',
737 'shortname' => 'manageown'
738 ));
739 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
740 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
741 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
742 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
743 $dg->role_assign($manageownrole, $user->id, $syscontext->id);
744 $this->setUser($user);
745
746 // Create a framework and assign competencies.
747 $framework = $lpg->create_framework();
748 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
749 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
750 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
751
752 // Create two plans and assign competencies.
753 $plan = $lpg->create_plan(array('userid' => $user->id));
754 $otherplan = $lpg->create_plan(array('userid' => $user->id));
755
756 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
757 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
758 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
759 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
760
761 $uclist = array(
762 $lpg->create_user_competency(array(
763 'userid' => $user->id,
764 'competencyid' => $c1->get_id(),
765 'proficiency' => true,
766 'grade' => 1
767 )),
768 $lpg->create_user_competency(array(
769 'userid' => $user->id,
770 'competencyid' => $c2->get_id(),
771 'proficiency' => false,
772 'grade' => 2
773 ))
774 );
775
776 // Change status of the plan to complete.
777 $record = $plan->to_record();
778 $record->status = \tool_lp\plan::STATUS_COMPLETE;
779
5bfab685
FM
780 try {
781 $plan = api::update_plan($record);
782 $this->fail('We cannot complete a plan using api::update_plan().');
783 } catch (coding_exception $e) {
784 }
785 api::complete_plan($plan);
192569ed
JPG
786
787 // Check that user compretencies are now in user_competency_plan objects and still in user_competency.
788 $this->assertEquals(2, \tool_lp\user_competency::count_records());
789 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
790
791 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
792
793 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
794 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
795 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
796 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
797 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
798
799 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
800 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
801 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
802 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
803 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
804
805 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
806 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
807 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
808 $this->assertNull($usercompetenciesplan[2]->get_grade());
809 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
810
192569ed
JPG
811 // Change status of the plan to active.
812 $record = $plan->to_record();
813 $record->status = \tool_lp\plan::STATUS_ACTIVE;
814
d805cc37
IT
815 try {
816 api::update_plan($record);
817 $this->fail('Completed plan can not be edited');
818 } catch (coding_exception $e) {
819 }
192569ed 820
d805cc37 821 api::reopen_plan($record->id);
192569ed
JPG
822 // Check that user_competency_plan objects are deleted if the plan status is changed to another status.
823 $this->assertEquals(2, \tool_lp\user_competency::count_records());
824 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
825 }
826
827 /**
828 * Test remove plan and the managing of archived user competencies.
829 */
830 public function test_delete_plan_manage_archived_competencies() {
831 $this->resetAfterTest(true);
832 $dg = $this->getDataGenerator();
833 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
834
835 $syscontext = context_system::instance();
836
837 // Create user and role for the test.
838 $user = $dg->create_user();
839 $managerole = $dg->create_role(array(
840 'name' => 'User manage own',
841 'shortname' => 'manageown'
842 ));
843 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $managerole, $syscontext->id);
844 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $managerole, $syscontext->id);
845 $dg->role_assign($managerole, $user->id, $syscontext->id);
846 $this->setUser($user);
847
848 // Create a framework and assign competencies.
849 $framework = $lpg->create_framework();
850 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
851 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
852 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
853
854 // Create completed plan with records in user_competency.
855 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
856
857 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
858 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
859
860 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
861 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
862
863 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
864 'planid' => $completedplan->get_id()));
865 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
866 'planid' => $completedplan->get_id()));
867
868 api::delete_plan($completedplan->get_id());
869
870 // Check that achived user competencies are deleted.
871 $this->assertEquals(0, \tool_lp\plan::count_records());
872 $this->assertEquals(2, \tool_lp\user_competency::count_records());
873 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
874 }
875
876 /**
877 * Test listing of plan competencies.
878 */
879 public function test_list_plan_competencies_manage_archived_competencies() {
880 $this->resetAfterTest(true);
881 $dg = $this->getDataGenerator();
882 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
883
884 $syscontext = context_system::instance();
885
886 // Create user and role for the test.
887 $user = $dg->create_user();
888 $viewrole = $dg->create_role(array(
889 'name' => 'User view',
890 'shortname' => 'view'
891 ));
892 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $viewrole, $syscontext->id);
893 assign_capability('tool/lp:planview', CAP_ALLOW, $viewrole, $syscontext->id);
894 $dg->role_assign($viewrole, $user->id, $syscontext->id);
895 $this->setUser($user);
896
897 // Create a framework and assign competencies.
898 $framework = $lpg->create_framework();
899 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
900 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
901 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
902
903 // Create draft plan with records in user_competency.
904 $draftplan = $lpg->create_plan(array('userid' => $user->id));
905
906 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c1->get_id()));
907 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c2->get_id()));
908 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c3->get_id()));
909
910 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
911 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
912
913 // Check that user_competency objects are returned when plan status is not complete.
914 $plancompetencies = api::list_plan_competencies($draftplan);
915
916 $this->assertCount(3, $plancompetencies);
917 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[0]->usercompetency);
918 $this->assertEquals($uc1->get_id(), $plancompetencies[0]->usercompetency->get_id());
919 $this->assertNull($plancompetencies[0]->usercompetencyplan);
920
921 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[1]->usercompetency);
922 $this->assertEquals($uc2->get_id(), $plancompetencies[1]->usercompetency->get_id());
923 $this->assertNull($plancompetencies[1]->usercompetencyplan);
924
925 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[2]->usercompetency);
926 $this->assertEquals(0, $plancompetencies[2]->usercompetency->get_id());
927 $this->assertNull($plancompetencies[2]->usercompetencyplan);
928
929 // Create completed plan with records in user_competency_plan.
930 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
931
932 $pc1 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
933 $pc2 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
934 $pc3 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c3->get_id()));
935
936 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
937 'planid' => $completedplan->get_id()));
938 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
939 'planid' => $completedplan->get_id()));
940
941 // Check that an exception is thrown when a user competency plan is missing.
942 try {
943 $plancompetencies = api::list_plan_competencies($completedplan);
944 $this->fail('All competencies in the plan must be associated to a user competency plan');
945 } catch (coding_exception $e) {
946 $this->assertTrue(true);
947 }
948
949 $ucp3 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c3->get_id(),
950 'planid' => $completedplan->get_id()));
951
952 // Check that user_competency_plan objects are returned when plan status is complete.
953 $plancompetencies = api::list_plan_competencies($completedplan);
954
955 $this->assertCount(3, $plancompetencies);
956 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[0]->usercompetencyplan);
957 $this->assertEquals($ucp1->get_id(), $plancompetencies[0]->usercompetencyplan->get_id());
958 $this->assertNull($plancompetencies[0]->usercompetency);
959 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[1]->usercompetencyplan);
960 $this->assertEquals($ucp2->get_id(), $plancompetencies[1]->usercompetencyplan->get_id());
961 $this->assertNull($plancompetencies[1]->usercompetency);
962 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[2]->usercompetencyplan);
963 $this->assertEquals($ucp3->get_id(), $plancompetencies[2]->usercompetencyplan->get_id());
964 $this->assertNull($plancompetencies[2]->usercompetency);
965 }
966
bee480a4
FM
967 public function test_create_template_cohort() {
968 $this->resetAfterTest(true);
969 $this->setAdminUser();
970
971 $dg = $this->getDataGenerator();
972 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
973
974 $c1 = $dg->create_cohort();
975 $c2 = $dg->create_cohort();
976 $t1 = $lpg->create_template();
977 $t2 = $lpg->create_template();
978
979 $this->assertEquals(0, \tool_lp\template_cohort::count_records());
980
981 // Create two relations with mixed parameters.
982 $result = api::create_template_cohort($t1->get_id(), $c1->id);
983 $result = api::create_template_cohort($t1, $c2);
984
985 $this->assertEquals(2, \tool_lp\template_cohort::count_records());
986 $this->assertInstanceOf('tool_lp\template_cohort', $result);
987 $this->assertEquals($c2->id, $result->get_cohortid());
988 $this->assertEquals($t1->get_id(), $result->get_templateid());
989 $this->assertEquals(2, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
990 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
991 }
992
993 public function test_delete_template_cohort() {
994 $this->resetAfterTest(true);
995 $this->setAdminUser();
996
997 $dg = $this->getDataGenerator();
998 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
999
1000 $c1 = $dg->create_cohort();
1001 $c2 = $dg->create_cohort();
1002 $t1 = $lpg->create_template();
1003 $t2 = $lpg->create_template();
1004 $tc1 = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
1005 $tc1 = $lpg->create_template_cohort(array('templateid' => $t2->get_id(), 'cohortid' => $c2->id));
1006
1007 $this->assertEquals(2, \tool_lp\template_cohort::count_records());
1008 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1009 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1010
1011 // Delete existing.
1012 $result = api::delete_template_cohort($t1->get_id(), $c1->id);
1013 $this->assertTrue($result);
1014 $this->assertEquals(1, \tool_lp\template_cohort::count_records());
1015 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1016 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1017
1018 // Delete non-existant.
1019 $result = api::delete_template_cohort($t1->get_id(), $c1->id);
1020 $this->assertTrue($result);
1021 $this->assertEquals(1, \tool_lp\template_cohort::count_records());
1022 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1023 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1024 }
4c0e8167 1025
914b580e
FM
1026 public function test_add_evidence_log() {
1027 $this->resetAfterTest(true);
1028 $dg = $this->getDataGenerator();
1029 $lpg = $dg->get_plugin_generator('tool_lp');
1030
1031 $u1 = $dg->create_user();
1032 $u1ctx = context_user::instance($u1->id);
1033 $f1 = $lpg->create_framework();
1034 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1035 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1036
1037 // Creating a standard evidence with minimal information.
1038 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata', 'error');
1039 $evidence->read();
1040 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1041 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1042 $this->assertSame(null, $uc->get_grade());
1043 $this->assertSame(null, $uc->get_proficiency());
1044 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1045 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1046 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $evidence->get_action());
1047 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1048 $this->assertEquals('error', $evidence->get_desccomponent());
1049 $this->assertSame(null, $evidence->get_desca());
1050 $this->assertSame(null, $evidence->get_url());
1051 $this->assertSame(null, $evidence->get_grade());
1052 $this->assertSame(null, $evidence->get_actionuserid());
1053
1054 // Creating a standard evidence with more information.
1055 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata', 'error',
1056 '$a', false, 'http://moodle.org', null, 2);
1057 $evidence->read();
1058 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1059 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1060 $this->assertSame(null, $uc->get_grade());
1061 $this->assertSame(null, $uc->get_proficiency());
1062 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1063 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1064 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $evidence->get_action());
1065 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1066 $this->assertEquals('error', $evidence->get_desccomponent());
1067 $this->assertEquals('$a', $evidence->get_desca());
1068 $this->assertEquals('http://moodle.org', $evidence->get_url());
1069 $this->assertSame(null, $evidence->get_grade());
1070 $this->assertEquals(2, $evidence->get_actionuserid());
1071
1072 // Creating a standard evidence and send for review.
1073 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
1074 'error', null, true);
1075 $evidence->read();
1076 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1077 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1078
1079 // Trying to pass a grade should fail.
1080 try {
1081 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata', 'error',
1082 null, false, null, 1);
1083 $this->fail('A grade can not be set');
1084 } catch (coding_exception $e) {
1085 $this->assertRegExp('/grade MUST NOT be set/', $e->getMessage());
1086 }
1087 }
1088
1089 public function test_add_evidence_suggest() {
1090 $this->resetAfterTest(true);
1091 $dg = $this->getDataGenerator();
1092 $lpg = $dg->get_plugin_generator('tool_lp');
1093
1094 $u1 = $dg->create_user();
1095 $u1ctx = context_user::instance($u1->id);
1096 $f1 = $lpg->create_framework();
1097 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1098 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1099
1100 // Creating an evidence with minimal information.
1101 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_SUGGEST, 'invaliddata',
1102 'error', null, false, null, 1, 2);
1103 $evidence->read();
1104 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1105 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1106 $this->assertSame(null, $uc->get_grade()); // We don't grade, we just suggest.
1107 $this->assertSame(null, $uc->get_proficiency());
1108 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1109 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1110 $this->assertEquals(\tool_lp\evidence::ACTION_SUGGEST, $evidence->get_action());
1111 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1112 $this->assertEquals('error', $evidence->get_desccomponent());
1113 $this->assertSame(null, $evidence->get_desca());
1114 $this->assertSame(null, $evidence->get_url());
1115 $this->assertEquals(1, $evidence->get_grade());
1116 $this->assertEquals(2, $evidence->get_actionuserid());
1117
1118 // Creating a standard evidence and send for review.
1119 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_SUGGEST, 'invaliddata',
1120 'error', null, true, null, 1, 2);
1121 $evidence->read();
1122 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1123 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1124
1125 // Trying not to pass a grade should fail.
1126 try {
1127 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_SUGGEST, 'invaliddata', 'error',
1128 false, null);
1129 $this->fail('A grade must be set');
1130 } catch (coding_exception $e) {
1131 $this->assertRegExp('/grade MUST be set/', $e->getMessage());
1132 }
1133 }
1134
1135 public function test_add_evidence_complete() {
1136 $this->resetAfterTest(true);
1137 $dg = $this->getDataGenerator();
1138 $lpg = $dg->get_plugin_generator('tool_lp');
1139
1140 $u1 = $dg->create_user();
1141 $u1ctx = context_user::instance($u1->id);
1142 $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1143 $scaleconfig = array(array('scaleid' => $scale->id));
1144 $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1145 $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1146 $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1147 $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1148 $c2scaleconfig = array(array('scaleid' => $scale->id));
1149 $c2scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1150 $c2scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 0, 'proficient' => 1);
1151 $c2scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 0);
1152 $c2scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 1, 'proficient' => 1);
1153 $f1 = $lpg->create_framework(array('scaleid' => $scale->id, 'scaleconfiguration' => $scaleconfig));
1154 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1155 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'scaleid' => $scale->id,
1156 'scaleconfiguration' => $c2scaleconfig));
1157 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1158
1159 // Creating an evidence with minimal information.
1160 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1161 'error');
1162 $evidence->read();
1163 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1164 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1165 $this->assertEquals(2, $uc->get_grade()); // The grade has been set automatically to the framework default.
1166 $this->assertEquals(0, $uc->get_proficiency());
1167 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1168 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1169 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $evidence->get_action());
1170 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1171 $this->assertEquals('error', $evidence->get_desccomponent());
1172 $this->assertSame(null, $evidence->get_desca());
1173 $this->assertSame(null, $evidence->get_url());
1174 $this->assertEquals(2, $evidence->get_grade());
1175 $this->assertSame(null, $evidence->get_actionuserid());
1176
1177 // Creating an evidence complete on competency with custom scale.
1178 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1179 'error');
1180 $evidence->read();
1181 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1182 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1183 $this->assertEquals(4, $uc->get_grade()); // The grade has been set automatically to the competency default.
1184 $this->assertEquals(true, $uc->get_proficiency());
1185 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1186 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1187 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $evidence->get_action());
1188 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1189 $this->assertEquals('error', $evidence->get_desccomponent());
1190 $this->assertSame(null, $evidence->get_desca());
1191 $this->assertSame(null, $evidence->get_url());
1192 $this->assertEquals(4, $evidence->get_grade());
1193 $this->assertSame(null, $evidence->get_actionuserid());
1194
1195 // Creating an evidence complete on a user competency with an existing grade.
1196 $uc = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get_id(), 'grade' => 1,
1197 'proficiency' => 0));
1198 $this->assertEquals(1, $uc->get_grade());
1199 $this->assertEquals(0, $uc->get_proficiency());
1200 $evidence = api::add_evidence($u1->id, $c3->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1201 'error');
1202 $evidence->read();
1203 $uc->read();
1204 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1205 $this->assertEquals(1, $uc->get_grade()); // The grade has not been changed.
1206 $this->assertEquals(0, $uc->get_proficiency());
1207 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1208 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1209 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $evidence->get_action());
1210 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1211 $this->assertEquals('error', $evidence->get_desccomponent());
1212 $this->assertSame(null, $evidence->get_desca());
1213 $this->assertSame(null, $evidence->get_url());
1214 $this->assertEquals(2, $evidence->get_grade()); // The complete grade has been set.
1215 $this->assertSame(null, $evidence->get_actionuserid());
1216
1217 // Creating a standard evidence and send for review.
1218 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1219 'error', null, true);
1220 $evidence->read();
1221 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1222 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1223
1224 // Trying to pass a grade should throw an exception.
1225 try {
1226 api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1227 'error', null, false, null, 1);
1228 } catch (coding_exception $e) {
1229 $this->assertRegExp('/grade MUST NOT be set/', $e->getMessage());
1230 }
1231 }
1232
1233 public function test_add_evidence_override() {
1234 $this->resetAfterTest(true);
1235 $dg = $this->getDataGenerator();
1236 $lpg = $dg->get_plugin_generator('tool_lp');
1237
1238 $u1 = $dg->create_user();
1239 $u1ctx = context_user::instance($u1->id);
1240 $f1 = $lpg->create_framework();
1241 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1242
1243 // Creating an evidence with minimal information.
1244 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1245 'error');
1246 $evidence->read();
1247 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1248 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1249 $this->assertSame(null, $uc->get_grade()); // We overrode with 'null'.
1250 $this->assertSame(null, $uc->get_proficiency());
1251 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1252 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1253 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1254 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1255 $this->assertEquals('error', $evidence->get_desccomponent());
1256 $this->assertSame(null, $evidence->get_desca());
1257 $this->assertSame(null, $evidence->get_url());
1258 $this->assertSame(null, $evidence->get_grade()); // We overrode with 'null'.
1259 $this->assertSame(null, $evidence->get_actionuserid());
1260
1261 // Creating an evidence with a grade information.
1262 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1263 'error', null, false, null, 3);
1264 $evidence->read();
1265 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1266 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1267 $this->assertEquals(3, $uc->get_grade());
1268 $this->assertEquals(true, $uc->get_proficiency());
1269 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1270 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1271 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1272 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1273 $this->assertEquals('error', $evidence->get_desccomponent());
1274 $this->assertSame(null, $evidence->get_desca());
1275 $this->assertSame(null, $evidence->get_url());
1276 $this->assertEquals(3, $evidence->get_grade());
1277 $this->assertSame(null, $evidence->get_actionuserid());
1278
1279 // Creating an evidence with another grade information.
1280 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1281 'error', null, false, null, 1);
1282 $evidence->read();
1283 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1284 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1285 $this->assertEquals(1, $uc->get_grade());
1286 $this->assertEquals(0, $uc->get_proficiency());
1287 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1288 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1289 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1290 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1291 $this->assertEquals('error', $evidence->get_desccomponent());
1292 $this->assertSame(null, $evidence->get_desca());
1293 $this->assertSame(null, $evidence->get_url());
1294 $this->assertEquals(1, $evidence->get_grade());
1295 $this->assertSame(null, $evidence->get_actionuserid());
1296
1297 // Creating reverting the grade and send for review.
1298 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1299 'error', null, true);
1300 $evidence->read();
1301 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1302 $this->assertSame(null, $uc->get_grade());
1303 $this->assertSame(null, $uc->get_proficiency());
1304 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1305 $this->assertSame(null, $evidence->get_grade());
1306 }
1307
1308 public function test_add_evidence_and_send_for_review() {
1309 $this->resetAfterTest(true);
1310 $dg = $this->getDataGenerator();
1311 $lpg = $dg->get_plugin_generator('tool_lp');
1312
1313 $u1 = $dg->create_user();
1314 $u1ctx = context_user::instance($u1->id);
1315 $f1 = $lpg->create_framework();
1316 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1317
1318 // Non-existing user competencies are created up for review.
1319 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
1320 'error', null, true);
1321 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1322 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1323
1324 // Existing user competencies sent for review don't change.
1325 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
1326 'error', null, true);
1327 $uc->read();
1328 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1329
1330 // A user competency with a status non-idle won't change.
1331 $uc->set_status(\tool_lp\user_competency::STATUS_IN_REVIEW);
1332 $uc->update();
1333 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
1334 'error', null, true);
1335 $uc->read();
1336 $this->assertEquals(\tool_lp\user_competency::STATUS_IN_REVIEW, $uc->get_status());
1337 }
1338
4c0e8167
SG
1339 /**
1340 * Test add evidence for existing user_competency.
1341 */
1342 public function test_add_evidence_existing_user_competency() {
1343 $this->resetAfterTest(true);
1344 $dg = $this->getDataGenerator();
1345 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1346
1347 $syscontext = context_system::instance();
1348
1349 // Create users.
1350 $user = $dg->create_user();
1351 $this->setUser($user);
1352
1353 // Create a framework and assign competencies.
1354 $framework = $lpg->create_framework();
1355 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1356 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1357 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1358 $uc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
914b580e
FM
1359 $this->assertSame(null, $uc->get_grade());
1360 $this->assertSame(null, $uc->get_proficiency());
4c0e8167
SG
1361
1362 // Create an evidence and check it was created with the right usercomptencyid and information.
914b580e
FM
1363 $evidence = api::add_evidence($user->id, $c1->get_id(), $syscontext->id, \tool_lp\evidence::ACTION_OVERRIDE,
1364 'invalidevidencedesc', 'tool_lp', array('a' => 'b'), false, 'http://moodle.org', 1, 2);
4c0e8167
SG
1365 $this->assertEquals(1, \tool_lp\evidence::count_records());
1366
1367 $evidence->read();
914b580e 1368 $uc->read();
4c0e8167
SG
1369 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1370 $this->assertEquals('invalidevidencedesc', $evidence->get_descidentifier());
1371 $this->assertEquals('tool_lp', $evidence->get_desccomponent());
1372 $this->assertEquals((object) array('a' => 'b'), $evidence->get_desca());
914b580e
FM
1373 $this->assertEquals('http://moodle.org', $evidence->get_url());
1374 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1375 $this->assertEquals(2, $evidence->get_actionuserid());
4c0e8167 1376 $this->assertEquals(1, $evidence->get_grade());
914b580e
FM
1377 $this->assertEquals(1, $uc->get_grade());
1378 $this->assertEquals(0, $uc->get_proficiency());
4c0e8167
SG
1379 }
1380
1381 /**
914b580e 1382 * Test add evidence for non-existing user_competency.
4c0e8167
SG
1383 */
1384 public function test_add_evidence_no_existing_user_competency() {
1385 $this->resetAfterTest(true);
1386 $dg = $this->getDataGenerator();
1387 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1388
1389 $syscontext = context_system::instance();
1390
1391 // Create users.
1392 $user = $dg->create_user();
1393 $this->setUser($user);
1394
1395 // Create a framework and assign competencies.
1396 $framework = $lpg->create_framework();
1397 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1398 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
914b580e 1399 $this->assertEquals(0, \tool_lp\user_competency::count_records());
4c0e8167 1400
914b580e
FM
1401 // Create an evidence without a user competency record.
1402 $evidence = api::add_evidence($user->id, $c1->get_id(), $syscontext->id, \tool_lp\evidence::ACTION_OVERRIDE,
1403 'invalidevidencedesc', 'tool_lp', 'Hello world!', false, 'http://moodle.org', 1, 2);
4c0e8167 1404 $this->assertEquals(1, \tool_lp\evidence::count_records());
914b580e 1405 $this->assertEquals(1, \tool_lp\user_competency::count_records());
4c0e8167 1406
914b580e
FM
1407 $uc = \tool_lp\user_competency::get_record(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
1408 $evidence->read();
1409 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1410 $this->assertEquals('invalidevidencedesc', $evidence->get_descidentifier());
1411 $this->assertEquals('tool_lp', $evidence->get_desccomponent());
1412 $this->assertEquals('Hello world!', $evidence->get_desca());
1413 $this->assertEquals('http://moodle.org', $evidence->get_url());
1414 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1415 $this->assertEquals(2, $evidence->get_actionuserid());
1416 $this->assertEquals(1, $evidence->get_grade());
1417 $this->assertEquals(1, $uc->get_grade());
1418 $this->assertEquals(0, $uc->get_proficiency());
1419 }
1420
1421 public function test_observe_course_completed() {
1422 $this->resetAfterTest(true);
1423 $dg = $this->getDataGenerator();
1424 $lpg = $dg->get_plugin_generator('tool_lp');
1425
1426 // Set-up users, framework, competencies and course competencies.
1427 $course = $dg->create_course();
1428 $coursectx = context_course::instance($course->id);
1429 $u1 = $dg->create_user();
1430 $f1 = $lpg->create_framework();
1431 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1432 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1433 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1434 $c4 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1435 $cc1 = $lpg->create_course_competency(array('competencyid' => $c1->get_id(), 'courseid' => $course->id,
1436 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_NONE));
1437 $cc2 = $lpg->create_course_competency(array('competencyid' => $c2->get_id(), 'courseid' => $course->id,
1438 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_EVIDENCE));
1439 $cc3 = $lpg->create_course_competency(array('competencyid' => $c3->get_id(), 'courseid' => $course->id,
1440 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_RECOMMEND));
1441 $cc4 = $lpg->create_course_competency(array('competencyid' => $c4->get_id(), 'courseid' => $course->id,
1442 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_COMPLETE));
1443
1444 $event = \core\event\course_completed::create(array(
1445 'objectid' => 1,
1446 'relateduserid' => $u1->id,
1447 'context' => $coursectx,
1448 'courseid' => $course->id,
1449 'other' => array('relateduserid' => $u1->id)
1450 ));
1451 $this->assertEquals(0, \tool_lp\user_competency::count_records());
1452 $this->assertEquals(0, \tool_lp\evidence::count_records());
1453
1454 // Let's go!
1455 api::observe_course_completed($event);
1456 $this->assertEquals(3, \tool_lp\user_competency::count_records());
1457 $this->assertEquals(3, \tool_lp\evidence::count_records());
1458
1459 // Outcome NONE did nothing.
1460 $this->assertFalse(\tool_lp\user_competency::record_exists_select('userid = :uid AND competencyid = :cid', array(
1461 'uid' => $u1->id, 'cid' => $c1->get_id()
1462 )));
1463
1464 // Outcome evidence.
1465 $uc2 = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1466 $ev2 = \tool_lp\evidence::get_record(array('usercompetencyid' => $uc2->get_id()));
1467
1468 $this->assertEquals(null, $uc2->get_grade());
1469 $this->assertEquals(null, $uc2->get_proficiency());
1470 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc2->get_status());
1471
1472 $this->assertEquals('evidence_coursecompleted', $ev2->get_descidentifier());
1473 $this->assertEquals('tool_lp', $ev2->get_desccomponent());
1474 $this->assertEquals($course->shortname, $ev2->get_desca());
1475 $this->assertStringEndsWith('/report/completion/index.php?course=' . $course->id, $ev2->get_url());
1476 $this->assertEquals(null, $ev2->get_grade());
1477 $this->assertEquals($coursectx->id, $ev2->get_contextid());
1478 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $ev2->get_action());
1479 $this->assertEquals(null, $ev2->get_actionuserid());
1480
1481 // Outcome recommend.
1482 $uc3 = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c3->get_id()));
1483 $ev3 = \tool_lp\evidence::get_record(array('usercompetencyid' => $uc3->get_id()));
1484
1485 $this->assertEquals(null, $uc3->get_grade());
1486 $this->assertEquals(null, $uc3->get_proficiency());
1487 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc3->get_status());
1488
1489 $this->assertEquals('evidence_coursecompleted', $ev3->get_descidentifier());
1490 $this->assertEquals('tool_lp', $ev3->get_desccomponent());
1491 $this->assertEquals($course->shortname, $ev3->get_desca());
1492 $this->assertStringEndsWith('/report/completion/index.php?course=' . $course->id, $ev3->get_url());
1493 $this->assertEquals(null, $ev3->get_grade());
1494 $this->assertEquals($coursectx->id, $ev3->get_contextid());
1495 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $ev3->get_action());
1496 $this->assertEquals(null, $ev3->get_actionuserid());
1497
1498 // Outcome complete.
1499 $uc4 = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c4->get_id()));
1500 $ev4 = \tool_lp\evidence::get_record(array('usercompetencyid' => $uc4->get_id()));
1501
1502 $this->assertEquals(3, $uc4->get_grade());
1503 $this->assertEquals(1, $uc4->get_proficiency());
1504 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc4->get_status());
1505
1506 $this->assertEquals('evidence_coursecompleted', $ev4->get_descidentifier());
1507 $this->assertEquals('tool_lp', $ev4->get_desccomponent());
1508 $this->assertEquals($course->shortname, $ev4->get_desca());
1509 $this->assertStringEndsWith('/report/completion/index.php?course=' . $course->id, $ev4->get_url());
1510 $this->assertEquals(3, $ev4->get_grade());
1511 $this->assertEquals($coursectx->id, $ev4->get_contextid());
1512 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $ev4->get_action());
1513 $this->assertEquals(null, $ev4->get_actionuserid());
4c0e8167 1514 }
d4c0a2f6
SG
1515
1516 /**
1517 * Test update ruleoutcome for course_competency.
1518 */
d660824b 1519 public function test_set_ruleoutcome_course_competency() {
d4c0a2f6
SG
1520 $this->resetAfterTest(true);
1521 $dg = $this->getDataGenerator();
1522 $lpg = $dg->get_plugin_generator('tool_lp');
1523 $u1 = $dg->create_user();
1524 $u2 = $dg->create_user();
d660824b 1525 $course = $dg->create_course();
d4c0a2f6
SG
1526
1527 $this->setAdminUser();
1528 $f = $lpg->create_framework();
1529 $c = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
d660824b 1530 $cc = api::add_competency_to_course($course->id, $c->get_id());
d4c0a2f6
SG
1531
1532 // Check record was created with default rule value Evidence.
1533 $this->assertEquals(1, \tool_lp\course_competency::count_records());
d660824b
FM
1534 $recordscc = api::list_course_competencies($course->id);
1535 $this->assertEquals(\tool_lp\course_competency::OUTCOME_EVIDENCE, $recordscc[0]['coursecompetency']->get_ruleoutcome());
d4c0a2f6
SG
1536
1537 // Check ruleoutcome value is updated to None.
d660824b
FM
1538 $this->assertTrue(api::set_course_competency_ruleoutcome($recordscc[0]['coursecompetency']->get_id(),
1539 \tool_lp\course_competency::OUTCOME_NONE));
1540 $recordscc = api::list_course_competencies($course->id);
1541 $this->assertEquals(\tool_lp\course_competency::OUTCOME_NONE, $recordscc[0]['coursecompetency']->get_ruleoutcome());
d4c0a2f6
SG
1542 }
1543
f610a957 1544}