MDL-48720 mod_forum: Only copy discussion preferences when moving posts
[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
65bcf17b 27 require_once('../../config.php');
65bcf17b 28
83da3d28 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
87b0b499 33 $mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
83da3d28 34 $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
501cdbd8 35
a6855934 36 $url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
8a876913
SH
37 if ($parent !== 0) {
38 $url->param('parent', $parent);
39 }
8a876913 40 $PAGE->set_url($url);
d3558659 41
01d0aceb 42 $discussion = $DB->get_record('forum_discussions', array('id' => $d), '*', MUST_EXIST);
74df2951 43 $course = $DB->get_record('course', array('id' => $discussion->course), '*', MUST_EXIST);
01d0aceb
SH
44 $forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
45 $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
68258534 46
bf553d9e 47 require_course_login($course, true, $cm);
50e07a49 48
4cabf99f 49 // move this down fix for MDL-6926
01d0aceb 50 require_once($CFG->dirroot.'/mod/forum/lib.php');
4cabf99f 51
bf0f06b1 52 $modcontext = context_module::instance($cm->id);
65bcf17b 53 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
54
9e86f2e7
AD
55 if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->rsstype && $forum->rssarticles) {
56 require_once("$CFG->libdir/rsslib.php");
57
7a241d81 58 $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': ' . format_string($forum->name);
43b92251 59 rss_add_http_header($modcontext, 'mod_forum', $forum, $rsstitle);
9e86f2e7
AD
60 }
61
03574650 62 // Move discussion if requested.
65bcf17b 63 if ($move > 0 and confirm_sesskey()) {
a5f77de6 64 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
65bcf17b 65
03574650
AN
66 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
67 print_error('cannotmovetonotexist', 'forum', $return);
68 }
69
65bcf17b 70 require_capability('mod/forum:movediscussions', $modcontext);
1fc49f00 71
65bcf17b 72 if ($forum->type == 'single') {
12e57b92 73 print_error('cannotmovefromsingleforum', 'forum', $return);
83da3d28 74 }
65bcf17b 75
4e445355 76 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
12e57b92 77 print_error('cannotmovetonotexist', 'forum', $return);
83da3d28 78 }
7c900d5d 79
5fad08b4
CF
80 if ($forumto->type == 'single') {
81 print_error('cannotmovetosingleforum', 'forum', $return);
82 }
83
8270f0d0 84 // Get target forum cm and check it is visible to current user.
85 $modinfo = get_fast_modinfo($course);
86 $forums = $modinfo->get_instances_of('forum');
87 if (!array_key_exists($forumto->id, $forums)) {
12e57b92 88 print_error('cannotmovetonotfound', 'forum', $return);
65bcf17b 89 }
8270f0d0 90 $cmto = $forums[$forumto->id];
91 if (!$cmto->uservisible) {
12e57b92 92 print_error('cannotmovenotvisible', 'forum', $return);
1fc49f00 93 }
65bcf17b 94
71595d00
DP
95 $destinationctx = context_module::instance($cmto->id);
96 require_capability('mod/forum:startdiscussion', $destinationctx);
ee8d825f 97
0faf6791 98 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
9146b979 99 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
65bcf17b 100 }
03574650
AN
101 // For each subscribed user in this forum and discussion, copy over per-discussion subscriptions if required.
102 $discussiongroup = $discussion->groupid == -1 ? 0 : $discussion->groupid;
c87b87e6
AN
103 $potentialsubscribers = \mod_forum\subscriptions::fetch_subscribed_users(
104 $forum,
03574650 105 $discussiongroup,
c87b87e6 106 $modcontext,
0603df04
AN
107 'u.id',
108 true
03574650
AN
109 );
110
111 // Pre-seed the subscribed_discussion caches.
112 // Firstly for the forum being moved to.
113 \mod_forum\subscriptions::fill_subscription_cache($forumto->id);
114 // And also for the discussion being moved.
115 \mod_forum\subscriptions::fill_subscription_cache($forum->id);
116 $subscriptionchanges = array();
eb451c79 117 $subscriptiontime = time();
03574650
AN
118 foreach ($potentialsubscribers as $subuser) {
119 $userid = $subuser->id;
4238983e 120 $targetsubscription = \mod_forum\subscriptions::is_subscribed($userid, $forumto, null, $cmto);
9ddb3766
AN
121 $discussionsubscribed = \mod_forum\subscriptions::is_subscribed($userid, $forum, $discussion->id);
122 $forumsubscribed = \mod_forum\subscriptions::is_subscribed($userid, $forum);
123
124 if ($forumsubscribed && !$discussionsubscribed && $targetsubscription) {
125 // The user has opted out of this discussion and the move would cause them to receive notifications again.
126 // Ensure they are unsubscribed from the discussion still.
127 $subscriptionchanges[$userid] = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
128 } else if (!$forumsubscribed && $discussionsubscribed && !$targetsubscription) {
129 // The user has opted into this discussion and would otherwise not receive the subscription after the move.
130 // Ensure they are subscribed to the discussion still.
131 $subscriptionchanges[$userid] = $subscriptiontime;
03574650
AN
132 }
133 }
134
4e445355 135 $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
136 $DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
71595d00 137
03574650
AN
138 // Delete the existing per-discussion subscriptions and replace them with the newly calculated ones.
139 $DB->delete_records('forum_discussion_subs', array('discussion' => $discussion->id));
140 $newdiscussion = clone $discussion;
141 $newdiscussion->forum = $forumto->id;
142 foreach ($subscriptionchanges as $userid => $preference) {
eb451c79 143 if ($preference != \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED) {
71bc139d
AN
144 // Users must have viewdiscussion to a discussion.
145 if (has_capability('mod/forum:viewdiscussion', $destinationctx, $userid)) {
146 \mod_forum\subscriptions::subscribe_user_to_discussion($userid, $newdiscussion, $destinationctx);
147 }
03574650
AN
148 } else {
149 \mod_forum\subscriptions::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx);
150 }
151 }
152
71595d00
DP
153 $params = array(
154 'context' => $destinationctx,
155 'objectid' => $discussion->id,
156 'other' => array(
157 'fromforumid' => $forum->id,
158 'toforumid' => $forumto->id,
159 )
160 );
161 $event = \mod_forum\event\discussion_moved::create($params);
162 $event->add_record_snapshot('forum_discussions', $discussion);
163 $event->add_record_snapshot('forum', $forum);
164 $event->add_record_snapshot('forum', $forumto);
165 $event->trigger();
65bcf17b 166
a74bea12 167 // Delete the RSS files for the 2 forums to force regeneration of the feeds
54eb02a4 168 require_once($CFG->dirroot.'/mod/forum/rsslib.php');
a74bea12
AD
169 forum_rss_delete_file($forum);
170 forum_rss_delete_file($forumto);
65bcf17b 171
e4c5abdc 172 redirect($return.'&moved=-1&sesskey='.sesskey());
1fc49f00 173 }
174
71595d00
DP
175 $params = array(
176 'context' => $modcontext,
177 'objectid' => $discussion->id,
178 );
179 $event = \mod_forum\event\discussion_viewed::create($params);
180 $event->add_record_snapshot('forum_discussions', $discussion);
181 $event->add_record_snapshot('forum', $forum);
182 $event->trigger();
501cdbd8 183
184 unset($SESSION->fromdiscussion);
185
279826e2 186 if ($mode) {
f2d042c4 187 set_user_preference('forum_displaymode', $mode);
279826e2 188 }
501cdbd8 189
acb50c1b 190 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 191
e92ea3d8 192 if ($parent) {
65bcf17b 193 // If flat AND parent, then force nested display this time
194 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
195 $displaymode = FORUM_MODE_NESTED;
e92ea3d8 196 }
197 } else {
501cdbd8 198 $parent = $discussion->firstpost;
501cdbd8 199 }
200
11b0c469 201 if (! $post = forum_get_post_full($parent)) {
12e57b92 202 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
501cdbd8 203 }
204
737aefb4
MN
205 if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
206 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
f37da850 207 }
208
65bcf17b 209 if ($mark == 'read' or $mark == 'unread') {
90f4745c 210 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
65bcf17b 211 if ($mark == 'read') {
90f4745c 212 forum_tp_add_read_record($USER->id, $postid);
65bcf17b 213 } else {
214 // unread
215 forum_tp_delete_read_records($USER->id, $postid);
216 }
217 }
218 }
cef1ce6a 219
6f1cc8d6 220 $searchform = forum_search_form($course);
39790bd8 221
01d0aceb
SH
222 $forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
223 if (empty($forumnode)) {
224 $forumnode = $PAGE->navbar;
225 } else {
c89fa3fe 226 $forumnode->make_active();
87a462ea 227 }
01d0aceb 228 $node = $forumnode->add(format_string($discussion->name), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
c89fa3fe 229 $node->display = false;
01d0aceb
SH
230 if ($node && $post->id != $discussion->firstpost) {
231 $node->add(format_string($post->subject), $PAGE->url);
232 }
233
15ca5e5e 234 $PAGE->set_title("$course->shortname: ".format_string($discussion->name));
235 $PAGE->set_heading($course->fullname);
236 $PAGE->set_button($searchform);
d7862878
FM
237 $renderer = $PAGE->get_renderer('mod_forum');
238
15ca5e5e 239 echo $OUTPUT->header();
ebfb73db 240
517e1782
AN
241 echo $OUTPUT->heading(format_string($forum->name), 2);
242 echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname');
243
ddb4a31d
AN
244 // is_guest should be used here as this also checks whether the user is a guest in the current course.
245 // Guests and visitors cannot subscribe - only enrolled users.
246 if ((!is_guest($modcontext, $USER) && isloggedin()) && has_capability('mod/forum:viewdiscussion', $modcontext)) {
ebfb73db
AN
247 // Discussion subscription.
248 if (\mod_forum\subscriptions::is_subscribable($forum)) {
517e1782
AN
249 echo html_writer::div(
250 forum_get_discussion_subscription_icon($forum, $post->discussion, null, true),
251 'discussionsubscription'
252 );
548453ec 253 echo forum_get_discussion_subscription_icon_preloaders();
ebfb73db
AN
254 }
255 }
ebfb73db 256
c6d691dc 257
9197e147 258/// Check to see if groups are being used in this forum
259/// If so, make sure the current person is allowed to see this discussion
8b79a625 260/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 261
8e18e1ec
PS
262 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
263 if (!$canreply and $forum->type !== 'news') {
264 if (isguestuser() or !isloggedin()) {
265 $canreply = true;
266 }
267 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
268 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
269 // normal users with temporary guest access see this link too, they are asked to enrol instead
270 $canreply = enrol_selfenrol_available($course->id);
271 }
9197e147 272 }
273
d7862878
FM
274 // Output the links to neighbour discussions.
275 $neighbours = forum_get_discussion_neighbours($cm, $discussion);
8744529f
JC
276 $neighbourlinks = $renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
277 echo $neighbourlinks;
d7862878 278
c6d691dc 279/// Print the controls across the top
c00037cd 280 echo '<div class="discussioncontrols clearfix">';
c6d691dc 281
6708a1f5 282 if (!empty($CFG->enableportfolios) && has_capability('mod/forum:exportdiscussion', $modcontext)) {
24ba58ee 283 require_once($CFG->libdir.'/portfoliolib.php');
0d06b6fd 284 $button = new portfolio_add_button();
37743241 285 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), 'mod_forum');
f3cc571a
DM
286 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
287 $buttonextraclass = '';
288 if (empty($button)) {
289 // no portfolio plugin available.
290 $button = '&nbsp;';
291 $buttonextraclass = ' noavailable';
292 }
293 echo html_writer::tag('div', $button, array('class' => 'discussioncontrol exporttoportfolio'.$buttonextraclass));
f357d2f8
DM
294 } else {
295 echo html_writer::tag('div', '&nbsp;', array('class'=>'discussioncontrol nullcontrol'));
10ae55f9 296 }
297
f357d2f8
DM
298 // groups selector not needed here
299 echo '<div class="discussioncontrol displaymode">';
300 forum_print_mode_form($discussion->id, $displaymode);
301 echo "</div>";
302
cef1ce6a 303 if ($forum->type != 'single'
304 && has_capability('mod/forum:movediscussions', $modcontext)) {
65bcf17b 305
f357d2f8 306 echo '<div class="discussioncontrol movediscussion">';
cef1ce6a 307 // Popup menu to move discussions to other forums. The discussion in a
308 // single discussion forum can't be moved.
65bcf17b 309 $modinfo = get_fast_modinfo($course);
310 if (isset($modinfo->instances['forum'])) {
65bcf17b 311 $forummenu = array();
5fad08b4
CF
312 // Check forum types and eliminate simple discussions.
313 $forumcheck = $DB->get_records('forum', array('course' => $course->id),'', 'id, type');
65bcf17b 314 foreach ($modinfo->instances['forum'] as $forumcm) {
ee8d825f 315 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
bf0f06b1 316 context_module::instance($forumcm->id))) {
65bcf17b 317 continue;
fcc69042 318 }
65bcf17b 319 $section = $forumcm->sectionnum;
71a56e08 320 $sectionname = get_section_name($course, $section);
f1a3e072 321 if (empty($forummenu[$section])) {
7487c856 322 $forummenu[$section] = array($sectionname => array());
f1a3e072 323 }
5fad08b4
CF
324 $forumidcompare = $forumcm->instance != $forum->id;
325 $forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
326 if ($forumidcompare and $forumtypecheck) {
f1a3e072 327 $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
7487c856 328 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
1fc49f00 329 }
330 }
331 if (!empty($forummenu)) {
12a24e00 332 echo '<div class="movediscussionoption">';
15e48a1a
SM
333 $select = new url_select($forummenu, '',
334 array(''=>get_string("movethisdiscussionto", "forum")),
335 'forummenu', get_string('move'));
f1a3e072 336 echo $OUTPUT->render($select);
1fc49f00 337 echo "</div>";
338 }
339 }
12a24e00 340 echo "</div>";
1fc49f00 341 }
12a24e00
RW
342 echo '<div class="clearfloat">&nbsp;</div>';
343 echo "</div>";
1fc49f00 344
a4f495bf 345 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
39790bd8 346 $a = new stdClass();
65bcf17b 347 $a->blockafter = $forum->blockafter;
a4f495bf 348 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
9146b979 349 echo $OUTPUT->notification(get_string('thisforumisthrottled','forum',$a));
a4f495bf 350 }
351
0468976c 352 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 353 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
9146b979 354 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 355 }
356
65bcf17b 357 if ($move == -1 and confirm_sesskey()) {
9146b979 358 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
8de14dc7 359 }
360
65bcf17b 361 $canrate = has_capability('mod/forum:rate', $modcontext);
362 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 363
8744529f
JC
364 echo $neighbourlinks;
365
ebfb73db
AN
366 // Add the subscription toggle JS.
367 $PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
1adbd2c3 368
ebfb73db 369 echo $OUTPUT->footer();