Merged from 1.7.
[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
f0da6b85 7 $id = optional_param('id', 0, PARAM_INT); // Course id
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);
ecc8403e 21 $currentgroup = get_current_group($course->id);
1febf1a7 22 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
583b57b4 23
ecc8403e 24
501cdbd8 25 unset($SESSION->fromdiscussion);
f93f848a 26
f781b794 27 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
28
29 $strforums = get_string("forums", "forum");
97485d07 30 $strforum = get_string("forum", "forum");
31 $strdescription = get_string("description");
32 $strdiscussions = get_string("discussions", "forum");
33 $strsubscribed = get_string("subscribed", "forum");
f37da850 34 $strunreadposts = get_string("unreadposts", "forum");
38994d6f 35 $strtracking = get_string('tracking', 'forum');
6de6cd5a 36 $strmarkallread = get_string('markallread', 'forum');
37 $strtrackforum = get_string('trackforum', 'forum');
38 $strnotrackforum = get_string('notrackforum', 'forum');
39 $strsubscribe = get_string('subscribe', 'forum');
40 $strunsubscribe = get_string('unsubscribe', 'forum');
41 $stryes = get_string('yes');
42 $strno = get_string('no');
33f48f03 43 $strrss = get_string("rss");
97485d07 44
6f1cc8d6 45 $searchform = forum_search_form($course);
f93f848a 46
f781b794 47
38fcc215 48 // Start of the table for General Forums
f781b794 49
fa5a5b52 50 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
9fd7165a 51 $generaltable->align = array ('left', 'left', 'center');
f37da850 52
c2468824 53 if ($usetracking = forum_tp_can_track_forums()) {
eaf50aef 54 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 55
f37da850 56 $generaltable->head[] = $strunreadposts;
9fd7165a 57 $generaltable->align[] = 'center';
38994d6f 58
38994d6f 59 $generaltable->head[] = $strtracking;
60 $generaltable->align[] = 'center';
f37da850 61 }
f93f848a 62
1febf1a7 63 if ($can_subscribe = has_capability('moodle/course:view', $coursecontext)) {
fa5a5b52 64 $generaltable->head[] = $strsubscribed;
9fd7165a 65 $generaltable->align[] = 'center';
f93f848a 66 }
f93f848a 67
ec81373f 68 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 69 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
33f48f03 70 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
71 $generaltable->head[] = $strrss;
9fd7165a 72 $generaltable->align[] = 'center';
33f48f03 73 }
74
fa5a5b52 75
8f0cd6ef 76 // Parse and organise all the forums. Most forums are course modules but
77 // some special ones are not. These get placed in the general forums
ecc8403e 78 // category with the forums in section 0.
79
80 $generalforums = array(); // For now
81 $learningforums = get_all_instances_in_course("forum", $course);
9fa33508 82
ecc8403e 83 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
84
85 if ($learningforums) { // Copy "full" data into this complete array
bd83d0a3 86 foreach ($learningforums as $key => $learningforum) {
87 $learningforum->keyreference = $key;
a0a4a3c9 88 $forums[$learningforum->id] = $learningforum;
ecc8403e 89 }
90 }
fa5a5b52 91
71fb9630 92 foreach ($forums as $forum) {
f0da6b85 93
94 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
95 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
96
b32c8dda 97 if (!has_capability('mod/forum:viewdiscussion', $context)) {
f0da6b85 98 if (isset($forum->keyreference)) {
99 unset($learningforums[$forum->keyreference]);
100 }
101 continue;
102 }
a0a4a3c9 103 if (!isset($forum->visible)) {
9c8b8eee 104 $forum->visible = instance_is_visible("forum", $forum);
0468976c 105 if (!$forum->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
9c8b8eee 106 if (isset($forum->keyreference)) {
107 unset($learningforums[$forum->keyreference]);
108 }
109 continue;
110 }
a0a4a3c9 111 }
37b15514 112 switch ($forum->type) {
0872b023 113 case "news":
114 case "social":
115 $generalforums[] = $forum;
bd83d0a3 116 if (isset($forum->keyreference)) { // Should always be
117 unset($learningforums[$forum->keyreference]);
ecc8403e 118 }
37b15514 119 break;
94361e02 120 default:
1936c10e 121 if (($course->id == SITEID) or empty($forum->section)) { // Site level or section 0
94361e02 122 $generalforums[] = $forum;
bd83d0a3 123 if (isset($forum->keyreference)) {
124 unset($learningforums[$forum->keyreference]);
a0a4a3c9 125 }
8f0cd6ef 126 }
94361e02 127 break;
71fb9630 128 }
37b15514 129 }
130 }
71fb9630 131
03877b04 132 /// Do course wide subscribe/unsubscribe
133 if (!is_null($subscribe) && !isguest()) {
134 $allforums = array_merge($generalforums, $learningforums);
135 if ($allforums) {
136 foreach ($allforums as $forum) {
137 if (!forum_is_forcesubscribed($forum->id)) {
138 $subscribed = forum_is_subscribed($USER->id, $forum->id);
139 if ($subscribe && !$subscribed) {
140 forum_subscribe($USER->id, $forum->id);
141 } elseif (!$subscribe && $subscribed) {
142 forum_unsubscribe($USER->id, $forum->id);
143 }
144 }
145 }
146 }
147 $returnto = forum_go_back_to("index.php?id=$course->id");
148 if ($subscribe) {
149 add_to_log($course->id, "forum", "subscribeall", "index.php?id=$course->id", $course->id);
150 redirect($returnto, get_string("nowallsubscribed", "forum", $course->shortname), 1);
151 } else {
152 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
153 redirect($returnto, get_string("nowallunsubscribed", "forum", $course->shortname), 1);
154 }
155 }
156
ecc8403e 157 /// First, let's process the general forums and build up a display
158
159 if ($generalforums) {
160 foreach ($generalforums as $forum) {
bbbf2d40 161
162 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
163 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
164
165 if (isset($forum->groupmode)) {
ecc8403e 166 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
167 } else {
168 $groupmode = NOGROUPS;
169 }
8f0cd6ef 170
bbbf2d40 171
172 // this is potentially wrong logic. could possibly check for if user has the right to hmmm
0468976c 173 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2862b309 174 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
ecc8403e 175 } else {
176 $count = count_records("forum_discussions", "forum", "$forum->id");
177 }
8f0cd6ef 178
eaf50aef 179 if ($usetracking) {
180 if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) {
0468976c 181 $groupid = ($groupmode==SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) ? $currentgroup : false;
38994d6f 182 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
183 if ($unread > 0) {
6de6cd5a 184 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 185 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 186 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 187 } else {
4067b968 188 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 189 }
630bd96b 190
bbbf2d40 191
eaf50aef 192 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
193 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
194 $forum->id.'">'.$stryes.'</a>';
195 }
630bd96b 196 else {
197 $trackedlink = $stryes;
198 }
f37da850 199 } else {
6de6cd5a 200 $unreadlink = '-';
eaf50aef 201 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.
202 $forum->id.'">'.$strno.'</a>';
f37da850 203 }
204 }
205
9a0eb679 206 $introoptions->para=false;
91121c89 207 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
ecc8403e 208
209 if ($forum->visible) {
3849dae8 210 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 211 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 212 } else {
3849dae8 213 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 214 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 215 }
216
f4646b44 217 //If this forum has RSS activated, calculate it
218 $rsslink = '';
219 if ($show_rss) {
220 if ($forum->rsstype and $forum->rssarticles) {
221 //Calculate the tolltip text
222 if ($forum->rsstype == 1) {
3849dae8 223 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 224 } else {
3849dae8 225 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 226 }
227 if (empty($USER->id)) {
228 $userid = 0;
229 } else {
230 $userid = $USER->id;
231 }
232 //Get html code for RSS link
233 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
234 }
235 }
236
ecc8403e 237 if ($can_subscribe) {
238 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 239 $sublink = $stryes;
ecc8403e 240 } else {
0468976c 241 if ($groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id)) {
6de6cd5a 242 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
3849dae8 243 $forumlink = format_string($forum->name,true);
ecc8403e 244 } else {
245 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 246 $subscribed = $stryes;
ecc8403e 247 $subtitle = get_string("unsubscribe", "forum");
248 } else {
6de6cd5a 249 $subscribed = $strno;
ecc8403e 250 $subtitle = get_string("subscribe", "forum");
251 }
1febf1a7 252 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE
253 && !has_capability('mod/forum:managesubscriptions', $context)) {
098d27d4 254 $sublink = '-';
255 } else {
256 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
257 }
ecc8403e 258 }
259 }
9a0eb679 260 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 261 if ($usetracking) {
f37da850 262 $row[] = $unreadlink;
38994d6f 263 $row[] = $trackedlink; // Tracking.
f37da850 264 }
265 $row[] = $sublink;
a9022f9e 266 if ($show_rss) {
f37da850 267 $row[] = $rsslink;
33f48f03 268 }
f37da850 269 $generaltable->data[] = $row;
ecc8403e 270 } else {
9a0eb679 271 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 272 if ($usetracking) {
f37da850 273 $row[] = $unreadlink;
38994d6f 274 $row[] = $trackedlink; // Tracking.
f37da850 275 }
a9022f9e 276 if ($show_rss) {
f37da850 277 $row[] = $rsslink;
f4646b44 278 }
f37da850 279 $generaltable->data[] = $row;
ecc8403e 280 }
281 }
8f0cd6ef 282 }
ecc8403e 283
38fcc215 284
285 // Start of the table for Learning Forums
286 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
287 $learningtable->align = array ("left", "left", "center");
288
c2468824 289 if ($usetracking) {
f37da850 290 $learningtable->head[] = $strunreadposts;
9fd7165a 291 $learningtable->align[] = 'center';
38994d6f 292
293 $learningtable->head[] = $strtracking;
294 $learningtable->align[] = 'center';
f37da850 295 }
296
1febf1a7 297 if ($can_subscribe) {
38fcc215 298 $learningtable->head[] = $strsubscribed;
9fd7165a 299 $learningtable->align[] = 'center';
38fcc215 300 }
301
ec81373f 302 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 303 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 304 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
305 $learningtable->head[] = $strrss;
9fd7165a 306 $learningtable->align[] = 'center';
38fcc215 307 }
308
bd83d0a3 309 /// Now let's process the learning forums
ecc8403e 310
1936c10e 311 if ($course->id != SITEID) { // Only real courses have learning forums
94361e02 312 // Add extra field for section number, at the front
fa5a5b52 313 array_unshift($learningtable->head, "");
314 array_unshift($learningtable->align, "center");
8f0cd6ef 315
a0b9571f 316
86c5e69a 317 if ($learningforums) {
a0b9571f 318 $currentsection = "";
1390f203 319 foreach ($learningforums as $key => $forum) {
ecc8403e 320 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
1febf1a7 321 $forum->visible = instance_is_visible("forum", $forum)
322 || has_capability('moodle/course:view', $coursecontext);
323
bbbf2d40 324 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
1febf1a7 325 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
bbbf2d40 326
0468976c 327 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
5b5f2971 328 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 329 } else {
330 $count = count_records("forum_discussions", "forum", "$forum->id");
331 }
8f0cd6ef 332
eaf50aef 333 if ($usetracking) {
334 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
335 !isset($untracked[$forum->id])) {
1febf1a7 336 $groupid = ($groupmode==SEPARATEGROUPS
aad6c6c2 337 && !has_capability('moodle/site:accessallgroups', $context))
1febf1a7 338 ? $currentgroup : false;
38994d6f 339 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
340 if ($unread > 0) {
6de6cd5a 341 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 342 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 343 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 344 } else {
331c512a 345 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 346 }
eaf50aef 347 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
348 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
349 $forum->id.'">'.$stryes.'</a>';
350 }
f37da850 351 } else {
6de6cd5a 352 $unreadlink = '-';
353 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 354 }
355 }
356
9a0eb679 357 $introoptions->para=false;
91121c89 358 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
8f0cd6ef 359
a0b9571f 360 if ($forum->section != $currentsection) {
361 $printsection = $forum->section;
ef6a649b 362 if ($currentsection) {
363 $learningtable->data[] = 'hr';
364 }
a0b9571f 365 $currentsection = $forum->section;
366 } else {
367 $printsection = "";
368 }
369
580f2fbc 370 if ($forum->visible) {
3849dae8 371 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 372 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 373 } else {
3849dae8 374 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 375 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 376 }
8f0cd6ef 377
f4646b44 378 //If this forum has RSS activated, calculate it
379 $rsslink = '';
380 if ($show_rss) {
381 if ($forum->rsstype and $forum->rssarticles) {
382 //Calculate the tolltip text
383 if ($forum->rsstype == 1) {
3849dae8 384 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 385 } else {
3849dae8 386 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 387 }
388 if (empty($USER->id)) {
389 $userid = 0;
390 } else {
391 $userid = $USER->id;
392 }
393 //Get html code for RSS link
394 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
395 }
396 }
ec81373f 397
94361e02 398 if ($can_subscribe) {
399 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 400 $sublink = $stryes;
501cdbd8 401 } else {
aad6c6c2 402 if ($groupmode and !has_capability('moodle/site:accessallgroups', $context)
1febf1a7 403 and !mygroupid($course->id)) {
6de6cd5a 404 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
bd83d0a3 405 if ($groupmode == SEPARATEGROUPS) {
3849dae8 406 $forumlink = format_string($forum->name,true);
bd83d0a3 407 }
94361e02 408 } else {
a0b9571f 409 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 410 $subscribed = $stryes;
411 $subtitle = $strunsubscribe;
a0b9571f 412 } else {
6de6cd5a 413 $subscribed = $strno;
414 $subtitle = $strsubscribe;
a0b9571f 415 }
416 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 417 }
501cdbd8 418 }
f37da850 419
9a0eb679 420 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 421 if ($usetracking) {
f37da850 422 $row[] = $unreadlink;
38994d6f 423 $row[] = $trackedlink; // Tracking.
f37da850 424 }
425 $row[] = $sublink;
a9022f9e 426 if ($show_rss) {
f37da850 427 $row[] = $rsslink;
33f48f03 428 }
f37da850 429 $learningtable->data[] = $row;
430
94361e02 431 } else {
9a0eb679 432 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 433 if ($usetracking) {
f37da850 434 $row[] = $unreadlink;
38994d6f 435 $row[] = $trackedlink; // Tracking.
f37da850 436 }
a9022f9e 437 if ($show_rss) {
f37da850 438 $row[] = $rsslink;
f4646b44 439 }
f37da850 440 $learningtable->data[] = $row;
f93f848a 441 }
f93f848a 442 }
443 }
444 }
445
fa5a5b52 446
447 /// Output the page
448
1936c10e 449 if ($course->id != SITEID) {
fa5a5b52 450 print_header("$course->shortname: $strforums", "$course->fullname",
38fcc215 451 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $strforums",
fa5a5b52 452 "", "", true, $searchform, navmenu($course));
453 } else {
8f0cd6ef 454 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
fa5a5b52 455 "", "", true, $searchform, navmenu($course));
456 }
457
03877b04 458 if (!isguest()) {
459 echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top"><td align="right" class="subscription">';
460 echo '<span class="helplink">';
461 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
462 echo '<br />';
463 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
464 echo '</span></td></tr></table>';
465 }
466
6031fe85 467 if ($generalforums) {
468 print_heading(get_string("generalforums", "forum"));
469 print_table($generaltable);
470 }
fa5a5b52 471
6031fe85 472 if ($learningforums) {
473 print_heading(get_string("learningforums", "forum"));
474 print_table($learningtable);
475 }
f93f848a 476
477 print_footer($course);
478
4067b968 479?>