Exemplo n.º 1
0
 /**
  * Test update plan and the managing of archived user competencies.
  */
 public function test_update_plan_manage_archived_competencies()
 {
     global $DB;
     $this->resetAfterTest(true);
     $dg = $this->getDataGenerator();
     $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
     $syscontext = context_system::instance();
     // Create users and roles for the test.
     $user = $dg->create_user();
     $manageownrole = $dg->create_role(array('name' => 'User manage own', 'shortname' => 'manageown'));
     assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
     assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
     assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
     assign_capability('moodle/competency:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
     $dg->role_assign($manageownrole, $user->id, $syscontext->id);
     $this->setUser($user);
     // Create a framework and assign competencies.
     $framework = $lpg->create_framework();
     $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     // Create two plans and assign competencies.
     $plan = $lpg->create_plan(array('userid' => $user->id));
     $otherplan = $lpg->create_plan(array('userid' => $user->id));
     $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
     $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
     $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
     $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
     $uclist = array($lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id(), 'proficiency' => true, 'grade' => 1)), $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id(), 'proficiency' => false, 'grade' => 2)));
     // Change status of the plan to complete.
     $record = $plan->to_record();
     $record->status = \core_competency\plan::STATUS_COMPLETE;
     try {
         $plan = api::update_plan($record);
         $this->fail('We cannot complete a plan using api::update_plan().');
     } catch (coding_exception $e) {
         // All good.
     }
     api::complete_plan($plan);
     // Check that user compretencies are now in user_competency_plan objects and still in user_competency.
     $this->assertEquals(2, \core_competency\user_competency::count_records());
     $this->assertEquals(3, \core_competency\user_competency_plan::count_records());
     $usercompetenciesplan = \core_competency\user_competency_plan::get_records();
     $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
     $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
     $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
     $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
     $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
     $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
     $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
     $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
     $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
     $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
     $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
     $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
     $this->assertNull($usercompetenciesplan[2]->get_proficiency());
     $this->assertNull($usercompetenciesplan[2]->get_grade());
     $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
     // Change status of the plan to active.
     $record = $plan->to_record();
     $record->status = \core_competency\plan::STATUS_ACTIVE;
     try {
         api::update_plan($record);
         $this->fail('Completed plan can not be edited');
     } catch (coding_exception $e) {
         // All good.
     }
     api::reopen_plan($record->id);
     // Check that user_competency_plan objects are deleted if the plan status is changed to another status.
     $this->assertEquals(2, \core_competency\user_competency::count_records());
     $this->assertEquals(0, \core_competency\user_competency_plan::count_records());
 }
Exemplo n.º 2
0
 /**
  * Test the plan reopened event.
  *
  */
 public function test_plan_reopened()
 {
     $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_COMPLETE));
     $planid = $plan->get_id();
     $contextid = $plan->get_context()->id;
     // Trigger and capture the event.
     $sink = $this->redirectEvents();
     $result = api::reopen_plan($plan->get_id());
     $this->assertTrue($result);
     // Get our event event.
     $events = $sink->get_events();
     $event = reset($events);
     $this->assertInstanceOf('\\core\\event\\competency_plan_reopened', $event);
     $this->assertEquals($planid, $event->objectid);
     $this->assertEquals($contextid, $event->contextid);
     $this->assertEquals($plan->get_userid(), $event->relateduserid);
     $this->assertEventContextNotUsed($event);
     $this->assertDebuggingNotCalled();
 }
Exemplo n.º 3
0
 /**
  * Reopen Learning plan.
  *
  * @param int $planid plan id (id is required)
  * @return boolean
  */
 public static function reopen_plan($planid)
 {
     $params = self::validate_parameters(self::reopen_plan_parameters(), array('planid' => $planid));
     return api::reopen_plan($params['planid']);
 }
Exemplo n.º 4
0
 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());
 }