Exemplo n.º 1
0
 /**
  * Unlink a plan from its template.
  *
  * @param  \core_competency\plan|int $planorid The plan or its ID.
  * @return bool
  */
 public static function unlink_plan_from_template($planorid)
 {
     global $DB;
     static::require_enabled();
     $plan = $planorid;
     if (!is_object($planorid)) {
         $plan = new plan($planorid);
     }
     // The user must be allowed to manage the plans of the user, nothing about the template.
     if (!$plan->can_manage()) {
         throw new required_capability_exception($plan->get_context(), 'moodle/competency:planmanage', 'nopermissions', '');
     }
     // Only plan with status DRAFT or ACTIVE can be unliked..
     if ($plan->get_status() == plan::STATUS_COMPLETE) {
         throw new coding_exception('Only draft or active plan can be unliked from a template');
     }
     // Early exit, it's already done...
     if (!$plan->is_based_on_template()) {
         return true;
     }
     // Fetch the template.
     $template = new template($plan->get_templateid());
     // Now, proceed by copying all competencies to the plan, then update the plan.
     $transaction = $DB->start_delegated_transaction();
     $competencies = template_competency::list_competencies($template->get_id(), false);
     $i = 0;
     foreach ($competencies as $competency) {
         $record = (object) array('planid' => $plan->get_id(), 'competencyid' => $competency->get_id(), 'sortorder' => $i++);
         $pc = new plan_competency(null, $record);
         $pc->create();
     }
     $plan->set_origtemplateid($template->get_id());
     $plan->set_templateid(null);
     $success = $plan->update();
     $transaction->allow_commit();
     // Trigger unlinked event.
     \core\event\competency_plan_unlinked::create_from_plan($plan)->trigger();
     return $success;
 }
Exemplo n.º 2
0
 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());
 }
Exemplo n.º 3
0
 /**
  * Get the competencies in this plan.
  *
  * @return competency[]
  */
 public function get_competencies()
 {
     $competencies = array();
     if ($this->get_status() == self::STATUS_COMPLETE) {
         // Get the competencies from the archive of the plan.
         $competencies = user_competency_plan::list_competencies($this->get_id(), $this->get_userid());
     } else {
         if ($this->is_based_on_template()) {
             // Get the competencies from the template.
             $competencies = template_competency::list_competencies($this->get_templateid());
         } else {
             // Get the competencies from the plan.
             $competencies = plan_competency::list_competencies($this->get_id());
         }
     }
     return $competencies;
 }