. /** * Page to edit a plan. * * @package tool_lp * @copyright 2015 David Monllao * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once(__DIR__ . '/../../../config.php'); require_once($CFG->libdir.'/adminlib.php'); $userid = optional_param('userid', false, PARAM_INT); $id = optional_param('id', false, PARAM_INT); // Set up the page. if (empty($id)) { $pagetitle = get_string('addnewplan', 'tool_lp'); } else { $pagetitle = get_string('editplan', 'tool_lp'); } // Default to the current user. if (!$userid) { $userid = $USER->id; } $context = context_user::instance($userid); $params = array('userid' => $userid); if ($id) { $params['id'] = $id; } $url = new moodle_url("/admin/tool/lp/editplan.php", $params); $PAGE->set_context($context); $PAGE->set_url($url); $PAGE->set_title($pagetitle); $PAGE->set_pagelayout('admin'); $PAGE->set_heading($pagetitle); $output = $PAGE->get_renderer('tool_lp'); // Custom data to pass to the form. $customdata = array('userid' => $userid, 'context' => $context); // User can create plan if he can_manage_user with active/complete status // or if he can_manage_user_draft with draft status. $cancreate = \tool_lp\plan::can_manage_user_draft($userid) || \tool_lp\plan::can_manage_user($userid); // If editing plan get the plan and check if user has permissions to edit it. if ($id) { $plan = \tool_lp\api::read_plan($id); if (!$plan->can_manage()) { throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', ''); } $customdata['plan'] = $plan; } else if (!$cancreate) { throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', ''); } $form = new \tool_lp\form\plan(null, $customdata); if ($form->is_cancelled()) { redirect(new moodle_url('/admin/tool/lp/plans.php?userid=' . $userid)); } echo $output->header(); echo $output->heading($pagetitle); $data = $form->get_data(); if ($data) { $data->descriptionformat = $data->description['format']; $data->description = $data->description['text']; if (empty($data->id)) { require_sesskey(); \tool_lp\api::create_plan($data); echo $output->notification(get_string('plancreated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button('/admin/tool/lp/plans.php?userid=' . $userid); } else { require_sesskey(); \tool_lp\api::update_plan($data); echo $output->notification(get_string('planupdated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button('/admin/tool/lp/plans.php?userid=' . $userid); } } else { $form->display(); } echo $output->footer();