-
Notifications
You must be signed in to change notification settings - Fork 23
/
post.php
801 lines (649 loc) · 30.6 KB
/
post.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
<?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 file to manage posts.
*
* @package mod_moodleoverflow
* @copyright 2017 Kennet Winter <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Include config and locallib.
use mod_moodleoverflow\anonymous;
use mod_moodleoverflow\review;
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
global $CFG, $USER, $DB, $PAGE, $SESSION, $OUTPUT;
require_once(dirname(__FILE__) . '/locallib.php');
require_once($CFG->libdir . '/completionlib.php');
// Declare optional parameters.
$moodleoverflow = optional_param('moodleoverflow', 0, PARAM_INT);
$reply = optional_param('reply', 0, PARAM_INT);
$edit = optional_param('edit', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_INT);
$count = 0;
$count += $moodleoverflow ? 1 : 0;
$count += $reply ? 1 : 0;
$count += $edit ? 1 : 0;
$count += $delete ? 1 : 0;
if ($count !== 1) {
throw new coding_exception('Exactly one parameter should be specified!');
}
// Set the URL that should be used to return to this page.
$PAGE->set_url('/mod/moodleoverflow/post.php', [
'moodleoverflow' => $moodleoverflow,
'reply' => $reply,
'edit' => $edit,
'delete' => $delete,
'confirm' => $confirm,
]);
// These params will be passed as hidden variables later in the form.
$pageparams = ['moodleoverflow' => $moodleoverflow, 'reply' => $reply, 'edit' => $edit];
// Get the system context instance.
$systemcontext = context_system::instance();
// Catch guests.
if (!isloggedin() || isguestuser()) {
// The user is starting a new discussion in a moodleoverflow instance.
if (!empty($moodleoverflow)) {
// Check the moodleoverflow instance is valid.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// The user is replying to an existing moodleoverflow discussion.
} else if (!empty($reply)) {
// Check if the related post exists.
if (!$parent = moodleoverflow_get_post_full($reply)) {
throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
}
// Check if the post is part of a valid discussion.
if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $parent->discussion])) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Check if the post is related to a valid moodleoverflow instance.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
}
// Get the related course.
if (!$course = $DB->get_record('course', ['id' => $moodleoverflow->course])) {
throw new moodle_exception('invalidcourseid');
}
// Get the related coursemodule and its context.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Get the context of the module.
$modulecontext = context_module::instance($cm->id);
// Set parameters for the page.
$PAGE->set_cm($cm, $course, $moodleoverflow);
$PAGE->set_context($modulecontext);
$PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
// The page should not be large, only pages containing broad tables are usually.
$PAGE->add_body_class('limitedwidth');
// The guest needs to login.
echo $OUTPUT->header();
$strlogin = get_string('noguestpost', 'forum') . '<br /><br />' . get_string('liketologin');
echo $OUTPUT->confirm($strlogin, get_login_url(), $CFG->wwwroot . '/mod/moodleoverflow/view.php?m=' . $moodleoverflow->id);
echo $OUTPUT->footer();
exit;
}
// First step: A general login is needed to post something.
require_login(0, false);
// First possibility: User is starting a new discussion in a moodleoverflow instance.
if (!empty($moodleoverflow)) {
// Check the moodleoverflow instance is valid.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// Get the related course.
if (!$course = $DB->get_record('course', ['id' => $moodleoverflow->course])) {
throw new moodle_exception('invalidcourseid');
}
// Get the related coursemodule.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Retrieve the contexts.
$modulecontext = context_module::instance($cm->id);
$coursecontext = context_course::instance($course->id);
// Check if the user can start a new discussion.
if (!moodleoverflow_user_can_post_discussion($moodleoverflow, $cm, $modulecontext)) {
// Catch unenrolled user.
if (!isguestuser() && !is_enrolled($coursecontext)) {
if (enrol_selfenrol_available($course->id)) {
$SESSION->wantsurl = qualified_me();
$SESSION->enrolcancel = get_local_referer(false);
redirect(new moodle_url('/enrol/index.php', [
'id' => $course->id,
'returnurl' => '/mod/moodleoverflow/view.php?m=' . $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);
// Load all the $post variables.
$post = new stdClass();
$post->course = $course->id;
$post->moodleoverflow = $moodleoverflow->id;
$post->discussion = 0;
$post->parent = 0;
$post->subject = '';
$post->userid = $USER->id;
$post->message = '';
// Unset where the user is coming from.
// Allows to calculate the correct return url later.
unset($SESSION->fromdiscussion);
} else if (!empty($reply)) {
// Second possibility: The user is writing a new reply.
// Check if the post exists.
if (!$parent = moodleoverflow_get_post_full($reply)) {
throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
}
// Check if the post is part of a discussion.
if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $parent->discussion])) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Check if the discussion is part of a moodleoverflow instance.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// Check if the moodleoverflow instance is part of a course.
if (!$course = $DB->get_record('course', ['id' => $discussion->course])) {
throw new moodle_exception('invalidcourseid');
}
// Retrieve the related coursemodule.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
}
// Ensure the coursemodule is set correctly.
$PAGE->set_cm($cm, $course, $moodleoverflow);
// Retrieve the other contexts.
$modulecontext = context_module::instance($cm->id);
$coursecontext = context_course::instance($course->id);
// Check whether the user is allowed to post.
if (!moodleoverflow_user_can_post($modulecontext, $parent)) {
// Give the user the chance to enroll himself to the course.
if (!isguestuser() && !is_enrolled($coursecontext)) {
$SESSION->wantsurl = qualified_me();
$SESSION->enrolcancel = get_local_referer(false);
redirect(new moodle_url('/enrol/index.php',
['id' => $course->id, 'returnurl' => '/mod/moodleoverflow/view.php?m=' . $moodleoverflow->id]),
get_string('youneedtoenrol'));
}
// Print the error message.
throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
}
// Make sure the user can post here.
if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $modulecontext)) {
throw new moodle_exception('activityiscurrentlyhidden');
}
// Load the $post variable.
$post = new stdClass();
$post->course = $course->id;
$post->moodleoverflow = $moodleoverflow->id;
$post->discussion = $parent->discussion;
$post->parent = $parent->id;
$post->subject = $discussion->name;
$post->userid = $USER->id;
$post->message = '';
// Append 'RE: ' to the discussions subject.
$strre = get_string('re', 'moodleoverflow');
if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
$post->subject = $strre . ' ' . $post->subject;
}
// Unset where the user is coming from.
// Allows to calculate the correct return url later.
unset($SESSION->fromdiscussion);
} else if (!empty($edit)) {
// Third possibility: The user is editing his own post.
// Check if the submitted post exists.
if (!$post = moodleoverflow_get_post_full($edit)) {
throw new moodle_exception('invalidpostid', 'moodleoverflow');
}
// Get the parent post of this post if it is not the starting post of the discussion.
if ($post->parent) {
if (!$parent = moodleoverflow_get_post_full($post->parent)) {
throw new moodle_exception('invalidparentpostid', 'moodleoverflow');
}
}
// Check if the post refers to a valid discussion.
if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion])) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Check if the post refers to a valid moodleoverflow instance.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
// Check if the post refers to a valid course.
if (!$course = $DB->get_record('course', ['id' => $discussion->course])) {
throw new moodle_exception('invalidcourseid');
}
// Retrieve the related coursemodule.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id)) {
throw new moodle_exception('invalidcoursemodule');
} else {
$modulecontext = context_module::instance($cm->id);
}
// Set the pages context.
$PAGE->set_cm($cm, $course, $moodleoverflow);
// Check if the post can be edited.
$beyondtime = ((time() - $post->created) > get_config('moodleoverflow', 'maxeditingtime'));
$alreadyreviewed = review::should_post_be_reviewed($post, $moodleoverflow) && $post->reviewed;
if (($beyondtime || $alreadyreviewed) && !has_capability('mod/moodleoverflow:editanypost', $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 ($post->userid <> $USER->id) {
// Check if the current user has not the capability to edit any post.
if (!has_capability('mod/moodleoverflow:editanypost', $modulecontext)) {
// Display the error. Capabilities are missing.
throw new moodle_exception('cannoteditposts', 'moodleoverflow');
}
}
// Load the $post variable.
$post->edit = $edit;
$post->course = $course->id;
$post->moodleoverflow = $moodleoverflow->id;
// Unset where the user is coming from.
// Allows to calculate the correct return url later.
unset($SESSION->fromdiscussion);
} else if (!empty($delete)) {
// Fourth possibility: The user is deleting a post.
// Check if the post is existing.
if (!$post = moodleoverflow_get_post_full($delete)) {
throw new moodle_exception('invalidpostid', 'moodleoverflow');
}
// Get the related discussion.
if (!$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion])) {
throw new moodle_exception('notpartofdiscussion', 'moodleoverflow');
}
// Get the related moodleoverflow instance.
if (!$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow])) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoveflow');
}
// Get the related coursemodule.
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $moodleoverflow->course)) {
throw new moodle_exception('invalidcoursemodule');
}
// Get the related course.
if (!$course = $DB->get_record('course', ['id' => $moodleoverflow->course])) {
throw new moodle_exception('invalidcourseid');
}
// Require a login and retrieve the modulecontext.
require_login($course, false, $cm);
$modulecontext = context_module::instance($cm->id);
// Check some capabilities.
$deleteownpost = has_capability('mod/moodleoverflow:deleteownpost', $modulecontext);
$deleteanypost = has_capability('mod/moodleoverflow:deleteanypost', $modulecontext);
if (!(($post->userid == $USER->id && $deleteownpost) || $deleteanypost)) {
throw new moodle_exception('cannotdeletepost', 'moodleoverflow');
}
// Count all replies of this post.
$replycount = moodleoverflow_count_replies($post, false);
// Has the user confirmed the deletion?
if (!empty($confirm) && confirm_sesskey()) {
// Check if the user has the capability to delete the post.
$timepassed = time() - $post->created;
if (($timepassed > get_config('moodleoverflow', 'maxeditingtime')) && !$deleteanypost) {
$url = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $post->discussion]);
throw new moodle_exception('cannotdeletepost', 'moodleoverflow', moodleoverflow_go_back_to($url));
}
// A normal user cannot delete his post if there are direct replies.
if ($replycount && !$deleteanypost) {
$url = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $post->discussion]);
throw new moodle_exception('couldnotdeletereplies', 'moodleoverflow', moodleoverflow_go_back_to($url));
} else {
// Delete the post.
// The post is the starting post of a discussion. Delete the topic as well.
if (!$post->parent) {
moodleoverflow_delete_discussion($discussion, $course, $cm, $moodleoverflow);
// Trigger the discussion deleted event.
$params = [
'objectid' => $discussion->id,
'context' => $modulecontext,
];
$event = \mod_moodleoverflow\event\discussion_deleted::create($params);
$event->trigger();
// Redirect the user back to start page of the moodleoverflow instance.
redirect("view.php?m=$discussion->moodleoverflow");
exit;
} else if (moodleoverflow_delete_post($post, $deleteanypost, $cm, $moodleoverflow)) {
// Delete a single post.
// Redirect back to the discussion.
$discussionurl = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id]);
redirect(moodleoverflow_go_back_to($discussionurl));
exit;
} else {
// Something went wrong.
throw new moodle_exception('errorwhiledelete', 'moodleoverflow');
}
}
} else {
// Deletion needs to be confirmed.
moodleoverflow_set_return();
$PAGE->navbar->add(get_string('delete', 'moodleoverflow'));
$PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
// The page should not be large, only pages containing broad tables are usually.
$PAGE->add_body_class('limitedwidth');
// Check if there are replies for the post.
if ($replycount) {
// Check if the user has capabilities to delete more than one post.
if (!$deleteanypost) {
throw new moodle_exception('couldnotdeletereplies', 'moodleoverflow',
moodleoverflow_go_back_to(new moodle_url('/mod/moodleoverflow/discussion.php',
['d' => $post->discussion, 'p' . $post->id])));
}
// Request a confirmation to delete the post.
echo $OUTPUT->header();
echo $OUTPUT->confirm(get_string("deletesureplural", "moodleoverflow", $replycount + 1),
"post.php?delete=$delete&confirm=$delete", $CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' .
$post->discussion . '#p' . $post->id);
} else {
// Delete a single post.
// Print a confirmation message.
echo $OUTPUT->header();
echo $OUTPUT->confirm(get_string("deletesure", "moodleoverflow", $replycount),
"post.php?delete=$delete&confirm=$delete",
$CFG->wwwroot . '/mod/moodleoverflow/discussion.php?d=' . $post->discussion . '#p' . $post->id);
}
}
echo $OUTPUT->footer();
exit;
} else {
// Last posibility: the action is not known.
throw new moodle_exception('unknownaction');
}
// Second step: The user must be logged on properly. Must be enrolled to the course as well.
require_login($course, false, $cm);
// Get the contexts.
$modulecontext = context_module::instance($cm->id);
$coursecontext = context_course::instance($course->id);
// Get the subject.
if ($edit) {
$subject = $discussion->name;
} else if ($reply) {
$subject = $post->subject;
} else if ($moodleoverflow) {
$subject = $post->subject;
}
// Get attachments.
$postid = empty($post->id) ? null : $post->id;
$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid,
$modulecontext->id,
'mod_moodleoverflow',
'attachment',
$postid,
mod_moodleoverflow_post_form::attachment_options($moodleoverflow));
if ($draftitemid && $edit && anonymous::is_post_anonymous($discussion, $moodleoverflow, $post->userid)
&& $post->userid != $USER->id) {
$usercontext = context_user::instance($USER->id);
$anonymousstr = get_string('anonymous', 'moodleoverflow');
foreach (get_file_storage()->get_area_files($usercontext->id, 'user', 'draft', $draftitemid) as $file) {
$file->set_author($anonymousstr);
}
}
$draftideditor = file_get_submitted_draft_itemid('message');
$currenttext = file_prepare_draft_area($draftideditor, $modulecontext->id, 'mod_moodleoverflow', 'post', $postid,
mod_moodleoverflow_post_form::editor_options($modulecontext, $postid), $post->message);
// Prepare the form.
$formarray = [
'course' => $course,
'cm' => $cm,
'coursecontext' => $coursecontext,
'modulecontext' => $modulecontext,
'moodleoverflow' => $moodleoverflow,
'post' => $post,
'edit' => $edit,
];
$mformpost = new mod_moodleoverflow_post_form('post.php', $formarray, 'post', '', ['id' => 'mformmoodleoverflow']);
// The current user is not the original author.
// Append the message to the end of the message.
if ($USER->id != $post->userid) {
// Create a temporary object.
$data = new stdClass();
$data->date = userdate($post->modified);
$post->messageformat = editors_get_preferred_format();
// Append the message depending on the messages format.
if ($post->messageformat == FORMAT_HTML) {
$data->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id' . $USER->id .
'&course=' . $post->course . '">' . fullname($USER) . '</a>';
$post->message .= '<p><span class="edited">(' . get_string('editedby', 'moodleoverflow', $data) . ')</span></p>';
} else {
$data->name = fullname($USER);
$post->message .= "\n\n(" . get_string('editedby', 'moodleoverflow', $data) . ')';
}
// Delete the temporary object.
unset($data);
}
// Define the heading for the form.
$formheading = '';
if (!empty($parent)) {
$heading = get_string('yourreply', 'moodleoverflow');
$formheading = get_string('reply', 'moodleoverflow');
} else {
$heading = get_string('yournewtopic', 'moodleoverflow');
}
// Set data for the form.
// TODO Refactor.
$param1 = (isset($discussion->id) ? [$discussion->id] : []);
$param2 = (isset($post->format) ? ['format' => $post->format] : []);
$param3 = (isset($discussion->timestart) ? ['timestart' => $discussion->timestart] : []);
$param4 = (isset($discussion->timeend) ? ['timeend' => $discussion->timeend] : []);
$param5 = (isset($discussion->id) ? ['discussion' => $discussion->id] : []);
$mformpost->set_data([
'attachments' => $draftitemid,
'general' => $heading,
'subject' => $subject,
'message' => [
'text' => $currenttext,
'format' => editors_get_preferred_format(),
'itemid' => $draftideditor,
],
'userid' => $post->userid,
'parent' => $post->parent,
'discussion' => $post->discussion,
'course' => $course->id,
] + $pageparams + $param1 + $param2 + $param3 + $param4 + $param5);
// Is it canceled?
if ($mformpost->is_cancelled()) {
// Redirect the user back.
if (!isset($discussion->id)) {
redirect(new moodle_url('/mod/moodleoverflow/view.php', ['m' => $moodleoverflow->id]));
} else {
redirect(new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id]));
}
// Cancel.
exit();
}
// Is it submitted?
if ($fromform = $mformpost->get_data()) {
// Redirect url in case of occuring errors.
if (empty($SESSION->fromurl)) {
$errordestination = "$CFG->wwwroot/mod/moodleoverflow/view.php?m=$moodleoverflow->id";
} else {
$errordestination = $SESSION->fromurl;
}
// Format the submitted data.
$fromform->messageformat = $fromform->message['format'];
$fromform->draftideditor = $fromform->message['itemid'];
$fromform->message = $fromform->message['text'];
$fromform->messagetrust = trusttext_trusted($modulecontext);
// If we are updating a post.
if ($fromform->edit) {
// Initiate some variables.
unset($fromform->groupid);
$fromform->id = $fromform->edit;
$message = '';
// The FORUM-Plugin had an bug: https://tracker.moodle.org/browse/MDL-4314
// This is a fix for it.
if (!$realpost = $DB->get_record('moodleoverflow_posts', ['id' => $fromform->id])) {
$realpost = new stdClass();
$realpost->userid = -1;
}
// Check the capabilities of the user.
// He may proceed if he can edit any post or if he has the startnewdiscussion
// capability or the capability to reply and is editing his own post.
$editanypost = has_capability('mod/moodleoverflow:editanypost', $modulecontext);
$replypost = has_capability('mod/moodleoverflow:replypost', $modulecontext);
$startdiscussion = has_capability('mod/moodleoverflow:startdiscussion', $modulecontext);
$ownpost = ($realpost->userid == $USER->id);
if (!(($ownpost && ($replypost || $startdiscussion)) || $editanypost)) {
throw new moodle_exception('cannotupdatepost', 'moodleoverflow');
}
// Update the post or print an error message.
$updatepost = $fromform;
$updatepost->moodleoverflow = $moodleoverflow->id;
if (!moodleoverflow_update_post($updatepost, $mformpost)) {
throw new moodle_exception('couldnotupdate', 'moodleoverflow', $errordestination);
}
// Create a success-message.
if ($realpost->userid == $USER->id) {
$message .= get_string('postupdated', 'moodleoverflow');
} else {
if (anonymous::is_post_anonymous($discussion, $moodleoverflow, $realpost->userid)) {
$name = get_string('anonymous', 'moodleoverflow');
} else {
$realuser = $DB->get_record('user', ['id' => $realpost->userid]);
$name = fullname($realuser);
}
$message .= get_string('editedpostupdated', 'moodleoverflow', $name);
}
// Create a link to go back to the discussion.
$discussionurl = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $discussion->id], 'p' . $fromform->id);
// Set some parameters.
$params = [
'context' => $modulecontext,
'objectid' => $fromform->id,
'other' => [
'discussionid' => $discussion->id,
'moodleoverflowid' => $moodleoverflow->id,
], ];
// If the editing user is not the original author, add the original author to the params.
if ($realpost->userid != $USER->id) {
$params['relateduserid'] = $realpost->userid;
}
// Trigger post updated event.
$event = \mod_moodleoverflow\event\post_updated::create($params);
$event->trigger();
// Redirect back to the discussion.
redirect(moodleoverflow_go_back_to($discussionurl), $message, null, \core\output\notification::NOTIFY_SUCCESS);
// Cancel.
exit;
} else if ($fromform->discussion) {
// Add a new post to an existing discussion.
// Set some basic variables.
unset($fromform->groupid);
$message = '';
$addpost = $fromform;
$addpost->moodleoverflow = $moodleoverflow->id;
// Create the new post.
if ($fromform->id = moodleoverflow_add_new_post($addpost)) {
// Subscribe to this thread.
$discussion = new \stdClass();
$discussion->id = $fromform->discussion;
$discussion->moodleoverflow = $moodleoverflow->id;
\mod_moodleoverflow\subscriptions::moodleoverflow_post_subscription($moodleoverflow, $discussion, $modulecontext);
// Print a success-message.
$message .= '<p>' . get_string("postaddedsuccess", "moodleoverflow") . '</p>';
$message .= '<p>' . get_string("postaddedtimeleft", "moodleoverflow",
format_time(get_config('moodleoverflow', 'maxeditingtime'))) . '</p>';
// Set the URL that links back to the discussion.
$link = '/mod/moodleoverflow/discussion.php';
$discussionurl = new moodle_url($link, ['d' => $discussion->id], 'p' . $fromform->id);
// Trigger post created event.
$params = [
'context' => $modulecontext,
'objectid' => $fromform->id,
'other' => [
'discussionid' => $discussion->id,
'moodleoverflowid' => $moodleoverflow->id,
], ];
$event = \mod_moodleoverflow\event\post_created::create($params);
$event->trigger();
redirect(
moodleoverflow_go_back_to($discussionurl),
$message,
\core\output\notification::NOTIFY_SUCCESS
);
// Print an error if the answer could not be added.
} else {
throw new moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
}
// The post has been added.
exit;
} else {
// Add a new discussion.
// The location to redirect the user after successfully posting.
$redirectto = new moodle_url('view.php', ['m' => $fromform->moodleoverflow]);
$discussion = $fromform;
$discussion->name = $fromform->subject;
// Check if the user is allowed to post here.
if (!moodleoverflow_user_can_post_discussion($moodleoverflow)) {
throw new moodle_exception('cannotcreatediscussion', 'moodleoverflow');
}
// Check if the creation of the new discussion failed.
if (!$discussion->id = moodleoverflow_add_discussion($discussion, $modulecontext)) {
throw new moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
} else { // The creation of the new discussion was successful.
$params = [
'context' => $modulecontext,
'objectid' => $discussion->id,
'other' => [
'moodleoverflowid' => $moodleoverflow->id,
],
];
$message = '<p>' . get_string("postaddedsuccess", "moodleoverflow") . '</p>';
// Trigger the discussion created event.
$params = [
'context' => $modulecontext,
'objectid' => $discussion->id,
];
$event = \mod_moodleoverflow\event\discussion_created::create($params);
$event->trigger();
// Subscribe to this thread.
$discussion->moodleoverflow = $moodleoverflow->id;
\mod_moodleoverflow\subscriptions::moodleoverflow_post_subscription($moodleoverflow, $discussion, $modulecontext);
}
// Redirect back to te discussion.
redirect(moodleoverflow_go_back_to($redirectto->out()), $message, null, \core\output\notification::NOTIFY_SUCCESS);
// Do not continue.
exit;
}
}
// If the script gets to this point, nothing has been submitted.
// We have to display the form.
// $course and $moodleoverflow are defined.
// $discussion is only used for replying and editing.
// Define the message to be displayed above the form.
$toppost = new stdClass();
$toppost->subject = get_string("addanewdiscussion", "moodleoverflow");
// Initiate the page.
$PAGE->set_title("$course->shortname: $moodleoverflow->name " . format_string($toppost->subject));
$PAGE->set_heading($course->fullname);
// The page should not be large, only pages containing broad tables are usually.
$PAGE->add_body_class('limitedwidth');
// Display the header.
echo $OUTPUT->header();
// Display the form.
$mformpost->display();
// Display the footer.
echo $OUTPUT->footer();