2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This page lets users to manage site wide learning plan templates.
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../../../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
28 $id = optional_param('id', 0, PARAM_INT);
29 $pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to where we can from.
33 // Always use the context from the framework when it exists.
34 $template = new \tool_lp\template($id);
35 $context = $template->get_context();
37 $context = context::instance_by_id($pagecontextid);
40 // We check that we have the permission to edit this framework, in its own context.
41 require_login(0, false);
42 \tool_lp\api::require_enabled();
43 require_capability('tool/lp:templatemanage', $context);
45 // We keep the original context in the URLs, so that we remain in the same context.
46 $url = new moodle_url("/admin/tool/lp/edittemplate.php", array('id' => $id, 'pagecontextid' => $pagecontextid));
49 $pagetitle = get_string('addnewtemplate', 'tool_lp');
50 list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, null, $pagetitle);
52 $template = \tool_lp\api::read_template($id);
53 $pagetitle = get_string('edittemplate', 'tool_lp');
54 list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle);
57 $form = new \tool_lp\form\template($url->out(false), array('persistent' => $template, 'context' => $context));
58 if ($form->is_cancelled()) {
62 $data = $form->get_data();
65 if (empty($data->id)) {
66 $template = \tool_lp\api::create_template($data);
67 $returnurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', [
68 'templateid' => $template->get_id(),
69 'pagecontextid' => $pagecontextid
71 $returnmsg = get_string('templatecreated', 'tool_lp');
73 \tool_lp\api::update_template($data);
74 $returnmsg = get_string('templateupdated', 'tool_lp');
76 redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS);
79 $output = $PAGE->get_renderer('tool_lp');
80 echo $output->header();
81 echo $output->heading($title);
82 if (!empty($subtitle)) {
83 echo $output->heading($subtitle, 3);
88 echo $output->footer();