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