3658a3ff0813cc38468c8b43676062e9fd49f470
[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 add_to_log($course->id, 'forum', 'view forums', "index.php?id=$course->id");
56 $strforums       = get_string('forums', 'forum');
57 $strforum        = get_string('forum', 'forum');
58 $strdescription  = get_string('description');
59 $strdiscussions  = get_string('discussions', 'forum');
60 $strsubscribed   = get_string('subscribed', 'forum');
61 $strunreadposts  = get_string('unreadposts', 'forum');
62 $strtracking     = get_string('tracking', 'forum');
63 $strmarkallread  = get_string('markallread', 'forum');
64 $strtrackforum   = get_string('trackforum', 'forum');
65 $strnotrackforum = get_string('notrackforum', 'forum');
66 $strsubscribe    = get_string('subscribe', 'forum');
67 $strunsubscribe  = get_string('unsubscribe', 'forum');
68 $stryes          = get_string('yes');
69 $strno           = get_string('no');
70 $strrss          = get_string('rss');
71 $stremaildigest  = get_string('emaildigest');
73 $searchform = forum_search_form($course);
75 // Retrieve the list of forum digest options for later.
76 $digestoptions = forum_get_user_digest_options();
77 $digestoptions_selector = new single_select(new moodle_url('/mod/forum/maildigest.php',
78     array(
79         'backtoindex' => 1,
80     )),
81     'maildigest',
82     $digestoptions,
83     null,
84     '');
85 $digestoptions_selector->method = 'post';
87 // Start of the table for General Forums
89 $generaltable = new html_table();
90 $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
91 $generaltable->align = array ('left', 'left', 'center');
93 if ($usetracking = forum_tp_can_track_forums()) {
94     $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
96     $generaltable->head[] = $strunreadposts;
97     $generaltable->align[] = 'center';
99     $generaltable->head[] = $strtracking;
100     $generaltable->align[] = 'center';
103 $subscribed_forums = forum_get_subscribed_forums($course);
105 $can_subscribe = is_enrolled($coursecontext);
106 if ($can_subscribe) {
107     $generaltable->head[] = $strsubscribed;
108     $generaltable->align[] = 'center';
110     $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
111     $generaltable->align[] = 'center';
114 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
115                  isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
116                  $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
117     $generaltable->head[] = $strrss;
118     $generaltable->align[] = 'center';
121 $usesections = course_format_uses_sections($course->format);
123 $table = new html_table();
125 // Parse and organise all the forums.  Most forums are course modules but
126 // some special ones are not.  These get placed in the general forums
127 // category with the forums in section 0.
129 $forums = $DB->get_records_sql("
130     SELECT f.*,
131            d.maildigest
132       FROM {forum} f
133  LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
134      WHERE f.course = ?
135     ", array($USER->id, $course->id));
137 $generalforums  = array();
138 $learningforums = array();
139 $modinfo = get_fast_modinfo($course);
141 if (!isset($modinfo->instances['forum'])) {
142     $modinfo->instances['forum'] = array();
145 foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
146     if (!$cm->uservisible or !isset($forums[$forumid])) {
147         continue;
148     }
150     $forum = $forums[$forumid];
152     if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
153         continue;   // Shouldn't happen
154     }
156     if (!has_capability('mod/forum:viewdiscussion', $context)) {
157         continue;
158     }
160     // fill two type array - order in modinfo is the same as in course
161     if ($forum->type == 'news' or $forum->type == 'social') {
162         $generalforums[$forum->id] = $forum;
164     } else if ($course->id == SITEID or empty($cm->sectionnum)) {
165         $generalforums[$forum->id] = $forum;
167     } else {
168         $learningforums[$forum->id] = $forum;
169     }
172 // Do course wide subscribe/unsubscribe if requested
173 if (!is_null($subscribe)) {
174     if (isguestuser() or !$can_subscribe) {
175         // there should not be any links leading to this place, just redirect
176         redirect(new moodle_url('/mod/forum/index.php', array('id' => $id)), get_string('subscribeenrolledonly', 'forum'));
177     }
178     // Can proceed now, the user is not guest and is enrolled
179     foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
180         $forum = $forums[$forumid];
181         $modcontext = context_module::instance($cm->id);
182         $cansub = false;
184         if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
185             $cansub = true;
186         }
187         if ($cansub && $cm->visible == 0 &&
188             !has_capability('mod/forum:managesubscriptions', $modcontext))
189         {
190             $cansub = false;
191         }
192         if (!forum_is_forcesubscribed($forum)) {
193             $subscribed = forum_is_subscribed($USER->id, $forum);
194             if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
195                 forum_subscribe($USER->id, $forumid);
196             } else if (!$subscribe && $subscribed) {
197                 forum_unsubscribe($USER->id, $forumid);
198             }
199         }
200     }
201     $returnto = forum_go_back_to("index.php?id=$course->id");
202     $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
203     if ($subscribe) {
204         add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
205         redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
206     } else {
207         add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
208         redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
209     }
212 /// First, let's process the general forums and build up a display
214 if ($generalforums) {
215     foreach ($generalforums as $forum) {
216         $cm      = $modinfo->instances['forum'][$forum->id];
217         $context = context_module::instance($cm->id);
219         $count = forum_count_discussions($forum, $cm, $course);
221         if ($usetracking) {
222             if ($forum->trackingtype == FORUM_TRACKING_OFF) {
223                 $unreadlink  = '-';
224                 $trackedlink = '-';
226             } else {
227                 if (isset($untracked[$forum->id])) {
228                         $unreadlink  = '-';
229                 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
230                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
231                     $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
232                                    $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
233                 } else {
234                     $unreadlink = '<span class="read">0</span>';
235                 }
237                 if ($forum->trackingtype == FORUM_TRACKING_ON) {
238                     $trackedlink = $stryes;
240                 } else {
241                     $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
242                     if (!isset($untracked[$forum->id])) {
243                         $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
244                     } else {
245                         $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
246                     }
247                 }
248             }
249         }
251         $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
252         $forumname = format_string($forum->name, true);
254         if ($cm->visible) {
255             $style = '';
256         } else {
257             $style = 'class="dimmed"';
258         }
259         $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
260         $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
262         $row = array ($forumlink, $forum->intro, $discussionlink);
263         if ($usetracking) {
264             $row[] = $unreadlink;
265             $row[] = $trackedlink;    // Tracking.
266         }
268         if ($can_subscribe) {
269             if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
270                 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
271                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
272                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
273             } else {
274                 $row[] = '-';
275             }
277             $digestoptions_selector->url->param('id', $forum->id);
278             if ($forum->maildigest === null) {
279                 $digestoptions_selector->selected = -1;
280             } else {
281                 $digestoptions_selector->selected = $forum->maildigest;
282             }
283             $row[] = $OUTPUT->render($digestoptions_selector);
284         }
286         //If this forum has RSS activated, calculate it
287         if ($show_rss) {
288             if ($forum->rsstype and $forum->rssarticles) {
289                 //Calculate the tooltip text
290                 if ($forum->rsstype == 1) {
291                     $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
292                 } else {
293                     $tooltiptext = get_string('rsssubscriberssposts', 'forum');
294                 }
296                 if (!isloggedin() && $course->id == SITEID) {
297                     $userid = guest_user()->id;
298                 } else {
299                     $userid = $USER->id;
300                 }
301                 //Get html code for RSS link
302                 $row[] = rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
303             } else {
304                 $row[] = '&nbsp;';
305             }
306         }
308         $generaltable->data[] = $row;
309     }
313 // Start of the table for Learning Forums
314 $learningtable = new html_table();
315 $learningtable->head  = array ($strforum, $strdescription, $strdiscussions);
316 $learningtable->align = array ('left', 'left', 'center');
318 if ($usetracking) {
319     $learningtable->head[] = $strunreadposts;
320     $learningtable->align[] = 'center';
322     $learningtable->head[] = $strtracking;
323     $learningtable->align[] = 'center';
326 if ($can_subscribe) {
327     $learningtable->head[] = $strsubscribed;
328     $learningtable->align[] = 'center';
330     $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
331     $learningtable->align[] = 'center';
334 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
335                  isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
336                  $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
337     $learningtable->head[] = $strrss;
338     $learningtable->align[] = 'center';
341 /// Now let's process the learning forums
343 if ($course->id != SITEID) {    // Only real courses have learning forums
344     // 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
345     $strsectionname  = get_string('sectionname', 'format_'.$course->format);
346     // Add extra field for section number, at the front
347     array_unshift($learningtable->head, $strsectionname);
348     array_unshift($learningtable->align, 'center');
351     if ($learningforums) {
352         $currentsection = '';
353             foreach ($learningforums as $forum) {
354             $cm      = $modinfo->instances['forum'][$forum->id];
355             $context = context_module::instance($cm->id);
357             $count = forum_count_discussions($forum, $cm, $course);
359             if ($usetracking) {
360                 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
361                     $unreadlink  = '-';
362                     $trackedlink = '-';
364                 } else {
365                     if (isset($untracked[$forum->id])) {
366                         $unreadlink  = '-';
367                     } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
368                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
369                         $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
370                                        $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/markasread') . '" alt="'.$strmarkallread.'" class="iconsmall" /></a></span>';
371                     } else {
372                         $unreadlink = '<span class="read">0</span>';
373                     }
375                     if ($forum->trackingtype == FORUM_TRACKING_ON) {
376                         $trackedlink = $stryes;
378                     } else {
379                         $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
380                         if (!isset($untracked[$forum->id])) {
381                             $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
382                         } else {
383                             $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
384                         }
385                     }
386                 }
387             }
389             $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
391             if ($cm->sectionnum != $currentsection) {
392                 $printsection = get_section_name($course, $cm->sectionnum);
393                 if ($currentsection) {
394                     $learningtable->data[] = 'hr';
395                 }
396                 $currentsection = $cm->sectionnum;
397             } else {
398                 $printsection = '';
399             }
401             $forumname = format_string($forum->name,true);
403             if ($cm->visible) {
404                 $style = '';
405             } else {
406                 $style = 'class="dimmed"';
407             }
408             $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
409             $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
411             $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
412             if ($usetracking) {
413                 $row[] = $unreadlink;
414                 $row[] = $trackedlink;    // Tracking.
415             }
417             if ($can_subscribe) {
418                 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
419                     $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
420                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
421                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
422                 } else {
423                     $row[] = '-';
424                 }
426                 $digestoptions_selector->url->param('id', $forum->id);
427                 if ($forum->maildigest === null) {
428                     $digestoptions_selector->selected = -1;
429                 } else {
430                     $digestoptions_selector->selected = $forum->maildigest;
431                 }
432                 $row[] = $OUTPUT->render($digestoptions_selector);
433             }
435             //If this forum has RSS activated, calculate it
436             if ($show_rss) {
437                 if ($forum->rsstype and $forum->rssarticles) {
438                     //Calculate the tolltip text
439                     if ($forum->rsstype == 1) {
440                         $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
441                     } else {
442                         $tooltiptext = get_string('rsssubscriberssposts', 'forum');
443                     }
444                     //Get html code for RSS link
445                     $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
446                 } else {
447                     $row[] = '&nbsp;';
448                 }
449             }
451             $learningtable->data[] = $row;
452         }
453     }
457 /// Output the page
458 $PAGE->navbar->add($strforums);
459 $PAGE->set_title("$course->shortname: $strforums");
460 $PAGE->set_heading($course->fullname);
461 $PAGE->set_button($searchform);
462 echo $OUTPUT->header();
464 // Show the subscribe all options only to non-guest, enrolled users
465 if (!isguestuser() && isloggedin() && $can_subscribe) {
466     echo $OUTPUT->box_start('subscription');
467     echo html_writer::tag('div',
468         html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
469             get_string('allsubscribe', 'forum')),
470         array('class'=>'helplink'));
471     echo html_writer::tag('div',
472         html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
473             get_string('allunsubscribe', 'forum')),
474         array('class'=>'helplink'));
475     echo $OUTPUT->box_end();
476     echo $OUTPUT->box('&nbsp;', 'clearer');
479 if ($generalforums) {
480     echo $OUTPUT->heading(get_string('generalforums', 'forum'), 2);
481     echo html_writer::table($generaltable);
484 if ($learningforums) {
485     echo $OUTPUT->heading(get_string('learningforums', 'forum'), 2);
486     echo html_writer::table($learningtable);
489 echo $OUTPUT->footer();