Proyectos de Subversion Moodle

Rev

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