1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Prints an instance of mod_stickynotes.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_stickynotes
|
|
|
21 |
* @copyright 2021 Olivier VALENTIN
|
|
|
22 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require(__DIR__.'/../../config.php');
|
|
|
26 |
require_once(__DIR__.'/lib.php');
|
|
|
27 |
require_once(__DIR__.'/../../lib/outputcomponents.php');
|
|
|
28 |
$PAGE->requires->js('/mod/stickynotes/assets/js_select.js');
|
|
|
29 |
global $DB, $USER;
|
|
|
30 |
|
|
|
31 |
// Declare optional parameters.
|
|
|
32 |
$edit = optional_param('edit', 0, PARAM_INT);
|
|
|
33 |
$create = optional_param('create', 0, PARAM_INT);
|
|
|
34 |
$delete = optional_param('delete', 0, PARAM_INT);
|
|
|
35 |
$note = optional_param('note', 0, PARAM_INT);
|
|
|
36 |
$col = optional_param('col', 0, PARAM_INT);
|
|
|
37 |
$confirm = optional_param('confirm', 0, PARAM_INT);
|
|
|
38 |
$ordernote = optional_param('ordernote', 0, PARAM_INT);
|
|
|
39 |
|
|
|
40 |
// These params will be passed as hidden variables later in the form.
|
|
|
41 |
$pageparams = array('edit' => $edit, 'create' => $create);
|
|
|
42 |
|
|
|
43 |
// Course module id.
|
|
|
44 |
$id = optional_param('id', 0, PARAM_INT);
|
|
|
45 |
|
|
|
46 |
// Activity instance id.
|
|
|
47 |
$s = optional_param('s', 0, PARAM_INT);
|
|
|
48 |
|
|
|
49 |
// Get the system context instance.
|
|
|
50 |
$systemcontext = context_system::instance();
|
|
|
51 |
|
|
|
52 |
if ($id) {
|
|
|
53 |
$cm = get_coursemodule_from_id('stickynotes', $id, 0, false, MUST_EXIST);
|
|
|
54 |
$moduleinstance = $DB->get_record('stickynotes', array('id' => $cm->instance), '*', MUST_EXIST);
|
|
|
55 |
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
$PAGE->set_url('/mod/stickynnotes/note.php', array(
|
|
|
59 |
'edit' => $edit,
|
|
|
60 |
'create' => $create,
|
|
|
61 |
'delete' => $delete,
|
|
|
62 |
));
|
|
|
63 |
|
|
|
64 |
require_login(0, false);
|
|
|
65 |
|
|
|
66 |
if (!empty($create)) {
|
|
|
67 |
// Case 1 : user creates a note
|
|
|
68 |
// Retrieve the related coursemodule.
|
|
|
69 |
if (!$cm = get_coursemodule_from_instance('stickynotes', $cm->instance)) {
|
|
|
70 |
throw new moodle_exception('invalidcoursemodule');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Check if the instance is part of a course.
|
|
|
74 |
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
|
|
75 |
throw new moodle_exception('invalidcourseid');
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Check if notes creation is locked.
|
|
|
79 |
if ($moduleinstance->locknotes == 1) {
|
|
|
80 |
throw new moodle_exception('activelocknotes', 'stickynotes');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Require a login and retrieve the modulecontext.
|
|
|
84 |
require_login($course, false, $cm);
|
|
|
85 |
$modulecontext = context_module::instance($cm->id);
|
|
|
86 |
|
|
|
87 |
// Check capability.
|
|
|
88 |
if (!has_capability('mod/stickynotes:createnote', $modulecontext)) {
|
|
|
89 |
throw new moodle_exception('cannotcreatenote', 'stickynotes');
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
$post = new stdClass();
|
|
|
93 |
$post->message = '';
|
|
|
94 |
$post->create = 1;
|
|
|
95 |
$post->choose_color = $moduleinstance->colors;
|
|
|
96 |
$post->stickyid = $cm->instance;
|
|
|
97 |
$post->stickycolid = $col;
|
|
|
98 |
|
|
|
99 |
// Define the page title for creating form.
|
|
|
100 |
$settitle = get_column_title($col);
|
|
|
101 |
$pagetitle = (get_string('createnote_title', 'stickynotes')).'"'.$settitle['title'].'"';
|
|
|
102 |
|
|
|
103 |
} else if ($edit) {
|
|
|
104 |
// Case 2 : user edits a note
|
|
|
105 |
// Retrieve the related coursemodule.
|
|
|
106 |
if (!$cm = get_coursemodule_from_instance('stickynotes', $cm->instance)) {
|
|
|
107 |
throw new moodle_exception('invalidcoursemodule');
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
// Check if the instance is part of a course.
|
|
|
111 |
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
|
|
112 |
throw new moodle_exception('invalidcourseid');
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// Check if notes creation is locked.
|
|
|
116 |
if ($moduleinstance->locknotes == 1) {
|
|
|
117 |
throw new moodle_exception('activelocknotes', 'stickynotes');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
// Check if note exists.
|
|
|
121 |
if (!$post = $DB->get_record('stickynotes_note', array('id' => $note))) {
|
|
|
122 |
throw new moodle_exception('cannotgetnote', 'stickynotes');
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
// Require a login and retrieve the modulecontext.
|
|
|
126 |
require_login($course, false, $cm);
|
|
|
127 |
$modulecontext = context_module::instance($cm->id);
|
|
|
128 |
|
|
|
129 |
// Check some capabilities.
|
|
|
130 |
$updateownnote = has_capability('mod/stickynotes:updateownnote', $modulecontext);
|
|
|
131 |
$updateanynote = has_capability('mod/stickynotes:updateanynote', $modulecontext);
|
|
|
132 |
|
|
|
133 |
if (!(($post->userid == $USER->id AND $updateownnote) OR $updateanynote)) {
|
|
|
134 |
throw new moodle_exception('cannotupdatenote', 'stickynotes');
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// Defines variables.
|
|
|
138 |
$post->edit = $edit;
|
|
|
139 |
$post->course = $course->id;
|
|
|
140 |
$post->message = $post->message;
|
|
|
141 |
$post->choose_color = $moduleinstance->colors;
|
|
|
142 |
$post->oldrank = $post->ordernote;
|
|
|
143 |
$post->oldcolumn = $post->stickycolid;
|
|
|
144 |
|
|
|
145 |
// Define the page title for creating form.
|
|
|
146 |
$pagetitle = (get_string('updatenote_title', 'stickynotes'));
|
|
|
147 |
|
|
|
148 |
} else if ($delete) {
|
|
|
149 |
// Case 3 : user deletes a note.
|
|
|
150 |
// Retrieve the related coursemodule.
|
|
|
151 |
if (!$cm = get_coursemodule_from_instance('stickynotes', $cm->instance)) {
|
|
|
152 |
throw new moodle_exception('invalidcoursemodule');
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// Check if the instance is part of a course.
|
|
|
156 |
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
|
|
157 |
throw new moodle_exception('invalidcourseid');
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
// Check if notes creation is locked.
|
|
|
161 |
if ($moduleinstance->locknotes == 1) {
|
|
|
162 |
throw new moodle_exception('activelocknotes', 'stickynotes');
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// Check if note exists.
|
|
|
166 |
if (!$post = $DB->get_record('stickynotes_note', array('id' => $note))) {
|
|
|
167 |
throw new moodle_exception('cantgetnote', 'stickynotes');
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// Require a login and retrieve the modulecontext.
|
|
|
171 |
require_login($course, false, $cm);
|
|
|
172 |
$modulecontext = context_module::instance($cm->id);
|
|
|
173 |
|
|
|
174 |
// Check some capabilities.
|
|
|
175 |
$deleteownnote = has_capability('mod/stickynotes:deleteownnote', $modulecontext);
|
|
|
176 |
$deleteanynote = has_capability('mod/stickynotes:deleteanynote', $modulecontext);
|
|
|
177 |
|
|
|
178 |
if (!(($post->userid == $USER->id AND $deleteownnote) OR $deleteanynote)) {
|
|
|
179 |
throw new moodle_exception('cannotdeletenote', 'stickynotes');
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
// User has confirmed deletion : note is deleted.
|
|
|
183 |
if (!empty($confirm) AND confirm_sesskey()) {
|
|
|
184 |
delete_stickynote($note, $modulecontext, $moduleinstance, $course, $cm, $post->userid);
|
|
|
185 |
|
|
|
186 |
// Trigger note deleted event.
|
|
|
187 |
$params = array(
|
|
|
188 |
'context' => $modulecontext,
|
|
|
189 |
'objectid' => $note
|
|
|
190 |
);
|
|
|
191 |
$event = \mod_stickynotes\event\note_deleted::create($params);
|
|
|
192 |
$event->trigger();
|
|
|
193 |
|
|
|
194 |
$returnurl = "view.php?id=".$cm->id;
|
|
|
195 |
redirect($returnurl);
|
|
|
196 |
} else {
|
|
|
197 |
// Shows form to confirm before delete.
|
|
|
198 |
$modulecontext = context_module::instance($cm->id);
|
|
|
199 |
$coursecontext = context_course::instance($course->id);
|
|
|
200 |
$PAGE->navbar->add(get_string('deletenote', 'stickynotes'));
|
|
|
201 |
$PAGE->set_title($course->shortname);
|
|
|
202 |
$PAGE->set_heading($course->fullname);
|
|
|
203 |
|
|
|
204 |
echo $OUTPUT->header();
|
|
|
205 |
echo $OUTPUT->heading(format_string($cm->name), 2);
|
|
|
206 |
echo $OUTPUT->confirm(get_string("deletenotesure", "stickynotes"),
|
|
|
207 |
"note.php?delete=$delete&confirm=$delete&id=".$cm->id."¬e=".$note,
|
|
|
208 |
$CFG->wwwroot . '/mod/stickynotes/view.php?id=' . $cm->id);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
echo $OUTPUT->footer();
|
|
|
212 |
exit;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
// If no action is triggered, set up the form.
|
|
|
216 |
|
|
|
217 |
require_login($course, false, $cm);
|
|
|
218 |
|
|
|
219 |
$modulecontext = context_module::instance($cm->id);
|
|
|
220 |
$coursecontext = context_course::instance($course->id);
|
|
|
221 |
|
|
|
222 |
// Get the original note.
|
|
|
223 |
$postid = empty($post->id) ? null : $post->id;
|
|
|
224 |
$postmessage = empty($post->message) ? null : $post->message;
|
|
|
225 |
if (!empty($edit)) {
|
|
|
226 |
$postcol = empty($post->stickycolid) ? null : $post->stickycolid;
|
|
|
227 |
} else {
|
|
|
228 |
$postcol = $col;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
$formarray = array(
|
|
|
232 |
'id' => $cm->id,
|
|
|
233 |
'course' => $course,
|
|
|
234 |
'cm' => $cm,
|
|
|
235 |
'modulecontext' => $modulecontext,
|
|
|
236 |
'edit' => $edit,
|
|
|
237 |
'create' => $create,
|
|
|
238 |
'post' => $post,
|
|
|
239 |
'note' => $postid,
|
|
|
240 |
'message' => $postmessage,
|
|
|
241 |
'stickycolid' => $postcol,
|
|
|
242 |
'stickyid' => $cm->instance,
|
|
|
243 |
'ordernote' => $ordernote,
|
|
|
244 |
);
|
|
|
245 |
|
|
|
246 |
$mformnote = new form_note('note.php', $formarray, 'post');
|
|
|
247 |
|
|
|
248 |
$mformnote->set_data(array(
|
|
|
249 |
'stickycolid' => $postcol,
|
|
|
250 |
'id' => $id,
|
|
|
251 |
) + $pageparams + $formarray);
|
|
|
252 |
|
|
|
253 |
// If form is cancelled, redirect activity view page.
|
|
|
254 |
if ($mformnote->is_cancelled()) {
|
|
|
255 |
$returnurl = "view.php?id=".$id;
|
|
|
256 |
redirect($returnurl);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
// If form is submitted.
|
|
|
260 |
if ($fromform = $mformnote->get_data()) {
|
|
|
261 |
|
|
|
262 |
// Redirect url in case of errors.
|
|
|
263 |
if (empty($SESSION->fromurl)) {
|
|
|
264 |
$errordestination = "$CFG->wwwroot/mod/stickynotes/view.php?id=$cm->id";
|
|
|
265 |
} else {
|
|
|
266 |
$errordestination = $SESSION->fromurl;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
// If user updates a note.
|
|
|
270 |
if ($fromform->edit) {
|
|
|
271 |
$fromform->userid = $USER->id;
|
|
|
272 |
$fromform->instance = $fromform->id;
|
|
|
273 |
$fromform->id = $fromform->note;
|
|
|
274 |
|
|
|
275 |
if ($fromform->nomove == 0) {
|
|
|
276 |
$fromform->ordernote = $fromform->oldrank;
|
|
|
277 |
$fromform->stickycolid = $fromform->oldcolumn;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
$returnurl = "view.php?id=".$fromform->instance;
|
|
|
281 |
$updatenote = update_stickynote($fromform);
|
|
|
282 |
|
|
|
283 |
// Trigger note updated event.
|
|
|
284 |
$params = array(
|
|
|
285 |
'context' => $modulecontext,
|
|
|
286 |
'objectid' => $fromform->id
|
|
|
287 |
);
|
|
|
288 |
$event = \mod_stickynotes\event\note_updated::create($params);
|
|
|
289 |
$event->trigger();
|
|
|
290 |
|
|
|
291 |
redirect($returnurl);
|
|
|
292 |
exit();
|
|
|
293 |
} else if ($fromform->create) {
|
|
|
294 |
// If user creates a new note.
|
|
|
295 |
$fromform->userid = $USER->id;
|
|
|
296 |
$returnurl = "view.php?id=".$fromform->id;
|
|
|
297 |
$fromform->ordernote = $ordernote;
|
|
|
298 |
|
|
|
299 |
// Finally, we can create note.
|
|
|
300 |
$createnote = insert_stickynote($fromform, $moduleinstance, $course, $cm);
|
|
|
301 |
|
|
|
302 |
// Trigger note created event.
|
|
|
303 |
$params = array(
|
|
|
304 |
'context' => $modulecontext,
|
|
|
305 |
'objectid' => $createnote
|
|
|
306 |
);
|
|
|
307 |
$event = \mod_stickynotes\event\note_created::create($params);
|
|
|
308 |
$event->trigger();
|
|
|
309 |
|
|
|
310 |
redirect($returnurl);
|
|
|
311 |
exit();
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
// Initiate the page.
|
|
|
316 |
$PAGE->set_title(format_string($cm->name));
|
|
|
317 |
$PAGE->set_heading(format_string($course->fullname));
|
|
|
318 |
|
|
|
319 |
// Display header.
|
|
|
320 |
echo $OUTPUT->header();
|
|
|
321 |
echo $OUTPUT->heading($pagetitle);
|
|
|
322 |
|
|
|
323 |
$mformnote->display();
|
|
|
324 |
|
|
|
325 |
echo $OUTPUT->footer();
|