-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpost.control.php
416 lines (349 loc) · 18.6 KB
/
post.control.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?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/>.
/**
* Class that is important to interact with posts.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_moodleoverflow\post;
// Import namespace from the locallib, needs a check later which namespaces are really needed.
use mod_moodleoverflow\anonymous;
use mod_moodleoverflow\capabilities;
use mod_moodleoverflow\review;
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__) . '/lib.php');
/**
* Class that makes checks to interact with posts.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class post_control {
/** @var string the Interaction type */
private $interaction;
/** @var object information about the post like the related moodleoverflow, post etc. .*/
private $information;
/** @var object prepost for the classes/post/post_form.php */
private $prepost;
/**
* Constructor
*
* @param object $urlparameter Parameter that were sent when post.php where opened.
*/
public function __construct($urlparameter) {
$this->information = new \stdClass;
$this->detect_interaction($urlparameter); // Detects interaction and makes security checks.
}
/**
* Returns the interaction type.
*/
public function get_interaction() {
return $this->interaction;
}
/**
* Returns the gathered important information in the build_prepost_() functions.
*/
public function get_information() {
return $this->information;
}
/**
* Retuns the prepared post.
*/
public function get_prepost() {
return $this->prepost;
}
/**
* Detects the interaction
* @param object $urlparamter parameter from the post.php
*/
private function detect_interaction($urlparameter) {
$count = 0;
$count += $urlparameter->create ? 1 : 0;
$count += $urlparameter->reply ? 1 : 0;
$count += $urlparameter->edit ? 1 : 0;
$count += $urlparameter->delete ? 1 : 0;
if ($count !== 1) {
throw new coding_exception('Exactly one parameter should be specified!');
}
if ($urlparameter->create) {
$this->interaction = 'create';
$this->information->moodleoverflowid = $urlparameter->create;
$this->build_prepost_create($this->information->moodleoverflowid);
} else if ($urlparameter->edit) {
$this->interaction = 'edit';
$this->information->editpostid = $urlparameter->edit;
$this->build_prepost_edit($this->information->editpostid);
} else if ($urlparameter->reply) {
$this->interaction = 'reply';
$this->information->replypostid = $urlparameter->edit;
$this->build_prepost_reply($this->information->replypostid);
} else if ($urlparameter->delete) {
$this->interaction = 'delete';
$this->information->deletepostid = $urlparameter->edit;
$this->build_prepost_delete($this->information->deletepostid);
} else {
throw new moodle_exception('unknownaction');
}
}
// Private functions.
// Build_prepost functions: makes important checks and saves all important information in $prepost object.
/**
* Function to prepare a new discussion in moodleoverflow.
*
* @param int $moodleoverflowid The ID of the moodleoverflow where the new discussion post is being created.
*/
private function build_prepost_create($moodleoverflowid) {
global $DB, $SESSION, $USER;
// Check the moodleoverflow instance is valid.
if (!$this->information->moodleoverflow = $DB->get_record('moodleoverflow', array('id' => $moodleoverflowid))) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// Get the related course.
if (!$this->information->course = $DB->get_record('course', array('id' => $this->information->moodleoverflow->course))) {
throw new moodle_exception('invalidcourseid');
}
// Get the related coursemodule.
if (!$this->information->cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid,
$this->information->course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Retrieve the contexts.
$this->information->modulecontext = context_module::instance($this->information->cm->id);
$this->information->coursecontext = context_module::instance($this->information->course->id);
// Check if the user can start a new discussion.
if (!moodleoverflow_user_can_post_discussion($this->information->moodleoverflow,
$this->information->cm,
$this->information->modulecontext)) {
// Catch unenrolled user.
if (!isguestuser() && !is_enrolled($this->information->coursecontext)) {
if (enrol_selfenrol_available($this->information->course->id)) {
$SESSION->wantsurl = qualified_me();
$SESSION->enrolcancel = get_local_referer(false);
redirect(new moodle_url('/enrol/index.php',
array('id' => $this->information->course->id,
'returnurl' => '/mod/moodleoverflow/view.php?m=' .
$this->information->moodleoverflow->id)),
get_string('youneedtoenrol'));
}
}
// Notify the user, that he can not post a new discussion.
throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
}
// Where is the user coming from?
$SESSION->fromurl = get_local_referer(false);
// Prepare the post.
$this->prepost = new stdClass();
$this->prepost->courseid = $this->information->course->id;
$this->prepost->moodleoverflowid = $this->information->moodleoverflow->id;
$this->prepost->discussionid = 0;
$this->prepost->parentid = 0;
$this->prepost->subject = '';
$this->prepost->userid = $USER->id;
$this->prepost->message = '';
// Unset where the user is coming from.
// Allows to calculate the correct return url later.
unset($SESSION->fromdiscussion);
}
/**
* Function to prepare a new post that replies to an existing post.
*
* @param int $replypostid The ID of the post that is being answered.
*/
private function build_prepost_reply($replypostid) {
global $DB, $PAGE, $SESSION, $USER;
// Check if the related post exists.
if (!$this->information->parent = moodleoverflow_get_post_full($replypostid)) {
throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
}
// Check if the post is part of a valid discussion.
if (!$this->information->discussion = $DB->get_record('moodleoverflow_discussions',
array('id' => $this->information->parent->discussion))) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Check if the post is related to a valid moodleoverflow instance.
if (!$this->information->moodleoverflow = $DB->get_record('moodleoverflow',
array('id' => $this->information->discussion->moodleoverflow))) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// Check if the moodleoverflow instance is part of a course.
if (!$this->information->course = $DB->get_record('course', array('id' => $this->information->discussion->course))) {
throw new moodle_exception('invalidcourseid');
}
// Retrieve the related coursemodule.
if (!$this->information->cm = get_coursemodule_from_instance('moodleoverflow',
$this->information->moodleoverflow->id,
$this->information->course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Ensure the coursemodule is set correctly.
$PAGE->set_cm($this->information->cm, $this->information->course, $this->information->moodleoverflow);
// Retrieve the contexts.
$this->information->modulecontext = context_module::instance($this->information->cm->id);
$this->information->coursecontext = context_module::instance($this->information->course->id);
// Check whether the user is allowed to post.
if (!moodleoverflow_user_can_post($this->information->modulecontext, $this->information->parent)) {
// Give the user the chance to enroll himself to the course.
if (!isguestuser() && !is_enrolled($this->information->coursecontext)) {
$SESSION->wantsurl = qualified_me();
$SESSION->enrolcancel = get_local_referer(false);
redirect(new moodle_url('/enrol/index.php',
array('id' => $this->information->course->id,
'returnurl' => '/mod/moodleoverflow/view.php?m=' .
$this->information->moodleoverflow->id)),
get_string('youneedtoenrol'));
}
// Print the error message.
throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
}
// Make sure the user can post here.
if (!$this->information->cm->visible &&
!has_capability('moodle/course:viewhiddenactivities', $this->information->modulecontext)) {
throw new moodle_exception('activityiscurrentlyhidden');
}
// Prepare a post.
$this->prepost = new stdClass();
$this->prepost->courseid = $this->information->course->id;
$this->prepost->moodleoverflowid = $this->information->moodleoverflow->id;
$this->prepost->discussionid = $this->information->discussion->id;
$this->prepost->parentid = $this->information->parent->id;
$this->prepost->subject = $this->information->discussion->name;
$this->prepost->userid = $USER->id;
$this->prepost->message = '';
// Append 'RE: ' to the discussions subject.
$strre = get_string('re', 'moodleoverflow');
if (!(substr($this->prepost->subject, 0, strlen($strre)) == $strre)) {
$this->prepost->subject = $strre . ' ' . $this->prepost->subject;
}
// Unset where the user is coming from.
// Allows to calculate the correct return url later.
unset($SESSION->fromdiscussion);
}
/**
* Function to prepare the edit of an existing post.
*
* @param int $editpostid The ID of the post that is being edited.
*/
private function build_prepost_edit($editpostid) {
global $DB, $PAGE, $SESSION, $USER;
// Third possibility: The user is editing his own post.
// Check if the submitted post exists.
if (!$this->information->relatedpost = moodleoverflow_get_post_full($editpostid)) {
throw new moodle_exception('invalidpostid', 'moodleoverflow');
}
// Get the parent post of this post if it is not the starting post of the discussion.
if ($this->information->relatedpost->parent) {
if (!$this->information->parent = moodleoverflow_get_post_full($this->information->relatedpost->parent)) {
throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
}
}
// Check if the post refers to a valid discussion.
if (!$this->information->discussion = $DB->get_record('moodleoverflow_discussions',
array('id' => $this->information->relatedpost->discussion))) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Check if the post refers to a valid moodleoverflow instance.
if (!$this->information->moodleoverflow = $DB->get_record('moodleoverflow',
array('id' => $this->information->discussion->moodleoverflow))) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// Check if the post refers to a valid course.
if (!$this->information->course = $DB->get_record('course', array('id' => $this->information->discussion->course))) {
throw new moodle_exception('invalidcourseid');
}
// Retrieve the related coursemodule.
if (!$this->information->cm = get_coursemodule_from_instance('moodleoverflow',
$this->information->moodleoverflow->id,
$this->information->course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Retrieve contexts.
$this->information->modulecontext = context_module::instance($this->information->cm->id);
// Set the pages context.
$PAGE->set_cm($this->information->cm, $this->information->course, $this->information->moodleoverflow);
// Check if the post can be edited.
$beyondtime = ((time() - $this->information->relatedpost->created) > get_config('moodleoverflow', 'maxeditingtime'));
$alreadyreviewed = review::should_post_be_reviewed($this->information->relatedpost, $this->information->moodleoverflow)
&& $this->information->relatedpost->reviewed;
if (($beyondtime || $alreadyreviewed) && !has_capability('mod/moodleoverflow:editanypost',
$this->information->modulecontext)) {
throw new moodle_exception('maxtimehaspassed', 'moodleoverflow', '',
format_time(get_config('moodleoverflow', 'maxeditingtime')));
}
// If the current user is not the one who posted this post.
if ($this->information->relatedpost->userid <> $USER->id) {
// Check if the current user has not the capability to edit any post.
if (!has_capability('mod/moodleoverflow:editanypost', $this->information->modulecontext)) {
// Display the error. Capabilities are missing.
throw new moodle_exception('cannoteditposts', 'moodleoverflow');
}
}
// Load the $post variable.
$this->prepost = $this->information->relatedpost;
$this->prepost->editid = $editpostid;
$this->prepost->course = $this->information->course->id;
$this->prepost->moodleoverflow = $this->information->moodleoverflow->id;
// Unset where the user is coming from.
// Allows to calculate the correct return url later.
unset($SESSION->fromdiscussion);
}
/**
* Function to prepare the deletion of a post.
*
* @param int $deletepostid The ID of the post that is being deleted.
*/
private function build_prepost_delete($deletepostid) {
global $DB, $USER;
// Check if the post is existing.
if (!$this->information->relatedpost = moodleoverflow_get_post_full($deletepostid)) {
throw new moodle_exception('invalidpostid', 'moodleoverflow');
}
// Get the related discussion.
if (!$this->information->discussion = $DB->get_record('moodleoverflow_discussions',
array('id' => $this->information->relatedpost->discussion))) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Get the related moodleoverflow instance.
if (!$this->information->moodleoverflow = $DB->get_record('moodleoverflow',
array('id' => $this->information->discussion->moodleoverflow))) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoveflow');
}
// Get the related coursemodule.
if (!$this->information->cm = get_coursemodule_from_instance('moodleoverflow',
$this->information->moodleoverflow->id,
$this->information->moodleoverflow->course)) {
throw new moodle_exception('invalidcoursemodule');
}
// Get the related course.
if (!$this->information->course = $DB->get_record('course', array('id' => $this->information->moodleoverflow->course))) {
throw new moodle_exception('invalidcourseid');
}
// Require a login and retrieve the modulecontext.
require_login($this->information->course, false, $this->information->cm);
$this->information->modulecontext = context_module::instance($this->information->cm->id);
// Check some capabilities.
$this->information->deleteownpost = has_capability('mod/moodleoverflow:deleteownpost', $this->information->modulecontext);
$this->information->deleteanypost = has_capability('mod/moodleoverflow:deleteanypost', $this->information->modulecontext);
if (!(($this->information->relatedpost->userid == $USER->id && $this->information->deleteownpost)
|| $this->information->deleteanypost)) {
throw new moodle_exception('cannotdeletepost', 'moodleoverflow');
}
// Count all replies of this post.
$this->information->replycount = moodleoverflow_count_replies($this->information->relatedpost, false);
}
}