MDL-52485 tool_lp: Template due date can be changed at will
[moodle.git] / admin / tool / lp / user_evidence_edit.php
CommitLineData
6c2b163c
FM
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 * User evidence (evidence of prior learning).
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../../../config.php');
26
27require_login(null, false);
28if (isguestuser()) {
29 throw new require_login_exception('Guests are not allowed here.');
30}
31
32$userid = optional_param('userid', $USER->id, PARAM_INT);
33$id = optional_param('id', null, PARAM_INT);
34$returntype = optional_param('return', 'list', PARAM_ALPHA);
35
36$url = new moodle_url('/admin/tool/lp/user_evidence_edit.php', array('id' => $id, 'userid' => $userid, 'return' => $returntype));
37
38$userevidence = null;
39if (empty($id)) {
40 $pagetitle = get_string('addnewuserevidence', 'tool_lp');
41 list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_user_evidence($userid, $url, null,
42 $pagetitle, $returntype);
43
44} else {
45 $userevidence = \tool_lp\api::read_user_evidence($id);
46
47 // The userid parameter must be the same as the owner of the evidence.
48 if ($userid != $userevidence->get_userid()) {
49 throw new coding_exception('Inconsistency between the userid parameter and the userid of the plan.');
50 }
51
52 $pagetitle = get_string('edituserevidence', 'tool_lp');
53 list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_user_evidence($userid, $url, $userevidence,
54 $pagetitle, $returntype);
55}
56
57// The context has been set to the user context in the page_helper.
58$context = $PAGE->context;
59
60$fileareaoptions = array('subdirs' => false);
61$customdata = array(
6c2b163c 62 'fileareaoptions' => $fileareaoptions,
7507b82a 63 'persistent' => $userevidence,
6c2b163c
FM
64 'userid' => $userid,
65);
66
67// Check if user has permissions to manage user evidence.
68if ($userevidence != null) {
69 if (!$userevidence->can_manage()) {
70 throw new required_capability_exception($context, 'tool/lp:userevidencemanage', 'nopermissions', '');
71 }
72 $customdata['evidence'] = $userevidence;
73
74} else if (!\tool_lp\user_evidence::can_manage_user($userid)) {
75 throw new required_capability_exception($context, 'tool/lp:userevidencemanage', 'nopermissions', '');
76}
77
78$form = new \tool_lp\form\user_evidence($url->out(false), $customdata);
79if ($form->is_cancelled()) {
80 redirect($returnurl);
81}
82
83// Load existing user evidence.
84$itemid = null;
85if ($userevidence) {
86 $itemid = $userevidence->get_id();
6c2b163c
FM
87}
88
89// Massaging the file API.
90$draftitemid = file_get_submitted_draft_itemid('files');
91file_prepare_draft_area($draftitemid, $context->id, 'tool_lp', 'userevidence', $itemid, $fileareaoptions);
92$form->set_data((object) array('files' => $draftitemid));
93
94// We're getting there...
95$output = $PAGE->get_renderer('tool_lp');
96echo $output->header();
97echo $output->heading($title);
98if (!empty($subtitle)) {
99 echo $output->heading($subtitle, 3);
100}
101
102// Hurray, the user has submitted the form! Everyone loves forms :)!
103if ($data = $form->get_data()) {
104 require_sesskey();
6c2b163c
FM
105 $draftitemid = $data->files;
106 unset($data->files);
107
108 if (empty($userevidence)) {
109 $userevidence = \tool_lp\api::create_user_evidence($data, $draftitemid);
110 echo $output->notification(get_string('userevidencecreated', 'tool_lp'), 'notifysuccess');
111 echo $output->continue_button($returnurl);
112 } else {
6c2b163c
FM
113 \tool_lp\api::update_user_evidence($data, $draftitemid);
114 echo $output->notification(get_string('userevidenceupdated', 'tool_lp'), 'notifysuccess');
115 echo $output->continue_button($returnurl);
116 }
117
118} else {
119 $form->display();
120}
121
122echo $output->footer();