Automatic installer.php lang files by installer_builder (20060808)
[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
70e3da07 7 $id = optional_param('id',0,PARAM_INT); // course
03877b04 8 $subscribe = optional_param('subscribe',null,PARAM_INT); // Subscribe/Unsubscribe all forums
f93f848a 9
10 if ($id) {
11 if (! $course = get_record("course", "id", $id)) {
12 error("Course ID is incorrect");
13 }
14 } else {
f781b794 15 if (! $course = get_site()) {
f93f848a 16 error("Could not find a top-level course!");
17 }
18 }
19
535136bd 20 require_course_login($course);
f93f848a 21
ecc8403e 22 $currentgroup = get_current_group($course->id);
23
501cdbd8 24 unset($SESSION->fromdiscussion);
f93f848a 25
f781b794 26 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
27
28 $strforums = get_string("forums", "forum");
97485d07 29 $strforum = get_string("forum", "forum");
30 $strdescription = get_string("description");
31 $strdiscussions = get_string("discussions", "forum");
32 $strsubscribed = get_string("subscribed", "forum");
f37da850 33 $strunreadposts = get_string("unreadposts", "forum");
38994d6f 34 $strtracking = get_string('tracking', 'forum');
6de6cd5a 35 $strmarkallread = get_string('markallread', 'forum');
36 $strtrackforum = get_string('trackforum', 'forum');
37 $strnotrackforum = get_string('notrackforum', 'forum');
38 $strsubscribe = get_string('subscribe', 'forum');
39 $strunsubscribe = get_string('unsubscribe', 'forum');
40 $stryes = get_string('yes');
41 $strno = get_string('no');
33f48f03 42 $strrss = get_string("rss");
97485d07 43
6f1cc8d6 44 $searchform = forum_search_form($course);
f93f848a 45
f781b794 46
38fcc215 47 // Start of the table for General Forums
f781b794 48
fa5a5b52 49 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
9fd7165a 50 $generaltable->align = array ('left', 'left', 'center');
f37da850 51
c2468824 52 if ($usetracking = forum_tp_can_track_forums()) {
eaf50aef 53 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 54
f37da850 55 $generaltable->head[] = $strunreadposts;
9fd7165a 56 $generaltable->align[] = 'center';
38994d6f 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
03877b04 127 /// Do course wide subscribe/unsubscribe
128 if (!is_null($subscribe) && !isguest()) {
129 $allforums = array_merge($generalforums, $learningforums);
130 if ($allforums) {
131 foreach ($allforums as $forum) {
132 if (!forum_is_forcesubscribed($forum->id)) {
133 $subscribed = forum_is_subscribed($USER->id, $forum->id);
134 if ($subscribe && !$subscribed) {
135 forum_subscribe($USER->id, $forum->id);
136 } elseif (!$subscribe && $subscribed) {
137 forum_unsubscribe($USER->id, $forum->id);
138 }
139 }
140 }
141 }
142 $returnto = forum_go_back_to("index.php?id=$course->id");
143 if ($subscribe) {
144 add_to_log($course->id, "forum", "subscribeall", "index.php?id=$course->id", $course->id);
145 redirect($returnto, get_string("nowallsubscribed", "forum", $course->shortname), 1);
146 } else {
147 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
148 redirect($returnto, get_string("nowallunsubscribed", "forum", $course->shortname), 1);
149 }
150 }
151
ecc8403e 152 /// First, let's process the general forums and build up a display
153
154 if ($generalforums) {
155 foreach ($generalforums as $forum) {
156 if (isset($forum->groupmode)) {
157 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
158 } else {
159 $groupmode = NOGROUPS;
160 }
8f0cd6ef 161
ecc8403e 162 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
2862b309 163 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
ecc8403e 164 } else {
165 $count = count_records("forum_discussions", "forum", "$forum->id");
166 }
8f0cd6ef 167
eaf50aef 168 if ($usetracking) {
169 if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) {
38994d6f 170 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
171 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
172 if ($unread > 0) {
6de6cd5a 173 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 174 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 175 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 176 } else {
177 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
178 }
630bd96b 179
eaf50aef 180 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
181 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
182 $forum->id.'">'.$stryes.'</a>';
183 }
630bd96b 184 else {
185 $trackedlink = $stryes;
186 }
f37da850 187 } else {
6de6cd5a 188 $unreadlink = '-';
eaf50aef 189 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.
190 $forum->id.'">'.$strno.'</a>';
f37da850 191 }
192 }
193
9a0eb679 194 $introoptions->para=false;
91121c89 195 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
ecc8403e 196
197 if ($forum->visible) {
3849dae8 198 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 199 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 200 } else {
3849dae8 201 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 202 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 203 }
204
f4646b44 205 //If this forum has RSS activated, calculate it
206 $rsslink = '';
207 if ($show_rss) {
208 if ($forum->rsstype and $forum->rssarticles) {
209 //Calculate the tolltip text
210 if ($forum->rsstype == 1) {
3849dae8 211 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 212 } else {
3849dae8 213 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 214 }
215 if (empty($USER->id)) {
216 $userid = 0;
217 } else {
218 $userid = $USER->id;
219 }
220 //Get html code for RSS link
221 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
222 }
223 }
224
ecc8403e 225 if ($can_subscribe) {
226 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 227 $sublink = $stryes;
ecc8403e 228 } else {
229 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
6de6cd5a 230 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
3849dae8 231 $forumlink = format_string($forum->name,true);
ecc8403e 232 } else {
233 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 234 $subscribed = $stryes;
ecc8403e 235 $subtitle = get_string("unsubscribe", "forum");
236 } else {
6de6cd5a 237 $subscribed = $strno;
ecc8403e 238 $subtitle = get_string("subscribe", "forum");
239 }
098d27d4 240 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !isteacher($forum->course)) {
241 $sublink = '-';
242 } else {
243 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
244 }
ecc8403e 245 }
246 }
9a0eb679 247 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 248 if ($usetracking) {
f37da850 249 $row[] = $unreadlink;
38994d6f 250 $row[] = $trackedlink; // Tracking.
f37da850 251 }
252 $row[] = $sublink;
a9022f9e 253 if ($show_rss) {
f37da850 254 $row[] = $rsslink;
33f48f03 255 }
f37da850 256 $generaltable->data[] = $row;
ecc8403e 257 } else {
9a0eb679 258 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 259 if ($usetracking) {
f37da850 260 $row[] = $unreadlink;
38994d6f 261 $row[] = $trackedlink; // Tracking.
f37da850 262 }
a9022f9e 263 if ($show_rss) {
f37da850 264 $row[] = $rsslink;
f4646b44 265 }
f37da850 266 $generaltable->data[] = $row;
ecc8403e 267 }
268 }
8f0cd6ef 269 }
ecc8403e 270
38fcc215 271
272 // Start of the table for Learning Forums
273 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
274 $learningtable->align = array ("left", "left", "center");
275
c2468824 276 if ($usetracking) {
f37da850 277 $learningtable->head[] = $strunreadposts;
9fd7165a 278 $learningtable->align[] = 'center';
38994d6f 279
280 $learningtable->head[] = $strtracking;
281 $learningtable->align[] = 'center';
f37da850 282 }
283
f4646b44 284 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
38fcc215 285 $learningtable->head[] = $strsubscribed;
9fd7165a 286 $learningtable->align[] = 'center';
38fcc215 287 }
288
ec81373f 289 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 290 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 291 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
292 $learningtable->head[] = $strrss;
9fd7165a 293 $learningtable->align[] = 'center';
38fcc215 294 }
295
bd83d0a3 296 /// Now let's process the learning forums
ecc8403e 297
94361e02 298 if ($course->category) { // Only real courses have learning forums
299 // Add extra field for section number, at the front
fa5a5b52 300 array_unshift($learningtable->head, "");
301 array_unshift($learningtable->align, "center");
8f0cd6ef 302
a0b9571f 303
86c5e69a 304 if ($learningforums) {
a0b9571f 305 $currentsection = "";
306
1390f203 307 foreach ($learningforums as $key => $forum) {
ecc8403e 308 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
8f0cd6ef 309
ecc8403e 310 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 311 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 312 } else {
313 $count = count_records("forum_discussions", "forum", "$forum->id");
314 }
8f0cd6ef 315
eaf50aef 316 if ($usetracking) {
317 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
318 !isset($untracked[$forum->id])) {
38994d6f 319 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
320 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
321 if ($unread > 0) {
6de6cd5a 322 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 323 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 324 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 325 } else {
331c512a 326 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 327 }
eaf50aef 328 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
329 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
330 $forum->id.'">'.$stryes.'</a>';
331 }
f37da850 332 } else {
6de6cd5a 333 $unreadlink = '-';
334 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 335 }
336 }
337
9a0eb679 338 $introoptions->para=false;
91121c89 339 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
8f0cd6ef 340
a0b9571f 341 if ($forum->section != $currentsection) {
342 $printsection = $forum->section;
ef6a649b 343 if ($currentsection) {
344 $learningtable->data[] = 'hr';
345 }
a0b9571f 346 $currentsection = $forum->section;
347 } else {
348 $printsection = "";
349 }
350
580f2fbc 351 if ($forum->visible) {
3849dae8 352 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 353 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 354 } else {
3849dae8 355 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 356 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 357 }
8f0cd6ef 358
f4646b44 359 //If this forum has RSS activated, calculate it
360 $rsslink = '';
361 if ($show_rss) {
362 if ($forum->rsstype and $forum->rssarticles) {
363 //Calculate the tolltip text
364 if ($forum->rsstype == 1) {
3849dae8 365 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 366 } else {
3849dae8 367 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 368 }
369 if (empty($USER->id)) {
370 $userid = 0;
371 } else {
372 $userid = $USER->id;
373 }
374 //Get html code for RSS link
375 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
376 }
377 }
ec81373f 378
94361e02 379 if ($can_subscribe) {
380 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 381 $sublink = $stryes;
501cdbd8 382 } else {
ecc8403e 383 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
6de6cd5a 384 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
bd83d0a3 385 if ($groupmode == SEPARATEGROUPS) {
3849dae8 386 $forumlink = format_string($forum->name,true);
bd83d0a3 387 }
94361e02 388 } else {
a0b9571f 389 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 390 $subscribed = $stryes;
391 $subtitle = $strunsubscribe;
a0b9571f 392 } else {
6de6cd5a 393 $subscribed = $strno;
394 $subtitle = $strsubscribe;
a0b9571f 395 }
396 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 397 }
501cdbd8 398 }
f37da850 399
9a0eb679 400 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 401 if ($usetracking) {
f37da850 402 $row[] = $unreadlink;
38994d6f 403 $row[] = $trackedlink; // Tracking.
f37da850 404 }
405 $row[] = $sublink;
a9022f9e 406 if ($show_rss) {
f37da850 407 $row[] = $rsslink;
33f48f03 408 }
f37da850 409 $learningtable->data[] = $row;
410
94361e02 411 } else {
9a0eb679 412 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 413 if ($usetracking) {
f37da850 414 $row[] = $unreadlink;
38994d6f 415 $row[] = $trackedlink; // Tracking.
f37da850 416 }
a9022f9e 417 if ($show_rss) {
f37da850 418 $row[] = $rsslink;
f4646b44 419 }
f37da850 420 $learningtable->data[] = $row;
f93f848a 421 }
f93f848a 422 }
423 }
424 }
425
fa5a5b52 426
427 /// Output the page
428
429 if ($course->category) {
430 print_header("$course->shortname: $strforums", "$course->fullname",
38fcc215 431 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $strforums",
fa5a5b52 432 "", "", true, $searchform, navmenu($course));
433 } else {
8f0cd6ef 434 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
fa5a5b52 435 "", "", true, $searchform, navmenu($course));
436 }
437
03877b04 438 if (!isguest()) {
439 echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top"><td align="right" class="subscription">';
440 echo '<span class="helplink">';
441 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
442 echo '<br />';
443 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
444 echo '</span></td></tr></table>';
445 }
446
6031fe85 447 if ($generalforums) {
448 print_heading(get_string("generalforums", "forum"));
449 print_table($generaltable);
450 }
fa5a5b52 451
6031fe85 452 if ($learningforums) {
453 print_heading(get_string("learningforums", "forum"));
454 print_table($learningtable);
455 }
f93f848a 456
457 print_footer($course);
458
3849dae8 459?>