MDL-49463 tool_lp: Ability to add evidence of prior learning
[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
2de75345 37
d9a39950 38// Set up the page.
2de75345
FM
39$url = new moodle_url("/admin/tool/lp/editcompetency.php", array('id' => $id, 'competencyframeworkid' => $competencyframeworkid,
40 'parentid' => $parentid, 'pagecontextid' => $pagecontextid));
41$frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid));
42$frameworkurl = new moodle_url('/admin/tool/lp/competencies.php', array('competencyframeworkid' => $competencyframeworkid,
43 'pagecontextid' => $pagecontextid));
d9a39950 44
8cb101a7 45$competency = null;
d9a39950 46$competencyframework = \tool_lp\api::read_framework($competencyframeworkid);
8cb101a7
FM
47if (!empty($id)) {
48 $competency = \tool_lp\api::read_competency($id);
49}
50
d9a39950 51$parent = null;
8cb101a7
FM
52if ($competency) {
53 $parent = $competency->get_parent();
54} else if ($parentid) {
d9a39950
DW
55 $parent = \tool_lp\api::read_competency($parentid);
56}
57
8cb101a7
FM
58if (empty($id)) {
59 $level = $parent ? $parent->get_level() + 1 : 1;
60 $pagetitle = get_string('taxonomy_add_' . $competencyframework->get_taxonomy($level), 'tool_lp');
61} else {
62 $pagetitle = get_string('taxonomy_edit_' . $competencyframework->get_taxonomy($competency->get_level()), 'tool_lp');
63}
64
2de75345
FM
65$PAGE->navigation->override_active_url($frameworksurl);
66$PAGE->set_context($pagecontext);
67$PAGE->set_pagelayout('admin');
68$PAGE->set_url($url);
69$PAGE->set_title($title);
70$PAGE->set_heading($title);
71$PAGE->navbar->add($competencyframework->get_shortname(), $frameworkurl);
72$output = $PAGE->get_renderer('tool_lp');
73
74$form = new \tool_lp\form\competency($url->out(false), array('id' => $id, 'competencyframework' => $competencyframework,
8cb101a7 75 'parent' => $parent, 'competency' => $competency));
d9a39950
DW
76
77if ($form->is_cancelled()) {
2de75345 78 redirect($frameworkurl);
d9a39950
DW
79}
80
81echo $output->header();
82echo $output->heading($pagetitle);
83
84$data = $form->get_data();
85if ($data) {
86 // Save the changes and continue back to the manage page.
87 // Massage the editor data.
88 $data->descriptionformat = $data->description['format'];
89 $data->description = $data->description['text'];
90 if (empty($data->id)) {
91 // Create new framework.
92 require_sesskey();
93 \tool_lp\api::create_competency($data);
94 echo $output->notification(get_string('competencycreated', 'tool_lp'), 'notifysuccess');
2de75345 95 echo $output->continue_button($frameworkurl);
d9a39950
DW
96 } else {
97 require_sesskey();
98 \tool_lp\api::update_competency($data);
99 echo $output->notification(get_string('competencyupdated', 'tool_lp'), 'notifysuccess');
2de75345 100 echo $output->continue_button($frameworkurl);
d9a39950
DW
101 }
102} else {
103 $form->display();
104}
105
d9a39950 106echo $output->footer();