MDL-1626 mod_forum: Update user interface to reflect per-discussion subscriptions
[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
65bcf17b 62/// move discussion if requested
63 if ($move > 0 and confirm_sesskey()) {
a5f77de6 64 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
65bcf17b 65
66 require_capability('mod/forum:movediscussions', $modcontext);
1fc49f00 67
65bcf17b 68 if ($forum->type == 'single') {
12e57b92 69 print_error('cannotmovefromsingleforum', 'forum', $return);
83da3d28 70 }
65bcf17b 71
4e445355 72 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
12e57b92 73 print_error('cannotmovetonotexist', 'forum', $return);
83da3d28 74 }
7c900d5d 75
5fad08b4
CF
76 if ($forumto->type == 'single') {
77 print_error('cannotmovetosingleforum', 'forum', $return);
78 }
79
8270f0d0 80 // Get target forum cm and check it is visible to current user.
81 $modinfo = get_fast_modinfo($course);
82 $forums = $modinfo->get_instances_of('forum');
83 if (!array_key_exists($forumto->id, $forums)) {
12e57b92 84 print_error('cannotmovetonotfound', 'forum', $return);
65bcf17b 85 }
8270f0d0 86 $cmto = $forums[$forumto->id];
87 if (!$cmto->uservisible) {
12e57b92 88 print_error('cannotmovenotvisible', 'forum', $return);
1fc49f00 89 }
65bcf17b 90
71595d00
DP
91 $destinationctx = context_module::instance($cmto->id);
92 require_capability('mod/forum:startdiscussion', $destinationctx);
ee8d825f 93
0faf6791 94 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
9146b979 95 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
65bcf17b 96 }
4e445355 97 $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
98 $DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
71595d00
DP
99
100 $params = array(
101 'context' => $destinationctx,
102 'objectid' => $discussion->id,
103 'other' => array(
104 'fromforumid' => $forum->id,
105 'toforumid' => $forumto->id,
106 )
107 );
108 $event = \mod_forum\event\discussion_moved::create($params);
109 $event->add_record_snapshot('forum_discussions', $discussion);
110 $event->add_record_snapshot('forum', $forum);
111 $event->add_record_snapshot('forum', $forumto);
112 $event->trigger();
65bcf17b 113
a74bea12 114 // Delete the RSS files for the 2 forums to force regeneration of the feeds
54eb02a4 115 require_once($CFG->dirroot.'/mod/forum/rsslib.php');
a74bea12
AD
116 forum_rss_delete_file($forum);
117 forum_rss_delete_file($forumto);
65bcf17b 118
e4c5abdc 119 redirect($return.'&moved=-1&sesskey='.sesskey());
1fc49f00 120 }
121
71595d00
DP
122 $params = array(
123 'context' => $modcontext,
124 'objectid' => $discussion->id,
125 );
126 $event = \mod_forum\event\discussion_viewed::create($params);
127 $event->add_record_snapshot('forum_discussions', $discussion);
128 $event->add_record_snapshot('forum', $forum);
129 $event->trigger();
501cdbd8 130
131 unset($SESSION->fromdiscussion);
132
279826e2 133 if ($mode) {
f2d042c4 134 set_user_preference('forum_displaymode', $mode);
279826e2 135 }
501cdbd8 136
acb50c1b 137 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 138
e92ea3d8 139 if ($parent) {
65bcf17b 140 // If flat AND parent, then force nested display this time
141 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
142 $displaymode = FORUM_MODE_NESTED;
e92ea3d8 143 }
144 } else {
501cdbd8 145 $parent = $discussion->firstpost;
501cdbd8 146 }
147
11b0c469 148 if (! $post = forum_get_post_full($parent)) {
12e57b92 149 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
501cdbd8 150 }
151
737aefb4
MN
152 if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
153 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
f37da850 154 }
155
65bcf17b 156 if ($mark == 'read' or $mark == 'unread') {
90f4745c 157 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
65bcf17b 158 if ($mark == 'read') {
90f4745c 159 forum_tp_add_read_record($USER->id, $postid);
65bcf17b 160 } else {
161 // unread
162 forum_tp_delete_read_records($USER->id, $postid);
163 }
164 }
165 }
cef1ce6a 166
6f1cc8d6 167 $searchform = forum_search_form($course);
39790bd8 168
01d0aceb
SH
169 $forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
170 if (empty($forumnode)) {
171 $forumnode = $PAGE->navbar;
172 } else {
c89fa3fe 173 $forumnode->make_active();
87a462ea 174 }
01d0aceb 175 $node = $forumnode->add(format_string($discussion->name), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
c89fa3fe 176 $node->display = false;
01d0aceb
SH
177 if ($node && $post->id != $discussion->firstpost) {
178 $node->add(format_string($post->subject), $PAGE->url);
179 }
180
15ca5e5e 181 $PAGE->set_title("$course->shortname: ".format_string($discussion->name));
182 $PAGE->set_heading($course->fullname);
183 $PAGE->set_button($searchform);
184 echo $OUTPUT->header();
ebfb73db
AN
185
186 $headingvalue = format_string($forum->name);
187 if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
188 // Discussion subscription.
189 if (\mod_forum\subscriptions::is_subscribable($forum)) {
190 $headingvalue .= '&nbsp;';
191 $headingvalue .= html_writer::tag('span', forum_get_discussion_subscription_icon($forum, $post->discussion), array(
192 'class' => 'discussionsubscription',
193 ));
194 }
195 }
196 echo $OUTPUT->heading($headingvalue, 2);
197
c6d691dc 198
9197e147 199/// Check to see if groups are being used in this forum
200/// If so, make sure the current person is allowed to see this discussion
8b79a625 201/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 202
8e18e1ec
PS
203 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
204 if (!$canreply and $forum->type !== 'news') {
205 if (isguestuser() or !isloggedin()) {
206 $canreply = true;
207 }
208 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
209 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
210 // normal users with temporary guest access see this link too, they are asked to enrol instead
211 $canreply = enrol_selfenrol_available($course->id);
212 }
9197e147 213 }
214
c6d691dc 215/// Print the controls across the top
c00037cd 216 echo '<div class="discussioncontrols clearfix">';
c6d691dc 217
6708a1f5 218 if (!empty($CFG->enableportfolios) && has_capability('mod/forum:exportdiscussion', $modcontext)) {
24ba58ee 219 require_once($CFG->libdir.'/portfoliolib.php');
0d06b6fd 220 $button = new portfolio_add_button();
37743241 221 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), 'mod_forum');
f3cc571a
DM
222 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
223 $buttonextraclass = '';
224 if (empty($button)) {
225 // no portfolio plugin available.
226 $button = '&nbsp;';
227 $buttonextraclass = ' noavailable';
228 }
229 echo html_writer::tag('div', $button, array('class' => 'discussioncontrol exporttoportfolio'.$buttonextraclass));
f357d2f8
DM
230 } else {
231 echo html_writer::tag('div', '&nbsp;', array('class'=>'discussioncontrol nullcontrol'));
10ae55f9 232 }
233
f357d2f8
DM
234 // groups selector not needed here
235 echo '<div class="discussioncontrol displaymode">';
236 forum_print_mode_form($discussion->id, $displaymode);
237 echo "</div>";
238
cef1ce6a 239 if ($forum->type != 'single'
240 && has_capability('mod/forum:movediscussions', $modcontext)) {
65bcf17b 241
f357d2f8 242 echo '<div class="discussioncontrol movediscussion">';
cef1ce6a 243 // Popup menu to move discussions to other forums. The discussion in a
244 // single discussion forum can't be moved.
65bcf17b 245 $modinfo = get_fast_modinfo($course);
246 if (isset($modinfo->instances['forum'])) {
65bcf17b 247 $forummenu = array();
5fad08b4
CF
248 // Check forum types and eliminate simple discussions.
249 $forumcheck = $DB->get_records('forum', array('course' => $course->id),'', 'id, type');
65bcf17b 250 foreach ($modinfo->instances['forum'] as $forumcm) {
ee8d825f 251 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
bf0f06b1 252 context_module::instance($forumcm->id))) {
65bcf17b 253 continue;
fcc69042 254 }
65bcf17b 255 $section = $forumcm->sectionnum;
71a56e08 256 $sectionname = get_section_name($course, $section);
f1a3e072 257 if (empty($forummenu[$section])) {
7487c856 258 $forummenu[$section] = array($sectionname => array());
f1a3e072 259 }
5fad08b4
CF
260 $forumidcompare = $forumcm->instance != $forum->id;
261 $forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
262 if ($forumidcompare and $forumtypecheck) {
f1a3e072 263 $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
7487c856 264 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
1fc49f00 265 }
266 }
267 if (!empty($forummenu)) {
12a24e00 268 echo '<div class="movediscussionoption">';
15e48a1a
SM
269 $select = new url_select($forummenu, '',
270 array(''=>get_string("movethisdiscussionto", "forum")),
271 'forummenu', get_string('move'));
f1a3e072 272 echo $OUTPUT->render($select);
1fc49f00 273 echo "</div>";
274 }
275 }
12a24e00 276 echo "</div>";
1fc49f00 277 }
12a24e00
RW
278 echo '<div class="clearfloat">&nbsp;</div>';
279 echo "</div>";
1fc49f00 280
a4f495bf 281 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
39790bd8 282 $a = new stdClass();
65bcf17b 283 $a->blockafter = $forum->blockafter;
a4f495bf 284 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
9146b979 285 echo $OUTPUT->notification(get_string('thisforumisthrottled','forum',$a));
a4f495bf 286 }
287
0468976c 288 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 289 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
9146b979 290 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 291 }
292
65bcf17b 293 if ($move == -1 and confirm_sesskey()) {
9146b979 294 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
8de14dc7 295 }
296
65bcf17b 297 $canrate = has_capability('mod/forum:rate', $modcontext);
298 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 299
ebfb73db
AN
300 // Add the subscription toggle JS.
301 $PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
1adbd2c3 302
ebfb73db 303 echo $OUTPUT->footer();