MDL-30811 forum: Update use of redirect to include message and type
[moodle.git] / mod / forum / index.php
CommitLineData
cd4e6b17 1<?php
f93f848a 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/**
01030f1b 19 * @package mod_forum
8f685009
SH
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
cd4e6b17 24require_once(dirname(__FILE__) . '/../../config.php');
25require_once($CFG->dirroot . '/course/lib.php');
26require_once($CFG->dirroot . '/mod/forum/lib.php');
27require_once($CFG->libdir . '/rsslib.php');
f93f848a 28
cd4e6b17 29$id = optional_param('id', 0, PARAM_INT); // Course id
30$subscribe = optional_param('subscribe', null, PARAM_INT); // Subscribe/Unsubscribe all forums
f93f848a 31
a6855934 32$url = new moodle_url('/mod/forum/index.php', array('id'=>$id));
cd4e6b17 33if ($subscribe !== null) {
c56ed7bb 34 require_sesskey();
cd4e6b17 35 $url->param('subscribe', $subscribe);
36}
37$PAGE->set_url($url);
38
39if ($id) {
40 if (! $course = $DB->get_record('course', array('id' => $id))) {
41 print_error('invalidcourseid');
42 }
43} else {
2a250a0b 44 $course = get_site();
cd4e6b17 45}
f93f848a 46
cd4e6b17 47require_course_login($course);
191b267b 48$PAGE->set_pagelayout('incourse');
bf0f06b1 49$coursecontext = context_course::instance($course->id);
583b57b4 50
ecc8403e 51
cd4e6b17 52unset($SESSION->fromdiscussion);
f93f848a 53
22881392
DP
54$params = array(
55 'context' => context_course::instance($course->id)
56);
57$event = \mod_forum\event\course_module_instance_list_viewed::create($params);
1e242b41 58$event->add_record_snapshot('course', $course);
22881392 59$event->trigger();
2f3f505f 60
cd4e6b17 61$strforums = get_string('forums', 'forum');
62$strforum = get_string('forum', 'forum');
63$strdescription = get_string('description');
64$strdiscussions = get_string('discussions', 'forum');
65$strsubscribed = get_string('subscribed', 'forum');
66$strunreadposts = get_string('unreadposts', 'forum');
67$strtracking = get_string('tracking', 'forum');
68$strmarkallread = get_string('markallread', 'forum');
69$strtrackforum = get_string('trackforum', 'forum');
70$strnotrackforum = get_string('notrackforum', 'forum');
71$strsubscribe = get_string('subscribe', 'forum');
72$strunsubscribe = get_string('unsubscribe', 'forum');
73$stryes = get_string('yes');
74$strno = get_string('no');
75$strrss = get_string('rss');
8e08c731 76$stremaildigest = get_string('emaildigest');
97485d07 77
cd4e6b17 78$searchform = forum_search_form($course);
f93f848a 79
8e08c731
AN
80// Retrieve the list of forum digest options for later.
81$digestoptions = forum_get_user_digest_options();
82$digestoptions_selector = new single_select(new moodle_url('/mod/forum/maildigest.php',
83 array(
84 'backtoindex' => 1,
85 )),
86 'maildigest',
87 $digestoptions,
88 null,
89 '');
90$digestoptions_selector->method = 'post';
f781b794 91
cd4e6b17 92// Start of the table for General Forums
f781b794 93
cd4e6b17 94$generaltable = new html_table();
95$generaltable->head = array ($strforum, $strdescription, $strdiscussions);
96$generaltable->align = array ('left', 'left', 'center');
f37da850 97
cd4e6b17 98if ($usetracking = forum_tp_can_track_forums()) {
99 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 100
cd4e6b17 101 $generaltable->head[] = $strunreadposts;
102 $generaltable->align[] = 'center';
38994d6f 103
cd4e6b17 104 $generaltable->head[] = $strtracking;
105 $generaltable->align[] = 'center';
106}
f93f848a 107
59075a43
AN
108// Fill the subscription cache for this course and user combination.
109\mod_forum\subscriptions::fill_subscription_cache_for_course($course->id, $USER->id);
cd4e6b17 110
1cd8c399
AD
111$can_subscribe = is_enrolled($coursecontext);
112if ($can_subscribe) {
cd4e6b17 113 $generaltable->head[] = $strsubscribed;
114 $generaltable->align[] = 'center';
8e08c731
AN
115
116 $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
117 $generaltable->align[] = 'center';
cd4e6b17 118}
119
120if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
121 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
122 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
123 $generaltable->head[] = $strrss;
124 $generaltable->align[] = 'center';
125}
90f4745c 126
7487c856 127$usesections = course_format_uses_sections($course->format);
7487c856
SH
128
129$table = new html_table();
f93f848a 130
cd4e6b17 131// Parse and organise all the forums. Most forums are course modules but
132// some special ones are not. These get placed in the general forums
133// category with the forums in section 0.
134
8e08c731
AN
135$forums = $DB->get_records_sql("
136 SELECT f.*,
137 d.maildigest
138 FROM {forum} f
139 LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
140 WHERE f.course = ?
141 ", array($USER->id, $course->id));
cd4e6b17 142
143$generalforums = array();
144$learningforums = array();
f20edd52 145$modinfo = get_fast_modinfo($course);
cd4e6b17 146
44ddd2a1 147foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
cd4e6b17 148 if (!$cm->uservisible or !isset($forums[$forumid])) {
149 continue;
33f48f03 150 }
151
cd4e6b17 152 $forum = $forums[$forumid];
fa5a5b52 153
0d4d49d1 154 if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
cd4e6b17 155 continue; // Shouldn't happen
156 }
157
158 if (!has_capability('mod/forum:viewdiscussion', $context)) {
159 continue;
160 }
ecc8403e 161
cd4e6b17 162 // fill two type array - order in modinfo is the same as in course
163 if ($forum->type == 'news' or $forum->type == 'social') {
164 $generalforums[$forum->id] = $forum;
dd97c328 165
cd4e6b17 166 } else if ($course->id == SITEID or empty($cm->sectionnum)) {
167 $generalforums[$forum->id] = $forum;
ecc8403e 168
cd4e6b17 169 } else {
170 $learningforums[$forum->id] = $forum;
2f3f505f 171 }
cd4e6b17 172}
2f3f505f 173
b688c449
EL
174// Do course wide subscribe/unsubscribe if requested
175if (!is_null($subscribe)) {
176 if (isguestuser() or !$can_subscribe) {
d954b54f
AN
177 // There should not be any links leading to this place, just redirect.
178 redirect(
179 new moodle_url('/mod/forum/index.php', array('id' => $id)),
180 get_string('subscribeenrolledonly', 'forum'),
181 null,
182 \core\output\notification::NOTIFY_ERROR
183 );
b688c449
EL
184 }
185 // Can proceed now, the user is not guest and is enrolled
44ddd2a1 186 foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
2f3f505f 187 $forum = $forums[$forumid];
bf0f06b1 188 $modcontext = context_module::instance($cm->id);
cd4e6b17 189 $cansub = false;
dd97c328 190
cd4e6b17 191 if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
192 $cansub = true;
2f3f505f 193 }
cd4e6b17 194 if ($cansub && $cm->visible == 0 &&
195 !has_capability('mod/forum:managesubscriptions', $modcontext))
196 {
197 $cansub = false;
2f3f505f 198 }
59075a43 199 if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
4238983e 200 $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
59075a43
AN
201 $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
202 if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
e3bbfb52 203 \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
cd4e6b17 204 } else if (!$subscribe && $subscribed) {
e3bbfb52 205 \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
cd4e6b17 206 }
207 }
208 }
3d8d8a8c 209 $returnto = forum_go_back_to(new moodle_url('/mod/forum/index.php', array('id' => $course->id)));
bf0f06b1 210 $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
cd4e6b17 211 if ($subscribe) {
d954b54f
AN
212 redirect(
213 $returnto,
214 get_string('nowallsubscribed', 'forum', $shortname),
215 null,
216 \core\output\notification::NOTIFY_SUCCESS
217 );
cd4e6b17 218 } else {
d954b54f
AN
219 redirect(
220 $returnto,
221 get_string('nowallunsubscribed', 'forum', $shortname),
222 null,
223 \core\output\notification::NOTIFY_SUCCESS
224 );
cd4e6b17 225 }
226}
dd97c328 227
cd4e6b17 228/// First, let's process the general forums and build up a display
229
230if ($generalforums) {
231 foreach ($generalforums as $forum) {
232 $cm = $modinfo->instances['forum'][$forum->id];
bf0f06b1 233 $context = context_module::instance($cm->id);
cd4e6b17 234
235 $count = forum_count_discussions($forum, $cm, $course);
236
237 if ($usetracking) {
238 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
239 $unreadlink = '-';
240 $trackedlink = '-';
241
242 } else {
243 if (isset($untracked[$forum->id])) {
244 $unreadlink = '-';
245 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
246 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
247 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
99938034 248 $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
cd4e6b17 249 } else {
250 $unreadlink = '<span class="read">0</span>';
251 }
2f3f505f 252
bd8f5d45 253 if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
cd4e6b17 254 $trackedlink = $stryes;
20e6782f
EM
255 } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
256 $trackedlink = '-';
cd4e6b17 257 } else {
7a311adb
AN
258 $aurl = new moodle_url('/mod/forum/settracking.php', array(
259 'id' => $forum->id,
260 'sesskey' => sesskey(),
261 ));
cd4e6b17 262 if (!isset($untracked[$forum->id])) {
5c2ed7e2 263 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
cd4e6b17 264 } else {
5c2ed7e2 265 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
cd4e6b17 266 }
267 }
268 }
269 }
270
271 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
0e35ba6f 272 $forumname = format_string($forum->name, true);
cd4e6b17 273
274 if ($cm->visible) {
275 $style = '';
2f3f505f 276 } else {
cd4e6b17 277 $style = 'class="dimmed"';
37b15514 278 }
cd4e6b17 279 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
280 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
702dc57b 281
cd4e6b17 282 $row = array ($forumlink, $forum->intro, $discussionlink);
283 if ($usetracking) {
284 $row[] = $unreadlink;
285 $row[] = $trackedlink; // Tracking.
286 }
3b8b9b6a 287
cd4e6b17 288 if ($can_subscribe) {
0d7cb0f8
AN
289 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
290 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
291 'cantsubscribe' => '-'), false, false, true);
8e08c731
AN
292
293 $digestoptions_selector->url->param('id', $forum->id);
294 if ($forum->maildigest === null) {
295 $digestoptions_selector->selected = -1;
296 } else {
297 $digestoptions_selector->selected = $forum->maildigest;
298 }
299 $row[] = $OUTPUT->render($digestoptions_selector);
cd4e6b17 300 }
301
302 //If this forum has RSS activated, calculate it
303 if ($show_rss) {
304 if ($forum->rsstype and $forum->rssarticles) {
9e86f2e7 305 //Calculate the tooltip text
cd4e6b17 306 if ($forum->rsstype == 1) {
9e86f2e7 307 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
cd4e6b17 308 } else {
9e86f2e7 309 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
03877b04 310 }
dbf4433d
RW
311
312 if (!isloggedin() && $course->id == SITEID) {
313 $userid = guest_user()->id;
314 } else {
315 $userid = $USER->id;
316 }
cd4e6b17 317 //Get html code for RSS link
dbf4433d 318 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
cd4e6b17 319 } else {
320 $row[] = '&nbsp;';
03877b04 321 }
322 }
cd4e6b17 323
324 $generaltable->data[] = $row;
325 }
326}
327
328
329// Start of the table for Learning Forums
330$learningtable = new html_table();
331$learningtable->head = array ($strforum, $strdescription, $strdiscussions);
332$learningtable->align = array ('left', 'left', 'center');
333
334if ($usetracking) {
335 $learningtable->head[] = $strunreadposts;
336 $learningtable->align[] = 'center';
337
338 $learningtable->head[] = $strtracking;
339 $learningtable->align[] = 'center';
340}
341
342if ($can_subscribe) {
343 $learningtable->head[] = $strsubscribed;
344 $learningtable->align[] = 'center';
8e08c731
AN
345
346 $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
347 $learningtable->align[] = 'center';
cd4e6b17 348}
349
350if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
351 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
352 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
353 $learningtable->head[] = $strrss;
354 $learningtable->align[] = 'center';
355}
356
357/// Now let's process the learning forums
358
359if ($course->id != SITEID) { // Only real courses have learning forums
4202a917 360 // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
b489d37d 361 $strsectionname = get_string('sectionname', 'format_'.$course->format);
cd4e6b17 362 // Add extra field for section number, at the front
7487c856 363 array_unshift($learningtable->head, $strsectionname);
cd4e6b17 364 array_unshift($learningtable->align, 'center');
03877b04 365
ecc8403e 366
cd4e6b17 367 if ($learningforums) {
368 $currentsection = '';
369 foreach ($learningforums as $forum) {
2f3f505f 370 $cm = $modinfo->instances['forum'][$forum->id];
bf0f06b1 371 $context = context_module::instance($cm->id);
dd97c328 372
90f4745c 373 $count = forum_count_discussions($forum, $cm, $course);
8f0cd6ef 374
eaf50aef 375 if ($usetracking) {
a90e92a5 376 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
377 $unreadlink = '-';
378 $trackedlink = '-';
90f4745c 379
380 } else {
381 if (isset($untracked[$forum->id])) {
cd4e6b17 382 $unreadlink = '-';
90f4745c 383 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
cd4e6b17 384 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 385 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
99938034 386 $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
38994d6f 387 } else {
90f4745c 388 $unreadlink = '<span class="read">0</span>';
38994d6f 389 }
630bd96b 390
bd8f5d45 391 if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
90f4745c 392 $trackedlink = $stryes;
20e6782f
EM
393 } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
394 $trackedlink = '-';
a90e92a5 395 } else {
a6855934 396 $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
90f4745c 397 if (!isset($untracked[$forum->id])) {
5c2ed7e2 398 $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
90f4745c 399 } else {
5c2ed7e2 400 $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
90f4745c 401 }
630bd96b 402 }
f37da850 403 }
404 }
405
4ba2221a 406 $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
cd4e6b17 407
408 if ($cm->sectionnum != $currentsection) {
71a56e08 409 $printsection = get_section_name($course, $cm->sectionnum);
cd4e6b17 410 if ($currentsection) {
411 $learningtable->data[] = 'hr';
412 }
413 $currentsection = $cm->sectionnum;
414 } else {
415 $printsection = '';
416 }
417
0e35ba6f 418 $forumname = format_string($forum->name,true);
ecc8403e 419
90f4745c 420 if ($cm->visible) {
421 $style = '';
ecc8403e 422 } else {
90f4745c 423 $style = 'class="dimmed"';
ecc8403e 424 }
90f4745c 425 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
426 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
ecc8403e 427
cd4e6b17 428 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
49b53f26 429 if ($usetracking) {
430 $row[] = $unreadlink;
431 $row[] = $trackedlink; // Tracking.
f4646b44 432 }
433
ecc8403e 434 if ($can_subscribe) {
0d7cb0f8
AN
435 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
436 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
437 'cantsubscribe' => '-'), false, false, true);
8e08c731
AN
438
439 $digestoptions_selector->url->param('id', $forum->id);
440 if ($forum->maildigest === null) {
441 $digestoptions_selector->selected = -1;
442 } else {
443 $digestoptions_selector->selected = $forum->maildigest;
444 }
445 $row[] = $OUTPUT->render($digestoptions_selector);
49b53f26 446 }
447
448 //If this forum has RSS activated, calculate it
90f4745c 449 if ($show_rss) {
450 if ($forum->rsstype and $forum->rssarticles) {
451 //Calculate the tolltip text
452 if ($forum->rsstype == 1) {
9e86f2e7 453 $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
90f4745c 454 } else {
9e86f2e7 455 $tooltiptext = get_string('rsssubscriberssposts', 'forum');
90f4745c 456 }
457 //Get html code for RSS link
aa60291e 458 $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
49b53f26 459 } else {
90f4745c 460 $row[] = '&nbsp;';
f4646b44 461 }
ecc8403e 462 }
49b53f26 463
cd4e6b17 464 $learningtable->data[] = $row;
ecc8403e 465 }
8f0cd6ef 466 }
cd4e6b17 467}
468
469
470/// Output the page
471$PAGE->navbar->add($strforums);
472$PAGE->set_title("$course->shortname: $strforums");
473$PAGE->set_heading($course->fullname);
474$PAGE->set_button($searchform);
475echo $OUTPUT->header();
476
b688c449
EL
477// Show the subscribe all options only to non-guest, enrolled users
478if (!isguestuser() && isloggedin() && $can_subscribe) {
cd4e6b17 479 echo $OUTPUT->box_start('subscription');
c56ed7bb
DM
480 echo html_writer::tag('div',
481 html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
482 get_string('allsubscribe', 'forum')),
483 array('class'=>'helplink'));
484 echo html_writer::tag('div',
485 html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
486 get_string('allunsubscribe', 'forum')),
487 array('class'=>'helplink'));
cd4e6b17 488 echo $OUTPUT->box_end();
489 echo $OUTPUT->box('&nbsp;', 'clearer');
490}
491
492if ($generalforums) {
66e2b9f8 493 echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
16be8974 494 echo html_writer::table($generaltable);
cd4e6b17 495}
496
497if ($learningforums) {
66e2b9f8 498 echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
16be8974 499 echo html_writer::table($learningtable);
cd4e6b17 500}
501
502echo $OUTPUT->footer();
503