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