MDL-64821 output: add wrapping class for ratings label
[moodle.git] / mod / forum / discuss.php
CommitLineData
1adbd2c3 1<?php
501cdbd8 2
8f685009
SH
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Displays a post, and all the posts below it.
20 * If no post is given, displays all posts in a discussion
21 *
01030f1b 22 * @package mod_forum
8f685009
SH
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
501cdbd8 26
5f9997ae 27require_once('../../config.php');
d3558659 28
5f9997ae
AN
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.
5f219cf1 35$pin = optional_param('pin', -1, PARAM_INT); // If set, pin or unpin this discussion.
68258534 36
5f9997ae
AN
37$url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
38if ($parent !== 0) {
39 $url->param('parent', $parent);
40}
41$PAGE->set_url($url);
50e07a49 42
615680c5
RW
43$vaultfactory = mod_forum\local\container::get_vault_factory();
44$discussionvault = $vaultfactory->get_discussion_vault();
45$discussion = $discussionvault->get_from_id($d);
46
47if (!$discussion) {
48 throw new \moodle_exception('Unable to find discussion with id ' . $discussionid);
49}
50
51$forumvault = $vaultfactory->get_forum_vault();
52$forum = $forumvault->get_from_id($discussion->get_forum_id());
53
54if (!$forum) {
55 throw new \moodle_exception('Unable to find forum with id ' . $discussion->get_forum_id());
56}
57
ac8c12c7
AN
58$course = $forum->get_course_record();
59$cm = $forum->get_course_module_record();
60
61require_course_login($course, true, $cm);
62
615680c5
RW
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();
66
67// Make sure we can render.
68if (!$capabilitymanager->can_view_discussions($USER)) {
69 throw new moodle_exception('noviewdiscussionspermission', 'mod_forum');
70}
71
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);
78
5f9997ae 79// move this down fix for MDL-6926
615680c5 80require_once($CFG->dirroot . '/mod/forum/lib.php');
9e86f2e7 81
615680c5 82$modcontext = $forum->get_context();
9e86f2e7 83
615680c5
RW
84if (
85 !empty($CFG->enablerssfeeds) &&
86 !empty($CFG->forum_enablerssfeeds) &&
87 $forum->get_rss_type() &&
88 $forum->get_rss_articles()
89) {
5f9997ae 90 require_once("$CFG->libdir/rsslib.php");
65bcf17b 91
615680c5
RW
92 $rsstitle = format_string(
93 $course->shortname,
94 true,
95 ['context' => context_course::instance($course->id)]
96 );
97 $rsstitle .= ': ' . format_string($forum->get_name());
98 rss_add_http_header($modcontext, 'mod_forum', $forumrecord, $rsstitle);
5f9997ae 99}
03574650 100
5f9997ae 101// Move discussion if requested.
615680c5
RW
102if ($move > 0 && confirm_sesskey()) {
103 $forumid = $forum->get_id();
104 $discussionid = $discussion->get_id();
105 $return = $discussionviewurl->out(false);
1fc49f00 106
615680c5 107 if (!$forumto = $DB->get_record('forum', ['id' => $move])) {
5f9997ae
AN
108 print_error('cannotmovetonotexist', 'forum', $return);
109 }
65bcf17b 110
615680c5
RW
111 if (!$capabilitymanager->can_move_discussions($USER)) {
112 if ($forum->get_type() == 'single') {
113 print_error('cannotmovefromsingleforum', 'forum', $return);
114 } else {
115 print_error('nopermissions', 'error', $return, get_capability_string('mod/forum:movediscussions'));
116 }
5f9997ae 117 }
65bcf17b 118
5f9997ae
AN
119 if ($forumto->type == 'single') {
120 print_error('cannotmovetosingleforum', 'forum', $return);
121 }
ee8d825f 122
5f9997ae
AN
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);
128 }
615680c5 129
5f9997ae
AN
130 $cmto = $forums[$forumto->id];
131 if (!$cmto->uservisible) {
132 print_error('cannotmovenotvisible', 'forum', $return);
133 }
03574650 134
5f9997ae
AN
135 $destinationctx = context_module::instance($cmto->id);
136 require_capability('mod/forum:startdiscussion', $destinationctx);
137
615680c5 138 if (!forum_move_attachments($discussionrecord, $forumid, $forumto->id)) {
5f9997ae
AN
139 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
140 }
141 // For each subscribed user in this forum and discussion, copy over per-discussion subscriptions if required.
615680c5 142 $discussiongroup = $discussion->get_group_id() == -1 ? 0 : $discussion->get_group_id();
5f9997ae 143 $potentialsubscribers = \mod_forum\subscriptions::fetch_subscribed_users(
615680c5 144 $forumrecord,
5f9997ae
AN
145 $discussiongroup,
146 $modcontext,
147 'u.id',
148 true
149 );
150
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.
615680c5
RW
155 \mod_forum\subscriptions::fill_subscription_cache($forumid);
156 $subscriptionchanges = [];
5f9997ae
AN
157 $subscriptiontime = time();
158 foreach ($potentialsubscribers as $subuser) {
159 $userid = $subuser->id;
160 $targetsubscription = \mod_forum\subscriptions::is_subscribed($userid, $forumto, null, $cmto);
615680c5
RW
161 $discussionsubscribed = \mod_forum\subscriptions::is_subscribed($userid, $forumrecord, $discussionid);
162 $forumsubscribed = \mod_forum\subscriptions::is_subscribed($userid, $forumrecord);
5f9997ae
AN
163
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;
03574650 172 }
5f9997ae 173 }
03574650 174
615680c5
RW
175 $DB->set_field('forum_discussions', 'forum', $forumto->id, ['id' => $discussionid]);
176 $DB->set_field('forum_read', 'forumid', $forumto->id, ['discussionid' => $discussionid]);
5f9997ae
AN
177
178 // Delete the existing per-discussion subscriptions and replace them with the newly calculated ones.
615680c5
RW
179 $DB->delete_records('forum_discussion_subs', ['discussion' => $discussionid]);
180 $newdiscussion = clone $discussionrecord;
5f9997ae
AN
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);
03574650 187 }
5f9997ae
AN
188 } else {
189 \mod_forum\subscriptions::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx);
03574650 190 }
1fc49f00 191 }
192
615680c5 193 $params = [
5f9997ae 194 'context' => $destinationctx,
615680c5
RW
195 'objectid' => $discussionid,
196 'other' => [
197 'fromforumid' => $forumid,
5f9997ae 198 'toforumid' => $forumto->id,
615680c5
RW
199 ]
200 ];
5f9997ae 201 $event = \mod_forum\event\discussion_moved::create($params);
615680c5
RW
202 $event->add_record_snapshot('forum_discussions', $discussionrecord);
203 $event->add_record_snapshot('forum', $forumrecord);
5f9997ae 204 $event->add_record_snapshot('forum', $forumto);
71595d00 205 $event->trigger();
501cdbd8 206
5f9997ae 207 // Delete the RSS files for the 2 forums to force regeneration of the feeds
615680c5
RW
208 require_once($CFG->dirroot . '/mod/forum/rsslib.php');
209 forum_rss_delete_file($forumrecord);
5f9997ae 210 forum_rss_delete_file($forumto);
501cdbd8 211
615680c5 212 redirect($return . '&move=-1&sesskey=' . sesskey());
5f9997ae 213}
87b007b4
CF
214// Pin or unpin discussion if requested.
215if ($pin !== -1 && confirm_sesskey()) {
615680c5
RW
216 if (!$capabilitymanager->can_pin_discussions($USER)) {
217 print_error('nopermissions', 'error', $return, get_capability_string('mod/forum:pindiscussions'));
218 }
87b007b4 219
615680c5 220 $params = ['context' => $modcontext, 'objectid' => $discussion->get_id(), 'other' => ['forumid' => $forum->get_id()]];
87b007b4
CF
221
222 switch ($pin) {
223 case FORUM_DISCUSSION_PINNED:
5f219cf1 224 // Pin the discussion and trigger discussion pinned event.
615680c5 225 forum_discussion_pin($modcontext, $forumrecord, $discussionrecord);
87b007b4
CF
226 break;
227 case FORUM_DISCUSSION_UNPINNED:
5f219cf1 228 // Unpin the discussion and trigger discussion unpinned event.
615680c5 229 forum_discussion_unpin($modcontext, $forumrecord, $discussionrecord);
87b007b4
CF
230 break;
231 default:
5f219cf1 232 echo $OUTPUT->notification("Invalid value when attempting to pin/unpin discussion");
87b007b4
CF
233 break;
234 }
235
615680c5 236 redirect($discussionviewurl->out(false));
87b007b4
CF
237}
238
1546987b 239// Trigger discussion viewed event.
615680c5 240forum_discussion_view($modcontext, $forumrecord, $discussionrecord);
501cdbd8 241
5f9997ae 242unset($SESSION->fromdiscussion);
501cdbd8 243
5f9997ae
AN
244if ($mode) {
245 set_user_preference('forum_displaymode', $mode);
246}
f37da850 247
5f9997ae 248$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
39790bd8 249
5f9997ae
AN
250if ($parent) {
251 // If flat AND parent, then force nested display this time
252 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
253 $displaymode = FORUM_MODE_NESTED;
01d0aceb 254 }
5f9997ae 255} else {
615680c5 256 $parent = $discussion->get_first_post_id();
5f9997ae
AN
257}
258
615680c5
RW
259$postvault = $vaultfactory->get_post_vault();
260if (!$post = $postvault->get_from_id($parent)) {
261 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f={$forum->get_id()}");
5f9997ae
AN
262}
263
615680c5
RW
264if (!$capabilitymanager->can_view_post($USER, $discussion, $post)) {
265 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id={$forum->get_id()}");
5f9997ae
AN
266}
267
615680c5
RW
268$istracked = forum_tp_is_tracked($forumrecord, $USER);
269if ($mark == 'read'|| $mark == 'unread') {
270 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forumrecord) && $istracked) {
5f9997ae
AN
271 if ($mark == 'read') {
272 forum_tp_add_read_record($USER->id, $postid);
273 } else {
274 // unread
275 forum_tp_delete_read_records($USER->id, $postid);
ebfb73db
AN
276 }
277 }
5f9997ae
AN
278}
279
280$searchform = forum_search_form($course);
281
282$forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
283if (empty($forumnode)) {
284 $forumnode = $PAGE->navbar;
285} else {
286 $forumnode->make_active();
287}
615680c5 288$node = $forumnode->add(format_string($discussion->get_name()), $discussionviewurl);
5f9997ae 289$node->display = false;
615680c5
RW
290if ($node && $post->get_id() != $discussion->get_first_post_id()) {
291 $node->add(format_string($post->get_subject()), $PAGE->url);
5f9997ae
AN
292}
293
615680c5 294$PAGE->set_title("$course->shortname: " . format_string($discussion->get_name()));
5f9997ae 295$PAGE->set_heading($course->fullname);
615680c5 296$PAGE->set_button(forum_search_form($course));
5f9997ae
AN
297
298echo $OUTPUT->header();
615680c5
RW
299echo $OUTPUT->heading(format_string($forum->get_name()), 2);
300echo $OUTPUT->heading(format_string($discussion->get_name()), 3, 'discussionname');
5f9997ae 301
615680c5
RW
302$rendererfactory = mod_forum\local\container::get_renderer_factory();
303$discussionrenderer = $rendererfactory->get_discussion_renderer($forum, $discussion, $displaymode);
304$orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
bc4c7337 305$replies = $postvault->get_replies_to_post($USER, $post, $capabilitymanager->can_view_any_private_reply($USER), $orderpostsby);
615680c5
RW
306$postids = array_map(function($post) {
307 return $post->get_id();
308}, array_merge([$post], array_values($replies)));
098d27d4 309
5f9997ae 310if ($move == -1 and confirm_sesskey()) {
615680c5
RW
311 $forumname = format_string($forum->get_name(), true);
312 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', $forumname), 'notifysuccess');
5f9997ae 313}
8de14dc7 314
615680c5 315echo $discussionrenderer->render($USER, $post, $replies);
5f9997ae 316echo $OUTPUT->footer();
615680c5
RW
317
318if ($istracked && !$CFG->forum_usermarksread) {
319 forum_tp_mark_posts_read($USER, $postids);
320}