new tags for new functions
[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);
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);
9fa33508 81
ecc8403e 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) {
f0da6b85 92
93 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
94 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
95
b32c8dda 96 if (!has_capability('mod/forum:viewdiscussion', $context)) {
f0da6b85 97 if (isset($forum->keyreference)) {
98 unset($learningforums[$forum->keyreference]);
99 }
100 continue;
101 }
a0a4a3c9 102 if (!isset($forum->visible)) {
9c8b8eee 103 $forum->visible = instance_is_visible("forum", $forum);
0468976c 104 if (!$forum->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
9c8b8eee 105 if (isset($forum->keyreference)) {
106 unset($learningforums[$forum->keyreference]);
107 }
108 continue;
109 }
a0a4a3c9 110 }
37b15514 111 switch ($forum->type) {
0872b023 112 case "news":
113 case "social":
114 $generalforums[] = $forum;
bd83d0a3 115 if (isset($forum->keyreference)) { // Should always be
116 unset($learningforums[$forum->keyreference]);
ecc8403e 117 }
37b15514 118 break;
bbbf2d40 119 /*
8f0cd6ef 120 case "teacher":
37b15514 121 if (isteacher($course->id)) {
ecc8403e 122 $forum->visible = true;
37b15514 123 $generalforums[] = $forum;
124 }
125 break;
bbbf2d40 126 */
94361e02 127 default:
a0a4a3c9 128 if (!$course->category or empty($forum->section)) { // Site level or section 0
94361e02 129 $generalforums[] = $forum;
bd83d0a3 130 if (isset($forum->keyreference)) {
131 unset($learningforums[$forum->keyreference]);
a0a4a3c9 132 }
8f0cd6ef 133 }
94361e02 134 break;
71fb9630 135 }
37b15514 136 }
137 }
71fb9630 138
03877b04 139 /// Do course wide subscribe/unsubscribe
140 if (!is_null($subscribe) && !isguest()) {
141 $allforums = array_merge($generalforums, $learningforums);
142 if ($allforums) {
143 foreach ($allforums as $forum) {
144 if (!forum_is_forcesubscribed($forum->id)) {
145 $subscribed = forum_is_subscribed($USER->id, $forum->id);
146 if ($subscribe && !$subscribed) {
147 forum_subscribe($USER->id, $forum->id);
148 } elseif (!$subscribe && $subscribed) {
149 forum_unsubscribe($USER->id, $forum->id);
150 }
151 }
152 }
153 }
154 $returnto = forum_go_back_to("index.php?id=$course->id");
155 if ($subscribe) {
156 add_to_log($course->id, "forum", "subscribeall", "index.php?id=$course->id", $course->id);
157 redirect($returnto, get_string("nowallsubscribed", "forum", $course->shortname), 1);
158 } else {
159 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
160 redirect($returnto, get_string("nowallunsubscribed", "forum", $course->shortname), 1);
161 }
162 }
163
ecc8403e 164 /// First, let's process the general forums and build up a display
165
166 if ($generalforums) {
167 foreach ($generalforums as $forum) {
bbbf2d40 168
169 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
170 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
171
172 if (isset($forum->groupmode)) {
ecc8403e 173 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
174 } else {
175 $groupmode = NOGROUPS;
176 }
8f0cd6ef 177
bbbf2d40 178
179 // this is potentially wrong logic. could possibly check for if user has the right to hmmm
0468976c 180 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2862b309 181 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
ecc8403e 182 } else {
183 $count = count_records("forum_discussions", "forum", "$forum->id");
184 }
8f0cd6ef 185
eaf50aef 186 if ($usetracking) {
187 if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) {
0468976c 188 $groupid = ($groupmode==SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) ? $currentgroup : false;
38994d6f 189 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
190 if ($unread > 0) {
6de6cd5a 191 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 192 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 193 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 194 } else {
195 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
196 }
630bd96b 197
bbbf2d40 198
eaf50aef 199 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
200 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
201 $forum->id.'">'.$stryes.'</a>';
202 }
630bd96b 203 else {
204 $trackedlink = $stryes;
205 }
f37da850 206 } else {
6de6cd5a 207 $unreadlink = '-';
eaf50aef 208 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.
209 $forum->id.'">'.$strno.'</a>';
f37da850 210 }
211 }
212
9a0eb679 213 $introoptions->para=false;
91121c89 214 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
ecc8403e 215
216 if ($forum->visible) {
3849dae8 217 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 218 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 219 } else {
3849dae8 220 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 221 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 222 }
223
f4646b44 224 //If this forum has RSS activated, calculate it
225 $rsslink = '';
226 if ($show_rss) {
227 if ($forum->rsstype and $forum->rssarticles) {
228 //Calculate the tolltip text
229 if ($forum->rsstype == 1) {
3849dae8 230 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 231 } else {
3849dae8 232 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 233 }
234 if (empty($USER->id)) {
235 $userid = 0;
236 } else {
237 $userid = $USER->id;
238 }
239 //Get html code for RSS link
240 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
241 }
242 }
243
ecc8403e 244 if ($can_subscribe) {
245 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 246 $sublink = $stryes;
ecc8403e 247 } else {
0468976c 248 if ($groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id)) {
6de6cd5a 249 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
3849dae8 250 $forumlink = format_string($forum->name,true);
ecc8403e 251 } else {
252 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 253 $subscribed = $stryes;
ecc8403e 254 $subtitle = get_string("unsubscribe", "forum");
255 } else {
6de6cd5a 256 $subscribed = $strno;
ecc8403e 257 $subtitle = get_string("subscribe", "forum");
258 }
098d27d4 259 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !isteacher($forum->course)) {
260 $sublink = '-';
261 } else {
262 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
263 }
ecc8403e 264 }
265 }
9a0eb679 266 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 267 if ($usetracking) {
f37da850 268 $row[] = $unreadlink;
38994d6f 269 $row[] = $trackedlink; // Tracking.
f37da850 270 }
271 $row[] = $sublink;
a9022f9e 272 if ($show_rss) {
f37da850 273 $row[] = $rsslink;
33f48f03 274 }
f37da850 275 $generaltable->data[] = $row;
ecc8403e 276 } else {
9a0eb679 277 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 278 if ($usetracking) {
f37da850 279 $row[] = $unreadlink;
38994d6f 280 $row[] = $trackedlink; // Tracking.
f37da850 281 }
a9022f9e 282 if ($show_rss) {
f37da850 283 $row[] = $rsslink;
f4646b44 284 }
f37da850 285 $generaltable->data[] = $row;
ecc8403e 286 }
287 }
8f0cd6ef 288 }
ecc8403e 289
38fcc215 290
291 // Start of the table for Learning Forums
292 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
293 $learningtable->align = array ("left", "left", "center");
294
c2468824 295 if ($usetracking) {
f37da850 296 $learningtable->head[] = $strunreadposts;
9fd7165a 297 $learningtable->align[] = 'center';
38994d6f 298
299 $learningtable->head[] = $strtracking;
300 $learningtable->align[] = 'center';
f37da850 301 }
302
f4646b44 303 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
38fcc215 304 $learningtable->head[] = $strsubscribed;
9fd7165a 305 $learningtable->align[] = 'center';
38fcc215 306 }
307
ec81373f 308 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 309 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 310 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
311 $learningtable->head[] = $strrss;
9fd7165a 312 $learningtable->align[] = 'center';
38fcc215 313 }
314
bd83d0a3 315 /// Now let's process the learning forums
ecc8403e 316
94361e02 317 if ($course->category) { // Only real courses have learning forums
318 // Add extra field for section number, at the front
fa5a5b52 319 array_unshift($learningtable->head, "");
320 array_unshift($learningtable->align, "center");
8f0cd6ef 321
a0b9571f 322
86c5e69a 323 if ($learningforums) {
a0b9571f 324 $currentsection = "";
1390f203 325 foreach ($learningforums as $key => $forum) {
ecc8403e 326 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
bbbf2d40 327 $forum->visible = instance_is_visible("forum", $forum);
328 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
329
0468976c 330 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
5b5f2971 331 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 332 } else {
333 $count = count_records("forum_discussions", "forum", "$forum->id");
334 }
8f0cd6ef 335
eaf50aef 336 if ($usetracking) {
337 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
338 !isset($untracked[$forum->id])) {
38994d6f 339 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
340 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
341 if ($unread > 0) {
6de6cd5a 342 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 343 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 344 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 345 } else {
331c512a 346 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 347 }
eaf50aef 348 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
349 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
350 $forum->id.'">'.$stryes.'</a>';
351 }
f37da850 352 } else {
6de6cd5a 353 $unreadlink = '-';
354 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 355 }
356 }
357
9a0eb679 358 $introoptions->para=false;
91121c89 359 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
8f0cd6ef 360
a0b9571f 361 if ($forum->section != $currentsection) {
362 $printsection = $forum->section;
ef6a649b 363 if ($currentsection) {
364 $learningtable->data[] = 'hr';
365 }
a0b9571f 366 $currentsection = $forum->section;
367 } else {
368 $printsection = "";
369 }
370
580f2fbc 371 if ($forum->visible) {
3849dae8 372 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 373 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 374 } else {
3849dae8 375 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 376 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 377 }
8f0cd6ef 378
f4646b44 379 //If this forum has RSS activated, calculate it
380 $rsslink = '';
381 if ($show_rss) {
382 if ($forum->rsstype and $forum->rssarticles) {
383 //Calculate the tolltip text
384 if ($forum->rsstype == 1) {
3849dae8 385 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 386 } else {
3849dae8 387 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 388 }
389 if (empty($USER->id)) {
390 $userid = 0;
391 } else {
392 $userid = $USER->id;
393 }
394 //Get html code for RSS link
395 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
396 }
397 }
ec81373f 398
94361e02 399 if ($can_subscribe) {
400 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 401 $sublink = $stryes;
501cdbd8 402 } else {
ecc8403e 403 if ($groupmode and !isteacheredit($course->id) 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
449 if ($course->category) {
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
b32c8dda 479?>