예제 #1
0
 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())));
 }
예제 #2
0
 /**
  * Create a learning plan from a template.
  *
  * @param  mixed $templateorid The template object or ID.
  * @param  int $userid
  * @return false|\core_competency\plan Returns false when the plan already exists.
  */
 public static function create_plan_from_template($templateorid, $userid)
 {
     static::require_enabled();
     $template = $templateorid;
     if (!is_object($template)) {
         $template = new template($template);
     }
     // The user must be able to view the template to use it as a base for a plan.
     if (!$template->can_read()) {
         throw new required_capability_exception($template->get_context(), 'moodle/competency:templateview', 'nopermissions', '');
     }
     // Can not create plan from a hidden template.
     if ($template->get_visible() == false) {
         throw new coding_exception('A plan can not be created from a hidden template');
     }
     // Convert the template to a plan.
     $record = $template->to_record();
     $record->templateid = $record->id;
     $record->userid = $userid;
     $record->name = $record->shortname;
     $record->status = plan::STATUS_ACTIVE;
     unset($record->id);
     unset($record->timecreated);
     unset($record->timemodified);
     unset($record->usermodified);
     // Remove extra keys.
     $properties = plan::properties_definition();
     foreach ($record as $key => $value) {
         if (!array_key_exists($key, $properties)) {
             unset($record->{$key});
         }
     }
     $plan = new plan(0, $record);
     if (!$plan->can_manage()) {
         throw new required_capability_exception($plan->get_context(), 'moodle/competency:planmanage', 'nopermissions', '');
     }
     // We first apply the permission checks as we wouldn't want to leak information by returning early that
     // the plan already exists.
     if (plan::record_exists_select('templateid = :templateid AND userid = :userid', array('templateid' => $template->get_id(), 'userid' => $userid))) {
         return false;
     }
     $plan->create();
     // Trigger created event.
     \core\event\competency_plan_created::create_from_plan($plan)->trigger();
     return $plan;
 }
예제 #3
0
 /**
  * Return true if the user of the evidence has plan.
  *
  * @return bool
  */
 public function user_has_plan()
 {
     return plan::record_exists_select('userid = ?', array($this->get_userid()));
 }