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