rss MDL-22204 fixed rss autodiscovery and some strings in the UI
[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 *
22 * @package mod-forum
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 }
40 if ($mode !== 0) {
41 $url->param('mode', $mode);
42 }
43 if ($move !== 0) {
44 $url->param('move', $move);
45 }
46 if ($mark !== '') {
47 $url->param('mark', $mark);
48 }
49 if ($postid !== 0) {
50 $url->param('postid', $postid);
51 }
52 $PAGE->set_url($url);
d3558659 53
4e445355 54 if (!$discussion = $DB->get_record('forum_discussions', array('id' => $d))) {
12e57b92 55 print_error('invaliddiscussionid', 'forum');
501cdbd8 56 }
57
4e445355 58 if (!$course = $DB->get_record('course', array('id' => $discussion->course))) {
12e57b92 59 print_error('invalidcourseid');
501cdbd8 60 }
61
4e445355 62 if (!$forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
9146b979 63 echo $OUTPUT->notification("Bad forum ID stored in this discussion");
68258534 64 }
65
0fa18d5a 66 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
12e57b92 67 print_error('invalidcoursemodule');
bbbf2d40 68 }
bf553d9e 69 require_course_login($course, true, $cm);
50e07a49 70
12f6d016 71/// Add ajax-related libs
f44b10ed
PS
72 $PAGE->requires->yui2_lib('event');
73 $PAGE->requires->yui2_lib('connection');
74 $PAGE->requires->yui2_lib('json');
12f6d016 75
4cabf99f 76 // move this down fix for MDL-6926
77 require_once('lib.php');
78
9af1611a 79 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
65bcf17b 80 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
81
82 if ($forum->type == 'news') {
49a0ba94 83 if (!($USER->id == $discussion->userid || (($discussion->timestart == 0
84 || $discussion->timestart <= time())
fbc21e82 85 && ($discussion->timeend == 0 || $discussion->timeend > time())))) {
12e57b92 86 print_error('invaliddiscussionid', 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
fbc21e82 87 }
68ddf8bc 88 }
89
65bcf17b 90/// move discussion if requested
91 if ($move > 0 and confirm_sesskey()) {
a5f77de6 92 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
65bcf17b 93
94 require_capability('mod/forum:movediscussions', $modcontext);
1fc49f00 95
65bcf17b 96 if ($forum->type == 'single') {
12e57b92 97 print_error('cannotmovefromsingleforum', 'forum', $return);
83da3d28 98 }
65bcf17b 99
4e445355 100 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
12e57b92 101 print_error('cannotmovetonotexist', 'forum', $return);
83da3d28 102 }
7c900d5d 103
65bcf17b 104 if (!$cmto = get_coursemodule_from_instance('forum', $forumto->id, $course->id)) {
12e57b92 105 print_error('cannotmovetonotfound', 'forum', $return);
65bcf17b 106 }
107
108 if (!coursemodule_visible_for_user($cmto)) {
12e57b92 109 print_error('cannotmovenotvisible', 'forum', $return);
1fc49f00 110 }
65bcf17b 111
ee8d825f 112 require_capability('mod/forum:startdiscussion',
113 get_context_instance(CONTEXT_MODULE,$cmto->id));
114
0faf6791 115 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
9146b979 116 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
65bcf17b 117 }
4e445355 118 $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
119 $DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
65bcf17b 120 add_to_log($course->id, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id, $cmto->id);
121
122 require_once($CFG->libdir.'/rsslib.php');
123 require_once('rsslib.php');
124
125 // Delete the RSS files for the 2 forums because we want to force
126 // the regeneration of the feeds since the discussions have been
127 // moved.
b9973acc 128 if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($forumto)) {
12e57b92 129 print_error('cannotpurgecachedrss', 'forum', $return);
65bcf17b 130 }
131
e4c5abdc 132 redirect($return.'&moved=-1&sesskey='.sesskey());
1fc49f00 133 }
134
1fc49f00 135 $logparameters = "d=$discussion->id";
136 if ($parent) {
839f2456 137 $logparameters .= "&amp;parent=$parent";
501cdbd8 138 }
69d79bc3 139
65bcf17b 140 add_to_log($course->id, 'forum', 'view discussion', "discuss.php?$logparameters", $discussion->id, $cm->id);
501cdbd8 141
142 unset($SESSION->fromdiscussion);
143
279826e2 144 if ($mode) {
f2d042c4 145 set_user_preference('forum_displaymode', $mode);
279826e2 146 }
501cdbd8 147
acb50c1b 148 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 149
e92ea3d8 150 if ($parent) {
65bcf17b 151 // If flat AND parent, then force nested display this time
152 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
153 $displaymode = FORUM_MODE_NESTED;
e92ea3d8 154 }
155 } else {
501cdbd8 156 $parent = $discussion->firstpost;
501cdbd8 157 }
158
11b0c469 159 if (! $post = forum_get_post_full($parent)) {
12e57b92 160 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
501cdbd8 161 }
162
c15b86dc 163
65bcf17b 164 if (!forum_user_can_view_post($post, $course, $cm, $forum, $discussion)) {
12e57b92 165 print_error('nopermissiontoview', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
f37da850 166 }
167
65bcf17b 168 if ($mark == 'read' or $mark == 'unread') {
90f4745c 169 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
65bcf17b 170 if ($mark == 'read') {
90f4745c 171 forum_tp_add_read_record($USER->id, $postid);
65bcf17b 172 } else {
173 // unread
174 forum_tp_delete_read_records($USER->id, $postid);
175 }
176 }
177 }
cef1ce6a 178
6f1cc8d6 179 $searchform = forum_search_form($course);
87a462ea 180
a6855934 181 $PAGE->navbar->add(format_string($discussion->name), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
87a462ea 182 if ($parent != $discussion->firstpost) {
15ca5e5e 183 $PAGE->navbar->add(format_string($post->subject));
87a462ea 184 }
15ca5e5e 185 $PAGE->set_title("$course->shortname: ".format_string($discussion->name));
186 $PAGE->set_heading($course->fullname);
187 $PAGE->set_button($searchform);
188 echo $OUTPUT->header();
c6d691dc 189
9197e147 190/// Check to see if groups are being used in this forum
191/// If so, make sure the current person is allowed to see this discussion
8b79a625 192/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 193
4f0c2d00 194 if (isguestuser() or !isloggedin() or (!is_enrolled($modcontext) and !is_viewing($modcontext))) {
525041df 195 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
4f0c2d00 196 // normal users with temporary guest access see this link too, they are asked to enrol instead
525041df 197 $canreply = ($forum->type != 'news'); // no reply in news forums
198
8b79a625 199 } else {
200 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
9197e147 201 }
202
c6d691dc 203/// Print the controls across the top
204
a1857045 205 echo '<table width="100%" class="discussioncontrols"><tr><td>';
c6d691dc 206
87b0b499 207 // groups selector not needed here
c6d691dc 208
c7a049a3 209 echo "</td><td>";
ec9c0d44 210 forum_print_mode_form($discussion->id, $displaymode);
c7a049a3 211 echo "</td><td>";
cef1ce6a 212
0d06b6fd 213 if (has_capability('mod/forum:exportdiscussion', $modcontext)) {
24ba58ee 214 require_once($CFG->libdir.'/portfoliolib.php');
0d06b6fd 215 $button = new portfolio_add_button();
24ba58ee 216 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), '/mod/forum/locallib.php');
0d06b6fd 217 $button->render();
218 echo '</td><td>';
10ae55f9 219 }
220
cef1ce6a 221 if ($forum->type != 'single'
222 && has_capability('mod/forum:movediscussions', $modcontext)) {
65bcf17b 223
cef1ce6a 224 // Popup menu to move discussions to other forums. The discussion in a
225 // single discussion forum can't be moved.
65bcf17b 226 $modinfo = get_fast_modinfo($course);
227 if (isset($modinfo->instances['forum'])) {
fcc69042 228 if ($course->format == 'weeks') {
229 $strsection = get_string("week");
230 } else {
231 $strsection = get_string("topic");
232 }
65bcf17b 233 $forummenu = array();
234 foreach ($modinfo->instances['forum'] as $forumcm) {
ee8d825f 235 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
236 get_context_instance(CONTEXT_MODULE,$forumcm->id))) {
65bcf17b 237 continue;
fcc69042 238 }
65bcf17b 239
65bcf17b 240 $section = $forumcm->sectionnum;
f1a3e072
PS
241 if (empty($forummenu[$section])) {
242 $forummenu[$section] = array("$strsection $section" => array());
243 }
65bcf17b 244 if ($forumcm->instance != $forum->id) {
f1a3e072
PS
245 $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
246 $forummenu[$section]["$strsection $section"][$url] = format_string($forumcm->name);
1fc49f00 247 }
248 }
249 if (!empty($forummenu)) {
ecf922b8 250
41a22032 251 echo "<div style=\"float:right;\">";
f1a3e072
PS
252 $select = new url_select($forummenu, '', array(''=>get_string("movethisdiscussionto", "forum")), 'forummenu');
253 echo $OUTPUT->render($select);
1fc49f00 254 echo "</div>";
255 }
256 }
257 }
02ebf404 258 echo "</td></tr></table>";
1fc49f00 259
a4f495bf 260 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
65bcf17b 261 $a = new object();
262 $a->blockafter = $forum->blockafter;
a4f495bf 263 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
9146b979 264 echo $OUTPUT->notification(get_string('thisforumisthrottled','forum',$a));
a4f495bf 265 }
266
0468976c 267 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 268 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
9146b979 269 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 270 }
271
65bcf17b 272 if ($move == -1 and confirm_sesskey()) {
9146b979 273 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
8de14dc7 274 }
275
65bcf17b 276 $canrate = has_capability('mod/forum:rate', $modcontext);
277 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 278
396fb912 279 echo $OUTPUT->footer();
65bcf17b 280
501cdbd8 281
1adbd2c3 282