public function test_unlink_plan_from_template() { $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $u1 = $dg->create_user(); $u2 = $dg->create_user(); $this->setAdminUser(); $f1 = $lpg->create_framework(); $f2 = $lpg->create_framework(); $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id())); $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); $tpl1 = $lpg->create_template(); $tpl2 = $lpg->create_template(); $tplc1a = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1a->get_id(), 'sortorder' => 9)); $tplc1b = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1b->get_id(), 'sortorder' => 8)); $tplc2a = $lpg->create_template_competency(array('templateid' => $tpl2->get_id(), 'competencyid' => $c2a->get_id())); $plan1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id(), 'status' => plan::STATUS_ACTIVE)); $plan2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => $tpl2->get_id())); $plan3 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id(), 'status' => plan::STATUS_COMPLETE)); // Check that we have what we expect at this stage. $this->assertEquals(2, \core_competency\template_competency::count_records(array('templateid' => $tpl1->get_id()))); $this->assertEquals(1, \core_competency\template_competency::count_records(array('templateid' => $tpl2->get_id()))); $this->assertEquals(0, \core_competency\plan_competency::count_records(array('planid' => $plan1->get_id()))); $this->assertEquals(0, \core_competency\plan_competency::count_records(array('planid' => $plan2->get_id()))); $this->assertTrue($plan1->is_based_on_template()); $this->assertTrue($plan2->is_based_on_template()); // Let's do this! $tpl1comps = \core_competency\template_competency::list_competencies($tpl1->get_id(), true); $tpl2comps = \core_competency\template_competency::list_competencies($tpl2->get_id(), true); api::unlink_plan_from_template($plan1); $plan1->read(); $plan2->read(); $this->assertCount(2, $tpl1comps); $this->assertCount(1, $tpl2comps); $this->assertEquals(2, \core_competency\template_competency::count_records(array('templateid' => $tpl1->get_id()))); $this->assertEquals(1, \core_competency\template_competency::count_records(array('templateid' => $tpl2->get_id()))); $this->assertEquals(2, \core_competency\plan_competency::count_records(array('planid' => $plan1->get_id()))); $this->assertEquals(0, \core_competency\plan_competency::count_records(array('planid' => $plan2->get_id()))); $this->assertFalse($plan1->is_based_on_template()); $this->assertEquals($tpl1->get_id(), $plan1->get_origtemplateid()); $this->assertTrue($plan2->is_based_on_template()); $this->assertEquals(null, $plan2->get_origtemplateid()); // Check we can unlink draft plan. try { api::unlink_plan_from_template($plan2); } catch (coding_exception $e) { $this->fail('Fail to unlink draft plan.'); } // Check we can not unlink completed plan. try { api::unlink_plan_from_template($plan3); $this->fail('We can not unlink completed plan.'); } catch (coding_exception $e) { // All good. } // Even the order remains. $plan1comps = \core_competency\plan_competency::list_competencies($plan1->get_id()); $before = reset($tpl1comps); $after = reset($plan1comps); $this->assertEquals($before->get_id(), $after->get_id()); $this->assertEquals($before->get_sortorder(), $after->get_sortorder()); $before = next($tpl1comps); $after = next($plan1comps); $this->assertEquals($before->get_id(), $after->get_id()); $this->assertEquals($before->get_sortorder(), $after->get_sortorder()); }
public function test_sync_plans_from_cohorts_with_templateduedate_task() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $user1 = $dg->create_user(); $user2 = $dg->create_user(); $user3 = $dg->create_user(); $user4 = $dg->create_user(); $user5 = $dg->create_user(); $cohort = $dg->create_cohort(); $tpl = $lpg->create_template(array('duedate' => time() + 400)); // Add 2 users to the cohort. cohort_add_member($cohort->id, $user1->id); cohort_add_member($cohort->id, $user2->id); // Creating plans from template cohort. $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id); $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id); $this->assertEquals(2, $created); $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task'); $this->assertInstanceOf('\\core\\task\\sync_plans_from_template_cohorts_task', $task); // Add two more users to the cohort. cohort_add_member($cohort->id, $user3->id); cohort_add_member($cohort->id, $user4->id); $task->execute(); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Test if remove user from cohort will affect plans. cohort_remove_member($cohort->id, $user3->id); cohort_remove_member($cohort->id, $user4->id); $task->execute(); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // The template is now hidden, and I've added a user with a missing plan. Nothing should happen. $tpl->set_visible(false); $tpl->update(); cohort_add_member($cohort->id, $user5->id); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); $task->execute(); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Now I set the template as visible again, the plan is created. $tpl->set_visible(true); $tpl->update(); $task->execute(); $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get_id()))); // Let's unlink the plan and run the task again, it should not be recreated. $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id())); \core_competency\api::unlink_plan_from_template($plan); $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id))); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); $task->execute(); $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id))); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Adding users to cohort that already exist in plans. cohort_add_member($cohort->id, $user3->id); cohort_add_member($cohort->id, $user4->id); $task->execute(); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); }
/** * Unlink the plan from the template. * * @param int $planid The plan id * @return bool */ public static function unlink_plan_from_template($planid) { $params = self::validate_parameters(self::unlink_plan_from_template_parameters(), array('planid' => $planid)); $plan = new plan($params['planid']); self::validate_context($plan->get_context()); return api::unlink_plan_from_template($plan); }
/** * Test the plan unlinked event. * */ public function test_plan_unlinked() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user1 = $dg->create_user(); $template = $lpg->create_template(); $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_ACTIVE, 'templateid' => $template->get_id())); $planid = $plan->get_id(); $contextid = $plan->get_context()->id; // Trigger and capture the event. $sink = $this->redirectEvents(); $result = api::unlink_plan_from_template($plan->get_id()); $this->assertTrue($result); // Get our event event. $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\core\\event\\competency_plan_unlinked', $event); $this->assertEquals($planid, $event->objectid); $this->assertEquals($contextid, $event->contextid); $this->assertEquals($plan->get_userid(), $event->relateduserid); $this->assertEventContextNotUsed($event); $this->assertDebuggingNotCalled(); }