MDL-30811 forum: Update use of redirect to include message and type
[moodle.git] / mod / forum / index.php
1 <?php
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/>.
18 /**
19  * @package   mod_forum
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  */
24 require_once(dirname(__FILE__) . '/../../config.php');
25 require_once($CFG->dirroot . '/course/lib.php');
26 require_once($CFG->dirroot . '/mod/forum/lib.php');
27 require_once($CFG->libdir . '/rsslib.php');
29 $id = optional_param('id', 0, PARAM_INT);                   // Course id
30 $subscribe = optional_param('subscribe', null, PARAM_INT);  // Subscribe/Unsubscribe all forums
32 $url = new moodle_url('/mod/forum/index.php', array('id'=>$id));
33 if ($subscribe !== null) {
34     require_sesskey();
35     $url->param('subscribe', $subscribe);
36 }
37 $PAGE->set_url($url);
39 if ($id) {
40     if (! $course = $DB->get_record('course', array('id' => $id))) {
41         print_error('invalidcourseid');
42     }
43 } else {
44     $course = get_site();
45 }
47 require_course_login($course);
48 $PAGE->set_pagelayout('incourse');
49 $coursecontext = context_course::instance($course->id);
52 unset($SESSION->fromdiscussion);
54 $params = array(
55     'context' => context_course::instance($course->id)
56 );
57 $event = \mod_forum\event\course_module_instance_list_viewed::create($params);
58 $event->add_record_snapshot('course', $course);
59 $event->trigger();
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');
76 $stremaildigest  = get_string('emaildigest');
78 $searchform = forum_search_form($course);
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';
92 // Start of the table for General Forums
94 $generaltable = new html_table();
95 $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
96 $generaltable->align = array ('left', 'left', 'center');
98 if ($usetracking = forum_tp_can_track_forums()) {
99     $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
101     $generaltable->head[] = $strunreadposts;
102     $generaltable->align[] = 'center';
104     $generaltable->head[] = $strtracking;
105     $generaltable->align[] = 'center';
108 // Fill the subscription cache for this course and user combination.
109 \mod_forum\subscriptions::fill_subscription_cache_for_course($course->id, $USER->id);
111 $can_subscribe = is_enrolled($coursecontext);
112 if ($can_subscribe) {
113     $generaltable->head[] = $strsubscribed;
114     $generaltable->align[] = 'center';
116     $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
117     $generaltable->align[] = 'center';
120 if ($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';
127 $usesections = course_format_uses_sections($course->format);
129 $table = new html_table();
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.
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));
143 $generalforums  = array();
144 $learningforums = array();
145 $modinfo = get_fast_modinfo($course);
147 foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
148     if (!$cm->uservisible or !isset($forums[$forumid])) {
149         continue;
150     }
152     $forum = $forums[$forumid];
154     if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
155         continue;   // Shouldn't happen
156     }
158     if (!has_capability('mod/forum:viewdiscussion', $context)) {
159         continue;
160     }
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;
166     } else if ($course->id == SITEID or empty($cm->sectionnum)) {
167         $generalforums[$forum->id] = $forum;
169     } else {
170         $learningforums[$forum->id] = $forum;
171     }
174 // Do course wide subscribe/unsubscribe if requested
175 if (!is_null($subscribe)) {
176     if (isguestuser() or !$can_subscribe) {
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             );
184     }
185     // Can proceed now, the user is not guest and is enrolled
186     foreach ($modinfo->get_instances_of('forum') as $forumid=>$cm) {
187         $forum = $forums[$forumid];
188         $modcontext = context_module::instance($cm->id);
189         $cansub = false;
191         if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
192             $cansub = true;
193         }
194         if ($cansub && $cm->visible == 0 &&
195             !has_capability('mod/forum:managesubscriptions', $modcontext))
196         {
197             $cansub = false;
198         }
199         if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
200             $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
201             $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
202             if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
203                 \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
204             } else if (!$subscribe && $subscribed) {
205                 \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
206             }
207         }
208     }
209     $returnto = forum_go_back_to(new moodle_url('/mod/forum/index.php', array('id' => $course->id)));
210     $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
211     if ($subscribe) {
212         redirect(
213                 $returnto,
214                 get_string('nowallsubscribed', 'forum', $shortname),
215                 null,
216                 \core\output\notification::NOTIFY_SUCCESS
217             );
218     } else {
219         redirect(
220                 $returnto,
221                 get_string('nowallunsubscribed', 'forum', $shortname),
222                 null,
223                 \core\output\notification::NOTIFY_SUCCESS
224             );
225     }
228 /// First, let's process the general forums and build up a display
230 if ($generalforums) {
231     foreach ($generalforums as $forum) {
232         $cm      = $modinfo->instances['forum'][$forum->id];
233         $context = context_module::instance($cm->id);
235         $count = forum_count_discussions($forum, $cm, $course);
237         if ($usetracking) {
238             if ($forum->trackingtype == FORUM_TRACKING_OFF) {
239                 $unreadlink  = '-';
240                 $trackedlink = '-';
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='.
248                                    $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
249                 } else {
250                     $unreadlink = '<span class="read">0</span>';
251                 }
253                 if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
254                     $trackedlink = $stryes;
255                 } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
256                     $trackedlink = '-';
257                 } else {
258                     $aurl = new moodle_url('/mod/forum/settracking.php', array(
259                             'id' => $forum->id,
260                             'sesskey' => sesskey(),
261                         ));
262                     if (!isset($untracked[$forum->id])) {
263                         $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
264                     } else {
265                         $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
266                     }
267                 }
268             }
269         }
271         $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
272         $forumname = format_string($forum->name, true);
274         if ($cm->visible) {
275             $style = '';
276         } else {
277             $style = 'class="dimmed"';
278         }
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>";
282         $row = array ($forumlink, $forum->intro, $discussionlink);
283         if ($usetracking) {
284             $row[] = $unreadlink;
285             $row[] = $trackedlink;    // Tracking.
286         }
288         if ($can_subscribe) {
289             $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
290                     'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
291                     'cantsubscribe' => '-'), false, false, true);
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);
300         }
302         //If this forum has RSS activated, calculate it
303         if ($show_rss) {
304             if ($forum->rsstype and $forum->rssarticles) {
305                 //Calculate the tooltip text
306                 if ($forum->rsstype == 1) {
307                     $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
308                 } else {
309                     $tooltiptext = get_string('rsssubscriberssposts', 'forum');
310                 }
312                 if (!isloggedin() && $course->id == SITEID) {
313                     $userid = guest_user()->id;
314                 } else {
315                     $userid = $USER->id;
316                 }
317                 //Get html code for RSS link
318                 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
319             } else {
320                 $row[] = '&nbsp;';
321             }
322         }
324         $generaltable->data[] = $row;
325     }
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');
334 if ($usetracking) {
335     $learningtable->head[] = $strunreadposts;
336     $learningtable->align[] = 'center';
338     $learningtable->head[] = $strtracking;
339     $learningtable->align[] = 'center';
342 if ($can_subscribe) {
343     $learningtable->head[] = $strsubscribed;
344     $learningtable->align[] = 'center';
346     $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
347     $learningtable->align[] = 'center';
350 if ($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';
357 /// Now let's process the learning forums
359 if ($course->id != SITEID) {    // Only real courses have learning forums
360     // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
361     $strsectionname  = get_string('sectionname', 'format_'.$course->format);
362     // Add extra field for section number, at the front
363     array_unshift($learningtable->head, $strsectionname);
364     array_unshift($learningtable->align, 'center');
367     if ($learningforums) {
368         $currentsection = '';
369             foreach ($learningforums as $forum) {
370             $cm      = $modinfo->instances['forum'][$forum->id];
371             $context = context_module::instance($cm->id);
373             $count = forum_count_discussions($forum, $cm, $course);
375             if ($usetracking) {
376                 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
377                     $unreadlink  = '-';
378                     $trackedlink = '-';
380                 } else {
381                     if (isset($untracked[$forum->id])) {
382                         $unreadlink  = '-';
383                     } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
384                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
385                         $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
386                                        $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
387                     } else {
388                         $unreadlink = '<span class="read">0</span>';
389                     }
391                     if (($forum->trackingtype == FORUM_TRACKING_FORCED) && ($CFG->forum_allowforcedreadtracking)) {
392                         $trackedlink = $stryes;
393                     } else if ($forum->trackingtype === FORUM_TRACKING_OFF || ($USER->trackforums == 0)) {
394                         $trackedlink = '-';
395                     } else {
396                         $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
397                         if (!isset($untracked[$forum->id])) {
398                             $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
399                         } else {
400                             $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
401                         }
402                     }
403                 }
404             }
406             $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
408             if ($cm->sectionnum != $currentsection) {
409                 $printsection = get_section_name($course, $cm->sectionnum);
410                 if ($currentsection) {
411                     $learningtable->data[] = 'hr';
412                 }
413                 $currentsection = $cm->sectionnum;
414             } else {
415                 $printsection = '';
416             }
418             $forumname = format_string($forum->name,true);
420             if ($cm->visible) {
421                 $style = '';
422             } else {
423                 $style = 'class="dimmed"';
424             }
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>";
428             $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
429             if ($usetracking) {
430                 $row[] = $unreadlink;
431                 $row[] = $trackedlink;    // Tracking.
432             }
434             if ($can_subscribe) {
435                 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
436                     'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
437                     'cantsubscribe' => '-'), false, false, true);
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);
446             }
448             //If this forum has RSS activated, calculate it
449             if ($show_rss) {
450                 if ($forum->rsstype and $forum->rssarticles) {
451                     //Calculate the tolltip text
452                     if ($forum->rsstype == 1) {
453                         $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
454                     } else {
455                         $tooltiptext = get_string('rsssubscriberssposts', 'forum');
456                     }
457                     //Get html code for RSS link
458                     $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
459                 } else {
460                     $row[] = '&nbsp;';
461                 }
462             }
464             $learningtable->data[] = $row;
465         }
466     }
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);
475 echo $OUTPUT->header();
477 // Show the subscribe all options only to non-guest, enrolled users
478 if (!isguestuser() && isloggedin() && $can_subscribe) {
479     echo $OUTPUT->box_start('subscription');
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'));
488     echo $OUTPUT->box_end();
489     echo $OUTPUT->box('&nbsp;', 'clearer');
492 if ($generalforums) {
493     echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
494     echo html_writer::table($generaltable);
497 if ($learningforums) {
498     echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
499     echo html_writer::table($learningtable);
502 echo $OUTPUT->footer();