MDL-52195 tool_lp: Normalise the navigation in templates
[moodle.git] / admin / tool / lp / edittemplate.php
CommitLineData
d9a39950
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * This page lets users to manage site wide learning plan templates.
19 *
20 * @package tool_lp
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../../../config.php');
26require_once($CFG->libdir.'/adminlib.php');
27
f0da26a4
FM
28$id = optional_param('id', 0, PARAM_INT);
29$pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to where we can from.
30
47855def 31$template = null;
f0da26a4
FM
32if (!empty($id)) {
33 // Always use the context from the framework when it exists.
34 $template = new \tool_lp\template($id);
35 $context = $template->get_context();
36} else {
37 $context = context::instance_by_id($pagecontextid);
38}
39
40// We check that we have the permission to edit this framework, in its own context.
47855def 41require_login(0, false);
f0da26a4
FM
42require_capability('tool/lp:templatemanage', $context);
43
44// We keep the original context in the URLs, so that we remain in the same context.
45$url = new moodle_url("/admin/tool/lp/edittemplate.php", array('id' => $id, 'pagecontextid' => $pagecontextid));
f0da26a4 46$formurl = new moodle_url("/admin/tool/lp/edittemplate.php", array('pagecontextid' => $pagecontextid));
d9a39950 47
d9a39950
DW
48if (empty($id)) {
49 $pagetitle = get_string('addnewtemplate', 'tool_lp');
47855def 50 list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, null, $pagetitle);
d9a39950 51} else {
47855def 52 $template = \tool_lp\api::read_template($id);
d9a39950 53 $pagetitle = get_string('edittemplate', 'tool_lp');
47855def 54 list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle);
d9a39950 55}
d9a39950 56
47855def 57$form = new \tool_lp\form\template($formurl->out(false), array('template' => $template, 'context' => $context));
d9a39950 58if ($form->is_cancelled()) {
47855def 59 redirect($returnurl);
d9a39950
DW
60}
61
47855def 62$output = $PAGE->get_renderer('tool_lp');
d9a39950 63echo $output->header();
47855def
FM
64echo $output->heading($title);
65if (!empty($subtitle)) {
66 echo $output->heading($subtitle, 3);
67}
d9a39950
DW
68
69$data = $form->get_data();
70if ($data) {
71 // Save the changes and continue back to the manage page.
72 // Massage the editor data.
73 $data->descriptionformat = $data->description['format'];
74 $data->description = $data->description['text'];
75 if (empty($data->id)) {
76 // Create new template.
77 require_sesskey();
f0da26a4 78 $data->contextid = $context->id;
d9a39950
DW
79 \tool_lp\api::create_template($data);
80 echo $output->notification(get_string('templatecreated', 'tool_lp'), 'notifysuccess');
47855def 81 echo $output->continue_button($returnurl);
d9a39950
DW
82 } else {
83 require_sesskey();
84 \tool_lp\api::update_template($data);
85 echo $output->notification(get_string('templateupdated', 'tool_lp'), 'notifysuccess');
47855def 86 echo $output->continue_button($returnurl);
d9a39950
DW
87 }
88} else {
89 $form->display();
90}
91
92
93echo $output->footer();