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