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 * Page to edit a plan.
21 * @copyright 2015 David Monllao
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 $userid = optional_param('userid', false, PARAM_INT
);
29 $id = optional_param('id', false, PARAM_INT
);
30 $returntype = optional_param('return', null, PARAM_ALPHA
);
32 require_login(0, false);
33 \core_competency\api
::require_enabled();
35 $url = new moodle_url('/admin/tool/lp/editplan.php', array('id' => $id, 'userid' => $userid, 'return' => $returntype));
39 $pagetitle = get_string('addnewplan', 'tool_lp');
40 list($title, $subtitle, $returnurl) = \tool_lp\page_helper
::setup_for_plan($userid, $url, null, $pagetitle, $returntype);
42 $plan = \core_competency\api
::read_plan($id);
44 // The userid parameter must be the same as the owner of the plan.
45 if ($userid != $plan->get('userid')) {
46 throw new coding_exception('Inconsistency between the userid parameter and the userid of the plan');
49 $pagetitle = get_string('editplan', 'tool_lp');
50 list($title, $subtitle, $returnurl) = \tool_lp\page_helper
::setup_for_plan($userid, $url, $plan, $pagetitle, $returntype);
53 $output = $PAGE->get_renderer('tool_lp');
55 // Custom data to pass to the form.
56 $customdata = array('userid' => $userid, 'context' => $PAGE->context
, 'persistent' => $plan);
58 // User can create plan if he can_manage_user with active/complete status
59 // or if he can_manage_user_draft with draft status.
60 $cancreate = \core_competency\plan
::can_manage_user_draft($userid) || \core_competency\plan
::can_manage_user($userid);
62 // If editing plan, check if user has permissions to edit it.
64 if (!$plan->can_manage()) {
65 throw new required_capability_exception($PAGE->context
, 'moodle/competency:planmanage', 'nopermissions', '');
67 if (!$plan->can_be_edited()) {
68 throw new coding_exception('Completed plan can not be edited');
70 } else if (!$cancreate) {
71 throw new required_capability_exception($PAGE->context
, 'moodle/competency:planmanage', 'nopermissions', '');
74 $form = new \tool_lp\form\
plan($url->out(false), $customdata);
75 if ($form->is_cancelled()) {
79 $data = $form->get_data();
82 if (empty($data->id
)) {
83 $plan = \core_competency\api
::create_plan($data);
84 $returnurl = new moodle_url('/admin/tool/lp/plan.php', ['id' => $plan->get('id')]);
85 $returnmsg = get_string('plancreated', 'tool_lp');
87 \core_competency\api
::update_plan($data);
88 $returnmsg = get_string('planupdated', 'tool_lp');
90 redirect($returnurl, $returnmsg, null, \core\output\notification
::NOTIFY_SUCCESS
);
93 echo $output->header();
94 echo $output->heading($title);
95 if (!empty($subtitle)) {
96 echo $output->heading($subtitle, 3);
101 echo $output->footer();