/** * Do the job. */ public function execute() { if (!api::is_enabled()) { return; } $records = plan::get_recordset_for_due_and_incomplete(); foreach ($records as $record) { $plan = new plan(0, $record); api::complete_plan($plan); } $records->close(); }
public function test_list_plans_to_review() { $dg = $this->getDataGenerator(); $this->resetAfterTest(); $ccg = $dg->get_plugin_generator('core_competency'); $sysctx = context_system::instance(); $this->setAdminUser(); $reviewer = $dg->create_user(); $roleallow = $dg->create_role(); $roleprohibit = $dg->create_role(); assign_capability('moodle/competency:planreview', CAP_ALLOW, $roleallow, $sysctx->id); assign_capability('moodle/competency:planreview', CAP_PROHIBIT, $roleprohibit, $sysctx->id); role_assign($roleallow, $reviewer->id, $sysctx->id); accesslib_clear_all_caches_for_unit_testing(); $u1 = $dg->create_user(); $u2 = $dg->create_user(); $f1 = $ccg->create_framework(); $comp1 = $ccg->create_competency(['competencyframeworkid' => $f1->get_id()]); $p1a = $ccg->create_plan(['userid' => $u1->id, 'status' => plan::STATUS_WAITING_FOR_REVIEW]); $p1b = $ccg->create_plan(['userid' => $u1->id, 'status' => plan::STATUS_IN_REVIEW, 'reviewerid' => $reviewer->id]); $p1c = $ccg->create_plan(['userid' => $u1->id, 'status' => plan::STATUS_DRAFT]); $p2a = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_WAITING_FOR_REVIEW]); $p2b = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_IN_REVIEW]); $p2c = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_ACTIVE]); $p2d = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_ACTIVE]); api::complete_plan($p2d); // The reviewer can review all plans waiting for review, or in review where they are the reviewer. $this->setUser($reviewer); $result = api::list_plans_to_review(); $this->assertEquals(3, $result['count']); $this->assertEquals($p1a->get_id(), $result['plans'][0]->plan->get_id()); $this->assertEquals($p1b->get_id(), $result['plans'][1]->plan->get_id()); $this->assertEquals($p2a->get_id(), $result['plans'][2]->plan->get_id()); // The reviewer cannot view the plans when they do not have the permission in the user's context. role_assign($roleprohibit, $reviewer->id, context_user::instance($u2->id)->id); accesslib_clear_all_caches_for_unit_testing(); $result = api::list_plans_to_review(); $this->assertEquals(2, $result['count']); $this->assertEquals($p1a->get_id(), $result['plans'][0]->plan->get_id()); $this->assertEquals($p1b->get_id(), $result['plans'][1]->plan->get_id()); }
/** * Complete Learning plan. * * @param int $planid plan id (id is required) * @return boolean */ public static function complete_plan($planid) { $params = self::validate_parameters(self::complete_plan_parameters(), array('planid' => $planid)); return api::complete_plan($params['planid']); }
/** * Test the plan completed event. * */ public function test_plan_completed() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user1 = $dg->create_user(); $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_ACTIVE)); $planid = $plan->get_id(); $contextid = $plan->get_context()->id; // Trigger and capture the event. $sink = $this->redirectEvents(); $result = api::complete_plan($plan->get_id()); $this->assertTrue($result); // Get our event event. $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\core\\event\\competency_plan_completed', $event); $this->assertEquals($planid, $event->objectid); $this->assertEquals($contextid, $event->contextid); $this->assertEquals($plan->get_userid(), $event->relateduserid); $this->assertEventContextNotUsed($event); $this->assertDebuggingNotCalled(); }
public function test_get_competency() { $this->resetAfterTest(); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $u1 = $dg->create_user(); $u2 = $dg->create_user(); $u3 = $dg->create_user(); $u4 = $dg->create_user(); $f1 = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $c4 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $tpl1 = $lpg->create_template(); $p1 = $lpg->create_plan(array('userid' => $u1->id)); $p2 = $lpg->create_plan(array('userid' => $u2->id)); $p3 = $lpg->create_plan(array('userid' => $u3->id, 'templateid' => $tpl1->get_id())); $p4 = $lpg->create_plan(array('userid' => $u4->id, 'templateid' => $tpl1->get_id())); $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c1->get_id())); $lpg->create_plan_competency(array('planid' => $p2->get_id(), 'competencyid' => $c2->get_id())); $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c3->get_id())); $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c4->get_id())); // Completing the plans and removing a competency from the template. api::complete_plan($p2); api::complete_plan($p4); api::remove_competency_from_template($tpl1->get_id(), $c4->get_id()); // We can find all competencies. $this->assertEquals($c1->to_record(), $p1->get_competency($c1->get_id())->to_record()); $this->assertEquals($c2->to_record(), $p2->get_competency($c2->get_id())->to_record()); $this->assertEquals($c3->to_record(), $p3->get_competency($c3->get_id())->to_record()); $this->assertEquals($c4->to_record(), $p4->get_competency($c4->get_id())->to_record()); // Getting the competency 4 from the non-completed plan based on a template p4, will throw an exception. $this->setExpectedException('coding_exception', 'The competency does not belong to this template: '); $p3->get_competency($c4->get_id()); }
public function test_validate_duedate() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user = $dg->create_user(); $record = array('userid' => $user->id, 'status' => plan::STATUS_DRAFT, 'duedate' => time() - 8000); // Ignore duedate validation on create/update draft plan. $plan = $lpg->create_plan($record); $this->assertInstanceOf('core_competency\\plan', $plan); // Passing from draft to active. $plan->set_status(plan::STATUS_ACTIVE); // Draft to active with duedate in the past. $expected = array('duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Draft to active: past date => past date(fail). $plan->set_duedate(time() - 100); $expected = array('duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Draft to active: past date => too soon (fail). $plan->set_duedate(time() + 100); $expected = array('duedate' => new lang_string('errorcannotsetduedatetoosoon', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Draft to active: past date => future date (pass). $plan->set_duedate(time() + plan::DUEDATE_THRESHOLD + 10); $this->assertEquals(true, $plan->validate()); // Draft to active: past date => unset date (pass). $plan->set_duedate(0); $this->assertEquals(true, $plan->validate()); // Updating active plan. $plan->update(); // Active to active: past => same past (pass). $record = $plan->to_record(); $record->duedate = 1; $DB->update_record(plan::TABLE, $record); $plan->read(); $plan->set_description(uniqid()); // Force revalidation. $this->assertTrue($plan->is_valid()); // Active to active: past => unset (pass). $plan->set_duedate(0); $this->assertTrue($plan->is_valid()); $plan->update(); // Active to active: unset => unset (pass). $plan->set_description(uniqid()); // Force revalidation. $this->assertTrue($plan->is_valid()); // Active to active: unset date => past date(fail). $plan->set_duedate(time() - 100); $expected = array('duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Active to active: unset date => too soon (fail). $plan->set_duedate(time() + 100); $expected = array('duedate' => new lang_string('errorcannotsetduedatetoosoon', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Active to active: unset date => future date (pass). $plan->set_duedate(time() + plan::DUEDATE_THRESHOLD + 10); $this->assertEquals(true, $plan->validate()); // Updating active plan with future date. $plan->update(); // Active to active: future => same future (pass). $plan->set_description(uniqid()); // Force revalidation. $this->assertTrue($plan->is_valid()); // Active to active: future date => unset date (pass). $plan->set_duedate(0); $this->assertEquals(true, $plan->validate()); // Active to active: future date => past date(fail). $plan->set_duedate(time() - 100); $expected = array('duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Active to active: future date => too soon (fail). $plan->set_duedate(time() + 100); $expected = array('duedate' => new lang_string('errorcannotsetduedatetoosoon', 'core_competency')); $this->assertEquals($expected, $plan->validate()); // Active to active: future date => future date (pass). $plan->set_duedate(time() + plan::DUEDATE_THRESHOLD + 10); $this->assertEquals(true, $plan->validate()); // Completing plan: with due date in the past. $record = $plan->to_record(); $record->status = plan::STATUS_ACTIVE; $record->duedate = time() - 200; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::complete_plan($plan->get_id()); $this->assertTrue($success); // Completing plan: with due date too soon (pass). $record = $plan->to_record(); $record->status = plan::STATUS_ACTIVE; $record->duedate = time() + 200; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::complete_plan($plan->get_id()); $this->assertTrue($success); // Completing plan: with due date in the future (pass). $record = $plan->to_record(); $record->status = plan::STATUS_ACTIVE; $record->duedate = time() + plan::DUEDATE_THRESHOLD + 10; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::complete_plan($plan->get_id()); $this->assertTrue($success); // Completing plan: with due date unset (pass). $record = $plan->to_record(); $record->status = plan::STATUS_ACTIVE; $record->duedate = 0; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::complete_plan($plan->get_id()); $this->assertTrue($success); // Reopening plan: with due date in the past => duedate unset. $record = $plan->to_record(); $record->status = plan::STATUS_COMPLETE; $record->duedate = time() - 200; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::reopen_plan($plan->get_id()); $this->assertTrue($success); $plan->read(); $this->assertEquals(0, $plan->get_duedate()); // Reopening plan: with due date too soon => duedate unset. $record = $plan->to_record(); $record->status = plan::STATUS_COMPLETE; $record->duedate = time() + 100; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::reopen_plan($plan->get_id()); $this->assertTrue($success); $plan->read(); $this->assertEquals(0, $plan->get_duedate()); // Reopening plan: with due date in the future => duedate unchanged. $record = $plan->to_record(); $record->status = plan::STATUS_COMPLETE; $duedate = time() + plan::DUEDATE_THRESHOLD + 10; $record->duedate = $duedate; $DB->update_record(plan::TABLE, $record); $success = core_competency\api::reopen_plan($plan->get_id()); $this->assertTrue($success); $plan->read(); // Check that the due date has not changed. $this->assertNotEquals(0, $plan->get_duedate()); $this->assertEquals($duedate, $plan->get_duedate()); }
public function test_delete_plan_removes_relations() { $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $user = $dg->create_user(); $plan = $lpg->create_plan(array('userid' => $user->id)); $framework = $lpg->create_framework(); $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp1->get_id())); $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp2->get_id())); $pc3 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp3->get_id())); // Complete the plan to generate user_competency_plan entries. api::complete_plan($plan); // Confirm the data we have. $this->assertEquals(3, plan_competency::count_records(array('planid' => $plan->get_id()))); $this->assertEquals(3, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id))); // Delete the plan now. api::delete_plan($plan->get_id()); $this->assertEquals(0, plan_competency::count_records(array('planid' => $plan->get_id()))); $this->assertEquals(0, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id))); }
/** * Test template statistics api functions. */ public function test_template_statistics() { $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $this->setAdminUser(); $u1 = $dg->create_user(); $u2 = $dg->create_user(); $u3 = $dg->create_user(); $u4 = $dg->create_user(); $c1 = $dg->create_course(); $framework = $lpg->create_framework(); // Create 6 competencies. $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp6 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // Link 5 out of 6 to a course. $lpg->create_course_competency(array('competencyid' => $comp1->get_id(), 'courseid' => $c1->id)); $lpg->create_course_competency(array('competencyid' => $comp2->get_id(), 'courseid' => $c1->id)); $lpg->create_course_competency(array('competencyid' => $comp3->get_id(), 'courseid' => $c1->id)); $lpg->create_course_competency(array('competencyid' => $comp4->get_id(), 'courseid' => $c1->id)); $lpg->create_course_competency(array('competencyid' => $comp5->get_id(), 'courseid' => $c1->id)); // Put all 6 in a template. $tpl = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_template(); $tplc1 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $comp1->get_id())); $tplc2 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $comp2->get_id())); $tplc3 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $comp3->get_id())); $tplc4 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $comp4->get_id())); $tplc5 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $comp5->get_id())); $tplc6 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $comp6->get_id())); // Create some plans from the template. $p1 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u1->id)); $p2 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u2->id)); $p3 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u3->id)); $p4 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u4->id)); // Rate some competencies. // User 1. $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $comp1->get_id(), 'proficiency' => true, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $comp2->get_id(), 'proficiency' => true, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $comp3->get_id(), 'proficiency' => true, 'grade' => 1)); // User 2. $lpg->create_user_competency(array('userid' => $u2->id, 'competencyid' => $comp1->get_id(), 'proficiency' => false, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u2->id, 'competencyid' => $comp2->get_id(), 'proficiency' => false, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u2->id, 'competencyid' => $comp3->get_id(), 'proficiency' => false, 'grade' => 1)); // User 3. $lpg->create_user_competency(array('userid' => $u3->id, 'competencyid' => $comp2->get_id(), 'proficiency' => false, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u3->id, 'competencyid' => $comp3->get_id(), 'proficiency' => true, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u3->id, 'competencyid' => $comp4->get_id(), 'proficiency' => false, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u3->id, 'competencyid' => $comp5->get_id(), 'proficiency' => true, 'grade' => 1)); // User 4. $lpg->create_user_competency(array('userid' => $u4->id, 'competencyid' => $comp3->get_id(), 'proficiency' => true, 'grade' => 1)); $lpg->create_user_competency(array('userid' => $u4->id, 'competencyid' => $comp5->get_id(), 'proficiency' => true, 'grade' => 1)); // Complete 3 out of 4 plans. api::complete_plan($p1->get_id()); api::complete_plan($p2->get_id()); api::complete_plan($p3->get_id()); // OK we have enough data - lets call some API functions and check for expected results. $result = api::count_competencies_in_template_with_no_courses($tpl->get_id()); $this->assertEquals(1, $result); $result = api::count_plans_for_template($tpl->get_id()); $this->assertEquals(4, $result); $result = api::count_plans_for_template($tpl->get_id(), plan::STATUS_COMPLETE); $this->assertEquals(3, $result); // This counts the records of competencies in completed plans for all users with a plan from this template. $result = api::count_user_competency_plans_for_template($tpl->get_id()); // There should be 3 plans * 6 competencies. $this->assertEquals(18, $result); // This counts the records of proficient competencies in completed plans for all users with a plan from this template. $result = api::count_user_competency_plans_for_template($tpl->get_id(), true); // There should be 5. $this->assertEquals(5, $result); // This counts the records of not proficient competencies in completed plans for all users with a plan from this template. $result = api::count_user_competency_plans_for_template($tpl->get_id(), false); // There should be 13. $this->assertEquals(13, $result); // This lists the plans based on this template, optionally filtered by status. $result = api::list_plans_for_template($tpl->get_id()); $this->assertEquals(4, count($result)); foreach ($result as $one) { $this->assertInstanceOf('\\core_competency\\plan', $one); } // This lists the plans based on this template, optionally filtered by status. $result = api::list_plans_for_template($tpl->get_id(), plan::STATUS_COMPLETE); $this->assertEquals(3, count($result)); foreach ($result as $one) { $this->assertInstanceOf('\\core_competency\\plan', $one); $this->assertEquals(plan::STATUS_COMPLETE, $one->get_status()); } $result = api::get_least_proficient_competencies_for_template($tpl->get_id(), 0, 2); // Our times completed counts should look like this: // - comp1 - 1 // - comp2 - 1 // - comp3 - 2 // - comp4 - 0 // - comp5 - 1 // - comp6 - 0 // // And this is a fullstop to make CiBoT happy. $this->assertEquals(2, count($result)); $leastarray = array($comp4->get_id(), $comp6->get_id()); foreach ($result as $one) { $this->assertInstanceOf('\\core_competency\\competency', $one); $this->assertContains($one->get_id(), $leastarray); } }