/** * Test the plan created event using template_cohort. * */ public function test_plan_created_using_templatecohort() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user1 = $dg->create_user(); $user2 = $dg->create_user(); $c1 = $dg->create_cohort(); // Add 2 users to the cohort. cohort_add_member($c1->id, $user1->id); cohort_add_member($c1->id, $user2->id); $t1 = $lpg->create_template(); $tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id)); // Trigger and capture the event. $sink = $this->redirectEvents(); api::create_plans_from_template_cohort($t1->get_id(), $c1->id); // Get our event event. $plans = core_competency\plan::get_records(array('templateid' => $t1->get_id()), 'id'); $events = $sink->get_events(); $this->assertCount(2, $events); $this->assertCount(2, $plans); $event = $events[0]; $plan = $plans[0]; // Check that the event data is valid. $this->assertInstanceOf('\\core\\event\\competency_plan_created', $event); $this->assertEquals($plan->get_id(), $event->objectid); $this->assertEquals($plan->get_context()->id, $event->contextid); $event = $events[1]; $plan = $plans[1]; $this->assertInstanceOf('\\core\\event\\competency_plan_created', $event); $this->assertEquals($plan->get_id(), $event->objectid); $this->assertEquals($plan->get_context()->id, $event->contextid); $this->assertEventContextNotUsed($event); $this->assertDebuggingNotCalled(); }
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()))); }
} // Capture the form submission. $form = new \tool_lp\form\template_cohorts($url->out(false), array('pagecontextid' => $pagecontextid)); if ($canmanagetemplate && ($data = $form->get_data()) && !empty($data->cohorts)) { $maxtocreate = 50; $maxreached = false; $i = 0; foreach ($data->cohorts as $cohortid) { // Create the template/cohort relationship. $relation = \core_competency\api::create_template_cohort($template, $cohortid); // Create a plan for each member if template visible, and the due date is not reached, and we didn't reach our limit yet. if ($template->get_visible() && $i < $maxtocreate && !$duedatereached) { // Only create a few plans right now. $tocreate = \core_competency\template_cohort::get_missing_plans($template->get_id(), $cohortid); if ($i + count($tocreate) <= $maxtocreate) { $i += \core_competency\api::create_plans_from_template_cohort($template, $cohortid); } else { $maxreached = true; } } } if ($i == 0) { $notification = get_string('noplanswerecreated', 'tool_lp'); } else { if ($i == 1) { $notification = get_string('oneplanwascreated', 'tool_lp'); } else { if ($maxreached) { $notification = get_string('aplanswerecreatedmoremayrequiresync', 'tool_lp', $i); } else { $notification = get_string('aplanswerecreated', 'tool_lp', $i);