Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Edit and save a new post to a discussion
19
 *
20
 * @package   mod_forum
21
 * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require_once('../../config.php');
26
require_once('lib.php');
1222 ariadna 27
require_once($CFG->libdir . '/completionlib.php');
1 efrain 28
 
29
$reply   = optional_param('reply', 0, PARAM_INT);
30
$forum   = optional_param('forum', 0, PARAM_INT);
31
$edit    = optional_param('edit', 0, PARAM_INT);
32
$delete  = optional_param('delete', 0, PARAM_INT);
33
$prune   = optional_param('prune', 0, PARAM_INT);
34
$name    = optional_param('name', '', PARAM_CLEAN);
35
$confirm = optional_param('confirm', 0, PARAM_INT);
36
$groupid = optional_param('groupid', null, PARAM_INT);
37
$subject = optional_param('subject', '', PARAM_TEXT);
38
 
39
// Values posted via the inpage reply form.
40
$prefilledpost = optional_param('post', '', PARAM_TEXT);
41
$prefilledpostformat = optional_param('postformat', FORMAT_MOODLE, PARAM_INT);
42
$prefilledprivatereply = optional_param('privatereply', false, PARAM_BOOL);
43
 
44
$PAGE->set_url('/mod/forum/post.php', array(
45
    'reply' => $reply,
46
    'forum' => $forum,
47
    'edit'  => $edit,
48
    'delete' => $delete,
49
    'prune' => $prune,
50
    'name'  => $name,
51
    'confirm' => $confirm,
52
    'groupid' => $groupid,
53
));
54
// These page_params will be passed as hidden variables later in the form.
55
$pageparams = array('reply' => $reply, 'forum' => $forum, 'edit' => $edit);
56
 
57
$sitecontext = context_system::instance();
58
 
59
$entityfactory = mod_forum\local\container::get_entity_factory();
60
$vaultfactory = mod_forum\local\container::get_vault_factory();
61
$managerfactory = mod_forum\local\container::get_manager_factory();
62
$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
63
$urlfactory = mod_forum\local\container::get_url_factory();
64
 
65
$forumvault = $vaultfactory->get_forum_vault();
66
$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
67
 
68
$discussionvault = $vaultfactory->get_discussion_vault();
69
$discussiondatamapper = $legacydatamapperfactory->get_discussion_data_mapper();
70
 
71
$postvault = $vaultfactory->get_post_vault();
72
$postdatamapper = $legacydatamapperfactory->get_post_data_mapper();
73
 
74
if (!isloggedin() or isguestuser()) {
75
    if (!isloggedin() and !get_local_referer()) {
76
        // No referer+not logged in - probably coming in via email  See MDL-9052.
77
        require_login();
78
    }
79
 
80
    if (!empty($forum)) {
81
        // User is starting a new discussion in a forum.
82
        $forumentity = $forumvault->get_from_id($forum);
83
        if (empty($forumentity)) {
84
            throw new \moodle_exception('invalidforumid', 'forum');
85
        }
86
    } else if (!empty($reply)) {
87
        // User is writing a new reply.
88
        $forumentity = $forumvault->get_from_post_id($reply);
89
        if (empty($forumentity)) {
90
            throw new \moodle_exception('invalidparentpostid', 'forum');
91
        }
92
    }
93
 
94
    $forum = $forumdatamapper->to_legacy_object($forumentity);
95
    $modcontext = $forumentity->get_context();
96
    $course = $forumentity->get_course_record();
97
    if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
98
        throw new \moodle_exception("invalidcoursemodule");
99
    }
100
 
101
    $PAGE->set_cm($cm, $course, $forum);
102
    $PAGE->set_context($modcontext);
103
    $PAGE->set_title($course->shortname);
104
    $PAGE->set_heading($course->fullname);
105
    $referer = get_local_referer(false);
106
 
107
    echo $OUTPUT->header();
108
    echo $OUTPUT->confirm(get_string('noguestpost', 'forum'), get_login_url(), $referer, [
109
        'confirmtitle' => get_string('noguestpost:title', 'forum'),
110
        'continuestr' => get_string('login')
111
    ]);
112
    echo $OUTPUT->footer();
113
    exit;
114
}
115
 
116
require_login(0, false);   // Script is useless unless they're logged in.
117
 
118
$canreplyprivately = false;
119
 
120
if (!empty($forum)) {
121
    // User is starting a new discussion in a forum.
122
    $forumentity = $forumvault->get_from_id($forum);
123
    if (empty($forumentity)) {
124
        throw new \moodle_exception('invalidforumid', 'forum');
125
    }
126
 
127
    $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
128
    $forum = $forumdatamapper->to_legacy_object($forumentity);
129
    $course = $forumentity->get_course_record();
130
    if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
131
        throw new \moodle_exception("invalidcoursemodule");
132
    }
133
 
134
    // Retrieve the contexts.
135
    $modcontext = $forumentity->get_context();
136
    $coursecontext = context_course::instance($course->id);
137
 
138
    if ($forumentity->is_in_group_mode() && null === $groupid) {
139
        $groupid = groups_get_activity_group($cm);
140
    }
141
 
142
    if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
143
        if (!isguestuser()) {
144
            if (!is_enrolled($coursecontext)) {
145
                if (enrol_selfenrol_available($course->id)) {
146
                    $SESSION->wantsurl = qualified_me();
147
                    $SESSION->enrolcancel = get_local_referer(false);
1222 ariadna 148
                    redirect(
149
                        new moodle_url('/enrol/index.php', array(
150
                            'id' => $course->id,
151
                            'returnurl' => '/mod/forum/view.php?f=' . $forum->id
152
                        )),
153
                        get_string('youneedtoenrol')
154
                    );
1 efrain 155
                }
156
            }
157
        }
158
        throw new \moodle_exception('nopostforum', 'forum');
159
    }
160
 
161
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
162
        redirect(
1222 ariadna 163
            $urlfactory->get_course_url_from_forum($forumentity),
164
            get_string('activityiscurrentlyhidden'),
165
            null,
166
            \core\output\notification::NOTIFY_ERROR
167
        );
1 efrain 168
    }
169
 
170
    // Load up the $post variable.
171
 
172
    $post = new stdClass();
173
    $post->course        = $course->id;
174
    $post->forum         = $forum->id;
175
    $post->discussion    = 0;           // Ie discussion # not defined yet.
176
    $post->parent        = 0;
177
    $post->subject       = $subject;
178
    $post->userid        = $USER->id;
179
    $post->message       = $prefilledpost;
180
    $post->messageformat = editors_get_preferred_format();
181
    $post->messagetrust  = 0;
182
    $post->groupid = $groupid;
183
 
184
    // Unsetting this will allow the correct return URL to be calculated later.
185
    unset($SESSION->fromdiscussion);
186
} else if (!empty($reply)) {
187
    // User is writing a new reply.
188
 
189
    $parententity = $postvault->get_from_id($reply);
190
    if (empty($parententity)) {
191
        throw new \moodle_exception('invalidparentpostid', 'forum');
192
    }
193
 
194
    $discussionentity = $discussionvault->get_from_id($parententity->get_discussion_id());
195
    if (empty($discussionentity)) {
196
        throw new \moodle_exception('notpartofdiscussion', 'forum');
197
    }
198
 
199
    $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
200
    if (empty($forumentity)) {
201
        throw new \moodle_exception('invalidforumid', 'forum');
202
    }
203
 
204
    $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
205
    $parent = $postdatamapper->to_legacy_object($parententity);
206
    $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
207
    $forum = $forumdatamapper->to_legacy_object($forumentity);
208
    $course = $forumentity->get_course_record();
209
    $modcontext = $forumentity->get_context();
210
    $coursecontext = context_course::instance($course->id);
211
 
212
    if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
213
        throw new \moodle_exception('invalidcoursemodule');
214
    }
215
 
216
    // Ensure lang, theme, etc. is set up properly. MDL-6926.
217
    $PAGE->set_cm($cm, $course, $forum);
218
 
219
    if (!$capabilitymanager->can_reply_to_post($USER, $discussionentity, $parententity)) {
220
        if (!isguestuser()) {
221
            if (!is_enrolled($coursecontext)) {  // User is a guest here!
222
                $SESSION->wantsurl = qualified_me();
223
                $SESSION->enrolcancel = get_local_referer(false);
1222 ariadna 224
                redirect(
225
                    new moodle_url('/enrol/index.php', array(
226
                        'id' => $course->id,
227
                        'returnurl' => '/mod/forum/view.php?f=' . $forum->id
228
                    )),
229
                    get_string('youneedtoenrol')
230
                );
1 efrain 231
            }
232
 
233
            // The forum has been locked. Just redirect back to the discussion page.
234
            if (forum_discussion_is_locked($forum, $discussion)) {
235
                redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
236
            }
237
        }
238
        throw new \moodle_exception('nopostforum', 'forum');
239
    }
240
 
241
    // Make sure user can post here.
242
    if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
243
        $groupmode = $cm->groupmode;
244
    } else {
245
        $groupmode = $course->groupmode;
246
    }
247
    if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
248
        if ($discussion->groupid == -1) {
249
            throw new \moodle_exception('nopostforum', 'forum');
250
        } else {
251
            if (!groups_is_member($discussion->groupid)) {
252
                throw new \moodle_exception('nopostforum', 'forum');
253
            }
254
        }
255
    }
256
 
257
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
258
        throw new \moodle_exception("activityiscurrentlyhidden");
259
    }
260
 
261
    if ($parententity->is_private_reply()) {
262
        throw new \moodle_exception('cannotreplytoprivatereply', 'forum');
263
    }
264
 
265
    // We always are going to honor the preferred format. We are creating a new post.
266
    $preferredformat = editors_get_preferred_format();
267
 
268
    // Only if there are prefilled contents coming.
269
    if (!empty($prefilledpost)) {
270
        // If the prefilled post is not HTML and the preferred format is HTML, convert to it.
271
        if ($prefilledpostformat != FORMAT_HTML and $preferredformat == FORMAT_HTML) {
272
            $prefilledpost = format_text($prefilledpost, $prefilledpostformat, ['context' => $modcontext]);
273
        }
274
    }
275
 
276
    // Load up the $post variable.
277
    $post = new stdClass();
278
    $post->course      = $course->id;
279
    $post->forum       = $forum->id;
280
    $post->discussion  = $parent->discussion;
281
    $post->parent      = $parent->id;
282
    $post->subject     = $subject ? $subject : $parent->subject;
283
    $post->userid      = $USER->id;
284
    $post->parentpostauthor = $parent->userid;
285
    $post->message     = $prefilledpost;
286
    $post->messageformat  = $preferredformat;
287
    $post->isprivatereply = $prefilledprivatereply;
288
    $canreplyprivately = $capabilitymanager->can_reply_privately_to_post($USER, $parententity);
289
 
290
    $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
291
 
292
    $strre = get_string('re', 'forum');
293
    if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
1222 ariadna 294
        $post->subject = $strre . ' ' . $post->subject;
1 efrain 295
    }
296
 
297
    // Unsetting this will allow the correct return URL to be calculated later.
298
    unset($SESSION->fromdiscussion);
299
} else if (!empty($edit)) {
300
    // User is editing their own post.
301
 
302
    $postentity = $postvault->get_from_id($edit);
303
    if (empty($postentity)) {
304
        throw new \moodle_exception('invalidpostid', 'forum');
305
    }
306
    if ($postentity->has_parent()) {
307
        $parententity = $postvault->get_from_id($postentity->get_parent_id());
308
        $parent = $postdatamapper->to_legacy_object($parententity);
309
    }
310
 
311
    $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
312
    if (empty($discussionentity)) {
313
        throw new \moodle_exception('notpartofdiscussion', 'forum');
314
    }
315
 
316
    $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
317
    if (empty($forumentity)) {
318
        throw new \moodle_exception('invalidforumid', 'forum');
319
    }
320
 
321
    $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
322
    $post = $postdatamapper->to_legacy_object($postentity);
323
    $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
324
    $forum = $forumdatamapper->to_legacy_object($forumentity);
325
    $course = $forumentity->get_course_record();
326
    $modcontext = $forumentity->get_context();
327
    $coursecontext = context_course::instance($course->id);
328
 
329
    if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
330
        throw new \moodle_exception('invalidcoursemodule');
331
    }
332
 
333
    $PAGE->set_cm($cm, $course, $forum);
334
 
335
    if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
336
        if (((time() - $post->created) > $CFG->maxeditingtime) and
1222 ariadna 337
            !has_capability('mod/forum:editanypost', $modcontext)
338
        ) {
1 efrain 339
            throw new \moodle_exception('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
340
        }
341
    }
342
    if (($post->userid <> $USER->id) and
1222 ariadna 343
        !has_capability('mod/forum:editanypost', $modcontext)
344
    ) {
1 efrain 345
        throw new \moodle_exception('cannoteditposts', 'forum');
346
    }
347
 
348
    // Load up the $post variable.
349
    $post->edit   = $edit;
350
    $post->course = $course->id;
351
    $post->forum  = $forum->id;
352
    $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
353
    if ($postentity->has_parent()) {
354
        $canreplyprivately = forum_user_can_reply_privately($modcontext, $parent);
355
    }
356
 
357
    $post = trusttext_pre_edit($post, 'message', $modcontext);
358
 
359
    // Unsetting this will allow the correct return URL to be calculated later.
360
    unset($SESSION->fromdiscussion);
361
} else if (!empty($delete)) {
362
    // User is deleting a post.
363
 
364
    $postentity = $postvault->get_from_id($delete);
365
    if (empty($postentity)) {
366
        throw new \moodle_exception('invalidpostid', 'forum');
367
    }
368
 
369
    $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
370
    if (empty($discussionentity)) {
371
        throw new \moodle_exception('notpartofdiscussion', 'forum');
372
    }
373
 
374
    $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
375
    if (empty($forumentity)) {
376
        throw new \moodle_exception('invalidforumid', 'forum');
377
    }
378
 
379
    $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
380
    $course = $forumentity->get_course_record();
381
    $cm = $forumentity->get_course_module_record();
382
    $modcontext = $forumentity->get_context();
383
 
384
    require_login($course, false, $cm);
385
 
386
    $replycount = $postvault->get_reply_count_for_post_id_in_discussion_id(
1222 ariadna 387
        $USER,
388
        $postentity->get_id(),
389
        $discussionentity->get_id(),
390
        true
391
    );
1 efrain 392
 
393
    if (!empty($confirm) && confirm_sesskey()) {
394
        // Do further checks and delete the post.
395
        $hasreplies = $replycount > 0;
396
 
397
        try {
398
            $capabilitymanager->validate_delete_post($USER, $discussionentity, $postentity, $hasreplies);
399
 
400
            if (!$postentity->has_parent()) {
401
                forum_delete_discussion(
402
                    $discussiondatamapper->to_legacy_object($discussionentity),
403
                    false,
404
                    $forumentity->get_course_record(),
405
                    $forumentity->get_course_module_record(),
406
                    $forumdatamapper->to_legacy_object($forumentity)
407
                );
408
 
409
                redirect(
410
                    $urlfactory->get_forum_view_url_from_forum($forumentity),
411
                    get_string('eventdiscussiondeleted', 'forum'),
412
                    null,
413
                    \core\output\notification::NOTIFY_SUCCESS
414
                );
415
            } else {
416
                forum_delete_post(
417
                    $postdatamapper->to_legacy_object($postentity),
418
                    has_capability('mod/forum:deleteanypost', $modcontext),
419
                    $forumentity->get_course_record(),
420
                    $forumentity->get_course_module_record(),
421
                    $forumdatamapper->to_legacy_object($forumentity)
422
                );
423
 
424
                if ($forumentity->get_type() == 'single') {
425
                    // Single discussion forums are an exception.
426
                    // We show the forum itself since it only has one discussion thread.
427
                    $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
428
                } else {
429
                    $discussionurl = $urlfactory->get_discussion_view_url_from_discussion($discussionentity);
430
                }
431
 
432
                redirect(
433
                    forum_go_back_to($discussionurl),
434
                    get_string('eventpostdeleted', 'forum'),
435
                    null,
436
                    \core\output\notification::NOTIFY_SUCCESS
437
                );
438
            }
439
        } catch (Exception $e) {
440
            redirect(
441
                $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
442
                $e->getMessage(),
443
                null,
444
                \core\output\notification::NOTIFY_ERROR
445
            );
446
        }
447
    } else {
448
 
449
        if (!$capabilitymanager->can_delete_post($USER, $discussionentity, $postentity)) {
450
            redirect(
1222 ariadna 451
                $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
452
                get_string('cannotdeletepost', 'forum'),
453
                null,
454
                \core\output\notification::NOTIFY_ERROR
455
            );
1 efrain 456
        }
457
 
458
        $post = $postdatamapper->to_legacy_object($postentity);
459
        $forum = $forumdatamapper->to_legacy_object($forumentity);
460
 
461
        // User just asked to delete something.
462
        forum_set_return();
463
        $PAGE->navbar->add(get_string('delete', 'forum'));
464
        $PAGE->set_title($course->shortname);
465
        $PAGE->set_heading($course->fullname);
466
        $PAGE->set_secondary_active_tab('modulepage');
467
        $PAGE->activityheader->disable();
468
 
469
        if ($replycount) {
470
            if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
471
                redirect(
1222 ariadna 472
                    forum_go_back_to($urlfactory->get_view_post_url_from_post($postentity)),
473
                    get_string('couldnotdeletereplies', 'forum'),
474
                    null,
475
                    \core\output\notification::NOTIFY_ERROR
476
                );
1 efrain 477
            }
478
 
479
            echo $OUTPUT->header();
480
            if (!$PAGE->has_secondary_navigation()) {
481
                echo $OUTPUT->heading(format_string($forum->name), 2);
482
            }
1222 ariadna 483
            echo $OUTPUT->confirm(
484
                get_string("deletesureplural", "forum", $replycount + 1),
1 efrain 485
                "post.php?delete=$delete&confirm=$delete",
1222 ariadna 486
                $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '#p' . $post->id
487
            );
1 efrain 488
 
489
            $postentities = [$postentity];
490
            if (empty($post->edit)) {
491
                $postvault = $vaultfactory->get_post_vault();
492
                $replies = $postvault->get_replies_to_post(
1222 ariadna 493
                    $USER,
494
                    $postentity,
495
                    // Note: All replies are fetched here as the user has deleteanypost.
496
                    true,
497
                    'created ASC'
498
                );
1 efrain 499
                $postentities = array_merge($postentities, $replies);
500
            }
501
 
502
            $rendererfactory = mod_forum\local\container::get_renderer_factory();
503
            $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_NESTED, true);
504
            echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
505
        } else {
506
            echo $OUTPUT->header();
507
            if (!$PAGE->has_secondary_navigation()) {
508
                echo $OUTPUT->heading(format_string($forum->name), 2);
509
            }
1222 ariadna 510
            echo $OUTPUT->confirm(
511
                get_string("deletesure", "forum", $replycount),
1 efrain 512
                "post.php?delete=$delete&confirm=$delete",
1222 ariadna 513
                $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $post->discussion . '#p' . $post->id
514
            );
1 efrain 515
 
516
            $rendererfactory = mod_forum\local\container::get_renderer_factory();
517
            $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
518
            echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
519
        }
520
    }
521
    echo $OUTPUT->footer();
522
    die;
523
} else if (!empty($prune)) {
524
    // Pruning.
525
 
526
    $postentity = $postvault->get_from_id($prune);
527
    if (empty($postentity)) {
528
        throw new \moodle_exception('invalidpostid', 'forum');
529
    }
530
 
531
    $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
532
    if (empty($discussionentity)) {
533
        throw new \moodle_exception('notpartofdiscussion', 'forum');
534
    }
535
 
536
    $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
537
    if (empty($forumentity)) {
538
        throw new \moodle_exception('invalidforumid', 'forum');
539
    }
540
 
541
    $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
542
    $post = $postdatamapper->to_legacy_object($postentity);
543
    $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
544
    $forum = $forumdatamapper->to_legacy_object($forumentity);
545
    $course = $forumentity->get_course_record();
546
    $modcontext = $forumentity->get_context();
547
    $coursecontext = context_course::instance($course->id);
548
 
549
    if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
550
        throw new \moodle_exception('invalidcoursemodule');
551
    }
552
 
553
    if (!$postentity->has_parent()) {
554
        redirect(
1222 ariadna 555
            $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
556
            get_string('alreadyfirstpost', 'forum'),
557
            null,
558
            \core\output\notification::NOTIFY_ERROR
559
        );
1 efrain 560
    }
561
    if (!$capabilitymanager->can_split_post($USER, $discussionentity, $postentity)) {
562
        redirect(
1222 ariadna 563
            $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
564
            get_string('cannotsplit', 'forum'),
565
            null,
566
            \core\output\notification::NOTIFY_ERROR
567
        );
1 efrain 568
    }
569
 
570
    $PAGE->set_cm($cm);
571
    $PAGE->set_context($modcontext);
572
    $PAGE->set_secondary_active_tab('modulepage');
573
    $PAGE->activityheader->disable();
574
 
575
    $prunemform = new mod_forum_prune_form(null, array('prune' => $prune, 'confirm' => $prune));
576
 
577
    if ($prunemform->is_cancelled()) {
578
        redirect(forum_go_back_to($urlfactory->get_discussion_view_url_from_discussion($discussionentity)));
579
    } else if ($fromform = $prunemform->get_data()) {
580
        // User submits the data.
581
        $newdiscussion = new stdClass();
582
        $newdiscussion->course       = $discussion->course;
583
        $newdiscussion->forum        = $discussion->forum;
584
        $newdiscussion->name         = $name;
585
        $newdiscussion->firstpost    = $post->id;
586
        $newdiscussion->userid       = $post->userid;
587
        $newdiscussion->groupid      = $discussion->groupid;
588
        $newdiscussion->assessed     = $discussion->assessed;
589
        $newdiscussion->usermodified = $post->userid;
590
        $newdiscussion->timestart    = $discussion->timestart;
591
        $newdiscussion->timeend      = $discussion->timeend;
592
 
593
        $newid = $DB->insert_record('forum_discussions', $newdiscussion);
594
 
595
        $newpost = new stdClass();
596
        $newpost->id      = $post->id;
597
        $newpost->parent  = 0;
598
        $newpost->subject = $name;
599
 
600
        $DB->update_record("forum_posts", $newpost);
601
        $postentity = $postvault->get_from_id($postentity->get_id());
602
 
603
        forum_change_discussionid($post->id, $newid);
604
 
605
        // Update last post in each discussion.
606
        forum_discussion_update_last_post($discussion->id);
607
        forum_discussion_update_last_post($newid);
608
 
609
        // Fire events to reflect the split..
610
        $params = array(
611
            'context' => $modcontext,
612
            'objectid' => $discussion->id,
613
            'other' => array(
614
                'forumid' => $forum->id,
615
            )
616
        );
617
        $event = \mod_forum\event\discussion_updated::create($params);
618
        $event->trigger();
619
 
620
        $params = array(
621
            'context' => $modcontext,
622
            'objectid' => $newid,
623
            'other' => array(
624
                'forumid' => $forum->id,
625
            )
626
        );
627
        $event = \mod_forum\event\discussion_created::create($params);
628
        $event->trigger();
629
 
630
        $params = array(
631
            'context' => $modcontext,
632
            'objectid' => $post->id,
633
            'other' => array(
634
                'discussionid' => $newid,
635
                'forumid' => $forum->id,
636
                'forumtype' => $forum->type,
637
            )
638
        );
639
        $event = \mod_forum\event\post_updated::create($params);
640
        $event->add_record_snapshot('forum_discussions', $discussion);
641
        $event->trigger();
642
 
643
        redirect(
644
            forum_go_back_to($urlfactory->get_discussion_view_url_from_post($postentity)),
645
            get_string('discussionsplit', 'forum'),
646
            null,
647
            \core\output\notification::NOTIFY_SUCCESS
648
        );
649
    } else {
650
        // Display the prune form.
651
        $course = $DB->get_record('course', array('id' => $forum->course));
652
        $subjectstr = format_string($post->subject, true);
653
        $PAGE->navbar->add($subjectstr, new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
654
        $PAGE->navbar->add(get_string("prunediscussion", "forum"));
1222 ariadna 655
        $PAGE->set_title(format_string($discussion->name) . ": " . format_string($post->subject));
1 efrain 656
        $PAGE->set_heading($course->fullname);
657
        echo $OUTPUT->header();
658
        if (!$PAGE->has_secondary_navigation()) {
659
            echo $OUTPUT->heading(format_string($forum->name), 2);
660
        }
661
        echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
662
 
663
        $prunemform->display();
664
 
665
        $postentity = $entityfactory->get_post_from_stdclass($post);
666
        $discussionentity = $entityfactory->get_discussion_from_stdclass($discussion);
667
        $forumentity = $entityfactory->get_forum_from_stdclass($forum, $modcontext, $cm, $course);
668
        $rendererfactory = mod_forum\local\container::get_renderer_factory();
669
        $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
670
        echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
671
    }
672
 
673
    echo $OUTPUT->footer();
674
    die;
675
} else {
676
    throw new \moodle_exception('unknowaction');
677
}
678
 
679
// From now on user must be logged on properly.
680
 
681
require_login($course, false, $cm);
682
 
683
if (isguestuser()) {
684
    // Just in case.
685
    throw new \moodle_exception('noguest');
686
}
687
 
688
$thresholdwarning = forum_check_throttling($forum, $cm);
689
$mformpost = new mod_forum_post_form('post.php', [
1222 ariadna 690
    'course' => $course,
691
    'cm' => $cm,
692
    'coursecontext' => $coursecontext,
693
    'modcontext' => $modcontext,
694
    'forum' => $forum,
695
    'post' => $post,
696
    'subscribe' => \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm),
697
    'thresholdwarning' => $thresholdwarning,
698
    'edit' => $edit,
699
    'canreplyprivately' => $canreplyprivately,
700
], 'post', '', array('id' => 'mformforum'));
1 efrain 701
 
702
$draftitemid = file_get_submitted_draft_itemid('attachments');
703
$postid = empty($post->id) ? null : $post->id;
704
$attachoptions = mod_forum_post_form::attachment_options($forum);
705
file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', $postid, $attachoptions);
706
 
707
// Load data into form NOW!
708
 
709
if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end.
710
    $data = new stdClass();
711
    $data->date = userdate($post->created);
712
    if ($post->messageformat == FORMAT_HTML) {
1222 ariadna 713
        $data->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $USER->id . '&course=' . $post->course . '">' .
714
            fullname($USER) . '</a>';
1 efrain 715
    } else {
716
        $data->name = fullname($USER);
717
    }
718
    unset($data);
719
}
720
 
721
$formheading = '';
722
if (!empty($parent)) {
723
    $heading = get_string("yourreply", "forum");
724
    $formheading = get_string('reply', 'forum');
725
} else {
726
    if ($forum->type == 'qanda') {
727
        $heading = get_string('yournewquestion', 'forum');
728
    } else {
729
        $heading = get_string('yournewtopic', 'forum');
730
    }
731
}
732
 
733
$postid = empty($post->id) ? null : $post->id;
734
$draftideditor = file_get_submitted_draft_itemid('message');
735
$editoropts = mod_forum_post_form::editor_options($modcontext, $postid);
736
$currenttext = file_prepare_draft_area($draftideditor, $modcontext->id, 'mod_forum', 'post', $postid, $editoropts, $post->message);
737
$discussionid = isset($discussion) ? $discussion->id : null;
738
$discussionsubscribe = \mod_forum\subscriptions::get_user_default_subscription($forum, $coursecontext, $cm, $discussionid);
739
 
740
$mformpost->set_data(
741
    array(
742
        'attachments' => $draftitemid,
743
        'general' => $heading,
744
        'subject' => $post->subject,
745
        'message' => array(
746
            'text' => $currenttext,
747
            'format' => !isset($post->messageformat) || !is_numeric($post->messageformat) ?
748
                editors_get_preferred_format() : $post->messageformat,
749
            'itemid' => $draftideditor
750
        ),
751
        'discussionsubscribe' => $discussionsubscribe,
752
        'mailnow' => !empty($post->mailnow),
753
        'userid' => $post->userid,
754
        'parent' => $post->parent,
755
        'discussion' => $post->discussion,
756
        'course' => $course->id,
757
        'isprivatereply' => $post->isprivatereply ?? false
758
    ) +
759
 
1222 ariadna 760
        $pageparams +
1 efrain 761
 
1222 ariadna 762
        (isset($post->format) ? array('format' => $post->format) : array()) +
1 efrain 763
 
1222 ariadna 764
        (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) +
1 efrain 765
 
1222 ariadna 766
        (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) +
1 efrain 767
 
1222 ariadna 768
        (isset($discussion->pinned) ? array('pinned' => $discussion->pinned) : array()) +
1 efrain 769
 
1222 ariadna 770
        (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
1 efrain 771
 
1222 ariadna 772
        (isset($discussion->id) ? array('discussion' => $discussion->id) : array())
1 efrain 773
);
774
 
775
// If we are being redirected via a no_submit_button press OR if the message is being prefilled.
776
// then set the initial 'dirty' state.
777
// - A prefilled post will exist when being redirected from the inpage reply form.
778
// - A no_submit_button press occurs when being redirected from the inpage add new discussion post form.
779
$dirty = $prefilledpost ? true : false;
780
if ($mformpost->no_submit_button_pressed()) {
781
    $data = $mformpost->get_submitted_data();
782
 
783
    // If a no submit button has been pressed but the default values haven't been then reset the form change.
784
    if (!$dirty && isset($data->message['text']) && !empty(trim($data->message['text']))) {
785
        $dirty = true;
786
    }
787
 
788
    if (!$dirty && isset($data->message['message']) && !empty(trim($data->message['message']))) {
789
        $dirty = true;
790
    }
791
}
792
$mformpost->set_initial_dirty_state($dirty);
793
 
794
if ($mformpost->is_cancelled()) {
795
    if (!isset($discussion->id) || $forum->type === 'single') {
796
        // Single forums don't have a discussion page.
797
        redirect($urlfactory->get_forum_view_url_from_forum($forumentity));
798
    } else {
799
        redirect($urlfactory->get_discussion_view_url_from_discussion($discussionentity));
800
    }
801
} else if ($mformpost->is_submitted() && !$mformpost->no_submit_button_pressed() && $fromform = $mformpost->get_data()) {
802
 
803
    $errordestination = get_local_referer(false) ?: $urlfactory->get_forum_view_url_from_forum($forumentity);
804
 
805
    $fromform->itemid        = $fromform->message['itemid'];
806
    $fromform->messageformat = $fromform->message['format'];
807
    $fromform->message       = $fromform->message['text'];
808
    // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
809
    $fromform->messagetrust  = trusttext_trusted($modcontext);
810
 
811
    // Do not clean text here, text cleaning can be done only after conversion to HTML.
812
    // Word counting now uses text formatting, there is no need to abuse trusttext_pre_edit() here.
813
 
814
    if ($fromform->edit) {
815
        // Updating a post.
816
        unset($fromform->groupid);
817
        $fromform->id = $fromform->edit;
818
        $message = '';
819
 
820
        if (!$capabilitymanager->can_edit_post($USER, $discussionentity, $postentity)) {
821
            redirect(
1222 ariadna 822
                $urlfactory->get_view_post_url_from_post($postentity),
823
                get_string('cannotupdatepost', 'forum'),
824
                null,
825
                \core\output\notification::ERROR
826
            );
1 efrain 827
        }
828
 
829
        if (isset($fromform->groupinfo) && $capabilitymanager->can_move_discussions($USER)) {
830
            // If the user has access to all groups and they are changing the group, then update the post.
831
            if (empty($fromform->groupinfo)) {
832
                $fromform->groupinfo = -1;
833
            }
834
 
835
            if (!$capabilitymanager->can_create_discussions($USER, $fromform->groupinfo)) {
836
                redirect(
1222 ariadna 837
                    $urlfactory->get_view_post_url_from_post($postentity),
838
                    get_string('cannotupdatepost', 'forum'),
839
                    null,
840
                    \core\output\notification::ERROR
841
                );
1 efrain 842
            }
843
 
844
            if ($discussionentity->get_group_id() != $fromform->groupinfo) {
845
                $DB->set_field('forum_discussions', 'groupid', $fromform->groupinfo, array('firstpost' => $fromform->id));
846
            }
847
        }
848
 
849
        // When editing first post/discussion.
850
        if (!$postentity->has_parent()) {
851
            if ($capabilitymanager->can_pin_discussions($USER)) {
852
                // Can change pinned if we have capability.
853
                $fromform->pinned = !empty($fromform->pinned) ? FORUM_DISCUSSION_PINNED : FORUM_DISCUSSION_UNPINNED;
854
            } else {
855
                // We don't have the capability to change so keep to previous value.
856
                unset($fromform->pinned);
857
            }
858
        }
859
        $updatepost = $fromform;
860
        $updatepost->forum = $forum->id;
861
        if (!forum_update_post($updatepost, $mformpost)) {
862
            throw new \moodle_exception("couldnotupdate", "forum", $errordestination);
863
        }
864
 
865
        forum_trigger_post_updated_event($post, $discussion, $modcontext, $forum);
866
 
867
        if ($USER->id === $postentity->get_author_id()) {
868
            $message .= get_string("postupdated", "forum");
869
        } else {
870
            $realuser = \core_user::get_user($postentity->get_author_id());
871
            $message .= get_string("editedpostupdated", "forum", fullname($realuser));
872
        }
873
 
874
        $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
875
        if ('single' == $forumentity->get_type()) {
876
            // Single discussion forums are an exception.
877
            // We show the forum itself since it only has one discussion thread.
878
            $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
879
        } else {
880
            $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
881
        }
882
 
883
        redirect(
884
            forum_go_back_to($discussionurl),
885
            $message . $subscribemessage,
886
            null,
887
            \core\output\notification::NOTIFY_SUCCESS
888
        );
889
    } else if ($fromform->discussion) {
890
        // Adding a new post to an existing discussion
891
        // Before we add this we must check that the user will not exceed the blocking threshold.
892
        forum_check_blocking_threshold($thresholdwarning);
893
 
894
        unset($fromform->groupid);
895
        $message = '';
896
        $addpost = $fromform;
897
        $addpost->forum = $forum->id;
898
        if ($fromform->id = forum_add_new_post($addpost, $mformpost)) {
899
            $postentity = $postvault->get_from_id($fromform->id);
900
            $fromform->deleted = 0;
901
            $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
902
 
903
            if (!empty($fromform->mailnow)) {
904
                $message .= get_string("postmailnow", "forum");
905
            } else {
1222 ariadna 906
                $message .= '<p>' . get_string("postaddedsuccess", "forum") . '</p>';
907
                $message .= '<p>' . get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
1 efrain 908
            }
909
 
910
            if ($forum->type == 'single') {
911
                // Single discussion forums are an exception.
912
                // We show the forum itself since it only has one discussion thread.
913
                $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
914
            } else {
915
                $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
916
            }
917
 
918
            $params = array(
919
                'context' => $modcontext,
920
                'objectid' => $fromform->id,
921
                'other' => array(
922
                    'discussionid' => $discussion->id,
923
                    'forumid' => $forum->id,
924
                    'forumtype' => $forum->type,
925
                )
926
            );
927
            $event = \mod_forum\event\post_created::create($params);
928
            $event->add_record_snapshot('forum_posts', $fromform);
929
            $event->add_record_snapshot('forum_discussions', $discussion);
930
            $event->trigger();
931
 
932
            // Update completion state.
933
            $completion = new completion_info($course);
1222 ariadna 934
            if (
935
                $completion->is_enabled($cm) &&
936
                ($forum->completionreplies || $forum->completionposts)
937
            ) {
1 efrain 938
                $completion->update_state($cm, COMPLETION_COMPLETE);
939
            }
940
 
941
            redirect(
942
                forum_go_back_to($discussionurl),
943
                $message . $subscribemessage,
944
                null,
945
                \core\output\notification::NOTIFY_SUCCESS
946
            );
947
        } else {
948
            throw new \moodle_exception("couldnotadd", "forum", $errordestination);
949
        }
950
        exit;
951
    } else {
952
        // Adding a new discussion.
953
        // The location to redirect to after successfully posting.
954
        $redirectto = new moodle_url('/mod/forum/view.php', array('f' => $fromform->forum));
955
 
956
        $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
957
 
958
        $discussion = $fromform;
959
        $discussion->name = $fromform->subject;
960
        $discussion->timelocked = 0;
961
 
962
        $newstopic = false;
963
        if ($forum->type == 'news' && !$fromform->parent) {
964
            $newstopic = true;
965
        }
966
 
967
        if (!empty($fromform->pinned) && $capabilitymanager->can_pin_discussions($USER)) {
968
            $discussion->pinned = FORUM_DISCUSSION_PINNED;
969
        } else {
970
            $discussion->pinned = FORUM_DISCUSSION_UNPINNED;
971
        }
972
 
973
        $allowedgroups = array();
974
        $groupstopostto = array();
975
 
976
        // If we are posting a copy to all groups the user has access to.
977
        if (isset($fromform->posttomygroups)) {
978
            // Post to each of my groups.
979
            require_capability('mod/forum:canposttomygroups', $modcontext);
980
 
981
            // Fetch all of this user's groups.
982
            // Note: all groups are returned when in visible groups mode so we must manually filter.
983
            $allowedgroups = groups_get_activity_allowed_groups($cm);
984
            foreach ($allowedgroups as $groupid => $group) {
985
                if ($capabilitymanager->can_create_discussions($USER, $groupid)) {
986
                    $groupstopostto[] = $groupid;
987
                }
988
            }
989
        } else if (isset($fromform->groupinfo)) {
990
            // Use the value provided in the dropdown group selection.
991
            $groupstopostto[] = $fromform->groupinfo;
992
            $redirectto->param('group', $fromform->groupinfo);
993
        } else if (isset($fromform->groupid) && !empty($fromform->groupid)) {
994
            // Use the value provided in the hidden form element instead.
995
            $groupstopostto[] = $fromform->groupid;
996
            $redirectto->param('group', $fromform->groupid);
997
        } else {
998
            // Use the value for all participants instead.
999
            $groupstopostto[] = -1;
1000
        }
1001
 
1002
        // Before we post this we must check that the user will not exceed the blocking threshold.
1003
        forum_check_blocking_threshold($thresholdwarning);
1004
 
1005
        foreach ($groupstopostto as $group) {
1006
            if (!$capabilitymanager->can_create_discussions($USER, $group)) {
1007
                throw new \moodle_exception('cannotcreatediscussion', 'forum');
1008
            }
1009
 
1010
            $discussion->groupid = $group;
1011
            $message = '';
1012
            if ($discussion->id = forum_add_discussion($discussion, $mformpost)) {
1013
 
1014
                $params = array(
1015
                    'context' => $modcontext,
1016
                    'objectid' => $discussion->id,
1017
                    'other' => array(
1018
                        'forumid' => $forum->id,
1019
                    )
1020
                );
1021
                $event = \mod_forum\event\discussion_created::create($params);
1022
                $event->add_record_snapshot('forum_discussions', $discussion);
1023
                $event->trigger();
1024
 
1025
                if ($fromform->mailnow) {
1026
                    $message .= get_string("postmailnow", "forum");
1027
                } else {
1222 ariadna 1028
                    $message .= '<p>' . get_string("postaddedsuccess", "forum") . '</p>';
1029
                    $message .= '<p>' . get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
1 efrain 1030
                }
1031
 
1032
                $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
1033
            } else {
1034
                throw new \moodle_exception("couldnotadd", "forum", $errordestination);
1035
            }
1036
        }
1037
 
1038
        // Update completion status.
1039
        $completion = new completion_info($course);
1222 ariadna 1040
        if (
1041
            $completion->is_enabled($cm) &&
1042
            ($forum->completiondiscussions || $forum->completionposts)
1043
        ) {
1 efrain 1044
            $completion->update_state($cm, COMPLETION_COMPLETE);
1045
        }
1046
 
1047
        // Redirect back to the discussion.
1048
        redirect(
1049
            forum_go_back_to($redirectto->out()),
1050
            $message . $subscribemessage,
1051
            null,
1052
            \core\output\notification::NOTIFY_SUCCESS
1053
        );
1054
    }
1055
}
1056
 
1057
 
1058
// This section is only shown after all checks are in place, and the forumentity and any relevant discussion and post
1059
// entity are available.
1060
 
1061
if (!empty($discussionentity)) {
1062
    $titlesubject = format_string($discussionentity->get_name(), true);
1063
} else if ('news' == $forumentity->get_type()) {
1064
    $titlesubject = get_string("addanewtopic", "forum");
1065
} else {
1066
    $titlesubject = get_string("addanewdiscussion", "forum");
1067
}
1068
 
1069
if (empty($post->edit)) {
1070
    $post->edit = '';
1071
}
1072
 
1073
if (empty($discussion->name)) {
1074
    if (empty($discussion)) {
1075
        $discussion = new stdClass();
1076
    }
1077
    $discussion->name = $forum->name;
1078
}
1079
 
1080
$strdiscussionname = '';
1081
if ('single' == $forumentity->get_type()) {
1082
    // There is only one discussion thread for this forum type. We should
1083
    // not show the discussion name (same as forum name in this case) in
1084
    // the breadcrumbs.
1085
    $strdiscussionname = '';
1086
} else if (!empty($discussionentity)) {
1087
    // Show the discussion name in the breadcrumbs.
1088
    $strdiscussionname = format_string($discussionentity->get_name()) . ': ';
1089
}
1090
 
1091
$forcefocus = empty($reply) ? null : 'message';
1092
 
1093
if (!empty($discussion->id)) {
1094
    $PAGE->navbar->add($titlesubject, $urlfactory->get_discussion_view_url_from_discussion($discussionentity));
1095
}
1096
 
1097
if ($edit) {
1098
    $PAGE->navbar->add(get_string('editdiscussiontopic', 'forum'), $PAGE->url);
1099
} else if ($reply) {
1100
    $PAGE->navbar->add(get_string('addreply', 'forum'));
1101
} else {
1102
    $PAGE->navbar->add(get_string('addanewdiscussion', 'forum'), $PAGE->url);
1103
}
1104
 
1105
$PAGE->set_title("{$course->shortname}: {$strdiscussionname}{$titlesubject}");
1106
$PAGE->set_heading($course->fullname);
1107
$PAGE->set_secondary_active_tab("modulepage");
1108
$activityheaderconfig['hidecompletion'] = true;
1109
$activityheaderconfig['description'] = '';
1110
 
1111
// Remove the activity description.
1112
$PAGE->activityheader->set_attrs($activityheaderconfig);
1113
echo $OUTPUT->header();
1114
 
1115
if ($edit) {
1116
    echo $OUTPUT->heading(get_string('editdiscussiontopic', 'forum'), 2);
1117
} else if ($reply) {
1118
    echo $OUTPUT->heading(get_string('replypostdiscussion', 'forum'), 2);
1119
} else {
1120
    echo $OUTPUT->heading(get_string('addanewdiscussion', 'forum'), 2);
1121
}
1122
 
1123
// Checkup.
1124
if (!empty($parententity) && !$capabilitymanager->can_view_post($USER, $discussionentity, $parententity)) {
1125
    throw new \moodle_exception('cannotreply', 'forum');
1126
}
1127
 
1128
if (empty($parententity) && empty($edit) && !$capabilitymanager->can_create_discussions($USER, $groupid)) {
1129
    throw new \moodle_exception('cannotcreatediscussion', 'forum');
1130
}
1131
 
1132
if (!empty($discussionentity) && 'qanda' == $forumentity->get_type()) {
1133
    $displaywarning = $capabilitymanager->must_post_before_viewing_discussion($USER, $discussionentity);
1134
    $displaywarning = $displaywarning && !forum_user_has_posted($forumentity->get_id(), $discussionentity->get_id(), $USER->id);
1135
    if ($displaywarning) {
1136
        echo $OUTPUT->notification(get_string('qandanotify', 'forum'));
1137
    }
1138
}
1139
 
1140
// If there is a warning message and we are not editing a post we need to handle the warning.
1141
if (!empty($thresholdwarning) && !$edit) {
1142
    // Here we want to throw an exception if they are no longer allowed to post.
1143
    forum_check_blocking_threshold($thresholdwarning);
1144
}
1145
 
1146
if (!empty($parententity)) {
1147
    $postentities = [$parententity];
1148
 
1149
    if (empty($post->edit)) {
1150
        if ('qanda' != $forumentity->get_type() || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
1151
            $replies = $postvault->get_replies_to_post(
1222 ariadna 1152
                $USER,
1153
                $parententity,
1154
                $capabilitymanager->can_view_any_private_reply($USER),
1155
                'created ASC'
1156
            );
1 efrain 1157
            $postentities = array_merge($postentities, $replies);
1158
        }
1159
    }
1160
 
1161
    $rendererfactory = mod_forum\local\container::get_renderer_factory();
1162
    $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_THREADED, true);
1163
    echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
1164
}
1165
 
1166
// Call print disclosure for enabled plagiarism plugins.
1167
if (!empty($CFG->enableplagiarism)) {
1222 ariadna 1168
    require_once($CFG->libdir . '/plagiarismlib.php');
1 efrain 1169
    echo plagiarism_print_disclosure($cm->id);
1170
}
1171
 
1172
if (!empty($formheading)) {
1173
    echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1174
}
1175
 
1176
if (!empty($postentity)) {
1177
    $data = (object) [
1178
        'tags' => core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $postentity->get_id())
1179
    ];
1180
    $mformpost->set_data($data);
1181
}
1182
 
1183
$mformpost->display();
1184
 
1185
echo $OUTPUT->footer();