MDL-52534 course: Course module form can be extended by plugins via callbacks.
[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;
20c2fe3e 29use tool_lp\competency;
90c7bdaf 30use tool_lp\competency_framework;
9373acf6 31use tool_lp\evidence;
4de456cd 32use tool_lp\user_competency;
a8902ee2 33use tool_lp\plan;
f610a957
FM
34
35/**
36 * API tests.
37 *
38 * @package tool_lp
39 * @copyright 2015 Frédéric Massart - FMCorz.net
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class tool_lp_api_testcase extends advanced_testcase {
43
44 public function test_get_framework_related_contexts() {
45 $this->resetAfterTest(true);
46 $dg = $this->getDataGenerator();
47 $cat1 = $dg->create_category();
48 $cat2 = $dg->create_category(array('parent' => $cat1->id));
49 $cat3 = $dg->create_category(array('parent' => $cat2->id));
f0da26a4 50 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
f610a957
FM
51
52 $cat1ctx = context_coursecat::instance($cat1->id);
53 $cat2ctx = context_coursecat::instance($cat2->id);
54 $cat3ctx = context_coursecat::instance($cat3->id);
55 $sysctx = context_system::instance();
56
57 $expected = array($cat1ctx->id => $cat1ctx);
f0da26a4 58 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'self'));
f610a957
FM
59
60 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
f0da26a4 61 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children'));
f610a957
FM
62
63 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
f0da26a4 64 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents'));
f610a957
FM
65 }
66
67 public function test_get_framework_related_contexts_with_capabilities() {
68 $this->resetAfterTest(true);
69 $dg = $this->getDataGenerator();
70 $user = $dg->create_user();
71 $cat1 = $dg->create_category();
72 $cat2 = $dg->create_category(array('parent' => $cat1->id));
73 $cat3 = $dg->create_category(array('parent' => $cat2->id));
f0da26a4 74 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
f610a957
FM
75
76 $cat1ctx = context_coursecat::instance($cat1->id);
77 $cat2ctx = context_coursecat::instance($cat2->id);
78 $cat3ctx = context_coursecat::instance($cat3->id);
79 $sysctx = context_system::instance();
80
81 $roleallow = create_role('Allow', 'allow', 'Allow read');
82 assign_capability('tool/lp:competencyread', CAP_ALLOW, $roleallow, $sysctx->id);
83 role_assign($roleallow, $user->id, $sysctx->id);
84
85 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
86 assign_capability('tool/lp:competencyread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
87 role_assign($roleprevent, $user->id, $cat2ctx->id);
88
89 accesslib_clear_all_caches_for_unit_testing();
90 $this->setUser($user);
91 $this->assertFalse(has_capability('tool/lp:competencyread', $cat2ctx));
92
93 $requiredcap = array('tool/lp:competencyread');
94
95 $expected = array();
f0da26a4 96 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'self', $requiredcap));
f610a957
FM
97
98 $expected = array($cat1ctx->id => $cat1ctx);
f0da26a4 99 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children', $requiredcap));
f610a957
FM
100
101 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
f0da26a4 102 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents', $requiredcap));
f610a957 103 }
f0da26a4
FM
104
105 public function test_get_template_related_contexts() {
106 $this->resetAfterTest(true);
107 $dg = $this->getDataGenerator();
108 $cat1 = $dg->create_category();
109 $cat2 = $dg->create_category(array('parent' => $cat1->id));
110 $cat3 = $dg->create_category(array('parent' => $cat2->id));
111 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
112
113 $cat1ctx = context_coursecat::instance($cat1->id);
114 $cat2ctx = context_coursecat::instance($cat2->id);
115 $cat3ctx = context_coursecat::instance($cat3->id);
116 $sysctx = context_system::instance();
117
118 $expected = array($cat1ctx->id => $cat1ctx);
119 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'self'));
120
121 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
122 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children'));
123
124 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
125 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents'));
126 }
127
128 public function test_get_template_related_contexts_with_capabilities() {
129 $this->resetAfterTest(true);
130 $dg = $this->getDataGenerator();
131 $user = $dg->create_user();
132 $cat1 = $dg->create_category();
133 $cat2 = $dg->create_category(array('parent' => $cat1->id));
134 $cat3 = $dg->create_category(array('parent' => $cat2->id));
135 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
136
137 $cat1ctx = context_coursecat::instance($cat1->id);
138 $cat2ctx = context_coursecat::instance($cat2->id);
139 $cat3ctx = context_coursecat::instance($cat3->id);
140 $sysctx = context_system::instance();
141
142 $roleallow = create_role('Allow', 'allow', 'Allow read');
143 assign_capability('tool/lp:templateread', CAP_ALLOW, $roleallow, $sysctx->id);
144 role_assign($roleallow, $user->id, $sysctx->id);
145
146 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
147 assign_capability('tool/lp:templateread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
148 role_assign($roleprevent, $user->id, $cat2ctx->id);
149
150 accesslib_clear_all_caches_for_unit_testing();
151 $this->setUser($user);
152 $this->assertFalse(has_capability('tool/lp:templateread', $cat2ctx));
153
154 $requiredcap = array('tool/lp:templateread');
155
156 $expected = array();
157 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'self', $requiredcap));
158
159 $expected = array($cat1ctx->id => $cat1ctx);
160 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children', $requiredcap));
161
162 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
163 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents', $requiredcap));
164 }
165
ec324dc6
FM
166 /**
167 * Test updating a template.
168 */
169 public function test_update_template() {
170 $cat = $this->getDataGenerator()->create_category();
171 $this->resetAfterTest(true);
172 $this->setAdminUser();
173
174 $syscontext = context_system::instance();
175 $template = api::create_template((object) array('shortname' => 'testing', 'contextid' => $syscontext->id));
176
177 $this->assertEquals('testing', $template->get_shortname());
178 $this->assertEquals($syscontext->id, $template->get_contextid());
179
180 // Simple update.
181 api::update_template((object) array('id' => $template->get_id(), 'shortname' => 'success'));
182 $template = api::read_template($template->get_id());
183 $this->assertEquals('success', $template->get_shortname());
184
185 // Trying to change the context.
186 $this->setExpectedException('coding_exception');
187 api::update_template((object) array('id' => $template->get_id(), 'contextid' => context_coursecat::instance($cat->id)));
188 }
189
b3979696
IT
190 /**
191 * Test listing framework with order param.
192 */
193 public function test_list_frameworks() {
194 $this->resetAfterTest(true);
195 $this->setAdminUser();
bf61cd0c 196 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
b3979696
IT
197
198 // Create a list of frameworks.
bf61cd0c
FM
199 $framework1 = $lpg->create_framework(array(
200 'shortname' => 'shortname_a',
201 'idnumber' => 'idnumber_c',
202 'description' => 'description',
203 'descriptionformat' => FORMAT_HTML,
204 'visible' => true,
205 'contextid' => context_system::instance()->id
206 ));
207
208 $framework2 = $lpg->create_framework(array(
209 'shortname' => 'shortname_b',
210 'idnumber' => 'idnumber_a',
211 'description' => 'description',
212 'descriptionformat' => FORMAT_HTML,
213 'visible' => true,
214 'contextid' => context_system::instance()->id
215 ));
216
217 $framework3 = $lpg->create_framework(array(
218 'shortname' => 'shortname_c',
219 'idnumber' => 'idnumber_b',
220 'description' => 'description',
221 'descriptionformat' => FORMAT_HTML,
222 'visible' => true,
223 'contextid' => context_system::instance()->id
224 ));
b3979696
IT
225
226 // Get frameworks list order by shortname desc.
227 $result = api::list_frameworks('shortname', 'DESC', null, 3, context_system::instance());
228
1896274f
FM
229 $f = (object) array_shift($result);
230 $this->assertEquals($framework3->get_id(), $f->get_id());
231 $f = (object) array_shift($result);
232 $this->assertEquals($framework2->get_id(), $f->get_id());
233 $f = (object) array_shift($result);
234 $this->assertEquals($framework1->get_id(), $f->get_id());
b3979696
IT
235
236 // Get frameworks list order by idnumber asc.
237 $result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance());
238
1896274f
FM
239 $f = (object) array_shift($result);
240 $this->assertEquals($framework2->get_id(), $f->get_id());
241 $f = (object) array_shift($result);
242 $this->assertEquals($framework3->get_id(), $f->get_id());
243 $f = (object) array_shift($result);
244 $this->assertEquals($framework1->get_id(), $f->get_id());
b3979696
IT
245 }
246
c61db7bb
IT
247 /**
248 * Test duplicate a framework.
249 */
250 public function test_duplicate_framework() {
251 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
252 $this->resetAfterTest(true);
253 $this->setAdminUser();
254
255 $syscontext = context_system::instance();
256 $params = array(
257 'shortname' => 'shortname_a',
258 'idnumber' => 'idnumber_c',
259 'description' => 'description',
260 'descriptionformat' => FORMAT_HTML,
261 'visible' => true,
262 'contextid' => $syscontext->id
263 );
264 $framework = $lpg->create_framework($params);
265 $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
266 $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
267 $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
268 $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
4c4a8d41
IT
269 $competency41 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(),
270 'parentid' => $competency4->get_id())
271 );
272 $competency42 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(),
273 'parentid' => $competency4->get_id())
274 );
c61db7bb
IT
275 $competencyidnumbers = array($competency1->get_idnumber(),
276 $competency2->get_idnumber(),
277 $competency3->get_idnumber(),
4c4a8d41
IT
278 $competency4->get_idnumber(),
279 $competency41->get_idnumber(),
280 $competency42->get_idnumber()
c61db7bb
IT
281 );
282
4c4a8d41
IT
283 $config = json_encode(array(
284 'base' => array('points' => 4),
285 'competencies' => array(
286 array('id' => $competency41->get_id(), 'points' => 3, 'required' => 0),
287 array('id' => $competency42->get_id(), 'points' => 2, 'required' => 1),
288 )
289 ));
290 $competency4->set_ruletype('tool_lp\competency_rule_points');
291 $competency4->set_ruleoutcome(\tool_lp\competency::OUTCOME_EVIDENCE);
292 $competency4->set_ruleconfig($config);
293 $competency4->update();
294
c61db7bb
IT
295 api::add_related_competency($competency1->get_id(), $competency2->get_id());
296 api::add_related_competency($competency3->get_id(), $competency4->get_id());
297
298 $frameworkduplicated1 = api::duplicate_framework($framework->get_id());
299 $frameworkduplicated2 = api::duplicate_framework($framework->get_id());
300
301 $this->assertEquals($framework->get_idnumber().'_1', $frameworkduplicated1->get_idnumber());
302 $this->assertEquals($framework->get_idnumber().'_2', $frameworkduplicated2->get_idnumber());
303
304 $competenciesfr1 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated1->get_id()));
305 $competenciesfr2 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated2->get_id()));
306
307 $competencyidsfr1 = array();
308 $competencyidsfr2 = array();
309
310 foreach ($competenciesfr1 as $cmp) {
311 $competencyidsfr1[] = $cmp->get_idnumber();
312 }
313 foreach ($competenciesfr2 as $cmp) {
314 $competencyidsfr2[] = $cmp->get_idnumber();
315 }
316
317 $this->assertEmpty(array_diff($competencyidsfr1, $competencyidnumbers));
318 $this->assertEmpty(array_diff($competencyidsfr2, $competencyidnumbers));
4c4a8d41
IT
319 $this->assertCount(6, $competenciesfr1);
320 $this->assertCount(6, $competenciesfr2);
c61db7bb
IT
321
322 // Test the related competencies.
323 reset($competenciesfr1);
324 $compduplicated1 = current($competenciesfr1);
325 $relatedcompetencies = $compduplicated1->get_related_competencies();
326 $comprelated = current($relatedcompetencies);
327 $this->assertEquals($comprelated->get_idnumber(), $competency2->get_idnumber());
5a4ee001 328
4c4a8d41
IT
329 // Check if config rule have been ported correctly.
330 $competency4duplicated = competency::get_record(array(
331 'idnumber' => $competency4->get_idnumber(),
332 'competencyframeworkid' => $frameworkduplicated2->get_id()
333 ));
334 $configduplicated = json_decode($competency4duplicated->get_ruleconfig(), true);
335 $configorigin = json_decode($config, true);
336 // Check that the 2 config have the same base.
337 $this->assertEquals($configorigin['base'], $configduplicated['base']);
338 $this->assertEquals(count($configorigin['competencies']), count($configduplicated['competencies']));
9a325eda 339 $competencyidsrules = array();
4c4a8d41
IT
340 foreach ($configduplicated['competencies'] as $key => $value) {
341 // Check that the only difference between the 2 config is id competency.
342 $this->assertEquals(1, count(array_diff($value, $configorigin['competencies'][$key])));
9a325eda 343 $competencyidsrules[] = $value['id'];
4c4a8d41 344 }
9a325eda 345 $this->assertTrue($competency4duplicated->is_parent_of($competencyidsrules));
5a4ee001 346
20e75095
IT
347 // Test duplicate an empty framework.
348 $emptyfrm = $lpg->create_framework();
349 $emptyfrmduplicated = api::duplicate_framework($emptyfrm->get_id());
350 $this->assertEquals($emptyfrm->get_idnumber().'_1', $emptyfrmduplicated->get_idnumber());
351 $nbcomp = api::count_competencies(array('competencyframeworkid' => $emptyfrmduplicated->get_id()));
352 $this->assertEquals(0, $nbcomp);
353
c61db7bb
IT
354 }
355
58405003
IT
356 /**
357 * Test update plan.
358 */
359 public function test_update_plan() {
360 $this->resetAfterTest(true);
361 $dg = $this->getDataGenerator();
362 $usermanageowndraft = $dg->create_user();
363 $usermanageown = $dg->create_user();
364 $usermanagedraft = $dg->create_user();
365 $usermanage = $dg->create_user();
366
367 $syscontext = context_system::instance();
368
369 // Creating specific roles.
370 $manageowndraftrole = $dg->create_role(array(
371 'name' => 'User manage own draft',
372 'shortname' => 'manage-own-draft'
373 ));
374 $manageownrole = $dg->create_role(array(
375 'name' => 'User manage own',
376 'shortname' => 'manage-own'
377 ));
378 $managedraftrole = $dg->create_role(array(
379 'name' => 'User manage draft',
380 'shortname' => 'manage-draft'
381 ));
382 $managerole = $dg->create_role(array(
383 'name' => 'User manage',
384 'shortname' => 'manage'
385 ));
386
387 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
388 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
389
390 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
391 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
392
393 assign_capability('tool/lp:planmanagedraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
394 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
395
396 assign_capability('tool/lp:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
397 assign_capability('tool/lp:planview', CAP_ALLOW, $managerole, $syscontext->id);
398
399 $dg->role_assign($manageowndraftrole, $usermanageowndraft->id, $syscontext->id);
400 $dg->role_assign($manageownrole, $usermanageown->id, $syscontext->id);
401 $dg->role_assign($managedraftrole, $usermanagedraft->id, $syscontext->id);
402 $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
403
404 // Create first learning plan with user create draft.
405 $this->setUser($usermanageowndraft);
406 $plan = array (
407 'name' => 'plan own draft',
408 'description' => 'plan own draft',
409 'userid' => $usermanageowndraft->id
410 );
411 $plan = api::create_plan((object)$plan);
412 $record = $plan->to_record();
413 $record->name = 'plan own draft modified';
414
415 // Check if user create draft can edit the plan name.
416 $plan = api::update_plan($record);
417 $this->assertInstanceOf('\tool_lp\plan', $plan);
418
3c230247 419 // The status cannot be changed in this method.
58405003
IT
420 $record->status = \tool_lp\plan::STATUS_ACTIVE;
421 try {
422 $plan = api::update_plan($record);
3c230247
FM
423 $this->fail('Updating the status is not allowed.');
424 } catch (coding_exception $e) {
425 $this->assertRegExp('/To change the status of a plan use the appropriate methods./', $e->getMessage());
58405003
IT
426 }
427
428 // Test when user with manage own plan capability try to edit other user plan.
429 $record->status = \tool_lp\plan::STATUS_DRAFT;
430 $record->name = 'plan create draft modified 2';
431 $this->setUser($usermanageown);
432 try {
433 $plan = api::update_plan($record);
434 $this->fail('User with manage own plan capability can only edit his own plan.');
435 } catch (required_capability_exception $e) {
436 $this->assertTrue(true);
437 }
438
439 // User with manage plan capability cannot edit the other user plans with status draft.
440 $this->setUser($usermanage);
3c230247 441 $record->name = 'plan create draft modified 3';
58405003
IT
442 try {
443 $plan = api::update_plan($record);
444 $this->fail('User with manage plan capability cannot edit the other user plans with status draft');
445 } catch (required_capability_exception $e) {
446 $this->assertTrue(true);
447 }
448
449 // User with manage draft capability can edit other user's learning plan if the status is draft.
450 $this->setUser($usermanagedraft);
451 $record->status = \tool_lp\plan::STATUS_DRAFT;
452 $record->name = 'plan manage draft modified 3';
453 $plan = api::update_plan($record);
454 $this->assertInstanceOf('\tool_lp\plan', $plan);
455
456 // User with manage plan capability can create/edit learning plan if status is active/complete.
457 $this->setUser($usermanage);
458 $plan = array (
459 'name' => 'plan create',
460 'description' => 'plan create',
461 'userid' => $usermanage->id,
462 'status' => \tool_lp\plan::STATUS_ACTIVE
463 );
464 $plan = api::create_plan((object)$plan);
5bfab685
FM
465
466 // Silently transition to complete status to avoid errors about transitioning to complete.
467 $plan->set_status(\tool_lp\plan::STATUS_COMPLETE);
468 $plan->update();
469
58405003
IT
470 $record = $plan->to_record();
471 $record->name = 'plan create own modified';
d805cc37
IT
472 try {
473 api::update_plan($record);
474 $this->fail('Completed plan can not be edited');
475 } catch (coding_exception $e) {
476 $this->assertTrue(true);
477 }
58405003
IT
478 }
479
2388d46d
FM
480 public function test_create_plan_from_template() {
481 $this->resetAfterTest(true);
482 $this->setAdminUser();
483
484 $u1 = $this->getDataGenerator()->create_user();
485 $tpl = $this->getDataGenerator()->get_plugin_generator('tool_lp')->create_template();
486
487 // Creating a new plan.
488 $plan = api::create_plan_from_template($tpl, $u1->id);
489 $record = $plan->to_record();
490 $this->assertInstanceOf('\tool_lp\plan', $plan);
491 $this->assertTrue(\tool_lp\plan::record_exists($plan->get_id()));
492 $this->assertEquals($tpl->get_id(), $plan->get_templateid());
493 $this->assertEquals($u1->id, $plan->get_userid());
494 $this->assertTrue($plan->is_based_on_template());
495
496 // Creating a plan that already exists.
497 $plan = api::create_plan_from_template($tpl, $u1->id);
498 $this->assertFalse($plan);
499
500 // Check that api::create_plan cannot be used.
501 $this->setExpectedException('coding_exception');
502 unset($record->id);
503 $plan = api::create_plan($record);
504 }
505
4de877eb
FM
506 public function test_update_plan_based_on_template() {
507 $this->resetAfterTest(true);
508 $dg = $this->getDataGenerator();
509 $lpg = $dg->get_plugin_generator('tool_lp');
510 $u1 = $dg->create_user();
511 $u2 = $dg->create_user();
512
513 $this->setAdminUser();
514 $tpl1 = $lpg->create_template();
515 $tpl2 = $lpg->create_template();
516 $up1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id()));
517 $up2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => null));
518
519 try {
520 // Trying to remove the template dependency.
521 $record = $up1->to_record();
522 $record->templateid = null;
523 api::update_plan($record);
524 $this->fail('A plan cannot be unlinked using api::update_plan()');
525 } catch (coding_exception $e) {
526 }
527
528 try {
529 // Trying to switch to another template.
530 $record = $up1->to_record();
531 $record->templateid = $tpl2->get_id();
532 api::update_plan($record);
533 $this->fail('A plan cannot be moved to another template.');
534 } catch (coding_exception $e) {
535 }
536
537 try {
538 // Trying to switch to using a template.
539 $record = $up2->to_record();
540 $record->templateid = $tpl1->get_id();
541 api::update_plan($record);
542 $this->fail('A plan cannot be update to use a template.');
543 } catch (coding_exception $e) {
544 }
545 }
546
6d2c2e86
FM
547 public function test_unlink_plan_from_template() {
548 $this->resetAfterTest(true);
549 $dg = $this->getDataGenerator();
550 $lpg = $dg->get_plugin_generator('tool_lp');
551 $u1 = $dg->create_user();
552 $u2 = $dg->create_user();
553
554 $this->setAdminUser();
555 $f1 = $lpg->create_framework();
556 $f2 = $lpg->create_framework();
557 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
558 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
559 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
560
561 $tpl1 = $lpg->create_template();
562 $tpl2 = $lpg->create_template();
563
564 $tplc1a = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1a->get_id(),
565 'sortorder' => 9));
566 $tplc1b = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1b->get_id(),
567 'sortorder' => 8));
568 $tplc2a = $lpg->create_template_competency(array('templateid' => $tpl2->get_id(), 'competencyid' => $c2a->get_id()));
569
a8902ee2 570 $plan1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id(), 'status' => plan::STATUS_ACTIVE));
6d2c2e86 571 $plan2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => $tpl2->get_id()));
a8902ee2 572 $plan3 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id(), 'status' => plan::STATUS_COMPLETE));
6d2c2e86
FM
573
574 // Check that we have what we expect at this stage.
575 $this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
576 $this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
577 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
578 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan2->get_id())));
579 $this->assertTrue($plan1->is_based_on_template());
580 $this->assertTrue($plan2->is_based_on_template());
581
582 // Let's do this!
583 $tpl1comps = \tool_lp\template_competency::list_competencies($tpl1->get_id(), true);
584 $tpl2comps = \tool_lp\template_competency::list_competencies($tpl2->get_id(), true);
585
586 api::unlink_plan_from_template($plan1);
587
588 $plan1->read();
589 $plan2->read();
590 $this->assertCount(2, $tpl1comps);
591 $this->assertCount(1, $tpl2comps);
592 $this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
593 $this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
594 $this->assertEquals(2, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
595 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan2->get_id())));
596 $this->assertFalse($plan1->is_based_on_template());
597 $this->assertEquals($tpl1->get_id(), $plan1->get_origtemplateid());
598 $this->assertTrue($plan2->is_based_on_template());
599 $this->assertEquals(null, $plan2->get_origtemplateid());
600
a8902ee2
SG
601 // Check we can unlink draft plan.
602 try {
603 api::unlink_plan_from_template($plan2);
604 } catch (coding_exception $e) {
605 $this->fail('Fail to unlink draft plan.');
606 }
607
608 // Check we can not unlink completed plan.
609 try {
610 api::unlink_plan_from_template($plan3);
611 $this->fail('We can not unlink completed plan.');
612 } catch (coding_exception $e) {
613 }
614
6d2c2e86
FM
615 // Even the order remains.
616 $plan1comps = \tool_lp\plan_competency::list_competencies($plan1->get_id());
617 $before = reset($tpl1comps);
618 $after = reset($plan1comps);
619 $this->assertEquals($before->get_id(), $after->get_id());
620 $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
621 $before = next($tpl1comps);
622 $after = next($plan1comps);
623 $this->assertEquals($before->get_id(), $after->get_id());
624 $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
625 }
626
c7999f6d
FM
627 public function test_update_template_updates_plans() {
628 $this->resetAfterTest(true);
629 $this->setAdminUser();
630
631 $dg = $this->getDataGenerator();
632 $u1 = $dg->create_user();
633 $u2 = $dg->create_user();
634 $lpg = $dg->get_plugin_generator('tool_lp');
635 $tpl1 = $lpg->create_template();
636 $tpl2 = $lpg->create_template();
637
c7999f6d
FM
638 // Create plans with data not matching templates.
639 $time = time();
640 $plan1 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u1->id,
641 'name' => 'Not good name', 'duedate' => $time + 3600, 'description' => 'Ahah', 'descriptionformat' => FORMAT_MARKDOWN));
642 $plan2 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u2->id,
643 'name' => 'Not right name', 'duedate' => $time + 3601, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
644 $plan3 = $lpg->create_plan(array('templateid' => $tpl2->get_id(), 'userid' => $u1->id,
645 'name' => 'Not sweet name', 'duedate' => $time + 3602, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
646
647 // Prepare our expectations.
648 $plan1->read();
649 $plan2->read();
650 $plan3->read();
651
652 $this->assertEquals($tpl1->get_id(), $plan1->get_templateid());
653 $this->assertEquals($tpl1->get_id(), $plan2->get_templateid());
654 $this->assertEquals($tpl2->get_id(), $plan3->get_templateid());
655 $this->assertNotEquals($tpl1->get_shortname(), $plan1->get_name());
656 $this->assertNotEquals($tpl1->get_shortname(), $plan2->get_name());
657 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
658 $this->assertNotEquals($tpl1->get_description(), $plan1->get_description());
659 $this->assertNotEquals($tpl1->get_description(), $plan2->get_description());
660 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
661 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
662 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
663 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
664 $this->assertNotEquals($tpl1->get_duedate(), $plan1->get_duedate());
665 $this->assertNotEquals($tpl1->get_duedate(), $plan2->get_duedate());
666 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
667
668 // Update the template without changing critical fields does not update the plans.
669 $data = $tpl1->to_record();
670 $data->visible = 0;
671 api::update_template($data);
672 $this->assertNotEquals($tpl1->get_shortname(), $plan1->get_name());
673 $this->assertNotEquals($tpl1->get_shortname(), $plan2->get_name());
674 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
675 $this->assertNotEquals($tpl1->get_description(), $plan1->get_description());
676 $this->assertNotEquals($tpl1->get_description(), $plan2->get_description());
677 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
678 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
679 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
680 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
681 $this->assertNotEquals($tpl1->get_duedate(), $plan1->get_duedate());
682 $this->assertNotEquals($tpl1->get_duedate(), $plan2->get_duedate());
683 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
684
685 // Now really update the template.
686 $data = $tpl1->to_record();
687 $data->shortname = 'Awesome!';
688 $data->description = 'This is too awesome!';
689 $data->descriptionformat = FORMAT_HTML;
a967d2aa 690 $data->duedate = $time + 200;
c7999f6d
FM
691 api::update_template($data);
692 $tpl1->read();
693
694 // Now confirm that the right plans were updated.
695 $plan1->read();
696 $plan2->read();
697 $plan3->read();
698
699 $this->assertEquals($tpl1->get_id(), $plan1->get_templateid());
700 $this->assertEquals($tpl1->get_id(), $plan2->get_templateid());
701 $this->assertEquals($tpl2->get_id(), $plan3->get_templateid());
702
703 $this->assertEquals($tpl1->get_shortname(), $plan1->get_name());
704 $this->assertEquals($tpl1->get_shortname(), $plan2->get_name());
705 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
706 $this->assertEquals($tpl1->get_description(), $plan1->get_description());
707 $this->assertEquals($tpl1->get_description(), $plan2->get_description());
708 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
709 $this->assertEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
710 $this->assertEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
711 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
712 $this->assertEquals($tpl1->get_duedate(), $plan1->get_duedate());
713 $this->assertEquals($tpl1->get_duedate(), $plan2->get_duedate());
714 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
715 }
716
5bfab685
FM
717 /**
718 * Test that the method to complete a plan.
719 */
720 public function test_complete_plan() {
721 global $DB;
722
723 $this->resetAfterTest(true);
724 $this->setAdminUser();
725 $dg = $this->getDataGenerator();
726 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
727 $user = $dg->create_user();
728
729 // Create a framework and assign competencies.
730 $framework = $lpg->create_framework();
731 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
732 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
733 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
d3275795 734 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
5bfab685
FM
735
736 // Create two plans and assign competencies.
737 $plan = $lpg->create_plan(array('userid' => $user->id));
738 $otherplan = $lpg->create_plan(array('userid' => $user->id));
739
740 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
741 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
742 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
743 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
744
745 $uclist = array(
746 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
747 'proficiency' => true, 'grade' => 1 )),
748 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
749 'proficiency' => false, 'grade' => 2 ))
750 );
751
752 $this->assertEquals(2, \tool_lp\user_competency::count_records());
753 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
754
755 // Change status of the plan to complete.
756 api::complete_plan($plan);
757
758 // Check that user competencies are now in user_competency_plan objects and still in user_competency.
759 $this->assertEquals(2, \tool_lp\user_competency::count_records());
760 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
761
762 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
763
764 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
765 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
766 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
767 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
768 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
769
770 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
771 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
772 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
773 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
774 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
775
776 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
777 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
778 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
779 $this->assertNull($usercompetenciesplan[2]->get_grade());
780 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
781
d3275795
SG
782 // Check we can not add competency to completed plan.
783 try {
784 api::add_competency_to_plan($plan->get_id(), $c4->get_id());
785 $this->fail('We can not add competency to completed plan.');
786 } catch (coding_exception $e) {
787 }
788
789 // Check we can not remove competency to completed plan.
790 try {
791 api::remove_competency_from_plan($plan->get_id(), $c3->get_id());
792 $this->fail('We can not remove competency to completed plan.');
793 } catch (coding_exception $e) {
794 }
795
5bfab685
FM
796 // Completing a plan that is completed throws an exception.
797 $this->setExpectedException('coding_exception');
798 api::complete_plan($plan);
799 }
800
3c230247
FM
801 /**
802 * Set-up the workflow data (review, active, ...).
803 *
804 * @return array
805 */
806 protected function setup_workflow_data() {
807 $this->resetAfterTest();
808
809 $dg = $this->getDataGenerator();
810 $user = $dg->create_user();
811 $reviewer = $dg->create_user();
812 $otheruser = $dg->create_user();
813
814 $syscontext = context_system::instance();
815 $userrole = $dg->create_role();
816 $reviewerrole = $dg->create_role();
817 $otheruserrole = $dg->create_role();
818
819 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $userrole, $syscontext->id);
820 assign_capability('tool/lp:planmanage', CAP_ALLOW, $reviewerrole, $syscontext->id);
821 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $reviewerrole, $syscontext->id);
822 $dg->role_assign($userrole, $user->id, $syscontext->id);
823 $dg->role_assign($reviewerrole, $reviewer->id, $syscontext->id);
824 accesslib_clear_all_caches_for_unit_testing();
825
826 $lpg = $dg->get_plugin_generator('tool_lp');
827 $tpl = $lpg->create_template();
828 $plan = $lpg->create_plan(array('userid' => $user->id));
829 $tplplan = $lpg->create_plan(array('userid' => $user->id, 'templateid' => $tpl->get_id()));
830
831 return array(
832 'dg' => $dg,
833 'lpg' => $lpg,
834 'user' => $user,
835 'reviewer' => $reviewer,
836 'otheruser' => $otheruser,
837 'plan' => $plan,
838 'tplplan' => $tplplan,
839 );
840 }
841
842 /**
843 * Testing requesting the review of a plan.
844 */
845 public function test_plan_request_review() {
846 $data = $this->setup_workflow_data();
847 extract($data);
848
849 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
850 $this->assertEquals(plan::STATUS_DRAFT, $tplplan->get_status());
851
852 // Foreign user cannot do anything.
853 $this->setUser($otheruser);
854 try {
855 api::plan_request_review($plan);
856 $this->fail('The user can not read the plan.');
857 } catch (required_capability_exception $e) {
858 $this->assertEquals('nopermissions', $e->errorcode);
859 }
860
861 // Can not change a plan based on a template.
862 $this->setUser($user);
863 try {
864 api::plan_request_review($tplplan);
865 $this->fail('The plan is based on a template.');
866 } catch (coding_exception $e) {
867 $this->assertRegExp('/Template plans cannot be reviewed./', $e->getMessage());
868 }
869
870 // Can not send for review when not draft.
871 $this->setUser($user);
872 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
873 try {
874 api::plan_request_review($plan);
875 $this->fail('The plan cannot be sent for review at this stage.');
876 } catch (coding_exception $e) {
877 $this->assertRegExp('/The plan cannot be sent for review at this stage./', $e->getMessage());
878 }
879
880 // Can not send for review when not draft.
881 $this->setUser($user);
882 $plan->set_status(plan::STATUS_ACTIVE);
883 try {
884 api::plan_request_review($plan);
885 $this->fail('The plan cannot be sent for review at this stage.');
886 } catch (coding_exception $e) {
887 $this->assertRegExp('/The plan cannot be sent for review at this stage./', $e->getMessage());
888 }
889
890 // Can not send for review when not draft.
891 $this->setUser($user);
892 $plan->set_status(plan::STATUS_IN_REVIEW);
893 try {
894 api::plan_request_review($plan);
895 $this->fail('The plan cannot be sent for review at this stage.');
896 } catch (coding_exception $e) {
897 $this->assertRegExp('/The plan cannot be sent for review at this stage./', $e->getMessage());
898 }
899
900 // Can not send for review when not draft.
901 $this->setUser($user);
902 $plan->set_status(plan::STATUS_COMPLETE);
903 try {
904 api::plan_request_review($plan);
905 $this->fail('The plan cannot be sent for review at this stage.');
906 } catch (coding_exception $e) {
907 $this->assertRegExp('/The plan cannot be sent for review at this stage./', $e->getMessage());
908 }
909
910 // Sending for review as a reviewer.
911 $this->setUser($reviewer);
912 $plan->set_status(plan::STATUS_DRAFT);
913 try {
914 api::plan_request_review($plan);
915 $this->fail('The user can not request a review.');
916 } catch (required_capability_exception $e) {
917 $this->assertEquals('nopermissions', $e->errorcode);
918 }
919
920 // Sending for review.
921 $this->setUser($user);
922 api::plan_request_review($plan);
923 $plan->read();
924 $this->assertEquals(plan::STATUS_WAITING_FOR_REVIEW, $plan->get_status());
925
926 // Sending for review by ID.
927 $plan->set_status(plan::STATUS_DRAFT);
928 $plan->update();
929 api::plan_request_review($plan->get_id());
930 $plan->read();
931 $this->assertEquals(plan::STATUS_WAITING_FOR_REVIEW, $plan->get_status());
932 }
933
934 /**
935 * Testing cancelling the review request.
936 */
937 public function test_plan_cancel_review_request() {
938 $data = $this->setup_workflow_data();
939 extract($data);
940
941 // Set waiting for review.
942 $tplplan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
943 $tplplan->update();
944 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
945 $plan->update();
946
947 // Foreign user cannot do anything.
948 $this->setUser($otheruser);
949 try {
950 api::plan_cancel_review_request($plan);
951 $this->fail('The user can not read the plan.');
952 } catch (required_capability_exception $e) {
953 $this->assertEquals('nopermissions', $e->errorcode);
954 }
955
956 // Can not change a plan based on a template.
957 $this->setUser($user);
958 try {
959 api::plan_cancel_review_request($tplplan);
960 $this->fail('The plan is based on a template.');
961 } catch (coding_exception $e) {
962 $this->assertRegExp('/Template plans cannot be reviewed./', $e->getMessage());
963 }
964
965 // Can not cancel review request when not waiting for review.
966 $this->setUser($user);
967 $plan->set_status(plan::STATUS_DRAFT);
968 try {
969 api::plan_cancel_review_request($plan);
970 $this->fail('The plan cannot be sent for review at this stage.');
971 } catch (coding_exception $e) {
972 $this->assertRegExp('/The plan review cannot be cancelled at this stage./', $e->getMessage());
973 }
974
975 // Can not cancel review request when not waiting for review.
976 $this->setUser($user);
977 $plan->set_status(plan::STATUS_IN_REVIEW);
978 try {
979 api::plan_cancel_review_request($plan);
980 $this->fail('The plan review cannot be cancelled at this stage.');
981 } catch (coding_exception $e) {
982 $this->assertRegExp('/The plan review cannot be cancelled at this stage./', $e->getMessage());
983 }
984
985 // Can not cancel review request when not waiting for review.
986 $this->setUser($user);
987 $plan->set_status(plan::STATUS_ACTIVE);
988 try {
989 api::plan_cancel_review_request($plan);
990 $this->fail('The plan review cannot be cancelled at this stage.');
991 } catch (coding_exception $e) {
992 $this->assertRegExp('/The plan review cannot be cancelled at this stage./', $e->getMessage());
993 }
994
995 // Can not cancel review request when not waiting for review.
996 $this->setUser($user);
997 $plan->set_status(plan::STATUS_COMPLETE);
998 try {
999 api::plan_cancel_review_request($plan);
1000 $this->fail('The plan review cannot be cancelled at this stage.');
1001 } catch (coding_exception $e) {
1002 $this->assertRegExp('/The plan review cannot be cancelled at this stage./', $e->getMessage());
1003 }
1004
1005 // Cancelling as a reviewer.
1006 $this->setUser($reviewer);
1007 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1008 try {
1009 api::plan_cancel_review_request($plan);
1010 $this->fail('The user can not cancel a review request.');
1011 } catch (required_capability_exception $e) {
1012 $this->assertEquals('nopermissions', $e->errorcode);
1013 }
1014
1015 // Cancelling review request.
1016 $this->setUser($user);
1017 api::plan_cancel_review_request($plan);
1018 $plan->read();
1019 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
1020
1021 // Cancelling review request by ID.
1022 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1023 $plan->update();
1024 api::plan_cancel_review_request($plan->get_id());
1025 $plan->read();
1026 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
1027 }
1028
1029 /**
1030 * Testing starting the review.
1031 */
1032 public function test_plan_start_review() {
1033 $data = $this->setup_workflow_data();
1034 extract($data);
1035
1036 // Set waiting for review.
1037 $tplplan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1038 $tplplan->update();
1039 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1040 $plan->update();
1041
1042 // Foreign user cannot do anything.
1043 $this->setUser($otheruser);
1044 try {
1045 api::plan_start_review($plan);
1046 $this->fail('The user can not read the plan.');
1047 } catch (required_capability_exception $e) {
1048 $this->assertEquals('nopermissions', $e->errorcode);
1049 }
1050
1051 // Can not change a plan based on a template.
1052 $this->setUser($reviewer);
1053 try {
1054 api::plan_start_review($tplplan);
1055 $this->fail('The plan is based on a template.');
1056 } catch (coding_exception $e) {
1057 $this->assertRegExp('/Template plans cannot be reviewed./', $e->getMessage());
1058 }
1059
1060 // Can not start a review when not waiting for review.
1061 $this->setUser($reviewer);
1062 $plan->set_status(plan::STATUS_DRAFT);
1063 try {
1064 api::plan_start_review($plan);
1065 $this->fail('The plan review cannot be started at this stage.');
1066 } catch (coding_exception $e) {
1067 $this->assertRegExp('/The plan review cannot be started at this stage./', $e->getMessage());
1068 }
1069
1070 // Can not start a review when not waiting for review.
1071 $this->setUser($reviewer);
1072 $plan->set_status(plan::STATUS_IN_REVIEW);
1073 try {
1074 api::plan_start_review($plan);
1075 $this->fail('The plan review cannot be started at this stage.');
1076 } catch (coding_exception $e) {
1077 $this->assertRegExp('/The plan review cannot be started at this stage./', $e->getMessage());
1078 }
1079
1080 // Can not start a review when not waiting for review.
1081 $this->setUser($reviewer);
1082 $plan->set_status(plan::STATUS_ACTIVE);
1083 try {
1084 api::plan_start_review($plan);
1085 $this->fail('The plan review cannot be started at this stage.');
1086 } catch (coding_exception $e) {
1087 $this->assertRegExp('/The plan review cannot be started at this stage./', $e->getMessage());
1088 }
1089
1090 // Can not start a review when not waiting for review.
1091 $this->setUser($reviewer);
1092 $plan->set_status(plan::STATUS_COMPLETE);
1093 try {
1094 api::plan_start_review($plan);
1095 $this->fail('The plan review cannot be started at this stage.');
1096 } catch (coding_exception $e) {
1097 $this->assertRegExp('/The plan review cannot be started at this stage./', $e->getMessage());
1098 }
1099
1100 // Starting as the owner.
1101 $this->setUser($user);
1102 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1103 try {
1104 api::plan_start_review($plan);
1105 $this->fail('The user can not start a review.');
1106 } catch (required_capability_exception $e) {
1107 $this->assertEquals('nopermissions', $e->errorcode);
1108 }
1109
1110 // Starting review.
1111 $this->setUser($reviewer);
1112 api::plan_start_review($plan);
1113 $plan->read();
1114 $this->assertEquals(plan::STATUS_IN_REVIEW, $plan->get_status());
1115 $this->assertEquals($reviewer->id, $plan->get_reviewerid());
1116
1117 // Starting review by ID.
1118 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1119 $plan->set_reviewerid(null);
1120 $plan->update();
1121 api::plan_start_review($plan->get_id());
1122 $plan->read();
1123 $this->assertEquals(plan::STATUS_IN_REVIEW, $plan->get_status());
1124 $this->assertEquals($reviewer->id, $plan->get_reviewerid());
1125 }
1126
1127 /**
1128 * Testing stopping the review.
1129 */
1130 public function test_plan_stop_review() {
1131 $data = $this->setup_workflow_data();
1132 extract($data);
1133
1134 // Set waiting for review.
1135 $tplplan->set_status(plan::STATUS_IN_REVIEW);
1136 $tplplan->update();
1137 $plan->set_status(plan::STATUS_IN_REVIEW);
1138 $plan->update();
1139
1140 // Foreign user cannot do anything.
1141 $this->setUser($otheruser);
1142 try {
1143 api::plan_stop_review($plan);
1144 $this->fail('The user can not read the plan.');
1145 } catch (required_capability_exception $e) {
1146 $this->assertEquals('nopermissions', $e->errorcode);
1147 }
1148
1149 // Can not change a plan based on a template.
1150 $this->setUser($reviewer);
1151 try {
1152 api::plan_stop_review($tplplan);
1153 $this->fail('The plan is based on a template.');
1154 } catch (coding_exception $e) {
1155 $this->assertRegExp('/Template plans cannot be reviewed./', $e->getMessage());
1156 }
1157
1158 // Can not stop a review whe not in review.
1159 $this->setUser($reviewer);
1160 $plan->set_status(plan::STATUS_DRAFT);
1161 try {
1162 api::plan_stop_review($plan);
1163 $this->fail('The plan review cannot be stopped at this stage.');
1164 } catch (coding_exception $e) {
1165 $this->assertRegExp('/The plan review cannot be stopped at this stage./', $e->getMessage());
1166 }
1167
1168 // Can not stop a review whe not in review.
1169 $this->setUser($reviewer);
1170 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1171 try {
1172 api::plan_stop_review($plan);
1173 $this->fail('The plan review cannot be stopped at this stage.');
1174 } catch (coding_exception $e) {
1175 $this->assertRegExp('/The plan review cannot be stopped at this stage./', $e->getMessage());
1176 }
1177
1178 // Can not stop a review whe not in review.
1179 $this->setUser($reviewer);
1180 $plan->set_status(plan::STATUS_ACTIVE);
1181 try {
1182 api::plan_stop_review($plan);
1183 $this->fail('The plan review cannot be stopped at this stage.');
1184 } catch (coding_exception $e) {
1185 $this->assertRegExp('/The plan review cannot be stopped at this stage./', $e->getMessage());
1186 }
1187
1188 // Can not stop a review whe not in review.
1189 $this->setUser($reviewer);
1190 $plan->set_status(plan::STATUS_COMPLETE);
1191 try {
1192 api::plan_stop_review($plan);
1193 $this->fail('The plan review cannot be stopped at this stage.');
1194 } catch (coding_exception $e) {
1195 $this->assertRegExp('/The plan review cannot be stopped at this stage./', $e->getMessage());
1196 }
1197
1198 // Stopping as the owner.
1199 $this->setUser($user);
1200 $plan->set_status(plan::STATUS_IN_REVIEW);
1201 try {
1202 api::plan_stop_review($plan);
1203 $this->fail('The user can not stop a review.');
1204 } catch (required_capability_exception $e) {
1205 $this->assertEquals('nopermissions', $e->errorcode);
1206 }
1207
1208 // Stopping review.
1209 $this->setUser($reviewer);
1210 api::plan_stop_review($plan);
1211 $plan->read();
1212 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
1213
1214 // Stopping review by ID.
1215 $plan->set_status(plan::STATUS_IN_REVIEW);
1216 $plan->update();
1217 api::plan_stop_review($plan->get_id());
1218 $plan->read();
1219 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
1220 }
1221
1222 /**
1223 * Testing approving the plan.
1224 */
1225 public function test_approve_plan() {
1226 $data = $this->setup_workflow_data();
1227 extract($data);
1228
1229 // Set waiting for review.
1230 $tplplan->set_status(plan::STATUS_IN_REVIEW);
1231 $tplplan->update();
1232 $plan->set_status(plan::STATUS_IN_REVIEW);
1233 $plan->update();
1234
1235 // Foreign user cannot do anything.
1236 $this->setUser($otheruser);
1237 try {
1238 api::approve_plan($plan);
1239 $this->fail('The user can not read the plan.');
1240 } catch (required_capability_exception $e) {
1241 $this->assertEquals('nopermissions', $e->errorcode);
1242 }
1243
1244 // Can not change a plan based on a template.
1245 $this->setUser($reviewer);
1246 try {
1247 api::approve_plan($tplplan);
1248 $this->fail('The plan is based on a template.');
1249 } catch (coding_exception $e) {
1250 $this->assertRegExp('/Template plans are already approved./', $e->getMessage());
1251 }
1252
1253 // Can not approve a plan already approved.
1254 $this->setUser($reviewer);
1255 $plan->set_status(plan::STATUS_ACTIVE);
1256 try {
1257 api::approve_plan($plan);
1258 $this->fail('The plan cannot be approved at this stage.');
1259 } catch (coding_exception $e) {
1260 $this->assertRegExp('/The plan cannot be approved at this stage./', $e->getMessage());
1261 }
1262
1263 // Can not approve a plan already approved.
1264 $this->setUser($reviewer);
1265 $plan->set_status(plan::STATUS_COMPLETE);
1266 try {
1267 api::approve_plan($plan);
1268 $this->fail('The plan cannot be approved at this stage.');
1269 } catch (coding_exception $e) {
1270 $this->assertRegExp('/The plan cannot be approved at this stage./', $e->getMessage());
1271 }
1272
1273 // Approve as the owner.
1274 $this->setUser($user);
1275 $plan->set_status(plan::STATUS_IN_REVIEW);
1276 try {
1277 api::approve_plan($plan);
1278 $this->fail('The user can not approve the plan.');
1279 } catch (required_capability_exception $e) {
1280 $this->assertEquals('nopermissions', $e->errorcode);
1281 }
1282
1283 // Approve plan from in review.
1284 $this->setUser($reviewer);
1285 api::approve_plan($plan);
1286 $plan->read();
1287 $this->assertEquals(plan::STATUS_ACTIVE, $plan->get_status());
1288
1289 // Approve plan by ID.
1290 $plan->set_status(plan::STATUS_IN_REVIEW);
1291 $plan->update();
1292 api::approve_plan($plan->get_id());
1293 $plan->read();
1294 $this->assertEquals(plan::STATUS_ACTIVE, $plan->get_status());
1295
1296 // Approve plan from draft.
1297 $plan->set_status(plan::STATUS_DRAFT);
1298 $plan->update();
1299 api::approve_plan($plan);
1300 $plan->read();
1301 $this->assertEquals(plan::STATUS_ACTIVE, $plan->get_status());
1302
1303 // Approve plan from waiting for review.
1304 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1305 $plan->update();
1306 api::approve_plan($plan);
1307 $plan->read();
1308 $this->assertEquals(plan::STATUS_ACTIVE, $plan->get_status());
1309 }
1310
1311 /**
1312 * Testing stopping the review.
1313 */
1314 public function test_unapprove_plan() {
1315 $data = $this->setup_workflow_data();
1316 extract($data);
1317
1318 // Set waiting for review.
1319 $tplplan->set_status(plan::STATUS_ACTIVE);
1320 $tplplan->update();
1321 $plan->set_status(plan::STATUS_ACTIVE);
1322 $plan->update();
1323
1324 // Foreign user cannot do anything.
1325 $this->setUser($otheruser);
1326 try {
1327 api::unapprove_plan($plan);
1328 $this->fail('The user can not read the plan.');
1329 } catch (required_capability_exception $e) {
1330 $this->assertEquals('nopermissions', $e->errorcode);
1331 }
1332
1333 // Can not change a plan based on a template.
1334 $this->setUser($reviewer);
1335 try {
1336 api::unapprove_plan($tplplan);
1337 $this->fail('The plan is based on a template.');
1338 } catch (coding_exception $e) {
1339 $this->assertRegExp('/Template plans are always approved./', $e->getMessage());
1340 }
1341
1342 // Can not unapprove a non-draft plan.
1343 $this->setUser($reviewer);
1344 $plan->set_status(plan::STATUS_DRAFT);
1345 try {
1346 api::unapprove_plan($plan);
1347 $this->fail('The plan cannot be sent back to draft at this stage.');
1348 } catch (coding_exception $e) {
1349 $this->assertRegExp('/The plan cannot be sent back to draft at this stage./', $e->getMessage());
1350 }
1351
1352 // Can not unapprove a non-draft plan.
1353 $this->setUser($reviewer);
1354 $plan->set_status(plan::STATUS_WAITING_FOR_REVIEW);
1355 try {
1356 api::unapprove_plan($plan);
1357 $this->fail('The plan cannot be sent back to draft at this stage.');
1358 } catch (coding_exception $e) {
1359 $this->assertRegExp('/The plan cannot be sent back to draft at this stage./', $e->getMessage());
1360 }
1361
1362 // Can not unapprove a non-draft plan.
1363 $this->setUser($reviewer);
1364 $plan->set_status(plan::STATUS_IN_REVIEW);
1365 try {
1366 api::unapprove_plan($plan);
1367 $this->fail('The plan cannot be sent back to draft at this stage.');
1368 } catch (coding_exception $e) {
1369 $this->assertRegExp('/The plan cannot be sent back to draft at this stage./', $e->getMessage());
1370 }
1371
1372 // Can not unapprove a non-draft plan.
1373 $this->setUser($reviewer);
1374 $plan->set_status(plan::STATUS_COMPLETE);
1375 try {
1376 api::unapprove_plan($plan);
1377 $this->fail('The plan cannot be sent back to draft at this stage.');
1378 } catch (coding_exception $e) {
1379 $this->assertRegExp('/The plan cannot be sent back to draft at this stage./', $e->getMessage());
1380 }
1381
1382 // Unapprove as the owner.
1383 $this->setUser($user);
1384 $plan->set_status(plan::STATUS_ACTIVE);
1385 try {
1386 api::unapprove_plan($plan);
1387 $this->fail('The user can not unapprove the plan.');
1388 } catch (required_capability_exception $e) {
1389 $this->assertEquals('nopermissions', $e->errorcode);
1390 }
1391
1392 // Unapprove plan.
1393 $this->setUser($reviewer);
1394 api::unapprove_plan($plan);
1395 $plan->read();
1396 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
1397
1398 // Unapprove plan by ID.
1399 $plan->set_status(plan::STATUS_ACTIVE);
1400 $plan->update();
1401 api::unapprove_plan($plan->get_id());
1402 $plan->read();
1403 $this->assertEquals(plan::STATUS_DRAFT, $plan->get_status());
1404 }
1405
192569ed
JPG
1406 /**
1407 * Test update plan and the managing of archived user competencies.
1408 */
1409 public function test_update_plan_manage_archived_competencies() {
1410 global $DB;
1411
1412 $this->resetAfterTest(true);
1413 $dg = $this->getDataGenerator();
1414 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1415
1416 $syscontext = context_system::instance();
1417
1418 // Create users and roles for the test.
1419 $user = $dg->create_user();
1420 $manageownrole = $dg->create_role(array(
1421 'name' => 'User manage own',
1422 'shortname' => 'manageown'
1423 ));
1424 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
1425 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
1426 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
1427 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
1428 $dg->role_assign($manageownrole, $user->id, $syscontext->id);
1429 $this->setUser($user);
1430
1431 // Create a framework and assign competencies.
1432 $framework = $lpg->create_framework();
1433 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1434 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1435 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1436
1437 // Create two plans and assign competencies.
1438 $plan = $lpg->create_plan(array('userid' => $user->id));
1439 $otherplan = $lpg->create_plan(array('userid' => $user->id));
1440
1441 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
1442 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
1443 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
1444 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
1445
1446 $uclist = array(
1447 $lpg->create_user_competency(array(
1448 'userid' => $user->id,
1449 'competencyid' => $c1->get_id(),
1450 'proficiency' => true,
1451 'grade' => 1
1452 )),
1453 $lpg->create_user_competency(array(
1454 'userid' => $user->id,
1455 'competencyid' => $c2->get_id(),
1456 'proficiency' => false,
1457 'grade' => 2
1458 ))
1459 );
1460
1461 // Change status of the plan to complete.
1462 $record = $plan->to_record();
1463 $record->status = \tool_lp\plan::STATUS_COMPLETE;
1464
5bfab685
FM
1465 try {
1466 $plan = api::update_plan($record);
1467 $this->fail('We cannot complete a plan using api::update_plan().');
1468 } catch (coding_exception $e) {
1469 }
1470 api::complete_plan($plan);
192569ed
JPG
1471
1472 // Check that user compretencies are now in user_competency_plan objects and still in user_competency.
1473 $this->assertEquals(2, \tool_lp\user_competency::count_records());
1474 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
1475
1476 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
1477
1478 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
1479 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
1480 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
1481 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
1482 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
1483
1484 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
1485 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
1486 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
1487 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
1488 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
1489
1490 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
1491 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
1492 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
1493 $this->assertNull($usercompetenciesplan[2]->get_grade());
1494 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
1495
192569ed
JPG
1496 // Change status of the plan to active.
1497 $record = $plan->to_record();
1498 $record->status = \tool_lp\plan::STATUS_ACTIVE;
1499
d805cc37
IT
1500 try {
1501 api::update_plan($record);
1502 $this->fail('Completed plan can not be edited');
1503 } catch (coding_exception $e) {
1504 }
192569ed 1505
d805cc37 1506 api::reopen_plan($record->id);
192569ed
JPG
1507 // Check that user_competency_plan objects are deleted if the plan status is changed to another status.
1508 $this->assertEquals(2, \tool_lp\user_competency::count_records());
1509 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
1510 }
1511
1512 /**
1513 * Test remove plan and the managing of archived user competencies.
1514 */
1515 public function test_delete_plan_manage_archived_competencies() {
1516 $this->resetAfterTest(true);
1517 $dg = $this->getDataGenerator();
1518 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1519
1520 $syscontext = context_system::instance();
1521
1522 // Create user and role for the test.
1523 $user = $dg->create_user();
1524 $managerole = $dg->create_role(array(
1525 'name' => 'User manage own',
1526 'shortname' => 'manageown'
1527 ));
1528 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $managerole, $syscontext->id);
1529 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $managerole, $syscontext->id);
1530 $dg->role_assign($managerole, $user->id, $syscontext->id);
1531 $this->setUser($user);
1532
1533 // Create a framework and assign competencies.
1534 $framework = $lpg->create_framework();
1535 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1536 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1537 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1538
1539 // Create completed plan with records in user_competency.
1540 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
1541
1542 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
1543 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
1544
1545 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
1546 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
1547
1548 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
1549 'planid' => $completedplan->get_id()));
1550 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
1551 'planid' => $completedplan->get_id()));
1552
1553 api::delete_plan($completedplan->get_id());
1554
1555 // Check that achived user competencies are deleted.
1556 $this->assertEquals(0, \tool_lp\plan::count_records());
1557 $this->assertEquals(2, \tool_lp\user_competency::count_records());
1558 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
1559 }
1560
1561 /**
1562 * Test listing of plan competencies.
1563 */
1564 public function test_list_plan_competencies_manage_archived_competencies() {
1565 $this->resetAfterTest(true);
1566 $dg = $this->getDataGenerator();
1567 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1568
1569 $syscontext = context_system::instance();
1570
1571 // Create user and role for the test.
1572 $user = $dg->create_user();
1573 $viewrole = $dg->create_role(array(
1574 'name' => 'User view',
1575 'shortname' => 'view'
1576 ));
1577 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $viewrole, $syscontext->id);
1578 assign_capability('tool/lp:planview', CAP_ALLOW, $viewrole, $syscontext->id);
1579 $dg->role_assign($viewrole, $user->id, $syscontext->id);
1580 $this->setUser($user);
1581
1582 // Create a framework and assign competencies.
1583 $framework = $lpg->create_framework();
1584 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1585 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1586 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1587
1588 // Create draft plan with records in user_competency.
1589 $draftplan = $lpg->create_plan(array('userid' => $user->id));
1590
1591 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c1->get_id()));
1592 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c2->get_id()));
1593 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c3->get_id()));
1594
1595 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
1596 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
1597
1598 // Check that user_competency objects are returned when plan status is not complete.
1599 $plancompetencies = api::list_plan_competencies($draftplan);
1600
1601 $this->assertCount(3, $plancompetencies);
1602 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[0]->usercompetency);
1603 $this->assertEquals($uc1->get_id(), $plancompetencies[0]->usercompetency->get_id());
1604 $this->assertNull($plancompetencies[0]->usercompetencyplan);
1605
1606 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[1]->usercompetency);
1607 $this->assertEquals($uc2->get_id(), $plancompetencies[1]->usercompetency->get_id());
1608 $this->assertNull($plancompetencies[1]->usercompetencyplan);
1609
1610 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[2]->usercompetency);
1611 $this->assertEquals(0, $plancompetencies[2]->usercompetency->get_id());
1612 $this->assertNull($plancompetencies[2]->usercompetencyplan);
1613
1614 // Create completed plan with records in user_competency_plan.
1615 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
1616
1617 $pc1 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
1618 $pc2 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
1619 $pc3 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c3->get_id()));
1620
1621 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
1622 'planid' => $completedplan->get_id()));
1623 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
1624 'planid' => $completedplan->get_id()));
192569ed
JPG
1625 $ucp3 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c3->get_id(),
1626 'planid' => $completedplan->get_id()));
1627
1628 // Check that user_competency_plan objects are returned when plan status is complete.
1629 $plancompetencies = api::list_plan_competencies($completedplan);
1630
1631 $this->assertCount(3, $plancompetencies);
1632 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[0]->usercompetencyplan);
1633 $this->assertEquals($ucp1->get_id(), $plancompetencies[0]->usercompetencyplan->get_id());
1634 $this->assertNull($plancompetencies[0]->usercompetency);
1635 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[1]->usercompetencyplan);
1636 $this->assertEquals($ucp2->get_id(), $plancompetencies[1]->usercompetencyplan->get_id());
1637 $this->assertNull($plancompetencies[1]->usercompetency);
1638 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[2]->usercompetencyplan);
1639 $this->assertEquals($ucp3->get_id(), $plancompetencies[2]->usercompetencyplan->get_id());
1640 $this->assertNull($plancompetencies[2]->usercompetency);
1641 }
1642
bee480a4
FM
1643 public function test_create_template_cohort() {
1644 $this->resetAfterTest(true);
1645 $this->setAdminUser();
1646
1647 $dg = $this->getDataGenerator();
1648 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1649
1650 $c1 = $dg->create_cohort();
1651 $c2 = $dg->create_cohort();
1652 $t1 = $lpg->create_template();
1653 $t2 = $lpg->create_template();
1654
1655 $this->assertEquals(0, \tool_lp\template_cohort::count_records());
1656
1657 // Create two relations with mixed parameters.
1658 $result = api::create_template_cohort($t1->get_id(), $c1->id);
1659 $result = api::create_template_cohort($t1, $c2);
1660
1661 $this->assertEquals(2, \tool_lp\template_cohort::count_records());
1662 $this->assertInstanceOf('tool_lp\template_cohort', $result);
1663 $this->assertEquals($c2->id, $result->get_cohortid());
1664 $this->assertEquals($t1->get_id(), $result->get_templateid());
1665 $this->assertEquals(2, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1666 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1667 }
1668
5a4ee001
IT
1669 public function test_delete_template() {
1670 $this->resetAfterTest(true);
1671 $this->setAdminUser();
1672
1673 $dg = $this->getDataGenerator();
1674 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1675
1676 $c1 = $dg->create_cohort();
1677 $c2 = $dg->create_cohort();
1678 $template = $lpg->create_template();
1679 $id = $template->get_id();
1680
1681 // Create 2 template cohorts.
1682 $tc1 = $lpg->create_template_cohort(array('templateid' => $template->get_id(), 'cohortid' => $c1->id));
1683 $tc1 = $lpg->create_template_cohort(array('templateid' => $template->get_id(), 'cohortid' => $c2->id));
1684
1685 // Check pre-test.
1686 $this->assertTrue(tool_lp\template::record_exists($id));
1687 $this->assertEquals(2, \tool_lp\template_cohort::count_records(array('templateid' => $id)));
1688
1689 $result = api::delete_template($template->get_id());
1690 $this->assertTrue($result);
1691
1692 // Check that the template deos not exist anymore.
1693 $this->assertFalse(tool_lp\template::record_exists($id));
1694
1695 // Test if associated cohorts are also deleted.
1696 $this->assertEquals(0, \tool_lp\template_cohort::count_records(array('templateid' => $id)));
1697 }
1698
bee480a4
FM
1699 public function test_delete_template_cohort() {
1700 $this->resetAfterTest(true);
1701 $this->setAdminUser();
1702
1703 $dg = $this->getDataGenerator();
1704 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
1705
1706 $c1 = $dg->create_cohort();
1707 $c2 = $dg->create_cohort();
1708 $t1 = $lpg->create_template();
1709 $t2 = $lpg->create_template();
1710 $tc1 = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
1711 $tc1 = $lpg->create_template_cohort(array('templateid' => $t2->get_id(), 'cohortid' => $c2->id));
1712
1713 $this->assertEquals(2, \tool_lp\template_cohort::count_records());
1714 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1715 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1716
1717 // Delete existing.
1718 $result = api::delete_template_cohort($t1->get_id(), $c1->id);
1719 $this->assertTrue($result);
1720 $this->assertEquals(1, \tool_lp\template_cohort::count_records());
1721 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1722 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1723
1724 // Delete non-existant.
1725 $result = api::delete_template_cohort($t1->get_id(), $c1->id);
1726 $this->assertTrue($result);
1727 $this->assertEquals(1, \tool_lp\template_cohort::count_records());
1728 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1729 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1730 }
4c0e8167 1731
914b580e
FM
1732 public function test_add_evidence_log() {
1733 $this->resetAfterTest(true);
1734 $dg = $this->getDataGenerator();
1735 $lpg = $dg->get_plugin_generator('tool_lp');
1736
1737 $u1 = $dg->create_user();
1738 $u1ctx = context_user::instance($u1->id);
1739 $f1 = $lpg->create_framework();
1740 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1741 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1742
1743 // Creating a standard evidence with minimal information.
1744 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata', 'error');
1745 $evidence->read();
1746 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1747 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1748 $this->assertSame(null, $uc->get_grade());
1749 $this->assertSame(null, $uc->get_proficiency());
1750 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1751 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1752 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $evidence->get_action());
1753 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1754 $this->assertEquals('error', $evidence->get_desccomponent());
1755 $this->assertSame(null, $evidence->get_desca());
1756 $this->assertSame(null, $evidence->get_url());
1757 $this->assertSame(null, $evidence->get_grade());
1758 $this->assertSame(null, $evidence->get_actionuserid());
1759
1760 // Creating a standard evidence with more information.
1761 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata', 'error',
1762 '$a', false, 'http://moodle.org', null, 2);
1763 $evidence->read();
1764 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1765 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1766 $this->assertSame(null, $uc->get_grade());
1767 $this->assertSame(null, $uc->get_proficiency());
1768 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1769 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1770 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $evidence->get_action());
1771 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1772 $this->assertEquals('error', $evidence->get_desccomponent());
1773 $this->assertEquals('$a', $evidence->get_desca());
1774 $this->assertEquals('http://moodle.org', $evidence->get_url());
1775 $this->assertSame(null, $evidence->get_grade());
1776 $this->assertEquals(2, $evidence->get_actionuserid());
1777
1778 // Creating a standard evidence and send for review.
1779 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
1780 'error', null, true);
1781 $evidence->read();
1782 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1783 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1784
1785 // Trying to pass a grade should fail.
1786 try {
1787 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata', 'error',
1788 null, false, null, 1);
1789 $this->fail('A grade can not be set');
1790 } catch (coding_exception $e) {
1791 $this->assertRegExp('/grade MUST NOT be set/', $e->getMessage());
1792 }
1793 }
1794
1795 public function test_add_evidence_suggest() {
1796 $this->resetAfterTest(true);
1797 $dg = $this->getDataGenerator();
1798 $lpg = $dg->get_plugin_generator('tool_lp');
1799
1800 $u1 = $dg->create_user();
1801 $u1ctx = context_user::instance($u1->id);
1802 $f1 = $lpg->create_framework();
1803 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1804 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1805
1806 // Creating an evidence with minimal information.
1807 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_SUGGEST, 'invaliddata',
1808 'error', null, false, null, 1, 2);
1809 $evidence->read();
1810 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1811 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1812 $this->assertSame(null, $uc->get_grade()); // We don't grade, we just suggest.
1813 $this->assertSame(null, $uc->get_proficiency());
1814 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1815 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1816 $this->assertEquals(\tool_lp\evidence::ACTION_SUGGEST, $evidence->get_action());
1817 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1818 $this->assertEquals('error', $evidence->get_desccomponent());
1819 $this->assertSame(null, $evidence->get_desca());
1820 $this->assertSame(null, $evidence->get_url());
1821 $this->assertEquals(1, $evidence->get_grade());
1822 $this->assertEquals(2, $evidence->get_actionuserid());
1823
1824 // Creating a standard evidence and send for review.
1825 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_SUGGEST, 'invaliddata',
1826 'error', null, true, null, 1, 2);
1827 $evidence->read();
1828 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1829 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1830
1831 // Trying not to pass a grade should fail.
1832 try {
1833 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_SUGGEST, 'invaliddata', 'error',
1834 false, null);
1835 $this->fail('A grade must be set');
1836 } catch (coding_exception $e) {
1837 $this->assertRegExp('/grade MUST be set/', $e->getMessage());
1838 }
1839 }
1840
1841 public function test_add_evidence_complete() {
1842 $this->resetAfterTest(true);
1843 $dg = $this->getDataGenerator();
1844 $lpg = $dg->get_plugin_generator('tool_lp');
1845
1846 $u1 = $dg->create_user();
1847 $u1ctx = context_user::instance($u1->id);
1848 $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1849 $scaleconfig = array(array('scaleid' => $scale->id));
1850 $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1851 $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1852 $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1853 $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1854 $c2scaleconfig = array(array('scaleid' => $scale->id));
1855 $c2scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1856 $c2scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 0, 'proficient' => 1);
1857 $c2scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 0);
1858 $c2scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 1, 'proficient' => 1);
1859 $f1 = $lpg->create_framework(array('scaleid' => $scale->id, 'scaleconfiguration' => $scaleconfig));
1860 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1861 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'scaleid' => $scale->id,
1862 'scaleconfiguration' => $c2scaleconfig));
1863 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1864
1865 // Creating an evidence with minimal information.
1866 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1867 'error');
1868 $evidence->read();
1869 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1870 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1871 $this->assertEquals(2, $uc->get_grade()); // The grade has been set automatically to the framework default.
1872 $this->assertEquals(0, $uc->get_proficiency());
1873 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1874 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1875 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $evidence->get_action());
1876 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1877 $this->assertEquals('error', $evidence->get_desccomponent());
1878 $this->assertSame(null, $evidence->get_desca());
1879 $this->assertSame(null, $evidence->get_url());
1880 $this->assertEquals(2, $evidence->get_grade());
1881 $this->assertSame(null, $evidence->get_actionuserid());
1882
1883 // Creating an evidence complete on competency with custom scale.
1884 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1885 'error');
1886 $evidence->read();
1887 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1888 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1889 $this->assertEquals(4, $uc->get_grade()); // The grade has been set automatically to the competency default.
1890 $this->assertEquals(true, $uc->get_proficiency());
1891 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1892 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1893 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $evidence->get_action());
1894 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1895 $this->assertEquals('error', $evidence->get_desccomponent());
1896 $this->assertSame(null, $evidence->get_desca());
1897 $this->assertSame(null, $evidence->get_url());
1898 $this->assertEquals(4, $evidence->get_grade());
1899 $this->assertSame(null, $evidence->get_actionuserid());
1900
1901 // Creating an evidence complete on a user competency with an existing grade.
1902 $uc = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get_id(), 'grade' => 1,
1903 'proficiency' => 0));
1904 $this->assertEquals(1, $uc->get_grade());
1905 $this->assertEquals(0, $uc->get_proficiency());
1906 $evidence = api::add_evidence($u1->id, $c3->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1907 'error');
1908 $evidence->read();
1909 $uc->read();
1910 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1911 $this->assertEquals(1, $uc->get_grade()); // The grade has not been changed.
1912 $this->assertEquals(0, $uc->get_proficiency());
1913 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1914 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1915 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $evidence->get_action());
1916 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1917 $this->assertEquals('error', $evidence->get_desccomponent());
1918 $this->assertSame(null, $evidence->get_desca());
1919 $this->assertSame(null, $evidence->get_url());
1920 $this->assertEquals(2, $evidence->get_grade()); // The complete grade has been set.
1921 $this->assertSame(null, $evidence->get_actionuserid());
1922
1923 // Creating a standard evidence and send for review.
1924 $evidence = api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1925 'error', null, true);
1926 $evidence->read();
1927 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
1928 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
1929
1930 // Trying to pass a grade should throw an exception.
1931 try {
1932 api::add_evidence($u1->id, $c2->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_COMPLETE, 'invaliddata',
1933 'error', null, false, null, 1);
1934 } catch (coding_exception $e) {
1935 $this->assertRegExp('/grade MUST NOT be set/', $e->getMessage());
1936 }
1937 }
1938
1939 public function test_add_evidence_override() {
1940 $this->resetAfterTest(true);
1941 $dg = $this->getDataGenerator();
1942 $lpg = $dg->get_plugin_generator('tool_lp');
1943
1944 $u1 = $dg->create_user();
1945 $u1ctx = context_user::instance($u1->id);
1946 $f1 = $lpg->create_framework();
1947 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1948
1949 // Creating an evidence with minimal information.
1950 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1951 'error');
1952 $evidence->read();
1953 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1954 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1955 $this->assertSame(null, $uc->get_grade()); // We overrode with 'null'.
1956 $this->assertSame(null, $uc->get_proficiency());
1957 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1958 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1959 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1960 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1961 $this->assertEquals('error', $evidence->get_desccomponent());
1962 $this->assertSame(null, $evidence->get_desca());
1963 $this->assertSame(null, $evidence->get_url());
1964 $this->assertSame(null, $evidence->get_grade()); // We overrode with 'null'.
1965 $this->assertSame(null, $evidence->get_actionuserid());
1966
1967 // Creating an evidence with a grade information.
1968 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1969 'error', null, false, null, 3);
1970 $evidence->read();
1971 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1972 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1973 $this->assertEquals(3, $uc->get_grade());
1974 $this->assertEquals(true, $uc->get_proficiency());
1975 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1976 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1977 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1978 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1979 $this->assertEquals('error', $evidence->get_desccomponent());
1980 $this->assertSame(null, $evidence->get_desca());
1981 $this->assertSame(null, $evidence->get_url());
1982 $this->assertEquals(3, $evidence->get_grade());
1983 $this->assertSame(null, $evidence->get_actionuserid());
1984
1985 // Creating an evidence with another grade information.
1986 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
1987 'error', null, false, null, 1);
1988 $evidence->read();
1989 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
1990 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc->get_status());
1991 $this->assertEquals(1, $uc->get_grade());
1992 $this->assertEquals(0, $uc->get_proficiency());
1993 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
1994 $this->assertEquals($u1ctx->id, $evidence->get_contextid());
1995 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
1996 $this->assertEquals('invaliddata', $evidence->get_descidentifier());
1997 $this->assertEquals('error', $evidence->get_desccomponent());
1998 $this->assertSame(null, $evidence->get_desca());
1999 $this->assertSame(null, $evidence->get_url());
2000 $this->assertEquals(1, $evidence->get_grade());
2001 $this->assertSame(null, $evidence->get_actionuserid());
2002
2003 // Creating reverting the grade and send for review.
2004 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_OVERRIDE, 'invaliddata',
2005 'error', null, true);
2006 $evidence->read();
2007 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
2008 $this->assertSame(null, $uc->get_grade());
2009 $this->assertSame(null, $uc->get_proficiency());
2010 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
2011 $this->assertSame(null, $evidence->get_grade());
2012 }
2013
2014 public function test_add_evidence_and_send_for_review() {
2015 $this->resetAfterTest(true);
2016 $dg = $this->getDataGenerator();
2017 $lpg = $dg->get_plugin_generator('tool_lp');
2018
2019 $u1 = $dg->create_user();
2020 $u1ctx = context_user::instance($u1->id);
2021 $f1 = $lpg->create_framework();
2022 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2023
2024 // Non-existing user competencies are created up for review.
2025 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
2026 'error', null, true);
2027 $uc = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
2028 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
2029
2030 // Existing user competencies sent for review don't change.
2031 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
2032 'error', null, true);
2033 $uc->read();
2034 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc->get_status());
2035
2036 // A user competency with a status non-idle won't change.
2037 $uc->set_status(\tool_lp\user_competency::STATUS_IN_REVIEW);
2038 $uc->update();
2039 $evidence = api::add_evidence($u1->id, $c1->get_id(), $u1ctx->id, \tool_lp\evidence::ACTION_LOG, 'invaliddata',
2040 'error', null, true);
2041 $uc->read();
2042 $this->assertEquals(\tool_lp\user_competency::STATUS_IN_REVIEW, $uc->get_status());
2043 }
2044
4c0e8167
SG
2045 /**
2046 * Test add evidence for existing user_competency.
2047 */
2048 public function test_add_evidence_existing_user_competency() {
2049 $this->resetAfterTest(true);
2050 $dg = $this->getDataGenerator();
2051 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2052
2053 $syscontext = context_system::instance();
2054
2055 // Create users.
2056 $user = $dg->create_user();
2057 $this->setUser($user);
2058
2059 // Create a framework and assign competencies.
2060 $framework = $lpg->create_framework();
2061 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2062 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2063 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2064 $uc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
914b580e
FM
2065 $this->assertSame(null, $uc->get_grade());
2066 $this->assertSame(null, $uc->get_proficiency());
4c0e8167
SG
2067
2068 // Create an evidence and check it was created with the right usercomptencyid and information.
914b580e
FM
2069 $evidence = api::add_evidence($user->id, $c1->get_id(), $syscontext->id, \tool_lp\evidence::ACTION_OVERRIDE,
2070 'invalidevidencedesc', 'tool_lp', array('a' => 'b'), false, 'http://moodle.org', 1, 2);
4c0e8167
SG
2071 $this->assertEquals(1, \tool_lp\evidence::count_records());
2072
2073 $evidence->read();
914b580e 2074 $uc->read();
4c0e8167
SG
2075 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
2076 $this->assertEquals('invalidevidencedesc', $evidence->get_descidentifier());
2077 $this->assertEquals('tool_lp', $evidence->get_desccomponent());
2078 $this->assertEquals((object) array('a' => 'b'), $evidence->get_desca());
914b580e
FM
2079 $this->assertEquals('http://moodle.org', $evidence->get_url());
2080 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
2081 $this->assertEquals(2, $evidence->get_actionuserid());
4c0e8167 2082 $this->assertEquals(1, $evidence->get_grade());
914b580e
FM
2083 $this->assertEquals(1, $uc->get_grade());
2084 $this->assertEquals(0, $uc->get_proficiency());
4c0e8167
SG
2085 }
2086
2087 /**
914b580e 2088 * Test add evidence for non-existing user_competency.
4c0e8167
SG
2089 */
2090 public function test_add_evidence_no_existing_user_competency() {
2091 $this->resetAfterTest(true);
2092 $dg = $this->getDataGenerator();
2093 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2094
2095 $syscontext = context_system::instance();
2096
2097 // Create users.
2098 $user = $dg->create_user();
2099 $this->setUser($user);
2100
2101 // Create a framework and assign competencies.
2102 $framework = $lpg->create_framework();
2103 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2104 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
914b580e 2105 $this->assertEquals(0, \tool_lp\user_competency::count_records());
4c0e8167 2106
914b580e
FM
2107 // Create an evidence without a user competency record.
2108 $evidence = api::add_evidence($user->id, $c1->get_id(), $syscontext->id, \tool_lp\evidence::ACTION_OVERRIDE,
2109 'invalidevidencedesc', 'tool_lp', 'Hello world!', false, 'http://moodle.org', 1, 2);
4c0e8167 2110 $this->assertEquals(1, \tool_lp\evidence::count_records());
914b580e 2111 $this->assertEquals(1, \tool_lp\user_competency::count_records());
4c0e8167 2112
914b580e
FM
2113 $uc = \tool_lp\user_competency::get_record(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
2114 $evidence->read();
2115 $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid());
2116 $this->assertEquals('invalidevidencedesc', $evidence->get_descidentifier());
2117 $this->assertEquals('tool_lp', $evidence->get_desccomponent());
2118 $this->assertEquals('Hello world!', $evidence->get_desca());
2119 $this->assertEquals('http://moodle.org', $evidence->get_url());
2120 $this->assertEquals(\tool_lp\evidence::ACTION_OVERRIDE, $evidence->get_action());
2121 $this->assertEquals(2, $evidence->get_actionuserid());
2122 $this->assertEquals(1, $evidence->get_grade());
2123 $this->assertEquals(1, $uc->get_grade());
2124 $this->assertEquals(0, $uc->get_proficiency());
2125 }
2126
9373acf6
FM
2127 public function test_add_evidence_applies_competency_rules() {
2128 $this->resetAfterTest(true);
2129 $dg = $this->getDataGenerator();
2130 $lpg = $dg->get_plugin_generator('tool_lp');
2131 $syscontext = context_system::instance();
2132 $ctxid = $syscontext->id;
2133
2134 $u1 = $dg->create_user();
2135
2136 // Setting up the framework.
2137 $f1 = $lpg->create_framework();
2138 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2139 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2140 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2141 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2142 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c2->get_id()));
2143 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2144 $c3a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c3->get_id()));
2145 $c4 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2146 $c4a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c4->get_id()));
2147 $c5 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2148
2149 // Setting up the rules.
2150 $c1->set_ruletype('tool_lp\\competency_rule_all');
2151 $c1->set_ruleoutcome(\tool_lp\competency::OUTCOME_COMPLETE);
2152 $c1->update();
2153 $c2->set_ruletype('tool_lp\\competency_rule_all');
2154 $c2->set_ruleoutcome(\tool_lp\competency::OUTCOME_RECOMMEND);
2155 $c2->update();
2156 $c3->set_ruletype('tool_lp\\competency_rule_all');
2157 $c3->set_ruleoutcome(\tool_lp\competency::OUTCOME_EVIDENCE);
2158 $c3->update();
2159 $c4->set_ruletype('tool_lp\\competency_rule_all');
2160 $c4->set_ruleoutcome(\tool_lp\competency::OUTCOME_NONE);
2161 $c4->update();
2162
2163 // Confirm the current data.
2164 $this->assertEquals(0, user_competency::count_records());
2165 $this->assertEquals(0, evidence::count_records());
2166
2167 // Let's do this!
2168 // First let's confirm that evidence not marking a completion have no impact.
2169 api::add_evidence($u1->id, $c1a, $ctxid, evidence::ACTION_LOG, 'commentincontext', 'core');
2170 $uc1a = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1a->get_id()));
2171 $this->assertSame(null, $uc1a->get_proficiency());
2172 $this->assertFalse(user_competency::record_exists_select('userid = ? AND competencyid = ?', array($u1->id, $c1->get_id())));
2173
2174 api::add_evidence($u1->id, $c2a, $ctxid, evidence::ACTION_SUGGEST, 'commentincontext', 'core', null, false, null, 1);
2175 $uc2a = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2a->get_id()));
2176 $this->assertSame(null, $uc2a->get_proficiency());
2177 $this->assertFalse(user_competency::record_exists_select('userid = ? AND competencyid = ?', array($u1->id, $c2->get_id())));
2178
2179 // Now let's try complete a competency but the rule won't match (not all children are complete).
2180 // The parent (the thing with the rule) will be created but won't have any evidence attached, and not
2181 // not be marked as completed.
2182 api::add_evidence($u1->id, $c1a, $ctxid, evidence::ACTION_COMPLETE, 'commentincontext', 'core');
2183 $uc1a = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1a->get_id()));
2184 $this->assertEquals(true, $uc1a->get_proficiency());
2185 $uc1 = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
2186 $this->assertSame(null, $uc1->get_proficiency());
2187 $this->assertEquals(0, evidence::count_records(array('usercompetencyid' => $uc1->get_id())));
2188
2189 // Now we complete the other child. That will mark the parent as complete with an evidence.
2190 api::add_evidence($u1->id, $c1b, $ctxid, evidence::ACTION_COMPLETE, 'commentincontext', 'core');
2191 $uc1b = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1b->get_id()));
2192 $this->assertEquals(true, $uc1a->get_proficiency());
2193 $uc1 = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c1->get_id()));
2194 $this->assertEquals(true, $uc1->get_proficiency());
2195 $this->assertEquals(user_competency::STATUS_IDLE, $uc1->get_status());
2196 $this->assertEquals(1, evidence::count_records(array('usercompetencyid' => $uc1->get_id())));
2197
2198 // Check rule recommending.
2199 api::add_evidence($u1->id, $c2a, $ctxid, evidence::ACTION_COMPLETE, 'commentincontext', 'core');
2200 $uc2a = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2a->get_id()));
2201 $this->assertEquals(true, $uc1a->get_proficiency());
2202 $uc2 = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
2203 $this->assertSame(null, $uc2->get_proficiency());
2204 $this->assertEquals(user_competency::STATUS_WAITING_FOR_REVIEW, $uc2->get_status());
2205 $this->assertEquals(1, evidence::count_records(array('usercompetencyid' => $uc2->get_id())));
2206
2207 // Check rule evidence.
2208 api::add_evidence($u1->id, $c3a, $ctxid, evidence::ACTION_COMPLETE, 'commentincontext', 'core');
2209 $uc3a = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c3a->get_id()));
2210 $this->assertEquals(true, $uc1a->get_proficiency());
2211 $uc3 = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c3->get_id()));
2212 $this->assertSame(null, $uc3->get_proficiency());
2213 $this->assertEquals(user_competency::STATUS_IDLE, $uc3->get_status());
2214 $this->assertEquals(1, evidence::count_records(array('usercompetencyid' => $uc3->get_id())));
2215
2216 // Check rule nothing.
2217 api::add_evidence($u1->id, $c4a, $ctxid, evidence::ACTION_COMPLETE, 'commentincontext', 'core');
2218 $uc4a = user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c4a->get_id()));
2219 $this->assertEquals(true, $uc1a->get_proficiency());
2220 $this->assertFalse(user_competency::record_exists_select('userid = ? AND competencyid = ?', array($u1->id, $c4->get_id())));
2221
2222 // Check marking on something that has no parent. This just checks that nothing breaks.
2223 api::add_evidence($u1->id, $c5, $ctxid, evidence::ACTION_COMPLETE, 'commentincontext', 'core');
2224 }
2225
914b580e
FM
2226 public function test_observe_course_completed() {
2227 $this->resetAfterTest(true);
2228 $dg = $this->getDataGenerator();
2229 $lpg = $dg->get_plugin_generator('tool_lp');
2230
2231 // Set-up users, framework, competencies and course competencies.
2232 $course = $dg->create_course();
2233 $coursectx = context_course::instance($course->id);
2234 $u1 = $dg->create_user();
2235 $f1 = $lpg->create_framework();
2236 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2237 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2238 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2239 $c4 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2240 $cc1 = $lpg->create_course_competency(array('competencyid' => $c1->get_id(), 'courseid' => $course->id,
2241 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_NONE));
2242 $cc2 = $lpg->create_course_competency(array('competencyid' => $c2->get_id(), 'courseid' => $course->id,
2243 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_EVIDENCE));
2244 $cc3 = $lpg->create_course_competency(array('competencyid' => $c3->get_id(), 'courseid' => $course->id,
2245 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_RECOMMEND));
2246 $cc4 = $lpg->create_course_competency(array('competencyid' => $c4->get_id(), 'courseid' => $course->id,
2247 'ruleoutcome' => \tool_lp\course_competency::OUTCOME_COMPLETE));
2248
2249 $event = \core\event\course_completed::create(array(
2250 'objectid' => 1,
2251 'relateduserid' => $u1->id,
2252 'context' => $coursectx,
2253 'courseid' => $course->id,
2254 'other' => array('relateduserid' => $u1->id)
2255 ));
2256 $this->assertEquals(0, \tool_lp\user_competency::count_records());
2257 $this->assertEquals(0, \tool_lp\evidence::count_records());
2258
2259 // Let's go!
2260 api::observe_course_completed($event);
2261 $this->assertEquals(3, \tool_lp\user_competency::count_records());
2262 $this->assertEquals(3, \tool_lp\evidence::count_records());
2263
2264 // Outcome NONE did nothing.
2265 $this->assertFalse(\tool_lp\user_competency::record_exists_select('userid = :uid AND competencyid = :cid', array(
2266 'uid' => $u1->id, 'cid' => $c1->get_id()
2267 )));
2268
2269 // Outcome evidence.
2270 $uc2 = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c2->get_id()));
2271 $ev2 = \tool_lp\evidence::get_record(array('usercompetencyid' => $uc2->get_id()));
2272
2273 $this->assertEquals(null, $uc2->get_grade());
2274 $this->assertEquals(null, $uc2->get_proficiency());
2275 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc2->get_status());
2276
2277 $this->assertEquals('evidence_coursecompleted', $ev2->get_descidentifier());
2278 $this->assertEquals('tool_lp', $ev2->get_desccomponent());
2279 $this->assertEquals($course->shortname, $ev2->get_desca());
2280 $this->assertStringEndsWith('/report/completion/index.php?course=' . $course->id, $ev2->get_url());
2281 $this->assertEquals(null, $ev2->get_grade());
2282 $this->assertEquals($coursectx->id, $ev2->get_contextid());
2283 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $ev2->get_action());
2284 $this->assertEquals(null, $ev2->get_actionuserid());
2285
2286 // Outcome recommend.
2287 $uc3 = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c3->get_id()));
2288 $ev3 = \tool_lp\evidence::get_record(array('usercompetencyid' => $uc3->get_id()));
2289
2290 $this->assertEquals(null, $uc3->get_grade());
2291 $this->assertEquals(null, $uc3->get_proficiency());
2292 $this->assertEquals(\tool_lp\user_competency::STATUS_WAITING_FOR_REVIEW, $uc3->get_status());
2293
2294 $this->assertEquals('evidence_coursecompleted', $ev3->get_descidentifier());
2295 $this->assertEquals('tool_lp', $ev3->get_desccomponent());
2296 $this->assertEquals($course->shortname, $ev3->get_desca());
2297 $this->assertStringEndsWith('/report/completion/index.php?course=' . $course->id, $ev3->get_url());
2298 $this->assertEquals(null, $ev3->get_grade());
2299 $this->assertEquals($coursectx->id, $ev3->get_contextid());
2300 $this->assertEquals(\tool_lp\evidence::ACTION_LOG, $ev3->get_action());
2301 $this->assertEquals(null, $ev3->get_actionuserid());
2302
2303 // Outcome complete.
2304 $uc4 = \tool_lp\user_competency::get_record(array('userid' => $u1->id, 'competencyid' => $c4->get_id()));
2305 $ev4 = \tool_lp\evidence::get_record(array('usercompetencyid' => $uc4->get_id()));
2306
2307 $this->assertEquals(3, $uc4->get_grade());
2308 $this->assertEquals(1, $uc4->get_proficiency());
2309 $this->assertEquals(\tool_lp\user_competency::STATUS_IDLE, $uc4->get_status());
2310
2311 $this->assertEquals('evidence_coursecompleted', $ev4->get_descidentifier());
2312 $this->assertEquals('tool_lp', $ev4->get_desccomponent());
2313 $this->assertEquals($course->shortname, $ev4->get_desca());
2314 $this->assertStringEndsWith('/report/completion/index.php?course=' . $course->id, $ev4->get_url());
2315 $this->assertEquals(3, $ev4->get_grade());
2316 $this->assertEquals($coursectx->id, $ev4->get_contextid());
2317 $this->assertEquals(\tool_lp\evidence::ACTION_COMPLETE, $ev4->get_action());
2318 $this->assertEquals(null, $ev4->get_actionuserid());
4c0e8167 2319 }
d4c0a2f6
SG
2320
2321 /**
2322 * Test update ruleoutcome for course_competency.
2323 */
d660824b 2324 public function test_set_ruleoutcome_course_competency() {
d4c0a2f6
SG
2325 $this->resetAfterTest(true);
2326 $dg = $this->getDataGenerator();
2327 $lpg = $dg->get_plugin_generator('tool_lp');
2328 $u1 = $dg->create_user();
2329 $u2 = $dg->create_user();
d660824b 2330 $course = $dg->create_course();
d4c0a2f6
SG
2331
2332 $this->setAdminUser();
2333 $f = $lpg->create_framework();
2334 $c = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
d660824b 2335 $cc = api::add_competency_to_course($course->id, $c->get_id());
d4c0a2f6
SG
2336
2337 // Check record was created with default rule value Evidence.
2338 $this->assertEquals(1, \tool_lp\course_competency::count_records());
d660824b
FM
2339 $recordscc = api::list_course_competencies($course->id);
2340 $this->assertEquals(\tool_lp\course_competency::OUTCOME_EVIDENCE, $recordscc[0]['coursecompetency']->get_ruleoutcome());
d4c0a2f6
SG
2341
2342 // Check ruleoutcome value is updated to None.
d660824b
FM
2343 $this->assertTrue(api::set_course_competency_ruleoutcome($recordscc[0]['coursecompetency']->get_id(),
2344 \tool_lp\course_competency::OUTCOME_NONE));
2345 $recordscc = api::list_course_competencies($course->id);
2346 $this->assertEquals(\tool_lp\course_competency::OUTCOME_NONE, $recordscc[0]['coursecompetency']->get_ruleoutcome());
d4c0a2f6
SG
2347 }
2348
4de456cd
SG
2349 /**
2350 * Test validation on grade on user_competency.
2351 */
2352 public function test_validate_grade_in_user_competency() {
2353 global $DB;
2354
2355 $this->resetAfterTest(true);
2356 $this->setAdminUser();
2357 $dg = $this->getDataGenerator();
2358 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2359 $user = $dg->create_user();
2360
2361 $dg->create_scale(array("id" => "1", "scale" => "value1, value2"));
2362 $dg->create_scale(array("id" => "2", "scale" => "value3, value4, value5, value6"));
2363
2364 $scaleconfiguration1 = '[{"scaleid":"1"},{"name":"value1","id":1,"scaledefault":1,"proficient":0},' .
2365 '{"name":"value2","id":2,"scaledefault":0,"proficient":1}]';
2366 $scaleconfiguration2 = '[{"scaleid":"2"},{"name":"value3","id":1,"scaledefault":1,"proficient":0},'
2367 . '{"name":"value4","id":2,"scaledefault":0,"proficient":1},'
2368 . '{"name":"value5","id":3,"scaledefault":0,"proficient":0},'
2369 . '{"name":"value6","id":4,"scaledefault":0,"proficient":0}]';
2370
2371 // Create a framework with scale configuration1.
2372 $frm = array(
2373 'scaleid' => 1,
2374 'scaleconfiguration' => $scaleconfiguration1
2375 );
2376 $framework = $lpg->create_framework($frm);
2377 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2378
2379 // Create competency with its own scale configuration.
2380 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(),
2381 'scaleid' => 2,
2382 'scaleconfiguration' => $scaleconfiguration2
2383 ));
2384
2385 // Detecte invalid grade in competency using its framework competency scale.
2386 try {
2387 $usercompetency = new user_competency(0, (object) array('userid' => $user->id, 'competencyid' => $c1->get_id(),
2388 'proficiency' => true, 'grade' => 3 ));
2389 $usercompetency->create();
2390 $this->fail('Invalid grade not detected in framework scale');
2391 } catch (\tool_lp\invalid_persistent_exception $e) {
2392 $this->assertTrue(true);
2393 }
2394
2395 // Detecte invalid grade in competency using its own scale.
2396 try {
2397 $usercompetency = new user_competency(0, (object) array('userid' => $user->id, 'competencyid' => $c2->get_id(),
2398 'proficiency' => true, 'grade' => 5 ));
2399 $usercompetency->create();
2400 $this->fail('Invalid grade not detected in competency scale');
2401 } catch (\tool_lp\invalid_persistent_exception $e) {
2402 $this->assertTrue(true);
2403 }
2404
2405 // Accept valid grade in competency using its framework competency scale.
2406 try {
2407 $usercompetency = new user_competency(0, (object) array('userid' => $user->id, 'competencyid' => $c1->get_id(),
2408 'proficiency' => true, 'grade' => 1 ));
2409 $usercompetency->create();
2410 $this->assertTrue(true);
2411 } catch (\tool_lp\invalid_persistent_exception $e) {
2412 $this->fail('Valide grade rejected in framework scale');
2413 }
2414
2415 // Accept valid grade in competency using its framework competency scale.
2416 try {
2417 $usercompetency = new user_competency(0, (object) array('userid' => $user->id, 'competencyid' => $c2->get_id(),
2418 'proficiency' => true, 'grade' => 4 ));
2419 $usercompetency->create();
2420 $this->assertTrue(true);
2421 } catch (\tool_lp\invalid_persistent_exception $e) {
4de456cd
SG
2422 $this->fail('Valide grade rejected in competency scale');
2423 }
2424 }
2425
964afa98
IT
2426 /**
2427 * Test when adding competency that belong to hidden framework to plan/template/course.
2428 */
2429 public function test_hidden_framework() {
2430 $this->resetAfterTest(true);
2431 $this->setAdminUser();
2432 $dg = $this->getDataGenerator();
2433 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2434 $user = $dg->create_user();
2435
2436 // Create a course.
2437 $cat1 = $dg->create_category();
2438 $course = $dg->create_course(array('category' => $cat1->id));
2439 // Create a template.
2440 $template = $lpg->create_template();
2441 // Create a plan.
2442 $plan = $lpg->create_plan(array('userid' => $user->id));
2443
2444 // Create a hidden framework.
2445 $frm = array(
2446 'visible' => false
2447 );
2448 $framework = $lpg->create_framework($frm);
2449 $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2450
2451 // Linking competency that belong to hidden framework to course.
2452 try {
2453 api::add_competency_to_course($course->id, $competency->get_id());
2454 $this->fail('A competency belonging to hidden framework can not be linked to course');
2455 } catch (coding_exception $e) {
2456 $this->assertTrue(true);
2457 }
2458
2459 // Adding competency that belong to hidden framework to template.
2460 try {
2461 api::add_competency_to_template($template->get_id(), $competency->get_id());
2462 $this->fail('A competency belonging to hidden framework can not be added to template');
2463 } catch (coding_exception $e) {
2464 $this->assertTrue(true);
2465 }
2466
2467 // Adding competency that belong to hidden framework to plan.
2468 try {
2469 api::add_competency_to_plan($plan->get_id(), $competency->get_id());
2470 $this->fail('A competency belonging to hidden framework can not be added to plan');
2471 } catch (coding_exception $e) {
2472 $this->assertTrue(true);
2473 }
2474 }
2475
2476 /**
2477 * Test when using hidden template in plan/cohort.
2478 */
2479 public function test_hidden_template() {
2480 $this->resetAfterTest(true);
2481 $this->setAdminUser();
2482 $dg = $this->getDataGenerator();
2483 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2484 $user = $dg->create_user();
2485
2486 // Create a cohort.
2487 $cohort = $dg->create_cohort();
2488 // Create a hidden template.
2489 $template = $lpg->create_template(array('visible' => false));
2490
2491 // Can not link hidden template to plan.
2492 try {
2493 api::create_plan_from_template($template->get_id(), $user->id);
2494 $this->fail('Can not link a hidden template to plan');
2495 } catch (coding_exception $e) {
2496 $this->assertTrue(true);
2497 }
2498
2499 // Can associate hidden template to cohort.
2500 $templatecohort = api::create_template_cohort($template->get_id(), $cohort->id);
2501 $this->assertInstanceOf('\tool_lp\template_cohort', $templatecohort);
2502 }
2503
d3275795
SG
2504 /**
2505 * Test that completed plan created form a template does not change when template is modified.
2506 */
2507 public function test_completed_plan_doesnot_change() {
2508 global $DB;
2509
2510 $this->resetAfterTest(true);
2511 $this->setAdminUser();
2512 $dg = $this->getDataGenerator();
2513 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2514 $user = $dg->create_user();
2515
2516 // Create a framework and assign competencies.
2517 $framework = $lpg->create_framework();
2518 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2519 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2520 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2521 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2522
2523 // Create template and assign competencies.
2524 $tp = $lpg->create_template();
2525 $tpc1 = $lpg->create_template_competency(array('templateid' => $tp->get_id(), 'competencyid' => $c1->get_id()));
2526 $tpc2 = $lpg->create_template_competency(array('templateid' => $tp->get_id(), 'competencyid' => $c2->get_id()));
2527 $tpc3 = $lpg->create_template_competency(array('templateid' => $tp->get_id(), 'competencyid' => $c3->get_id()));
2528
2529 // Create a plan form template and change it status to complete.
2530 $plan = $lpg->create_plan(array('userid' => $user->id, 'templateid' => $tp->get_id()));
2531 api::complete_plan($plan);
2532
2533 // Check user competency plan created correctly.
2534 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
2535 $ucp = \tool_lp\user_competency_plan::get_records();
2536 $this->assertEquals($ucp[0]->get_competencyid(), $c1->get_id());
2537 $this->assertEquals($ucp[1]->get_competencyid(), $c2->get_id());
2538 $this->assertEquals($ucp[2]->get_competencyid(), $c3->get_id());
2539
2540 // Add and remove a competency from the template.
2541 api::add_competency_to_template($tp->get_id(), $c4->get_id());
2542 api::remove_competency_from_template($tp->get_id(), $c1->get_id());
2543
2544 // Check that user competency plan did not change.
2545 $competencies = $plan->get_competencies();
2546 $this->assertEquals(3, count($competencies));
2547 $ucp1 = array($c1->get_id(), $c2->get_id(), $c3->get_id());
2548 $ucp2 = array();
2549 foreach ($competencies as $id => $cmp) {
2550 $ucp2[] = $id;
2551 }
2552 $this->assertEquals(0, count(array_diff($ucp1, $ucp2)));
2553 }
20c2fe3e
FM
2554
2555 protected function setup_framework_for_reset_rules_tests() {
2556 $this->resetAfterTest(true);
2557 $dg = $this->getDataGenerator();
2558 $lpg = $dg->get_plugin_generator('tool_lp');
2559
2560 $this->setAdminUser();
2561 $f1 = $lpg->create_framework();
2562 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2563 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2564 $c1a1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
2565 $c1a1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a1->get_id()));
2566 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2567 $c1b1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2568 $c1b1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b1->get_id()));
2569 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2570 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2571
2572 $c1->set_ruleoutcome(competency::OUTCOME_EVIDENCE);
2573 $c1->set_ruletype('tool_lp\\competency_rule_all');
2574 $c1->update();
2575 $c1a->set_ruleoutcome(competency::OUTCOME_EVIDENCE);
2576 $c1a->set_ruletype('tool_lp\\competency_rule_all');
2577 $c1a->update();
2578 $c1a1->set_ruleoutcome(competency::OUTCOME_EVIDENCE);
2579 $c1a1->set_ruletype('tool_lp\\competency_rule_all');
2580 $c1a1->update();
2581 $c1b->set_ruleoutcome(competency::OUTCOME_EVIDENCE);
2582 $c1b->set_ruletype('tool_lp\\competency_rule_all');
2583 $c1b->update();
2584 $c2->set_ruleoutcome(competency::OUTCOME_EVIDENCE);
2585 $c2->set_ruletype('tool_lp\\competency_rule_all');
2586 $c2->update();
2587
2588 return array(
2589 'f1' => $f1,
2590 'c1' => $c1,
2591 'c1a' => $c1a,
2592 'c1a1' => $c1a1,
2593 'c1a1a' => $c1a1a,
2594 'c1b' => $c1b,
2595 'c1b1' => $c1b1,
2596 'c1b1a' => $c1b1a,
2597 'c2' => $c2,
2598 'c2a' => $c2a,
2599 );
2600 }
2601
2602 public function test_moving_competency_reset_rules_updown() {
2603 extract($this->setup_framework_for_reset_rules_tests());
2604
2605 // Moving up and down doesn't change anything.
2606 api::move_down_competency($c1a->get_id());
2607 $c1->read();
2608 $c1a->read();
2609 $c1a1->read();
2610 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1->get_ruleoutcome());
2611 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a->get_ruleoutcome());
2612 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a1->get_ruleoutcome());
2613 api::move_up_competency($c1a->get_id());
2614 $c1->read();
2615 $c1a->read();
2616 $c1a1->read();
2617 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1->get_ruleoutcome());
2618 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a->get_ruleoutcome());
2619 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a1->get_ruleoutcome());
2620 }
2621
2622 public function test_moving_competency_reset_rules_parent() {
2623 extract($this->setup_framework_for_reset_rules_tests());
2624
2625 // Moving out of parent will reset the parent, and the destination.
2626 api::set_parent_competency($c1a->get_id(), $c1b->get_id());
2627 $c1->read();
2628 $c1a->read();
2629 $c1a1->read();
2630 $c1b->read();
2631 $c2->read();
2632 $this->assertEquals(competency::OUTCOME_NONE, $c1->get_ruleoutcome());
2633 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a->get_ruleoutcome());
2634 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a1->get_ruleoutcome());
2635 $this->assertEquals(competency::OUTCOME_NONE, $c1b->get_ruleoutcome());
2636 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c2->get_ruleoutcome());
2637 }
2638
2639 public function test_moving_competency_reset_rules_totoplevel() {
2640 extract($this->setup_framework_for_reset_rules_tests());
2641
2642 // Moving to top level only affects the initial parent.
2643 api::set_parent_competency($c1a1->get_id(), 0);
2644 $c1->read();
2645 $c1a->read();
2646 $c1a1->read();
2647 $c1b->read();
2648 $c2->read();
2649 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1->get_ruleoutcome());
2650 $this->assertEquals(competency::OUTCOME_NONE, $c1a->get_ruleoutcome());
2651 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a1->get_ruleoutcome());
2652 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1b->get_ruleoutcome());
2653 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c2->get_ruleoutcome());
2654 }
2655
2656 public function test_moving_competency_reset_rules_fromtoplevel() {
2657 extract($this->setup_framework_for_reset_rules_tests());
2658
2659 // Moving from top level only affects the destination parent.
2660 api::set_parent_competency($c2->get_id(), $c1a1->get_id());
2661 $c1->read();
2662 $c1a->read();
2663 $c1a1->read();
2664 $c1b->read();
2665 $c2->read();
2666 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1->get_ruleoutcome());
2667 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a->get_ruleoutcome());
2668 $this->assertEquals(competency::OUTCOME_NONE, $c1a1->get_ruleoutcome());
2669 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1b->get_ruleoutcome());
2670 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c2->get_ruleoutcome());
2671 }
2672
2673 public function test_moving_competency_reset_rules_child() {
2674 extract($this->setup_framework_for_reset_rules_tests());
2675
2676 // Moving to a child of self resets self, parent and destination.
2677 api::set_parent_competency($c1a->get_id(), $c1a1->get_id());
2678 $c1->read();
2679 $c1a->read();
2680 $c1a1->read();
2681 $c1b->read();
2682 $c2->read();
2683 $this->assertEquals(competency::OUTCOME_NONE, $c1->get_ruleoutcome());
2684 $this->assertEquals(competency::OUTCOME_NONE, $c1a->get_ruleoutcome());
2685 $this->assertEquals(competency::OUTCOME_NONE, $c1a1->get_ruleoutcome());
2686 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1b->get_ruleoutcome());
2687 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c2->get_ruleoutcome());
2688 }
2689
2690 public function test_create_competency_reset_rules() {
2691 extract($this->setup_framework_for_reset_rules_tests());
2692
2693 // Adding a new competency resets the rule of its parent.
2694 api::create_competency((object) array('shortname' => 'A', 'parentid' => $c1->get_id(), 'idnumber' => 'A',
2695 'competencyframeworkid' => $f1->get_id()));
2696 $c1->read();
2697 $c1a->read();
2698 $c1a1->read();
2699 $c1b->read();
2700 $c2->read();
2701 $this->assertEquals(competency::OUTCOME_NONE, $c1->get_ruleoutcome());
2702 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a->get_ruleoutcome());
2703 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1a1->get_ruleoutcome());
2704 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1b->get_ruleoutcome());
2705 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c2->get_ruleoutcome());
2706 }
2707
2708 public function test_delete_competency_reset_rules() {
2709 extract($this->setup_framework_for_reset_rules_tests());
2710
2711 // Deleting a competency resets the rule of its parent.
2712 api::delete_competency($c1a->get_id());
2713 $c1->read();
2714 $c1b->read();
2715 $c2->read();
2716 $this->assertEquals(competency::OUTCOME_NONE, $c1->get_ruleoutcome());
2717 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c1b->get_ruleoutcome());
2718 $this->assertEquals(competency::OUTCOME_EVIDENCE, $c2->get_ruleoutcome());
2719 }
8fabc738
SG
2720
2721 public function test_template_has_related_data() {
2722 $this->resetAfterTest(true);
2723 $this->setAdminUser();
2724
2725 $dg = $this->getDataGenerator();
2726 $user = $dg->create_user();
2727 $lpg = $dg->get_plugin_generator('tool_lp');
2728 $tpl1 = $lpg->create_template();
2729 $tpl2 = $lpg->create_template();
2730
2731 // Create plans for first template.
2732 $time = time();
2733 $plan1 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $user->id,
2734 'name' => 'Not good name', 'duedate' => $time + 3600, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
2735
2736 $this->assertTrue(api::template_has_related_data($tpl1->get_id()));
2737 $this->assertFalse(api::template_has_related_data($tpl2->get_id()));
2738
2739 }
2740
2741 public function test_delete_template_delete_plans() {
2742 $this->resetAfterTest(true);
2743 $this->setAdminUser();
2744
2745 $dg = $this->getDataGenerator();
2746 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2747
2748 $u1 = $dg->create_user();
2749 $f = $lpg->create_framework();
2750 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
2751 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
2752
2753 $tpl = $lpg->create_template();
2754
2755 $tplc1 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1->get_id(),
2756 'sortorder' => 1));
2757 $tplc2 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c2->get_id(),
2758 'sortorder' => 2));
2759
2760 $p1 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u1->id));
2761
2762 // Check pre-test.
2763 $this->assertTrue(tool_lp\template::record_exists($tpl->get_id()));
2764 $this->assertEquals(2, \tool_lp\template_competency::count_competencies($tpl->get_id()));
2765 $this->assertEquals(1, count(\tool_lp\plan::get_records(array('templateid' => $tpl->get_id()))));
2766
2767 $result = api::delete_template($tpl->get_id(), true);
2768 $this->assertTrue($result);
2769
2770 // Check that the template does not exist anymore.
2771 $this->assertFalse(tool_lp\template::record_exists($tpl->get_id()));
2772
2773 // Check that associated competencies are also deleted.
2774 $this->assertEquals(0, \tool_lp\template_competency::count_competencies($tpl->get_id()));
2775
2776 // Check that associated plan are also deleted.
2777 $this->assertEquals(0, count(\tool_lp\plan::get_records(array('templateid' => $tpl->get_id()))));
2778 }
2779
2780 public function test_delete_template_unlink_plans() {
2781 $this->resetAfterTest(true);
2782 $this->setAdminUser();
2783
2784 $dg = $this->getDataGenerator();
2785 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
2786
2787 $u1 = $dg->create_user();
2788 $f = $lpg->create_framework();
2789 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
2790 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
2791
2792 $tpl = $lpg->create_template();
2793
2794 $tplc1 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1->get_id(),
2795 'sortorder' => 1));
2796 $tplc2 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c2->get_id(),
2797 'sortorder' => 2));
2798
2799 $p1 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u1->id));
2800
2801 // Check pre-test.
2802 $this->assertTrue(tool_lp\template::record_exists($tpl->get_id()));
2803 $this->assertEquals(2, \tool_lp\template_competency::count_competencies($tpl->get_id()));
2804 $this->assertEquals(1, count(\tool_lp\plan::get_records(array('templateid' => $tpl->get_id()))));
2805
2806 $result = api::delete_template($tpl->get_id(), false);
2807 $this->assertTrue($result);
2808
2809 // Check that the template does not exist anymore.
2810 $this->assertFalse(tool_lp\template::record_exists($tpl->get_id()));
2811
2812 // Check that associated competencies are also deleted.
2813 $this->assertEquals(0, \tool_lp\template_competency::count_competencies($tpl->get_id()));
2814
2815 // Check that associated plan still exist but unlink from template.
2816 $plans = \tool_lp\plan::get_records(array('id' => $p1->get_id()));
2817 $this->assertEquals(1, count($plans));
2818 $this->assertEquals($plans[0]->get_origtemplateid(), $tpl->get_id());
2819 $this->assertNull($plans[0]->get_templateid());
2820 }
9a325eda
IT
2821
2822 public function test_delete_competency() {
2823 $this->resetAfterTest(true);
2824 $dg = $this->getDataGenerator();
2825 $lpg = $dg->get_plugin_generator('tool_lp');
2826 $this->setAdminUser();
2827
2828 $u1 = $dg->create_user();
2829
2830 $f1 = $lpg->create_framework();
2831 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2832 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2833 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2834 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
2835 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2836 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2837
2838 // Set rules on parent competency.
2839 $c1->set_ruleoutcome(competency::OUTCOME_EVIDENCE);
2840 $c1->set_ruletype('tool_lp\\competency_rule_all');
2841 $c1->update();
2842
2843 // If we delete competeny, the related competencies relations and evidences should be deleted.
2844 // Create related competencies using one of c1a competency descendants.
2845 $rc = $lpg->create_related_competency(array(
2846 'competencyid' => $c2->get_id(),
2847 'relatedcompetencyid' => $c11b->get_id()
2848 ));
2849 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
2850
2851 // Creating a standard evidence with minimal information.
2852 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
2853 $evidence = $lpg->create_evidence(array('usercompetencyid' => $uc2->get_id()));
2854 $this->assertEquals($uc2->get_id(), $evidence->get_usercompetencyid());
2855 $uc2->delete();
2856
2857 $this->assertTrue(api::delete_competency($c1a->get_id()));
2858 $this->assertFalse(competency::record_exists($c1a->get_id()));
2859
2860 // Check that on delete, we reset the rule on parent competency.
2861 $c1->read();
2862 $this->assertNull($c1->get_ruletype());
2863 $this->assertNull($c1->get_ruletype());
2864 $this->assertEquals(competency::OUTCOME_NONE, $c1->get_ruleoutcome());
2865
2866 // Check that descendants were also deleted.
2867 $this->assertFalse(competency::record_exists($c1b->get_id()));
2868 $this->assertFalse(competency::record_exists($c11b->get_id()));
2869 $this->assertFalse(competency::record_exists($c12b->get_id()));
2870
2871 // Check if evidence are also deleted.
2872 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
2873
2874 // Check if related conpetency relation is deleted.
2875 $this->assertEquals(0, count(api::list_related_competencies($c2->get_id())));
2876
2877 // Delete a simple competency.
2878 $this->assertTrue(api::delete_competency($c2->get_id()));
2879 $this->assertFalse(competency::record_exists($c2->get_id()));
2880 }
2881
2882 public function test_delete_competency_used_in_plan() {
2883 $this->resetAfterTest(true);
2884 $dg = $this->getDataGenerator();
2885 $lpg = $dg->get_plugin_generator('tool_lp');
2886 $this->setAdminUser();
2887
2888 $u1 = $dg->create_user();
2889
2890 $plan = $lpg->create_plan((object) array('userid' => $u1->id));
2891
2892 $f1 = $lpg->create_framework();
2893 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2894 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2895 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2896 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
2897 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2898 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2899
2900 // Add competency to plan.
2901 $pc = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c11b->get_id()));
2902 // We can not delete a competency , if competency or competency children is associated to plan.
2903 $this->assertFalse(api::delete_competency($c1a->get_id()));
2904
2905 // We can delete the competency if we remove the competency from the plan.
2906 $pc->delete();
2907
2908 $this->assertTrue(api::delete_competency($c1a->get_id()));
2909 $this->assertFalse(competency::record_exists($c1a->get_id()));
2910 $this->assertFalse(competency::record_exists($c1b->get_id()));
2911 $this->assertFalse(competency::record_exists($c11b->get_id()));
2912 $this->assertFalse(competency::record_exists($c12b->get_id()));
2913 }
2914
2915 public function test_delete_competency_used_in_usercompetency() {
2916 $this->resetAfterTest(true);
2917 $dg = $this->getDataGenerator();
2918 $lpg = $dg->get_plugin_generator('tool_lp');
2919 $this->setAdminUser();
2920
2921 $u1 = $dg->create_user();
2922
2923 $f1 = $lpg->create_framework();
2924 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2925 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2926 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2927 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
2928 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2929 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2930
2931 // Create user competency.
2932 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
2933
2934 // We can not delete a competency , if competency or competency children exist in user competency.
2935 $this->assertFalse(api::delete_competency($c1a->get_id()));
2936
2937 // We can delete the competency if we remove the competency from user competency.
2938 $uc1->delete();
2939
2940 $this->assertTrue(api::delete_competency($c1a->get_id()));
2941 $this->assertFalse(competency::record_exists($c1a->get_id()));
2942 $this->assertFalse(competency::record_exists($c1b->get_id()));
2943 $this->assertFalse(competency::record_exists($c11b->get_id()));
2944 $this->assertFalse(competency::record_exists($c12b->get_id()));
2945 }
2946
2947 public function test_delete_competency_used_in_usercompetencyplan() {
2948 $this->resetAfterTest(true);
2949 $dg = $this->getDataGenerator();
2950 $lpg = $dg->get_plugin_generator('tool_lp');
2951 $this->setAdminUser();
2952
2953 $u1 = $dg->create_user();
2954
2955 $plan = $lpg->create_plan((object) array('userid' => $u1->id));
2956
2957 $f1 = $lpg->create_framework();
2958 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2959 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2960 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2961 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
2962 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2963 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2964
2965 // Create user competency plan.
2966 $uc2 = $lpg->create_user_competency_plan(array(
2967 'userid' => $u1->id,
2968 'competencyid' => $c11b->get_id(),
2969 'planid' => $plan->get_id()
2970 ));
2971
2972 // We can not delete a competency , if competency or competency children exist in user competency plan.
2973 $this->assertFalse(api::delete_competency($c1a->get_id()));
2974
2975 // We can delete the competency if we remove the competency from user competency plan.
2976 $uc2->delete();
2977
2978 $this->assertTrue(api::delete_competency($c1a->get_id()));
2979 $this->assertFalse(competency::record_exists($c1a->get_id()));
2980 $this->assertFalse(competency::record_exists($c1b->get_id()));
2981 $this->assertFalse(competency::record_exists($c11b->get_id()));
2982 $this->assertFalse(competency::record_exists($c12b->get_id()));
2983 }
2984
2985 public function test_delete_competency_used_in_template() {
2986 $this->resetAfterTest(true);
2987 $dg = $this->getDataGenerator();
2988 $lpg = $dg->get_plugin_generator('tool_lp');
2989 $this->setAdminUser();
2990
2991 $template = $lpg->create_template();
2992
2993 $f1 = $lpg->create_framework();
2994 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2995 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2996 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
2997 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
2998 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
2999 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3000
3001 // Add competency to a template.
3002 $tc = $lpg->create_template_competency(array(
3003 'templateid' => $template->get_id(),
3004 'competencyid' => $c11b->get_id()
3005 ));
3006 // We can not delete a competency , if competency or competency children is linked to template.
3007 $this->assertFalse(api::delete_competency($c1a->get_id()));
3008
3009 // We can delete the competency if we remove the competency from template.
3010 $tc->delete();
3011
3012 $this->assertTrue(api::delete_competency($c1a->get_id()));
3013 $this->assertFalse(competency::record_exists($c1a->get_id()));
3014 $this->assertFalse(competency::record_exists($c1b->get_id()));
3015 $this->assertFalse(competency::record_exists($c11b->get_id()));
3016 $this->assertFalse(competency::record_exists($c12b->get_id()));
3017 }
3018
3019 public function test_delete_competency_used_in_course() {
3020 $this->resetAfterTest(true);
3021 $dg = $this->getDataGenerator();
3022 $lpg = $dg->get_plugin_generator('tool_lp');
3023 $this->setAdminUser();
3024
3025 $cat1 = $dg->create_category();
3026
3027 $course = $dg->create_course(array('category' => $cat1->id));
3028
3029 $f1 = $lpg->create_framework();
3030 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3031 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3032 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3033 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3034 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3035 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3036
3037 // Add competency to course.
3038 $cc = $lpg->create_course_competency(array(
3039 'courseid' => $course->id,
3040 'competencyid' => $c11b->get_id()
3041 ));
3042
3043 // We can not delete a competency if the competency or competencies children is linked to a course.
3044 $this->assertFalse(api::delete_competency($c1a->get_id()));
3045
3046 // We can delete the competency if we remove the competency from course.
3047 $cc->delete();
3048
3049 $this->assertTrue(api::delete_competency($c1a->get_id()));
3050 $this->assertFalse(competency::record_exists($c1a->get_id()));
3051 $this->assertFalse(competency::record_exists($c1b->get_id()));
3052 $this->assertFalse(competency::record_exists($c11b->get_id()));
3053 $this->assertFalse(competency::record_exists($c12b->get_id()));
3054 }
90c7bdaf
IT
3055
3056 public function test_delete_framework() {
3057 $this->resetAfterTest(true);
3058 $dg = $this->getDataGenerator();
3059 $lpg = $dg->get_plugin_generator('tool_lp');
3060 $this->setAdminUser();
3061
3062 $u1 = $dg->create_user();
3063
3064 $f1 = $lpg->create_framework();
3065 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3066 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3067 $c2id = $c2->get_id();
3068 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3069 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3070 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3071 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3072
3073 // If we delete framework, the related competencies relations and evidences should be deleted.
3074 // Create related competencies using one of c1a competency descendants.
3075 $rc = $lpg->create_related_competency(array(
3076 'competencyid' => $c2->get_id(),
3077 'relatedcompetencyid' => $c11b->get_id()
3078 ));
3079 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
3080
3081 // Creating a standard evidence with minimal information.
3082 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3083 $evidence = $lpg->create_evidence(array('usercompetencyid' => $uc2->get_id()));
3084 $this->assertEquals($uc2->get_id(), $evidence->get_usercompetencyid());
3085 $uc2->delete();
3086
3087 $this->assertTrue(api::delete_framework($f1->get_id()));
3088 $this->assertFalse(competency_framework::record_exists($f1->get_id()));
3089
3090 // Check that all competencies were also deleted.
3091 $this->assertFalse(competency::record_exists($c1->get_id()));
3092 $this->assertFalse(competency::record_exists($c2->get_id()));
3093 $this->assertFalse(competency::record_exists($c1a->get_id()));
3094 $this->assertFalse(competency::record_exists($c1b->get_id()));
3095 $this->assertFalse(competency::record_exists($c11b->get_id()));
3096 $this->assertFalse(competency::record_exists($c12b->get_id()));
3097
3098 // Check if evidence are also deleted.
3099 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
3100
3101 // Check if related conpetency relation is deleted.
3102 $this->assertEquals(0, count(\tool_lp\related_competency::get_multiple_relations(array($c2id))));
3103
3104 // Delete a simple framework.
3105 $f2 = $lpg->create_framework();
3106 $this->assertTrue(api::delete_framework($f2->get_id()));
3107 $this->assertFalse(competency_framework::record_exists($f2->get_id()));
3108 }
3109
3110 public function test_delete_framework_competency_used_in_plan() {
3111 $this->resetAfterTest(true);
3112 $dg = $this->getDataGenerator();
3113 $lpg = $dg->get_plugin_generator('tool_lp');
3114 $this->setAdminUser();
3115
3116 $u1 = $dg->create_user();
3117
3118 $plan = $lpg->create_plan((object) array('userid' => $u1->id));
3119
3120 $f1 = $lpg->create_framework();
3121 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3122 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3123 $c2id = $c2->get_id();
3124 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3125 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3126 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3127 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3128
3129 // Create related competencies.
3130 $rc = $lpg->create_related_competency(array(
3131 'competencyid' => $c2->get_id(),
3132 'relatedcompetencyid' => $c11b->get_id()
3133 ));
3134 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
3135
3136 // Creating a standard evidence with minimal information.
3137 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3138 $usercompetencyid = $uc2->get_id();
3139 $evidence = $lpg->create_evidence(array('usercompetencyid' => $usercompetencyid));
3140 $this->assertEquals($uc2->get_id(), $evidence->get_usercompetencyid());
3141 $uc2->delete();
3142
3143 // Add competency to plan.
3144 $pc = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c11b->get_id()));
3145 // We can not delete a framework , if competency or competency children is associated to plan.
3146 $this->assertFalse(api::delete_framework($f1->get_id()));
3147 // Check that none of associated data are deleted.
3148 $this->assertEquals($usercompetencyid, $evidence->read()->get_usercompetencyid());
3149 $this->assertEquals($c2->get_id(), $rc->read()->get_competencyid());
3150
3151 // We can delete the competency if we remove the competency from the plan.
3152 $pc->delete();
3153
3154 $this->assertTrue(api::delete_framework($f1->get_id()));
3155 $this->assertFalse(competency::record_exists($c1->get_id()));
3156 $this->assertFalse(competency::record_exists($c2->get_id()));
3157 $this->assertFalse(competency::record_exists($c1a->get_id()));
3158 $this->assertFalse(competency::record_exists($c1b->get_id()));
3159 $this->assertFalse(competency::record_exists($c11b->get_id()));
3160 $this->assertFalse(competency::record_exists($c12b->get_id()));
3161 // Check if evidence are also deleted.
3162 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
3163
3164 // Check if related conpetency relation is deleted.
3165 $this->assertEquals(0, count(\tool_lp\related_competency::get_multiple_relations(array($c2id))));
3166 }
3167
3168 public function test_delete_framework_competency_used_in_usercompetency() {
3169 $this->resetAfterTest(true);
3170 $dg = $this->getDataGenerator();
3171 $lpg = $dg->get_plugin_generator('tool_lp');
3172 $this->setAdminUser();
3173
3174 $u1 = $dg->create_user();
3175
3176 $f1 = $lpg->create_framework();
3177 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3178 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3179 $c2id = $c2->get_id();
3180 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3181 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3182 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3183 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3184
3185 // Create related competencies.
3186 $rc = $lpg->create_related_competency(array(
3187 'competencyid' => $c2->get_id(),
3188 'relatedcompetencyid' => $c11b->get_id()
3189 ));
3190 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
3191
3192 // Creating a standard evidence with minimal information.
3193 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3194 $usercompetencyid = $uc1->get_id();
3195 $evidence = $lpg->create_evidence(array('usercompetencyid' => $usercompetencyid));
3196 $this->assertEquals($uc1->get_id(), $evidence->get_usercompetencyid());
3197 $uc1->delete();
3198
3199 // Create user competency.
3200 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3201
3202 // We can not delete a framework , if competency or competency children exist in user competency.
3203 $this->assertFalse(api::delete_framework($f1->get_id()));
3204 // Check that none of associated data are deleted.
3205 $this->assertEquals($usercompetencyid, $evidence->read()->get_usercompetencyid());
3206 $this->assertEquals($c2->get_id(), $rc->read()->get_competencyid());
3207
3208 // We can delete the framework if we remove the competency from user competency.
3209 $uc2->delete();
3210
3211 $this->assertTrue(api::delete_framework($f1->get_id()));
3212 $this->assertFalse(competency::record_exists($c1->get_id()));
3213 $this->assertFalse(competency::record_exists($c2->get_id()));
3214 $this->assertFalse(competency::record_exists($c1a->get_id()));
3215 $this->assertFalse(competency::record_exists($c1b->get_id()));
3216 $this->assertFalse(competency::record_exists($c11b->get_id()));
3217 $this->assertFalse(competency::record_exists($c12b->get_id()));
3218 // Check if evidence are also deleted.
3219 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
3220
3221 // Check if related conpetency relation is deleted.
3222 $this->assertEquals(0, count(\tool_lp\related_competency::get_multiple_relations(array($c2id))));
3223 }
3224
3225 public function test_delete_framework_competency_used_in_usercompetencyplan() {
3226 $this->resetAfterTest(true);
3227 $dg = $this->getDataGenerator();
3228 $lpg = $dg->get_plugin_generator('tool_lp');
3229 $this->setAdminUser();
3230
3231 $u1 = $dg->create_user();
3232
3233 $plan = $lpg->create_plan((object) array('userid' => $u1->id));
3234
3235 $f1 = $lpg->create_framework();
3236 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3237 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3238 $c2id = $c2->get_id();
3239 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3240 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3241 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3242 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3243
3244 // Create related competencies.
3245 $rc = $lpg->create_related_competency(array(
3246 'competencyid' => $c2->get_id(),
3247 'relatedcompetencyid' => $c11b->get_id()
3248 ));
3249 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
3250
3251 // Creating a standard evidence with minimal information.
3252 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3253 $usercompetencyid = $uc1->get_id();
3254 $evidence = $lpg->create_evidence(array('usercompetencyid' => $usercompetencyid));
3255 $this->assertEquals($uc1->get_id(), $evidence->get_usercompetencyid());
3256 $uc1->delete();
3257
3258 // Create user competency plan.
3259 $uc2 = $lpg->create_user_competency_plan(array(
3260 'userid' => $u1->id,
3261 'competencyid' => $c11b->get_id(),
3262 'planid' => $plan->get_id()
3263 ));
3264
3265 // We can not delete a framework , if competency or competency children exist in user competency plan.
3266 $this->assertFalse(api::delete_framework($f1->get_id()));
3267 // Check that none of associated data are deleted.
3268 $this->assertEquals($usercompetencyid, $evidence->read()->get_usercompetencyid());
3269 $this->assertEquals($c2->get_id(), $rc->read()->get_competencyid());
3270
3271 // We can delete the framework if we remove the competency from user competency plan.
3272 $uc2->delete();
3273
3274 $this->assertTrue(api::delete_framework($f1->get_id()));
3275 $this->assertFalse(competency::record_exists($c1->get_id()));
3276 $this->assertFalse(competency::record_exists($c2->get_id()));
3277 $this->assertFalse(competency::record_exists($c1a->get_id()));
3278 $this->assertFalse(competency::record_exists($c1b->get_id()));
3279 $this->assertFalse(competency::record_exists($c11b->get_id()));
3280 $this->assertFalse(competency::record_exists($c12b->get_id()));
3281 // Check if evidence are also deleted.
3282 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
3283
3284 // Check if related conpetency relation is deleted.
3285 $this->assertEquals(0, count(\tool_lp\related_competency::get_multiple_relations(array($c2id))));
3286 }
3287
3288 public function test_delete_framework_competency_used_in_template() {
3289 $this->resetAfterTest(true);
3290 $dg = $this->getDataGenerator();
3291 $lpg = $dg->get_plugin_generator('tool_lp');
3292 $this->setAdminUser();
3293
3294 $u1 = $dg->create_user();
3295 $template = $lpg->create_template();
3296
3297 $f1 = $lpg->create_framework();
3298 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3299 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3300 $c2id = $c2->get_id();
3301 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3302 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3303 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3304 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3305
3306 // Create related competencies.
3307 $rc = $lpg->create_related_competency(array(
3308 'competencyid' => $c2->get_id(),
3309 'relatedcompetencyid' => $c11b->get_id()
3310 ));
3311 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
3312
3313 // Creating a standard evidence with minimal information.
3314 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3315 $usercompetencyid = $uc1->get_id();
3316 $evidence = $lpg->create_evidence(array('usercompetencyid' => $usercompetencyid));
3317 $this->assertEquals($uc1->get_id(), $evidence->get_usercompetencyid());
3318 $uc1->delete();
3319
3320 // Add competency to a template.
3321 $tc = $lpg->create_template_competency(array(
3322 'templateid' => $template->get_id(),
3323 'competencyid' => $c11b->get_id()
3324 ));
3325 // We can not delete a framework , if competency or competency children is linked to template.
3326 $this->assertFalse(api::delete_framework($f1->get_id()));
3327 // Check that none of associated data are deleted.
3328 $this->assertEquals($usercompetencyid, $evidence->read()->get_usercompetencyid());
3329 $this->assertEquals($c2->get_id(), $rc->read()->get_competencyid());
3330
3331 // We can delete the framework if we remove the competency from template.
3332 $tc->delete();
3333
3334 $this->assertTrue(api::delete_framework($f1->get_id()));
3335 $this->assertFalse(competency::record_exists($c1->get_id()));
3336 $this->assertFalse(competency::record_exists($c2->get_id()));
3337 $this->assertFalse(competency::record_exists($c1a->get_id()));
3338 $this->assertFalse(competency::record_exists($c1b->get_id()));
3339 $this->assertFalse(competency::record_exists($c11b->get_id()));
3340 $this->assertFalse(competency::record_exists($c12b->get_id()));
3341 // Check if evidence are also deleted.
3342 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
3343
3344 // Check if related conpetency relation is deleted.
3345 $this->assertEquals(0, count(\tool_lp\related_competency::get_multiple_relations(array($c2id))));
3346 }
3347
3348 public function test_delete_framework_competency_used_in_course() {
3349 $this->resetAfterTest(true);
3350 $dg = $this->getDataGenerator();
3351 $lpg = $dg->get_plugin_generator('tool_lp');
3352 $this->setAdminUser();
3353
3354 $cat1 = $dg->create_category();
3355 $u1 = $dg->create_user();
3356 $course = $dg->create_course(array('category' => $cat1->id));
3357
3358 $f1 = $lpg->create_framework();
3359 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3360 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
3361 $c2id = $c2->get_id();
3362 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
3363 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1a->get_id()));
3364 $c11b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3365 $c12b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1b->get_id()));
3366
3367 // Create related competencies.
3368 $rc = $lpg->create_related_competency(array(
3369 'competencyid' => $c2->get_id(),
3370 'relatedcompetencyid' => $c11b->get_id()
3371 ));
3372 $this->assertEquals($c11b->get_id(), $rc->get_relatedcompetencyid());
3373
3374 // Creating a standard evidence with minimal information.
3375 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c11b->get_id()));
3376 $usercompetencyid = $uc1->get_id();
3377 $evidence = $lpg->create_evidence(array('usercompetencyid' => $usercompetencyid));
3378 $this->assertEquals($uc1->get_id(), $evidence->get_usercompetencyid());
3379 $uc1->delete();
3380
3381 // Add competency to course.
3382 $cc = $lpg->create_course_competency(array(
3383 'courseid' => $course->id,
3384 'competencyid' => $c11b->get_id()
3385 ));
3386
3387 // We can not delete a framework if the competency or competencies children is linked to a course.
3388 $this->assertFalse(api::delete_framework($f1->get_id()));
3389 // Check that none of associated data are deleted.
3390 $this->assertEquals($usercompetencyid, $evidence->read()->get_usercompetencyid());
3391 $this->assertEquals($c2->get_id(), $rc->read()->get_competencyid());
3392
3393 // We can delete the framework if we remove the competency from course.
3394 $cc->delete();
3395
3396 $this->assertTrue(api::delete_framework($f1->get_id()));
3397 $this->assertFalse(competency::record_exists($c1->get_id()));
3398 $this->assertFalse(competency::record_exists($c2->get_id()));
3399 $this->assertFalse(competency::record_exists($c1a->get_id()));
3400 $this->assertFalse(competency::record_exists($c1b->get_id()));
3401 $this->assertFalse(competency::record_exists($c11b->get_id()));
3402 $this->assertFalse(competency::record_exists($c12b->get_id()));
3403 // Check if evidence are also deleted.
3404 $this->assertEquals(0, tool_lp\user_evidence_competency::count_records(array('competencyid' => $c11b->get_id())));
3405
3406 // Check if related conpetency relation is deleted.
3407 $this->assertEquals(0, count(\tool_lp\related_competency::get_multiple_relations(array($c2id))));
3408 }
f610a957 3409}