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