MDL-52485 tool_lp: Template due date can be changed at will
[moodle.git] / admin / tool / lp / editcompetency.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 competencies.
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
d9a39950
DW
28$title = get_string('competencies', 'tool_lp');
29$id = optional_param('id', 0, PARAM_INT);
30$competencyframeworkid = required_param('competencyframeworkid', PARAM_INT);
2de75345 31$pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to the context we came from.
d9a39950
DW
32$parentid = optional_param('parentid', 0, PARAM_INT);
33
2de75345
FM
34require_login();
35$pagecontext = context::instance_by_id($pagecontextid);
36
d9a39950 37// Set up the page.
2de75345
FM
38$url = new moodle_url("/admin/tool/lp/editcompetency.php", array('id' => $id, 'competencyframeworkid' => $competencyframeworkid,
39 'parentid' => $parentid, 'pagecontextid' => $pagecontextid));
40$frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid));
41$frameworkurl = new moodle_url('/admin/tool/lp/competencies.php', array('competencyframeworkid' => $competencyframeworkid,
42 'pagecontextid' => $pagecontextid));
d9a39950 43
8cb101a7 44$competency = null;
d9a39950 45$competencyframework = \tool_lp\api::read_framework($competencyframeworkid);
8cb101a7
FM
46if (!empty($id)) {
47 $competency = \tool_lp\api::read_competency($id);
48}
49
d9a39950 50$parent = null;
8cb101a7
FM
51if ($competency) {
52 $parent = $competency->get_parent();
53} else if ($parentid) {
d9a39950
DW
54 $parent = \tool_lp\api::read_competency($parentid);
55}
56
8cb101a7
FM
57if (empty($id)) {
58 $level = $parent ? $parent->get_level() + 1 : 1;
59 $pagetitle = get_string('taxonomy_add_' . $competencyframework->get_taxonomy($level), 'tool_lp');
60} else {
61 $pagetitle = get_string('taxonomy_edit_' . $competencyframework->get_taxonomy($competency->get_level()), 'tool_lp');
62}
63
2de75345
FM
64$PAGE->navigation->override_active_url($frameworksurl);
65$PAGE->set_context($pagecontext);
66$PAGE->set_pagelayout('admin');
67$PAGE->set_url($url);
68$PAGE->set_title($title);
69$PAGE->set_heading($title);
70$PAGE->navbar->add($competencyframework->get_shortname(), $frameworkurl);
71$output = $PAGE->get_renderer('tool_lp');
72
7507b82a
FM
73$form = new \tool_lp\form\competency($url->out(false), array('competencyframework' => $competencyframework,
74 'parent' => $parent, 'persistent' => $competency));
d9a39950
DW
75
76if ($form->is_cancelled()) {
2de75345 77 redirect($frameworkurl);
d9a39950
DW
78}
79
80echo $output->header();
81echo $output->heading($pagetitle);
82
83$data = $form->get_data();
84if ($data) {
7507b82a
FM
85 require_sesskey();
86 if (empty($competency)) {
d9a39950
DW
87 \tool_lp\api::create_competency($data);
88 echo $output->notification(get_string('competencycreated', 'tool_lp'), 'notifysuccess');
2de75345 89 echo $output->continue_button($frameworkurl);
d9a39950 90 } else {
d9a39950
DW
91 \tool_lp\api::update_competency($data);
92 echo $output->notification(get_string('competencyupdated', 'tool_lp'), 'notifysuccess');
2de75345 93 echo $output->continue_button($frameworkurl);
d9a39950
DW
94 }
95} else {
96 $form->display();
97}
98
d9a39950 99echo $output->footer();