Recently improved navbar now supports filterall.
[moodle.git] / mod / forum / index.php
CommitLineData
41905731 1<?php // $Id$
f93f848a 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
48b29ba4 5 require_once("$CFG->libdir/rsslib.php");
f93f848a 6
7 optional_variable($id); // course
8
9 if ($id) {
10 if (! $course = get_record("course", "id", $id)) {
11 error("Course ID is incorrect");
12 }
13 } else {
f781b794 14 if (! $course = get_site()) {
f93f848a 15 error("Could not find a top-level course!");
16 }
17 }
18
535136bd 19 require_course_login($course);
f93f848a 20
ecc8403e 21 $currentgroup = get_current_group($course->id);
22
501cdbd8 23 unset($SESSION->fromdiscussion);
f93f848a 24
f781b794 25 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
26
27 $strforums = get_string("forums", "forum");
97485d07 28 $strforum = get_string("forum", "forum");
29 $strdescription = get_string("description");
30 $strdiscussions = get_string("discussions", "forum");
31 $strsubscribed = get_string("subscribed", "forum");
f37da850 32 $strunreadposts = get_string("unreadposts", "forum");
33f48f03 33 $strrss = get_string("rss");
97485d07 34
6f1cc8d6 35 $searchform = forum_search_form($course);
f93f848a 36
f781b794 37
38fcc215 38 // Start of the table for General Forums
f781b794 39
fa5a5b52 40 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
9fd7165a 41 $generaltable->align = array ('left', 'left', 'center');
f37da850 42
43 if ($CFG->forum_trackreadposts) {
44 $generaltable->head[] = $strunreadposts;
9fd7165a 45 $generaltable->align[] = 'center';
f37da850 46 }
f93f848a 47
f4646b44 48 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
fa5a5b52 49 $generaltable->head[] = $strsubscribed;
9fd7165a 50 $generaltable->align[] = 'center';
f93f848a 51 }
f93f848a 52
ec81373f 53 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 54 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
33f48f03 55 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
56 $generaltable->head[] = $strrss;
9fd7165a 57 $generaltable->align[] = 'center';
33f48f03 58 }
59
fa5a5b52 60
8f0cd6ef 61 // Parse and organise all the forums. Most forums are course modules but
62 // some special ones are not. These get placed in the general forums
ecc8403e 63 // category with the forums in section 0.
64
65 $generalforums = array(); // For now
66 $learningforums = get_all_instances_in_course("forum", $course);
67
68 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
69
70 if ($learningforums) { // Copy "full" data into this complete array
bd83d0a3 71 foreach ($learningforums as $key => $learningforum) {
72 $learningforum->keyreference = $key;
a0a4a3c9 73 $forums[$learningforum->id] = $learningforum;
ecc8403e 74 }
75 }
fa5a5b52 76
71fb9630 77 foreach ($forums as $forum) {
a0a4a3c9 78 if (!isset($forum->visible)) {
9c8b8eee 79 $forum->visible = instance_is_visible("forum", $forum);
80 if (!$forum->visible and !isteacher($course->id)) {
81 if (isset($forum->keyreference)) {
82 unset($learningforums[$forum->keyreference]);
83 }
84 continue;
85 }
a0a4a3c9 86 }
37b15514 87 switch ($forum->type) {
0872b023 88 case "news":
89 case "social":
90 $generalforums[] = $forum;
bd83d0a3 91 if (isset($forum->keyreference)) { // Should always be
92 unset($learningforums[$forum->keyreference]);
ecc8403e 93 }
37b15514 94 break;
8f0cd6ef 95 case "teacher":
37b15514 96 if (isteacher($course->id)) {
ecc8403e 97 $forum->visible = true;
37b15514 98 $generalforums[] = $forum;
99 }
100 break;
94361e02 101 default:
a0a4a3c9 102 if (!$course->category or empty($forum->section)) { // Site level or section 0
94361e02 103 $generalforums[] = $forum;
bd83d0a3 104 if (isset($forum->keyreference)) {
105 unset($learningforums[$forum->keyreference]);
a0a4a3c9 106 }
8f0cd6ef 107 }
94361e02 108 break;
71fb9630 109 }
37b15514 110 }
111 }
71fb9630 112
ecc8403e 113 /// First, let's process the general forums and build up a display
114
115 if ($generalforums) {
116 foreach ($generalforums as $forum) {
117 if (isset($forum->groupmode)) {
118 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
119 } else {
120 $groupmode = NOGROUPS;
121 }
8f0cd6ef 122
ecc8403e 123 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
2862b309 124 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
ecc8403e 125 } else {
126 $count = count_records("forum_discussions", "forum", "$forum->id");
127 }
8f0cd6ef 128
f37da850 129 if ($CFG->forum_trackreadposts) {
130 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
1051a2a4 131 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
f37da850 132 if ($unread > 0) {
d0388ebe 133 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
f37da850 134 } else {
d0388ebe 135 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
f37da850 136 }
137 }
138
9a0eb679 139 $introoptions->para=false;
140 $forum->intro = format_text(shorten_text(trim($forum->intro), $CFG->forum_shortpost), FORMAT_HTML, $introoptions);
ecc8403e 141
142 if ($forum->visible) {
3849dae8 143 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 144 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 145 } else {
3849dae8 146 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 147 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 148 }
149
f4646b44 150 //If this forum has RSS activated, calculate it
151 $rsslink = '';
152 if ($show_rss) {
153 if ($forum->rsstype and $forum->rssarticles) {
154 //Calculate the tolltip text
155 if ($forum->rsstype == 1) {
3849dae8 156 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 157 } else {
3849dae8 158 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 159 }
160 if (empty($USER->id)) {
161 $userid = 0;
162 } else {
163 $userid = $USER->id;
164 }
165 //Get html code for RSS link
166 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
167 }
168 }
169
ecc8403e 170 if ($can_subscribe) {
171 if (forum_is_forcesubscribed($forum->id)) {
172 $sublink = get_string("yes");
173 } else {
174 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
175 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
3849dae8 176 $forumlink = format_string($forum->name,true);
ecc8403e 177 } else {
178 if (forum_is_subscribed($USER->id, $forum->id)) {
179 $subscribed = get_string("yes");
180 $subtitle = get_string("unsubscribe", "forum");
181 } else {
182 $subscribed = get_string("no");
183 $subtitle = get_string("subscribe", "forum");
184 }
185 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
186 }
187 }
9a0eb679 188 $row = array ($forumlink, $forum->intro, $discussionlink);
f37da850 189 if ($CFG->forum_trackreadposts) {
190 $row[] = $unreadlink;
191 }
192 $row[] = $sublink;
f4646b44 193 if (!empty($rsslink)) {
f37da850 194 $row[] = $rsslink;
33f48f03 195 }
f37da850 196 $generaltable->data[] = $row;
ecc8403e 197 } else {
9a0eb679 198 $row = array ($forumlink, $forum->intro, $discussionlink);
f37da850 199 if ($CFG->forum_trackreadposts) {
200 $row[] = $unreadlink;
201 }
f4646b44 202 if (!empty($rsslink)) {
f37da850 203 $row[] = $rsslink;
f4646b44 204 }
f37da850 205 $generaltable->data[] = $row;
ecc8403e 206 }
207 }
8f0cd6ef 208 }
ecc8403e 209
38fcc215 210
211 // Start of the table for Learning Forums
212 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
213 $learningtable->align = array ("left", "left", "center");
214
f37da850 215 if ($CFG->forum_trackreadposts) {
216 $learningtable->head[] = $strunreadposts;
9fd7165a 217 $learningtable->align[] = 'center';
f37da850 218 }
219
f4646b44 220 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
38fcc215 221 $learningtable->head[] = $strsubscribed;
9fd7165a 222 $learningtable->align[] = 'center';
38fcc215 223 }
224
ec81373f 225 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 226 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 227 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
228 $learningtable->head[] = $strrss;
9fd7165a 229 $learningtable->align[] = 'center';
38fcc215 230 }
231
bd83d0a3 232 /// Now let's process the learning forums
ecc8403e 233
94361e02 234 if ($course->category) { // Only real courses have learning forums
235 // Add extra field for section number, at the front
fa5a5b52 236 array_unshift($learningtable->head, "");
237 array_unshift($learningtable->align, "center");
8f0cd6ef 238
a0b9571f 239
86c5e69a 240 if ($learningforums) {
a0b9571f 241 $currentsection = "";
242
1390f203 243 foreach ($learningforums as $key => $forum) {
ecc8403e 244 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
8f0cd6ef 245
ecc8403e 246 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 247 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 248 } else {
249 $count = count_records("forum_discussions", "forum", "$forum->id");
250 }
8f0cd6ef 251
f37da850 252 if ($CFG->forum_trackreadposts) {
253 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
1051a2a4 254 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
f37da850 255 if ($unread > 0) {
4c3b9e02 256 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
f37da850 257 } else {
4c3b9e02 258 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
f37da850 259 }
260 }
261
9a0eb679 262 $introoptions->para=false;
263 $forum->intro = format_text(shorten_text(trim($forum->intro), $CFG->forum_shortpost), FORMAT_HTML, $introoptions);
8f0cd6ef 264
a0b9571f 265 if ($forum->section != $currentsection) {
266 $printsection = $forum->section;
ef6a649b 267 if ($currentsection) {
268 $learningtable->data[] = 'hr';
269 }
a0b9571f 270 $currentsection = $forum->section;
271 } else {
272 $printsection = "";
273 }
274
580f2fbc 275 if ($forum->visible) {
3849dae8 276 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 277 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 278 } else {
3849dae8 279 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 280 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 281 }
8f0cd6ef 282
f4646b44 283 //If this forum has RSS activated, calculate it
284 $rsslink = '';
285 if ($show_rss) {
286 if ($forum->rsstype and $forum->rssarticles) {
287 //Calculate the tolltip text
288 if ($forum->rsstype == 1) {
3849dae8 289 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 290 } else {
3849dae8 291 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 292 }
293 if (empty($USER->id)) {
294 $userid = 0;
295 } else {
296 $userid = $USER->id;
297 }
298 //Get html code for RSS link
299 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
300 }
301 }
ec81373f 302
94361e02 303 if ($can_subscribe) {
304 if (forum_is_forcesubscribed($forum->id)) {
305 $sublink = get_string("yes");
501cdbd8 306 } else {
ecc8403e 307 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
a0b9571f 308 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
bd83d0a3 309 if ($groupmode == SEPARATEGROUPS) {
3849dae8 310 $forumlink = format_string($forum->name,true);
bd83d0a3 311 }
94361e02 312 } else {
a0b9571f 313 if (forum_is_subscribed($USER->id, $forum->id)) {
314 $subscribed = get_string("yes");
315 $subtitle = get_string("unsubscribe", "forum");
316 } else {
317 $subscribed = get_string("no");
318 $subtitle = get_string("subscribe", "forum");
319 }
320 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 321 }
501cdbd8 322 }
f37da850 323
9a0eb679 324 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
f37da850 325 if ($CFG->forum_trackreadposts) {
326 $row[] = $unreadlink;
327 }
328 $row[] = $sublink;
f4646b44 329 if (!empty($rsslink)) {
f37da850 330 $row[] = $rsslink;
33f48f03 331 }
f37da850 332 $learningtable->data[] = $row;
333
94361e02 334 } else {
9a0eb679 335 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
f37da850 336 if ($CFG->forum_trackreadposts) {
337 $row[] = $unreadlink;
338 }
f4646b44 339 if (!empty($rsslink)) {
f37da850 340 $row[] = $rsslink;
f4646b44 341 }
f37da850 342 $learningtable->data[] = $row;
f93f848a 343 }
f93f848a 344 }
345 }
346 }
347
fa5a5b52 348
349 /// Output the page
350
351 if ($course->category) {
352 print_header("$course->shortname: $strforums", "$course->fullname",
38fcc215 353 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $strforums",
fa5a5b52 354 "", "", true, $searchform, navmenu($course));
355 } else {
8f0cd6ef 356 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
fa5a5b52 357 "", "", true, $searchform, navmenu($course));
358 }
359
6031fe85 360 if ($generalforums) {
361 print_heading(get_string("generalforums", "forum"));
362 print_table($generaltable);
363 }
fa5a5b52 364
6031fe85 365 if ($learningforums) {
366 print_heading(get_string("learningforums", "forum"));
367 print_table($learningtable);
368 }
f93f848a 369
370 print_footer($course);
371
3849dae8 372?>