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