/** * @expectedException coding_exception */ public function test_create_plan_from_template() { $this->resetAfterTest(true); $this->setAdminUser(); $u1 = $this->getDataGenerator()->create_user(); $tpl = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_template(); // Creating a new plan. $plan = api::create_plan_from_template($tpl, $u1->id); $record = $plan->to_record(); $this->assertInstanceOf('\\core_competency\\plan', $plan); $this->assertTrue(\core_competency\plan::record_exists($plan->get_id())); $this->assertEquals($tpl->get_id(), $plan->get_templateid()); $this->assertEquals($u1->id, $plan->get_userid()); $this->assertTrue($plan->is_based_on_template()); // Creating a plan that already exists. $plan = api::create_plan_from_template($tpl, $u1->id); $this->assertFalse($plan); // Check that api::create_plan cannot be used. unset($record->id); $plan = api::create_plan($record); }
/** * Create a new learning plan. * * @param array $plan List of fields for the plan. * @return array New plan record. */ public static function create_plan($plan) { global $PAGE; $params = self::validate_parameters(self::create_plan_parameters(), array('plan' => $plan)); $params = $params['plan']; $context = context_user::instance($params['userid']); self::validate_context($context); $output = $PAGE->get_renderer('core'); $params = (object) $params; $result = api::create_plan($params); $exporter = new plan_exporter($result, array('template' => null)); return $exporter->export($output); }
if (!$plan->can_be_edited()) { throw new coding_exception('Completed plan can not be edited'); } } else { if (!$cancreate) { throw new required_capability_exception($PAGE->context, 'moodle/competency:planmanage', 'nopermissions', ''); } } $form = new \tool_lp\form\plan($url->out(false), $customdata); if ($form->is_cancelled()) { redirect($returnurl); } $data = $form->get_data(); if ($data) { if (empty($data->id)) { $plan = \core_competency\api::create_plan($data); $returnurl = new moodle_url('/admin/tool/lp/plan.php', ['id' => $plan->get_id()]); $returnmsg = get_string('plancreated', 'tool_lp'); } else { \core_competency\api::update_plan($data); $returnmsg = get_string('planupdated', 'tool_lp'); } redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS); } echo $output->header(); echo $output->heading($title); if (!empty($subtitle)) { echo $output->heading($subtitle, 3); } $form->display(); echo $output->footer();
/** * Test the plan created event. * */ public function test_plan_created() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user = $dg->create_user(); $plan = array('name' => 'plan', 'userid' => $user->id); // Trigger and capture the event. $sink = $this->redirectEvents(); $plan = api::create_plan((object) $plan); // Get our event event. $events = $sink->get_events(); $event = reset($events); // 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); $this->assertEventContextNotUsed($event); $this->assertDebuggingNotCalled(); }