-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.php
253 lines (193 loc) · 9.39 KB
/
submit.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?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/>.
/**
* The page for submitting in mod_annopy.
*
* @package mod_annopy
* @copyright 2023 coactum GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\notification;
use mod_annopy\local\helper;
require(__DIR__.'/../../config.php');
require_once('./submit_form.php');
global $DB;
// Course Module ID.
$id = required_param('id', PARAM_INT);
// Module instance ID as alternative.
$a = optional_param('a', null, PARAM_INT);
// ID of the submission to be edited (if existing).
$submissionid = optional_param('submissionid', '0', PARAM_INT);
// Set the basic variables $course, $cm and $moduleinstance.
if ($id) {
[$course, $cm] = get_course_and_cm_from_cmid($id, 'annopy');
$moduleinstance = $DB->get_record('annopy', ['id' => $cm->instance], '*', MUST_EXIST);
} else {
throw new moodle_exception('missingparameter');
}
if (!$cm) {
throw new moodle_exception(get_string('incorrectmodule', 'annopy'));
} else if (!$course) {
throw new moodle_exception(get_string('incorrectcourseid', 'annopy'));
} else if (!$coursesections = $DB->get_record("course_sections", ["id" => $cm->section])) {
throw new moodle_exception(get_string('incorrectmodule', 'annopy'));
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/annopy:addsubmission', $context);
$data = new stdClass();
$data->id = $cm->id;
// Get submission that should be edited.
if ($DB->record_exists('annopy_submissions', ['annopy' => $moduleinstance->id])) {
$submission = $DB->get_record('annopy_submissions', ['annopy' => $moduleinstance->id]);
// Prevent editing of submissions not started by this user.
if ($submission->author != $USER->id) {
redirect(new moodle_url('/mod/annopy/view.php?id=' . $cm->id), get_string('editsubmissionnotpossible', 'mod_annopy'),
null, notification::NOTIFY_ERROR);
}
$data->submissionid = $submission->id;
$data->timecreated = time();
$data->submission = $submission->content;
$data->submissionformat = $submission->format;
$data->title = $submission->title;
$title = get_string('editsubmission', 'mod_annopy');
} else {
$submission = false;
$data->submissionid = null;
$data->timecreated = time();
$data->submission = '';
$data->submissionformat = FORMAT_HTML;
$data->title = get_string('submission', 'mod_annopy');
$title = get_string('addsubmission', 'mod_annopy');
}
list ($editoroptions, $attachmentoptions) = helper::annopy_get_editor_and_attachment_options($course, $context, $moduleinstance);
$data = file_prepare_standard_editor($data, 'submission', $editoroptions, $context,
'mod_annopy', 'submission', $data->submissionid);
$data = file_prepare_standard_filemanager($data, 'attachment', $attachmentoptions, $context,
'mod_annopy', 'attachment', $data->submissionid);
// Create form.
$form = new mod_annopy_submit_form(null, ['editoroptions' => $editoroptions]);
// Set existing data for this submission.
$form->set_data($data);
if ($form->is_cancelled()) {
redirect($CFG->wwwroot . '/mod/annopy/view.php?id=' . $cm->id);
} else if ($fromform = $form->get_data()) {
global $DB;
if (isset($fromform->submissionid)) {
if ($fromform->submissionid !== 0) { // Update existing submission.
// Get existing submission.
$submission = $DB->get_record('annopy_submissions',
['annopy' => $moduleinstance->id, 'id' => $fromform->submissionid]);
// Set new version and time modified.
$submission->currentversion += 1;
$submission->timemodified = time();
// Set editor for plugin files.
$fromform = file_postupdate_standard_editor($fromform, 'submission', $editoroptions, $editoroptions['context'],
'mod_annopy', 'submission', $submission->id);
$submissiontext = file_rewrite_pluginfile_urls($fromform->submission, 'pluginfile.php', $context->id,
'mod_annopy', 'submission', $submission->id);
// Set submission title, content and format.
$submission->title = format_text($fromform->title, 1, ['para' => false]);
$submission->content = format_text($submissiontext,
$fromform->submission_editor['format'], ['para' => false]);
$submission->format = (int) $fromform->submission_editor['format'];
// Update submission.
$updated = $DB->update_record('annopy_submissions', $submission);
if ($updated) {
// Trigger submission updated event.
$event = \mod_annopy\event\submission_updated::create([
'objectid' => $submission->id,
'context' => $context,
]);
$event->trigger();
redirect(new moodle_url('/mod/annopy/view.php', ['id' => $id]),
get_string('submissionmodified', 'mod_annopy'), null, notification::NOTIFY_SUCCESS);
} else {
redirect(new moodle_url('/mod/annopy/view.php', ['id' => $id]),
get_string('submissionnotmodified', 'mod_annopy'), null, notification::NOTIFY_ERROR);
}
} else if ($fromform->submissionid === 0) { // New submission.
if (!$DB->get_record('annopy_submissions', ['annopy' => $moduleinstance->id])) { // No submission made yet.
// Create new submission object.
$submission = new stdClass();
$submission->annopy = (int) $moduleinstance->id;
$submission->author = $USER->id;
// Set new version, time created, and modified.
$submission->currentversion = 1;
$submission->timecreated = time();
$submission->timemodified = null;
// Set submission title, content and format.
$submission->title = format_text($fromform->title, 1, ['para' => false]);
$submission->content = '';
$submission->format = 1;
// Save submission.
$submission->id = $DB->insert_record('annopy_submissions', $submission);
// Set editor for plugin files.
$fromform = file_postupdate_standard_editor($fromform, 'submission', $editoroptions, $editoroptions['context'],
'mod_annopy', 'submission', $submission->id);
$submissiontext = file_rewrite_pluginfile_urls($fromform->submission, 'pluginfile.php',
$context->id, 'mod_annopy', 'submission', $submission->id);
// Set submission text and format.
$submission->content = format_text($submissiontext,
$fromform->submission_editor['format'], ['para' => false]);
$submission->format = (int) $fromform->submission_editor['format'];
// Update submission with formatted content.
$updated = $DB->update_record('annopy_submissions', $submission);
if ($updated) {
// Trigger submission created event.
$event = \mod_annopy\event\submission_created::create([
'objectid' => $submission->id,
'context' => $context,
]);
$event->trigger();
redirect(new moodle_url('/mod/annopy/view.php',
['id' => $id]),
get_string('submissioncreated', 'mod_annopy'), null, notification::NOTIFY_SUCCESS);
} else {
redirect(new moodle_url('/mod/annopy/view.php',
['id' => $id]),
get_string('submissionnotcreated', 'mod_annopy'), null, notification::NOTIFY_ERROR);
}
} else {
redirect(new moodle_url('/mod/annopy/view.php',
['id' => $id]),
get_string('submissionfaileddoubled', 'mod_annopy'), null, notification::NOTIFY_ERROR);
}
}
}
}
// Get the name for this activity.
$modulename = format_string($moduleinstance->name, true, [
'context' => $context,
]);
$PAGE->set_url('/mod/annopy/submit.php', ['id' => $id]);
$PAGE->navbar->add($title);
$PAGE->set_title($modulename . ' - ' . $title);
$PAGE->set_heading($course->fullname);
if ($CFG->branch < 400) {
$PAGE->force_settings_menu();
}
echo $OUTPUT->header();
if ($CFG->branch < 400) {
echo $OUTPUT->heading($modulename);
if ($moduleinstance->intro) {
echo $OUTPUT->box(format_module_intro('annopy', $moduleinstance, $cm->id), 'generalbox', 'intro');
}
}
echo $OUTPUT->heading($title, 4);
// Display the form for adding or editing the submission.
$form->display();
echo $OUTPUT->footer();