forked from coactum/moodle-mod_margic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
306 lines (239 loc) · 11.4 KB
/
edit.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?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 the edit entry form in mod_margic.
*
* @package mod_margic
* @copyright 2022 coactum GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_margic\local\helper;
use mod_margic\event\invalid_access_attempt;
use core\output\notification;
use mod_margic\output\margic_entry;
require(__DIR__.'/../../config.php');
require_once('./edit_form.php');
require_once($CFG->dirroot . '/mod/margic/locallib.php');
global $DB;
// Course Module ID.
$id = required_param('id', PARAM_INT);
// Module instance ID as alternative.
$m = optional_param('m', null, PARAM_INT);
// ID of the entry to be edited (if existing).
$entryid = optional_param('entryid', '0', PARAM_INT);
$margic = margic::get_margic_instance($id, $m, false, 'currententry', 0, 1);
$moduleinstance = $margic->get_module_instance();
$course = $margic->get_course();
$context = $margic->get_context();
$cm = $margic->get_course_module();
if (! $cm) {
throw new moodle_exception(get_string('incorrectmodule', 'margic'));
}
if (! $course) {
throw new moodle_exception(get_string('incorrectcourseid', 'margic'));
}
if (! $coursesections = $DB->get_record("course_sections", ["id" => $cm->section])) {
throw new moodle_exception(get_string('incorrectmodule', 'margic'));
}
require_login($course, true, $cm);
require_capability('mod/margic:addentries', $context);
// Prevent creating and editing of entries if user is not allowed to edit entry or activity is not available.
if (($entryid && !$moduleinstance->editentries) || !helper::margic_available($moduleinstance)) {
// Trigger invalid_access_attempt with redirect to the view page.
$params = [
'objectid' => $id,
'context' => $context,
'other' => ['file' => 'edit.php'],
];
$event = invalid_access_attempt::create($params);
$event->trigger();
redirect('view.php?id='.$id, get_string('editentrynotpossible', 'margic'), null, notification::NOTIFY_ERROR);
}
// Header.
if ($entryid) {
$title = get_string('editentry', 'mod_margic');
} else {
$title = get_string('addnewentry', 'mod_margic');
}
$data = new stdClass();
$data->id = $cm->id;
// Get the single record specified by firstkey.
if ($DB->record_exists('margic_entries', ['margic' => $moduleinstance->id, "id" => $entryid])) {
$entry = $DB->get_record('margic_entries', ['margic' => $moduleinstance->id, "id" => $entryid]);
$notnewestentry = false;
// Prevent editing of entries that are not the newest version of a base entry or a unedited entry.
if (isset($entry->baseentry)) { // If entry has a base entry check if this entry is the newest childentry.
$otherchildentries = $DB->get_records('margic_entries',
['margic' => $moduleinstance->id, 'baseentry' => $entry->baseentry], 'timecreated DESC');
if ($entry->timecreated < $otherchildentries[array_key_first($otherchildentries)]->timecreated) {
$notnewestentry = true;
}
} else { // If this entry has no base entry check if it has childentries and cant therefore be edited.
$childentries = $DB->get_records('margic_entries', ['margic' => $moduleinstance->id, 'baseentry' => $entry->id],
'timecreated DESC');
if (!empty($childentries)) {
$notnewestentry = true;
}
}
// Prevent editing of entries not started by this user or if it is not the newest child entry.
if ($entry->userid != $USER->id || $notnewestentry) {
// Trigger invalid_access_attempt with redirect to the view page.
$params = [
'objectid' => $id,
'context' => $context,
'other' => ['file' => 'edit.php'],
];
$event = invalid_access_attempt::create($params);
$event->trigger();
redirect('view.php?id='.$id, get_string('editentrynotpossible', 'margic'), null, notification::NOTIFY_ERROR);
}
$data->entryid = $entry->id;
$data->timecreated = time();
$data->text = $entry->text;
$data->textformat = $entry->format;
$PAGE->requires->js_call_amd('mod_margic/annotations', 'init',
['cmid' => $cm->id, 'canmakeannotations' => false, 'myuserid' => $USER->id]);
} else {
$entry = false;
$data->entryid = null;
$data->timecreated = time();
$data->text = '';
$data->textformat = FORMAT_HTML;
}
list ($editoroptions, $attachmentoptions) = helper::margic_get_editor_and_attachment_options($course, $context, $moduleinstance);
$data = file_prepare_standard_editor($data, 'text', $editoroptions, $context, 'mod_margic', 'entry', $data->entryid);
$data = file_prepare_standard_filemanager($data, 'attachment', $attachmentoptions, $context,
'mod_margic', 'attachment', $data->entryid);
// Create form.
$form = new mod_margic_entry_form(null,
['editentrydates' => $moduleinstance->editentrydates, 'editoroptions' => $editoroptions]);
// Set existing data for this entry.
$form->set_data($data);
if ($form->is_cancelled()) {
redirect($CFG->wwwroot . '/mod/margic/view.php?id=' . $cm->id);
} else if ($fromform = $form->get_data()) {
$timenow = time();
// Relink using the proper entryid because draft area didn't have an itemid associated when creating new entry.
$newentry = new stdClass();
$newentry->margic = $moduleinstance->id;
$newentry->userid = $USER->id;
if ($moduleinstance->editentrydates) {
$newentry->timecreated = $fromform->timecreated;
$newentry->timemodified = $fromform->timecreated;
} else {
$newentry->timecreated = $timenow;
$newentry->timemodified = $timenow;
}
$newentry->text = '';
$newentry->format = 1;
if ($fromform->entryid != 0 && $entry != false) { // If existing entry is edited.
if (!isset($entry->baseentry)) {
$newentry->baseentry = $fromform->entryid;
} else {
$newentry->baseentry = $entry->baseentry;
}
// Check if timecreated is not older then connected entries.
if ($moduleinstance->editentrydates) {
$baseentry = $DB->get_record('margic_entries', ['margic' => $moduleinstance->id, "id" => $newentry->baseentry]);
if ($newentry->timecreated <= $baseentry->timemodified) {
redirect(new moodle_url('/mod/margic/view.php?id=' . $cm->id), get_string('timecreatedinvalid', 'mod_margic'),
null, notification::NOTIFY_ERROR);
}
$connectedentries = $DB->get_records('margic_entries',
['margic' => $moduleinstance->id, 'baseentry' => $newentry->baseentry], 'timecreated DESC');
if ($connectedentries && $newentry->timecreated <= $connectedentries[array_key_first($connectedentries)]->timecreated) {
redirect(new moodle_url('/mod/margic/view.php?id=' . $cm->id), get_string('timecreatedinvalid', 'mod_margic'),
null, notification::NOTIFY_ERROR);
}
}
// Update timemodified for base entry.
$baseentry = $DB->get_record('margic_entries', ['margic' => $moduleinstance->id, "id" => $newentry->baseentry]);
$baseentry->timemodified = $newentry->timecreated;
$DB->update_record('margic_entries', $baseentry);
}
if (! $newentry->id = $DB->insert_record("margic_entries", $newentry)) {
throw new moodle_exception(get_string('generalerrorinsert', 'margic'));
}
$fromform = file_postupdate_standard_editor($fromform, 'text', $editoroptions, $editoroptions['context'],
'mod_margic', 'entry', $newentry->id);
$entrytext = file_rewrite_pluginfile_urls($fromform->text, 'pluginfile.php', $context->id,
'mod_margic', 'entry', $newentry->id);
$newentry->text = format_text($entrytext, $fromform->textformat, ['para' => false]);
$newentry->format = $fromform->textformat;
$DB->update_record('margic_entries', $newentry);
if ($entry && $fromform->entryid) {
// Trigger module entry updated event.
$event = \mod_margic\event\entry_updated::create([
'objectid' => $newentry->id,
'context' => $context,
]);
} else {
// Trigger module entry created event.
$event = \mod_margic\event\entry_created::create([
'objectid' => $newentry->id,
'context' => $context,
]);
}
$event->trigger();
if ($moduleinstance->editentrydates && $fromform->timecreated > $timenow) {
redirect(new moodle_url('/mod/margic/view.php?id=' . $cm->id), get_string('entryaddedoredited', 'mod_margic') .
' ' . get_string('editdateinfuture', 'mod_margic'), null, notification::NOTIFY_WARNING);
} else {
redirect(new moodle_url('/mod/margic/view.php?id=' . $cm->id), get_string('entryaddedoredited', 'mod_margic'),
null, notification::NOTIFY_SUCCESS);
}
}
$PAGE->set_url('/mod/margic/edit.php', ['id' => $id]);
$PAGE->navbar->add($title);
$PAGE->set_title(format_string($moduleinstance->name) . ' - ' . $title);
$PAGE->set_heading($course->fullname);
if ($CFG->branch < 400) {
$PAGE->force_settings_menu();
}
echo $OUTPUT->header();
if ($CFG->branch < 400) {
echo $OUTPUT->heading(format_string($moduleinstance->name));
if ($moduleinstance->intro) {
echo $OUTPUT->box(format_module_intro('margic', $moduleinstance, $cm->id), 'generalbox', 'intro');
}
}
echo $OUTPUT->heading($title, 4);
// If existing entry is edited render entry.
if ($entry) {
$edittimes = helper::margic_get_edittime_options($moduleinstance);
$grades = make_grades_menu($moduleinstance->scale); // For select in grading_form.
$currentgroups = groups_get_activity_group($cm, true); // Get a list of the currently allowed groups for this course.
if ($currentgroups) {
$allowedusers = get_users_by_capability($context, 'mod/margic:addentries', '', $sort = 'lastname ASC, firstname ASC',
'', '', $currentgroups);
} else {
$allowedusers = true;
}
$strmanager = get_string_manager();
$gradingstr = get_string('needsgrading', 'margic');
$regradingstr = get_string('needsregrading', 'margic');
if ($entry->baseentry) { // If edited entry is child entry get base entry for rendering.
$entry = $DB->get_record('margic_entries', ['margic' => $moduleinstance->id, "id" => $entry->baseentry]);
}
$page = new margic_entry($margic, $cm, $context, $moduleinstance, $entry, $margic->get_annotationarea_width(),
$moduleinstance->editentries, $edittimes->edittimestarts, $edittimes->edittimenotstarted, $edittimes->edittimeends,
$edittimes->edittimehasended, has_capability('mod/margic:manageentries', $context), $course, false, true, false,
false, true, $grades, $currentgroups, $allowedusers, $strmanager, $gradingstr, $regradingstr, sesskey(), true);
echo $OUTPUT->render($page);
}
// Display the form for editing the entry.
$form->display();
echo $OUTPUT->footer();