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