-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadditional_request.php
223 lines (175 loc) · 7.97 KB
/
additional_request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Display the form for a student to request additional time on an existing extension.
*
* @package local_extension
* @author Nicholas Hoobin <[email protected]>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_extension\attachment_processor;
require_once(__DIR__ . '/../../config.php');
global $CFG, $PAGE;
$requestid = required_param('id', PARAM_INT);
$cmid = required_param('cmid', PARAM_INT);
$courseid = required_param('courseid', PARAM_INT);
$PAGE->set_url(new moodle_url('/local/extension/additional_request.php'), [
'id' => $requestid,
'cmid' => $cmid,
'courseid' => $courseid,
]);
$selfurl = $PAGE->url;
$cm = get_fast_modinfo($courseid)->get_cm($cmid);
require_login($courseid, false, $cm);
$request = \local_extension\request::from_id($requestid);
$context = context_module::instance($cmid);
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('pluginname', 'local_extension'));
$PAGE->set_heading(get_string('page_heading_additional_request', 'local_extension'));
$PAGE->requires->css('/local/extension/styles.css');
$PAGE->add_body_class('local_extension');
$requestuser = core_user::get_user($request->request->userid);
$pageurl = new moodle_url('/local/extension/status.php', ['id' => $request->requestid]);
$obj = ['id' => $requestid, 'name' => fullname($requestuser)];
$PAGE->navbar->ignore_active();
$PAGE->navbar->add(get_string('breadcrumb_nav_index', 'local_extension'), new moodle_url('/local/extension/index.php'));
$PAGE->navbar->add(get_string('breadcrumb_nav_status', 'local_extension', $obj), $pageurl);
$PAGE->navbar->add(get_string('breadcrumb_nav_additional', 'local_extension'));
$cm = $request->mods[$cmid]->localcm;
$event = $request->mods[$cmid]->event;
$course = $request->mods[$cmid]->course;
$handler = $request->mods[$cmid]->handler;
$params = [
'request' => $request,
'cmid' => $cmid,
'instance' => $handler->get_instance($request->mods[$cmid]),
'reviewdate' => 0,
];
$requestform = new \local_extension\form\additional_request(null, $params);
if ($requestform->is_cancelled()) {
$statusurl = new moodle_url('/local/extension/status.php', array('id' => $requestid));
redirect($statusurl);
} else if ($requestdata = $requestform->get_data()) {
// Did we submit to review the contents?
if ($requestdata->review) {
// Provide the new due date to the review form.
$due = 'due' . $cmid;
$params['reviewdate'] = $requestdata->$due;
// Unset the submit button text. We have changed this.
unset($requestdata->submitbutton);
$reviewform = new \local_extension\form\additional_review(null, $params);
$reviewform->set_data($requestdata);
// Show the review page before processing the data.
echo $OUTPUT->header();
$reviewform->display();
echo $OUTPUT->footer();
exit();
}
}
$reviewform = new \local_extension\form\additional_review(null, $params);
$reviewdata = $reviewform->get_data();
if ($reviewform->is_cancelled()) {
redirect($PAGE->url);
} else if ($reviewdata = $reviewform->get_data()) {
// Use the same time for the attachments and comments.
$time = time();
// The review has been confirmed, try to submit the extension request!
$notifycontent = [];
// Update the requested cm length.
$due = 'due' . $cmid;
$newdate = $reviewdata->$due;
$cm->cm->data = $newdate;
$cm->cm->length = $newdate - $event->timestart;
$cm->update_data();
// Update the state of the cm to state::REOPENED.
$reviewdata->s = \local_extension\state::STATE_REOPENED;
$notifycontent[] = \local_extension\state::instance()->update_cm_state($request, $USER, $reviewdata, $time);
// Adding the comment to the notify content.
$comment = $reviewdata->commentarea;
if (!empty($comment)) {
$notifycontent[] = $request->add_comment($USER, $comment, $time);
}
// Obtain any new attachments that have been added.
$draftcontext = context_user::instance($USER->id);
$usercontext = context_user::instance($request->request->userid);
$itemid = $requestid;
$draftitemid = file_get_submitted_draft_itemid('attachments');
$fs = get_file_storage();
$draftfiles = $fs->get_area_files($draftcontext->id, 'user', 'draft', $draftitemid, 'id');
$oldfiles = $fs->get_area_files($usercontext->id, 'local_extension', 'attachments', $itemid, 'id');
$processor = new attachment_processor($oldfiles);
$draftfiles = $processor->rename_new_files($draftfiles);
// File count must be greater that 1, as an item is the directory '.'.
if (count($draftfiles) > 1) {
// We need to add the existing files to the draft area so they are saved and merged with the new area.
foreach ($oldfiles as $oldfile) {
$filerecord = new stdClass();
$filerecord->contextid = $draftcontext->id;
$filerecord->component = 'user';
$filerecord->filearea = 'draft';
$filerecord->itemid = $draftitemid;
// Check if see if the pathname hash exists before adding the file.
$hash = $fs->get_pathname_hash(
$usercontext->id,
'user',
'draft',
$draftitemid,
$oldfile->get_filepath(),
$oldfile->get_filename()
);
// We do not delete / update / modify the old file. Ideally we will not reach this state due to the previous validation.
if (!array_key_exists($hash, $draftfiles)) {
$fs->create_file_from_storedfile($filerecord, $oldfile);
}
}
file_save_draft_area_files($draftitemid, $usercontext->id, 'local_extension', 'attachments', $itemid);
$draftnames = [];
$oldnames = [];
foreach ($draftfiles as $file) {
$draftnames[$file->get_filename()] = $file;
}
foreach ($oldfiles as $file) {
$oldnames[$file->get_filename()] = $file;
}
// This diff array will contain all the new files to be attached.
$diff = array_diff_key($draftnames, $oldnames);
foreach ($diff as $file) {
$notifycontent[] = $request->add_attachment_history($file, $time);
}
}
// Cleaning up the array.
$notifycontent = array_filter($notifycontent, function($obj) {
return !is_null($obj);
});
// Notify the core set of subscribers with updated details.
$request->notify_subscribers($notifycontent, $USER->id);
// We've modified the length of the request, too many people may be notified, lets reset the list of subscriptions.
$request->reset_subscribers($cmid);
// The state has changed / dates are different. Triggers may associate new users or set other rules.
$request->process_triggers();
// Update the lastmod.
$request->update_lastmod($USER->id, $time);
// Invalidate the cache for this request. The content has changed.
$request->get_data_cache()->delete($request->requestid);
$url = new moodle_url('/local/extension/status.php', ['id' => $requestid]);
redirect($url);
}
// Output the initial form to request an additional extension.
echo $OUTPUT->header();
$requestform->display();
echo $OUTPUT->footer();