forked from coactum/moodle-mod_margic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.php
208 lines (158 loc) · 8.26 KB
/
annotations.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
<?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/>.
/**
* File for handling the annotation form.
*
* @package mod_margic
* @copyright 2022 coactum GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\notification;
require(__DIR__.'/../../config.php');
require_once($CFG->dirroot . '/mod/margic/locallib.php');
global $DB, $CFG;
// Course Module ID.
$id = required_param('id', PARAM_INT);
// Module instance ID as alternative.
$m = optional_param('m', null, PARAM_INT);
// Param if annotations should be returned via ajax.
$getannotations = optional_param('getannotations', 0, PARAM_INT);
// Param if annotation should be deleted.
$deleteannotation = optional_param('deleteannotation', 0, PARAM_INT); // Annotation to be deleted.
$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 (!$moduleinstance) {
throw new moodle_exception(get_string('incorrectmodule', 'margic'));
}
if (! $coursesections = $DB->get_record("course_sections", ["id" => $cm->section])) {
throw new moodle_exception(get_string('incorrectmodule', 'margic'));
}
require_login($course, true, $cm);
// Get annotation (ajax).
if ($getannotations) {
$annotations = $margic->get_annotations();
if ($annotations) {
echo json_encode($annotations);
} else {
echo json_encode([]);
}
die;
}
require_capability('mod/margic:makeannotations', $context);
// Header.
$PAGE->set_url('/mod/margic/annotations.php', ['id' => $id]);
$PAGE->set_title(format_string($moduleinstance->name));
$urlparams = ['id' => $id, 'annotationmode' => 1];
$redirecturl = new moodle_url('/mod/margic/view.php', $urlparams);
// Delete annotation.
if (has_capability('mod/margic:deleteannotations', $context) && $deleteannotation !== 0) {
require_sesskey();
global $USER;
$a = $DB->get_record('margic_annotations', ['id' => $deleteannotation, 'margic' => $moduleinstance->id]);
if (isset($a) && ($moduleinstance->overwriteannotations || $a->userid == $USER->id)) {
$DB->delete_records('margic_annotations', ['id' => $deleteannotation, 'margic' => $moduleinstance->id]);
// Trigger module annotation deleted event.
$event = \mod_margic\event\annotation_deleted::create(['objectid' => $deleteannotation, 'context' => $context]);
$event->trigger();
redirect($redirecturl, get_string('annotationdeleted', 'mod_margic'), null, notification::NOTIFY_SUCCESS);
} else {
redirect($redirecturl, get_string('notallowedtodothis', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
}
// Save annotation.
require_once($CFG->dirroot . '/mod/margic/annotation_form.php');
// Instantiate form.
$mform = new mod_margic_annotation_form(null, ['types' => $margic->get_errortypes_for_form()]);
if ($fromform = $mform->get_data()) {
// In this case you process validated data. $mform->get_data() returns data posted in form.
if ((isset($fromform->annotationid) && $fromform->annotationid !== 0) && isset($fromform->text)) { // Update annotation.
$annotation = $DB->get_record('margic_annotations', ['margic' => $cm->instance, 'entry' => $fromform->entry,
'id' => $fromform->annotationid, ]);
// Prevent changes by user in hidden form fields.
if (!$annotation) {
redirect($redirecturl, get_string('annotationinvalid', 'mod_margic'), null, notification::NOTIFY_ERROR);
} else if (!$moduleinstance->overwriteannotations && $annotation->userid != $USER->id) {
redirect($redirecturl, get_string('notallowedtodothis', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
if (!isset($fromform->type)) {
redirect($redirecturl, get_string('errtypedeleted', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
$annotation->timemodified = time();
$annotation->text = format_text($fromform->text, 2, ['para' => false]);
$annotation->type = $fromform->type;
if ($moduleinstance->overwriteannotations) {
$annotation->userid = $USER->id;
}
$DB->update_record('margic_annotations', $annotation);
// Trigger module annotation updated event.
$event = \mod_margic\event\annotation_updated::create(['objectid' => $fromform->annotationid, 'context' => $context]);
$event->trigger();
$urlparams = ['id' => $id, 'annotationmode' => 1, 'focusannotation' => $fromform->annotationid];
$redirecturl = new moodle_url('/mod/margic/view.php', $urlparams);
redirect($redirecturl, get_string('annotationedited', 'mod_margic'), null, notification::NOTIFY_SUCCESS);
} else if ((!isset($fromform->annotationid) || $fromform->annotationid === 0) && isset($fromform->text)) { // New annotation.
if ($fromform->startcontainer != -1 && $fromform->endcontainer != -1 &&
$fromform->startoffset != -1 && $fromform->endoffset != -1) {
if (!isset($fromform->type)) {
redirect($redirecturl, get_string('errtypedeleted', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
if (preg_match("/[^a-zA-Z0-9()-\/[\]]/", $fromform->startcontainer)
|| preg_match("/[^a-zA-Z0-9()-\/[\]]/", $fromform->endcontainer)) {
redirect($redirecturl, get_string('annotationinvalid', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
if (!$DB->record_exists('margic_entries', ['margic' => $cm->instance, 'id' => $fromform->entry])) {
redirect($redirecturl, get_string('annotationinvalid', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
$annotation = new stdClass();
$annotation->margic = (int) $cm->instance;
$annotation->entry = (int) $fromform->entry;
$annotation->userid = $USER->id;
$annotation->timecreated = time();
$annotation->timemodified = 0;
$annotation->type = $fromform->type;
$annotation->startcontainer = $fromform->startcontainer;
$annotation->endcontainer = $fromform->endcontainer;
$annotation->startoffset = $fromform->startoffset;
$annotation->endoffset = $fromform->endoffset;
$annotation->annotationstart = $fromform->annotationstart;
$annotation->annotationend = $fromform->annotationend;
$annotation->exact = $fromform->exact;
$annotation->prefix = $fromform->prefix;
$annotation->suffix = $fromform->suffix;
$annotation->text = $fromform->text;
$newid = $DB->insert_record('margic_annotations', $annotation);
// Trigger module annotation created event.
$event = \mod_margic\event\annotation_created::create(['objectid' => $newid, 'context' => $context]);
$event->trigger();
$urlparams = ['id' => $id, 'annotationmode' => 1, 'focusannotation' => $newid];
$redirecturl = new moodle_url('/mod/margic/view.php', $urlparams);
redirect($redirecturl, get_string('annotationadded', 'mod_margic'), null, notification::NOTIFY_SUCCESS);
} else {
redirect($redirecturl, get_string('annotationinvalid', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
}
} else {
redirect($redirecturl, get_string('annotationinvalid', 'mod_margic'), null, notification::NOTIFY_ERROR);
}