3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Displays a post, and all the posts below it.
20 * If no post is given, displays all posts in a discussion
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('../../config.php');
29 $d = required_param('d', PARAM_INT
); // Discussion ID
30 $parent = optional_param('parent', 0, PARAM_INT
); // If set, then display this post and all children.
31 $mode = optional_param('mode', 0, PARAM_INT
); // If set, changes the layout of the thread
32 $move = optional_param('move', 0, PARAM_INT
); // If set, moves this discussion to another forum
33 $mark = optional_param('mark', '', PARAM_ALPHA
); // Used for tracking read posts if user initiated.
34 $postid = optional_param('postid', 0, PARAM_INT
); // Used for tracking read posts if user initiated.
35 $pin = optional_param('pin', -1, PARAM_INT
); // If set, pin or unpin this discussion.
37 $url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
39 $url->param('parent', $parent);
43 $vaultfactory = mod_forum\local\container
::get_vault_factory();
44 $discussionvault = $vaultfactory->get_discussion_vault();
45 $discussion = $discussionvault->get_from_id($d);
48 throw new \
moodle_exception('errordiscussionnotfound', 'mod_forum');
51 $forumvault = $vaultfactory->get_forum_vault();
52 $forum = $forumvault->get_from_id($discussion->get_forum_id());
55 throw new \
moodle_exception('errorforumnotfound', 'mod_forum');
58 $course = $forum->get_course_record();
59 $cm = $forum->get_course_module_record();
61 require_course_login($course, true, $cm);
63 $managerfactory = mod_forum\local\container
::get_manager_factory();
64 $capabilitymanager = $managerfactory->get_capability_manager($forum);
65 $urlfactory = mod_forum\local\container
::get_url_factory();
67 // Make sure we can render.
68 if (!$capabilitymanager->can_view_discussions($USER)) {
69 throw new moodle_exception('noviewdiscussionspermission', 'mod_forum');
72 $datamapperfactory = mod_forum\local\container
::get_legacy_data_mapper_factory();
73 $forumdatamapper = $datamapperfactory->get_forum_data_mapper();
74 $forumrecord = $forumdatamapper->to_legacy_object($forum);
75 $discussiondatamapper = $datamapperfactory->get_discussion_data_mapper();
76 $discussionrecord = $discussiondatamapper->to_legacy_object($discussion);
77 $discussionviewurl = $urlfactory->get_discussion_view_url_from_discussion($discussion);
79 // move this down fix for MDL-6926
80 require_once($CFG->dirroot
. '/mod/forum/lib.php');
82 $modcontext = $forum->get_context();
85 !empty($CFG->enablerssfeeds
) &&
86 !empty($CFG->forum_enablerssfeeds
) &&
87 $forum->get_rss_type() &&
88 $forum->get_rss_articles()
90 require_once("$CFG->libdir/rsslib.php");
92 $rsstitle = format_string(
95 ['context' => context_course
::instance($course->id
)]
97 $rsstitle .= ': ' . format_string($forum->get_name());
98 rss_add_http_header($modcontext, 'mod_forum', $forumrecord, $rsstitle);
101 // Move discussion if requested.
102 if ($move > 0 && confirm_sesskey()) {
103 $forumid = $forum->get_id();
104 $discussionid = $discussion->get_id();
105 $return = $discussionviewurl->out(false);
107 if (!$forumto = $DB->get_record('forum', ['id' => $move])) {
108 print_error('cannotmovetonotexist', 'forum', $return);
111 if (!$capabilitymanager->can_move_discussions($USER)) {
112 if ($forum->get_type() == 'single') {
113 print_error('cannotmovefromsingleforum', 'forum', $return);
115 print_error('nopermissions', 'error', $return, get_capability_string('mod/forum:movediscussions'));
119 if ($forumto->type
== 'single') {
120 print_error('cannotmovetosingleforum', 'forum', $return);
123 // Get target forum cm and check it is visible to current user.
124 $modinfo = get_fast_modinfo($course);
125 $forums = $modinfo->get_instances_of('forum');
126 if (!array_key_exists($forumto->id
, $forums)) {
127 print_error('cannotmovetonotfound', 'forum', $return);
130 $cmto = $forums[$forumto->id
];
131 if (!$cmto->uservisible
) {
132 print_error('cannotmovenotvisible', 'forum', $return);
135 $destinationctx = context_module
::instance($cmto->id
);
136 require_capability('mod/forum:startdiscussion', $destinationctx);
138 if (!forum_move_attachments($discussionrecord, $forumid, $forumto->id
)) {
139 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
141 // For each subscribed user in this forum and discussion, copy over per-discussion subscriptions if required.
142 $discussiongroup = $discussion->get_group_id() == -1 ?
0 : $discussion->get_group_id();
143 $potentialsubscribers = \mod_forum\subscriptions
::fetch_subscribed_users(
151 // Pre-seed the subscribed_discussion caches.
152 // Firstly for the forum being moved to.
153 \mod_forum\subscriptions
::fill_subscription_cache($forumto->id
);
154 // And also for the discussion being moved.
155 \mod_forum\subscriptions
::fill_subscription_cache($forumid);
156 $subscriptionchanges = [];
157 $subscriptiontime = time();
158 foreach ($potentialsubscribers as $subuser) {
159 $userid = $subuser->id
;
160 $targetsubscription = \mod_forum\subscriptions
::is_subscribed($userid, $forumto, null, $cmto);
161 $discussionsubscribed = \mod_forum\subscriptions
::is_subscribed($userid, $forumrecord, $discussionid);
162 $forumsubscribed = \mod_forum\subscriptions
::is_subscribed($userid, $forumrecord);
164 if ($forumsubscribed && !$discussionsubscribed && $targetsubscription) {
165 // The user has opted out of this discussion and the move would cause them to receive notifications again.
166 // Ensure they are unsubscribed from the discussion still.
167 $subscriptionchanges[$userid] = \mod_forum\subscriptions
::FORUM_DISCUSSION_UNSUBSCRIBED
;
168 } else if (!$forumsubscribed && $discussionsubscribed && !$targetsubscription) {
169 // The user has opted into this discussion and would otherwise not receive the subscription after the move.
170 // Ensure they are subscribed to the discussion still.
171 $subscriptionchanges[$userid] = $subscriptiontime;
175 $DB->set_field('forum_discussions', 'forum', $forumto->id
, ['id' => $discussionid]);
176 $DB->set_field('forum_read', 'forumid', $forumto->id
, ['discussionid' => $discussionid]);
178 // Delete the existing per-discussion subscriptions and replace them with the newly calculated ones.
179 $DB->delete_records('forum_discussion_subs', ['discussion' => $discussionid]);
180 $newdiscussion = clone $discussionrecord;
181 $newdiscussion->forum
= $forumto->id
;
182 foreach ($subscriptionchanges as $userid => $preference) {
183 if ($preference != \mod_forum\subscriptions
::FORUM_DISCUSSION_UNSUBSCRIBED
) {
184 // Users must have viewdiscussion to a discussion.
185 if (has_capability('mod/forum:viewdiscussion', $destinationctx, $userid)) {
186 \mod_forum\subscriptions
::subscribe_user_to_discussion($userid, $newdiscussion, $destinationctx);
189 \mod_forum\subscriptions
::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx);
194 'context' => $destinationctx,
195 'objectid' => $discussionid,
197 'fromforumid' => $forumid,
198 'toforumid' => $forumto->id
,
201 $event = \mod_forum\event\discussion_moved
::create($params);
202 $event->add_record_snapshot('forum_discussions', $discussionrecord);
203 $event->add_record_snapshot('forum', $forumrecord);
204 $event->add_record_snapshot('forum', $forumto);
207 // Delete the RSS files for the 2 forums to force regeneration of the feeds
208 require_once($CFG->dirroot
. '/mod/forum/rsslib.php');
209 forum_rss_delete_file($forumrecord);
210 forum_rss_delete_file($forumto);
212 redirect($return . '&move=-1&sesskey=' . sesskey());
214 // Pin or unpin discussion if requested.
215 if ($pin !== -1 && confirm_sesskey()) {
216 if (!$capabilitymanager->can_pin_discussions($USER)) {
217 print_error('nopermissions', 'error', $return, get_capability_string('mod/forum:pindiscussions'));
220 $params = ['context' => $modcontext, 'objectid' => $discussion->get_id(), 'other' => ['forumid' => $forum->get_id()]];
223 case FORUM_DISCUSSION_PINNED
:
224 // Pin the discussion and trigger discussion pinned event.
225 forum_discussion_pin($modcontext, $forumrecord, $discussionrecord);
227 case FORUM_DISCUSSION_UNPINNED
:
228 // Unpin the discussion and trigger discussion unpinned event.
229 forum_discussion_unpin($modcontext, $forumrecord, $discussionrecord);
232 echo $OUTPUT->notification("Invalid value when attempting to pin/unpin discussion");
236 redirect($discussionviewurl->out(false));
239 // Trigger discussion viewed event.
240 forum_discussion_view($modcontext, $forumrecord, $discussionrecord);
242 unset($SESSION->fromdiscussion
);
244 $saveddisplaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode
);
247 $displaymode = $mode;
249 $displaymode = $saveddisplaymode;
252 if (get_user_preferences('forum_useexperimentalui', false)) {
253 if ($displaymode == FORUM_MODE_NESTED
) {
254 $displaymode = FORUM_MODE_NESTED_V2
;
257 if ($displaymode == FORUM_MODE_NESTED_V2
) {
258 $displaymode = FORUM_MODE_NESTED
;
262 if ($displaymode != $saveddisplaymode) {
263 set_user_preference('forum_displaymode', $displaymode);
267 // If flat AND parent, then force nested display this time
268 if ($displaymode == FORUM_MODE_FLATOLDEST
or $displaymode == FORUM_MODE_FLATNEWEST
) {
269 $displaymode = FORUM_MODE_NESTED
;
272 $parent = $discussion->get_first_post_id();
275 $postvault = $vaultfactory->get_post_vault();
276 if (!$post = $postvault->get_from_id($parent)) {
277 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f={$forum->get_id()}");
280 if (!$capabilitymanager->can_view_post($USER, $discussion, $post)) {
281 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id={$forum->get_id()}");
284 $istracked = forum_tp_is_tracked($forumrecord, $USER);
285 if ($mark == 'read'||
$mark == 'unread') {
286 if ($CFG->forum_usermarksread
&& forum_tp_can_track_forums($forumrecord) && $istracked) {
287 if ($mark == 'read') {
288 forum_tp_add_read_record($USER->id
, $postid);
291 forum_tp_delete_read_records($USER->id
, $postid);
296 $searchform = forum_search_form($course);
298 $forumnode = $PAGE->navigation
->find($cm->id
, navigation_node
::TYPE_ACTIVITY
);
299 if (empty($forumnode)) {
300 $forumnode = $PAGE->navbar
;
302 $forumnode->make_active();
304 $node = $forumnode->add(format_string($discussion->get_name()), $discussionviewurl);
305 $node->display
= false;
306 if ($node && $post->get_id() != $discussion->get_first_post_id()) {
307 $node->add(format_string($post->get_subject()), $PAGE->url
);
310 $isnestedv2displaymode = $displaymode == FORUM_MODE_NESTED_V2
;
311 $PAGE->set_title("$course->shortname: " . format_string($discussion->get_name()));
312 $PAGE->set_heading($course->fullname
);
313 if ($isnestedv2displaymode) {
314 $PAGE->add_body_class('nested-v2-display-mode reset-style');
315 $settingstrigger = $OUTPUT->render_from_template('mod_forum/settings_drawer_trigger', null);
316 $PAGE->add_header_action($settingstrigger);
318 $PAGE->set_button(forum_search_form($course));
321 echo $OUTPUT->header();
322 if (!$isnestedv2displaymode) {
323 echo $OUTPUT->heading(format_string($forum->get_name()), 2);
324 echo $OUTPUT->heading(format_string($discussion->get_name()), 3, 'discussionname');
327 $rendererfactory = mod_forum\local\container
::get_renderer_factory();
328 $discussionrenderer = $rendererfactory->get_discussion_renderer($forum, $discussion, $displaymode);
329 $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ?
'created DESC' : 'created ASC';
330 $replies = $postvault->get_replies_to_post($USER, $post, $capabilitymanager->can_view_any_private_reply($USER), $orderpostsby);
332 if ($move == -1 and confirm_sesskey()) {
333 $forumname = format_string($forum->get_name(), true);
334 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', $forumname), 'notifysuccess');
337 echo $discussionrenderer->render($USER, $post, $replies);
338 echo $OUTPUT->footer();
340 if ($istracked && !$CFG->forum_usermarksread
) {
341 if ($displaymode == FORUM_MODE_THREADED
) {
342 forum_tp_add_read_record($USER->id
, $post->get_id());
344 $postids = array_map(function($post) {
345 return $post->get_id();
346 }, array_merge([$post], array_values($replies)));
347 forum_tp_mark_posts_read($USER, $postids);