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