ce365c705cdba89136153078026a3f3edb7f6518
[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 = get_context_instance(CONTEXT_COURSE, $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 $strsectionname  = get_string('sectionname', 'format_'.$course->format);
73 $searchform = forum_search_form($course);
76 // Start of the table for General Forums
78 $generaltable = new html_table();
79 $generaltable->head  = array ($strforum, $strdescription, $strdiscussions);
80 $generaltable->align = array ('left', 'left', 'center');
82 if ($usetracking = forum_tp_can_track_forums()) {
83     $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
85     $generaltable->head[] = $strunreadposts;
86     $generaltable->align[] = 'center';
88     $generaltable->head[] = $strtracking;
89     $generaltable->align[] = 'center';
90 }
92 $subscribed_forums = forum_get_subscribed_forums($course);
94 $can_subscribe = is_enrolled($coursecontext);
95 if ($can_subscribe) {
96     $generaltable->head[] = $strsubscribed;
97     $generaltable->align[] = 'center';
98 }
100 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
101                  isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
102                  $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
103     $generaltable->head[] = $strrss;
104     $generaltable->align[] = 'center';
107 $usesections = course_format_uses_sections($course->format);
108 $sections = get_all_sections($course->id);
110 $table = new html_table();
112 // Parse and organise all the forums.  Most forums are course modules but
113 // some special ones are not.  These get placed in the general forums
114 // category with the forums in section 0.
116 $forums = $DB->get_records('forum', array('course' => $course->id));
118 $generalforums  = array();
119 $learningforums = array();
120 $modinfo =& get_fast_modinfo($course);
122 if (!isset($modinfo->instances['forum'])) {
123     $modinfo->instances['forum'] = array();
126 foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
127     if (!$cm->uservisible or !isset($forums[$forumid])) {
128         continue;
129     }
131     $forum = $forums[$forumid];
133     if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
134         continue;   // Shouldn't happen
135     }
137     if (!has_capability('mod/forum:viewdiscussion', $context)) {
138         continue;
139     }
141     // fill two type array - order in modinfo is the same as in course
142     if ($forum->type == 'news' or $forum->type == 'social') {
143         $generalforums[$forum->id] = $forum;
145     } else if ($course->id == SITEID or empty($cm->sectionnum)) {
146         $generalforums[$forum->id] = $forum;
148     } else {
149         $learningforums[$forum->id] = $forum;
150     }
153 /// Do course wide subscribe/unsubscribe
154 if (!is_null($subscribe) and !isguestuser()) {
155     foreach ($modinfo->instances['forum'] as $forumid=>$cm) {
156         $forum = $forums[$forumid];
157         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
158         $cansub = false;
160         if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
161             $cansub = true;
162         }
163         if ($cansub && $cm->visible == 0 &&
164             !has_capability('mod/forum:managesubscriptions', $modcontext))
165         {
166             $cansub = false;
167         }
168         if (!forum_is_forcesubscribed($forum)) {
169             $subscribed = forum_is_subscribed($USER->id, $forum);
170             if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
171                 forum_subscribe($USER->id, $forumid);
172             } else if (!$subscribe && $subscribed) {
173                 forum_unsubscribe($USER->id, $forumid);
174             }
175         }
176     }
177     $returnto = forum_go_back_to("index.php?id=$course->id");
178     if ($subscribe) {
179         add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
180         redirect($returnto, get_string('nowallsubscribed', 'forum', format_string($course->shortname)), 1);
181     } else {
182         add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
183         redirect($returnto, get_string('nowallunsubscribed', 'forum', format_string($course->shortname)), 1);
184     }
187 /// First, let's process the general forums and build up a display
189 if ($generalforums) {
190     foreach ($generalforums as $forum) {
191         $cm      = $modinfo->instances['forum'][$forum->id];
192         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
194         $count = forum_count_discussions($forum, $cm, $course);
196         if ($usetracking) {
197             if ($forum->trackingtype == FORUM_TRACKING_OFF) {
198                 $unreadlink  = '-';
199                 $trackedlink = '-';
201             } else {
202                 if (isset($untracked[$forum->id])) {
203                         $unreadlink  = '-';
204                 } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
205                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
206                     $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
207                                    $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/clear') . '" alt="'.$strmarkallread.'" /></a></span>';
208                 } else {
209                     $unreadlink = '<span class="read">0</span>';
210                 }
212                 if ($forum->trackingtype == FORUM_TRACKING_ON) {
213                     $trackedlink = $stryes;
215                 } else {
216                     $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
217                     if (!isset($untracked[$forum->id])) {
218                         $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
219                     } else {
220                         $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
221                     }
222                 }
223             }
224         }
226         $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
227         $forumname = format_string($forum->name, true);;
229         if ($cm->visible) {
230             $style = '';
231         } else {
232             $style = 'class="dimmed"';
233         }
234         $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
235         $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
237         $row = array ($forumlink, $forum->intro, $discussionlink);
238         if ($usetracking) {
239             $row[] = $unreadlink;
240             $row[] = $trackedlink;    // Tracking.
241         }
243         if ($can_subscribe) {
244             if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
245                 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
246                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
247                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
248             } else {
249                 $row[] = '-';
250             }
251         }
253         //If this forum has RSS activated, calculate it
254         if ($show_rss) {
255             if ($forum->rsstype and $forum->rssarticles) {
256                 //Calculate the tooltip text
257                 if ($forum->rsstype == 1) {
258                     $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
259                 } else {
260                     $tooltiptext = get_string('rsssubscriberssposts', 'forum');
261                 }
262                 //Get html code for RSS link
263                 $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
264             } else {
265                 $row[] = '&nbsp;';
266             }
267         }
269         $generaltable->data[] = $row;
270     }
274 // Start of the table for Learning Forums
275 $learningtable = new html_table();
276 $learningtable->head  = array ($strforum, $strdescription, $strdiscussions);
277 $learningtable->align = array ('left', 'left', 'center');
279 if ($usetracking) {
280     $learningtable->head[] = $strunreadposts;
281     $learningtable->align[] = 'center';
283     $learningtable->head[] = $strtracking;
284     $learningtable->align[] = 'center';
287 if ($can_subscribe) {
288     $learningtable->head[] = $strsubscribed;
289     $learningtable->align[] = 'center';
292 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
293                  isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
294                  $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
295     $learningtable->head[] = $strrss;
296     $learningtable->align[] = 'center';
299 /// Now let's process the learning forums
301 if ($course->id != SITEID) {    // Only real courses have learning forums
302     // Add extra field for section number, at the front
303     array_unshift($learningtable->head, $strsectionname);
304     array_unshift($learningtable->align, 'center');
307     if ($learningforums) {
308         $currentsection = '';
309             foreach ($learningforums as $forum) {
310             $cm      = $modinfo->instances['forum'][$forum->id];
311             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
313             $count = forum_count_discussions($forum, $cm, $course);
315             if ($usetracking) {
316                 if ($forum->trackingtype == FORUM_TRACKING_OFF) {
317                     $unreadlink  = '-';
318                     $trackedlink = '-';
320                 } else {
321                     if (isset($untracked[$forum->id])) {
322                         $unreadlink  = '-';
323                     } else if ($unread = forum_tp_count_forum_unread_posts($cm, $course)) {
324                         $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
325                         $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
326                                        $forum->id.'&amp;mark=read"><img src="'.$OUTPUT->pix_url('t/clear') . '" alt="'.$strmarkallread.'" /></a></span>';
327                     } else {
328                         $unreadlink = '<span class="read">0</span>';
329                     }
331                     if ($forum->trackingtype == FORUM_TRACKING_ON) {
332                         $trackedlink = $stryes;
334                     } else {
335                         $aurl = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
336                         if (!isset($untracked[$forum->id])) {
337                             $trackedlink = $OUTPUT->single_button($aurl, $stryes, 'post', array('title'=>$strnotrackforum));
338                         } else {
339                             $trackedlink = $OUTPUT->single_button($aurl, $strno, 'post', array('title'=>$strtrackforum));
340                         }
341                     }
342                 }
343             }
345             $forum->intro = shorten_text(format_module_intro('forum', $forum, $cm->id), $CFG->forum_shortpost);
347             if ($cm->sectionnum != $currentsection) {
348                 $printsection = get_section_name($course, $sections[$cm->sectionnum]);
349                 if ($currentsection) {
350                     $learningtable->data[] = 'hr';
351                 }
352                 $currentsection = $cm->sectionnum;
353             } else {
354                 $printsection = '';
355             }
357             $forumname = format_string($forum->name,true);;
359             if ($cm->visible) {
360                 $style = '';
361             } else {
362                 $style = 'class="dimmed"';
363             }
364             $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
365             $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
367             $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
368             if ($usetracking) {
369                 $row[] = $unreadlink;
370                 $row[] = $trackedlink;    // Tracking.
371             }
373             if ($can_subscribe) {
374                 if ($forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE) {
375                     $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
376                         'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
377                         'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
378                 } else {
379                     $row[] = '-';
380                 }
381             }
383             //If this forum has RSS activated, calculate it
384             if ($show_rss) {
385                 if ($forum->rsstype and $forum->rssarticles) {
386                     //Calculate the tolltip text
387                     if ($forum->rsstype == 1) {
388                         $tooltiptext = get_string('rsssubscriberssdiscussions', 'forum');
389                     } else {
390                         $tooltiptext = get_string('rsssubscriberssposts', 'forum');
391                     }
392                     //Get html code for RSS link
393                     $row[] = rss_get_link($context->id, $USER->id, 'mod_forum', $forum->id, $tooltiptext);
394                 } else {
395                     $row[] = '&nbsp;';
396                 }
397             }
399             $learningtable->data[] = $row;
400         }
401     }
405 /// Output the page
406 $PAGE->navbar->add($strforums);
407 $PAGE->set_title("$course->shortname: $strforums");
408 $PAGE->set_heading($course->fullname);
409 $PAGE->set_button($searchform);
410 echo $OUTPUT->header();
412 if (!isguestuser()) {
413     echo $OUTPUT->box_start('subscription');
414     echo html_writer::tag('div',
415         html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
416             get_string('allsubscribe', 'forum')),
417         array('class'=>'helplink'));
418     echo html_writer::tag('div',
419         html_writer::link(new moodle_url('/mod/forum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
420             get_string('allunsubscribe', 'forum')),
421         array('class'=>'helplink'));
422     echo $OUTPUT->box_end();
423     echo $OUTPUT->box('&nbsp;', 'clearer');
426 if ($generalforums) {
427     echo $OUTPUT->heading(get_string('generalforums', 'forum'));
428     echo html_writer::table($generaltable);
431 if ($learningforums) {
432     echo $OUTPUT->heading(get_string('learningforums', 'forum'));
433     echo html_writer::table($learningtable);
436 echo $OUTPUT->footer();