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