MDL-52282 tool_lp: Template cohorts sync doesn't recreate unlinked plans

This commit is contained in:
Frederic Massart 2015-12-16 13:00:45 +08:00
parent 964afa98e7
commit f442860781
2 changed files with 31 additions and 2 deletions

View File

@ -52,11 +52,11 @@ class sync_plans_from_template_cohorts_task extends \core\task\scheduled_task {
*/
public function execute() {
$missingplans = template_cohort::get_all_missing_plans(true);
$missingplans = template_cohort::get_all_missing_plans();
foreach ($missingplans as $missingplan) {
foreach ($missingplan['userids'] as $userid) {
api::create_plan_from_template($missingplan['template']->get_id(), $userid);
api::create_plan_from_template($missingplan['template'], $userid);
}
}
}

View File

@ -46,6 +46,7 @@ class tool_lp_task_testcase extends advanced_testcase {
$user2 = $dg->create_user();
$user3 = $dg->create_user();
$user4 = $dg->create_user();
$user5 = $dg->create_user();
$cohort = $dg->create_cohort();
$tpl = $lpg->create_template();
@ -78,6 +79,34 @@ class tool_lp_task_testcase extends advanced_testcase {
$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()));
\tool_lp\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);