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