/** * Test updating a template. * * @expectedException coding_exception */ public function test_update_template() { $cat = $this->getDataGenerator()->create_category(); $this->resetAfterTest(true); $this->setAdminUser(); $syscontext = context_system::instance(); $template = api::create_template((object) array('shortname' => 'testing', 'contextid' => $syscontext->id)); $this->assertEquals('testing', $template->get_shortname()); $this->assertEquals($syscontext->id, $template->get_contextid()); // Simple update. api::update_template((object) array('id' => $template->get_id(), 'shortname' => 'success')); $template = api::read_template($template->get_id()); $this->assertEquals('success', $template->get_shortname()); // Trying to change the context. api::update_template((object) array('id' => $template->get_id(), 'contextid' => context_coursecat::instance($cat->id))); }
/** * Test the template created event. * */ public function test_template_created() { $this->resetAfterTest(true); $this->setAdminUser(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); // Use DataGenerator to have a template record with the right format. $record = $lpg->create_template()->to_record(); $record->id = 0; $record->shortname = "New shortname"; // Trigger and capture the event. $sink = $this->redirectEvents(); $template = api::create_template((object) $record); // Get our event event. $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\core\\event\\competency_template_created', $event); $this->assertEquals($template->get_id(), $event->objectid); $this->assertEquals($template->get_contextid(), $event->contextid); $this->assertEventContextNotUsed($event); $this->assertDebuggingNotCalled(); }
/** * Create a new learning plan template * * @param array $template The list of fields for the template. * @return \stdClass Record of new template. */ public static function create_template($template) { global $PAGE; $params = self::validate_parameters(self::create_template_parameters(), array('template' => $template)); $params = $params['template']; $context = self::get_context_from_params($params); self::validate_context($context); $output = $PAGE->get_renderer('core'); unset($params['contextlevel']); unset($params['instanceid']); $params = (object) $params; $params->contextid = $context->id; $result = api::create_template($params); $exporter = new template_exporter($result); $record = $exporter->export($output); return $record; }
if (empty($id)) { $pagetitle = get_string('addnewtemplate', 'tool_lp'); list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, null, $pagetitle, $returntype); } else { $template = \core_competency\api::read_template($id); $pagetitle = get_string('edittemplate', 'tool_lp'); list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle, $returntype); } $form = new \tool_lp\form\template($url->out(false), array('persistent' => $template, 'context' => $context)); if ($form->is_cancelled()) { redirect($returnurl); } $data = $form->get_data(); if ($data) { if (empty($data->id)) { $template = \core_competency\api::create_template($data); $returnurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', ['templateid' => $template->get_id(), 'pagecontextid' => $pagecontextid]); $returnmsg = get_string('templatecreated', 'tool_lp'); } else { \core_competency\api::update_template($data); $returnmsg = get_string('templateupdated', 'tool_lp'); } redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS); } $output = $PAGE->get_renderer('tool_lp'); echo $output->header(); echo $output->heading($title); if (!empty($subtitle)) { echo $output->heading($subtitle, 3); } $form->display();