MDL-9628 First attempts at trimming grade_tree after fetching, for expanded/contracte...
[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);
3b27b0fe 21 $currentgroup = get_and_set_current_group($course, groupmode($course));
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);
6ba65fa0 152 redirect($returnto, get_string("nowallsubscribed", "forum", format_string($course->shortname)), 1);
03877b04 153 } else {
154 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
6ba65fa0 155 redirect($returnto, get_string("nowallunsubscribed", "forum", format_string($course->shortname)), 1);
03877b04 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
511ec76b 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
49b53f26 219 $row = array ($forumlink, $forum->intro, $discussionlink);
220 if ($usetracking) {
221 $row[] = $unreadlink;
222 $row[] = $trackedlink; // Tracking.
f4646b44 223 }
224
ecc8403e 225 if ($can_subscribe) {
226 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 227 $sublink = $stryes;
ecc8403e 228 } else {
0468976c 229 if ($groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id)) {
6de6cd5a 230 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
3849dae8 231 $forumlink = format_string($forum->name,true);
ecc8403e 232 } else {
233 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 234 $subscribed = $stryes;
ecc8403e 235 $subtitle = get_string("unsubscribe", "forum");
236 } else {
6de6cd5a 237 $subscribed = $strno;
ecc8403e 238 $subtitle = get_string("subscribe", "forum");
239 }
1febf1a7 240 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE
241 && !has_capability('mod/forum:managesubscriptions', $context)) {
098d27d4 242 $sublink = '-';
243 } else {
244 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
245 }
ecc8403e 246 }
247 }
f37da850 248 $row[] = $sublink;
49b53f26 249 }
250
251 //If this forum has RSS activated, calculate it
252 if ($show_rss and $forum->rsstype and $forum->rssarticles) {
253 //Calculate the tolltip text
254 if ($forum->rsstype == 1) {
255 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
256 } else {
257 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f37da850 258 }
49b53f26 259 if (empty($USER->id)) {
260 $userid = 0;
261 } else {
262 $userid = $USER->id;
f4646b44 263 }
49b53f26 264 //Get html code for RSS link
265 $row[] = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
ecc8403e 266 }
49b53f26 267
268 $generaltable->data[] = $row;
ecc8403e 269 }
8f0cd6ef 270 }
ecc8403e 271
38fcc215 272
273 // Start of the table for Learning Forums
274 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
275 $learningtable->align = array ("left", "left", "center");
276
c2468824 277 if ($usetracking) {
f37da850 278 $learningtable->head[] = $strunreadposts;
9fd7165a 279 $learningtable->align[] = 'center';
38994d6f 280
281 $learningtable->head[] = $strtracking;
282 $learningtable->align[] = 'center';
f37da850 283 }
284
1febf1a7 285 if ($can_subscribe) {
38fcc215 286 $learningtable->head[] = $strsubscribed;
9fd7165a 287 $learningtable->align[] = 'center';
38fcc215 288 }
289
ec81373f 290 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 291 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 292 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
293 $learningtable->head[] = $strrss;
9fd7165a 294 $learningtable->align[] = 'center';
38fcc215 295 }
296
bd83d0a3 297 /// Now let's process the learning forums
ecc8403e 298
1936c10e 299 if ($course->id != SITEID) { // Only real courses have learning forums
94361e02 300 // Add extra field for section number, at the front
2f8d5316 301 if ($course->format == 'weeks' or $course->format == 'weekscss') {
302 array_unshift($learningtable->head, $strweek);
303 } else {
304 array_unshift($learningtable->head, $strsection);
305 }
fa5a5b52 306 array_unshift($learningtable->align, "center");
8f0cd6ef 307
a0b9571f 308
86c5e69a 309 if ($learningforums) {
a0b9571f 310 $currentsection = "";
1390f203 311 foreach ($learningforums as $key => $forum) {
ecc8403e 312 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
1febf1a7 313 $forum->visible = instance_is_visible("forum", $forum)
314 || has_capability('moodle/course:view', $coursecontext);
315
bbbf2d40 316 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
1febf1a7 317 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
bbbf2d40 318
0468976c 319 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
5b5f2971 320 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 321 } else {
322 $count = count_records("forum_discussions", "forum", "$forum->id");
323 }
8f0cd6ef 324
eaf50aef 325 if ($usetracking) {
326 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
327 !isset($untracked[$forum->id])) {
1febf1a7 328 $groupid = ($groupmode==SEPARATEGROUPS
aad6c6c2 329 && !has_capability('moodle/site:accessallgroups', $context))
1febf1a7 330 ? $currentgroup : false;
38994d6f 331 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
332 if ($unread > 0) {
6de6cd5a 333 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 334 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
edebb94f 335 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
38994d6f 336 } else {
331c512a 337 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 338 }
eaf50aef 339 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
340 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
341 $forum->id.'">'.$stryes.'</a>';
342 }
f37da850 343 } else {
6de6cd5a 344 $unreadlink = '-';
345 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 346 }
347 }
348
9a0eb679 349 $introoptions->para=false;
91121c89 350 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
8f0cd6ef 351
a0b9571f 352 if ($forum->section != $currentsection) {
353 $printsection = $forum->section;
ef6a649b 354 if ($currentsection) {
355 $learningtable->data[] = 'hr';
356 }
a0b9571f 357 $currentsection = $forum->section;
358 } else {
359 $printsection = "";
360 }
361
580f2fbc 362 if ($forum->visible) {
3849dae8 363 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 364 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 365 } else {
3849dae8 366 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 367 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 368 }
8f0cd6ef 369
49b53f26 370 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
371 if ($usetracking) {
372 $row[] = $unreadlink;
373 $row[] = $trackedlink; // Tracking.
f4646b44 374 }
ec81373f 375
94361e02 376 if ($can_subscribe) {
377 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 378 $sublink = $stryes;
501cdbd8 379 } else {
aad6c6c2 380 if ($groupmode and !has_capability('moodle/site:accessallgroups', $context)
1febf1a7 381 and !mygroupid($course->id)) {
6de6cd5a 382 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
bd83d0a3 383 if ($groupmode == SEPARATEGROUPS) {
3849dae8 384 $forumlink = format_string($forum->name,true);
bd83d0a3 385 }
94361e02 386 } else {
a0b9571f 387 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 388 $subscribed = $stryes;
389 $subtitle = $strunsubscribe;
a0b9571f 390 } else {
6de6cd5a 391 $subscribed = $strno;
392 $subtitle = $strsubscribe;
a0b9571f 393 }
394 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 395 }
501cdbd8 396 }
962d0133 397 $row[] = $sublink;
49b53f26 398 }
399
400 //If this forum has RSS activated, calculate it
401 if ($show_rss and $forum->rsstype and $forum->rssarticles) {
402 //Calculate the tolltip text
403 if ($forum->rsstype == 1) {
404 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
405 } else {
406 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f37da850 407 }
49b53f26 408 if (empty($USER->id)) {
409 $userid = 0;
410 } else {
411 $userid = $USER->id;
f4646b44 412 }
49b53f26 413 //Get html code for RSS link
414 $row[] = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
f93f848a 415 }
49b53f26 416
417 $learningtable->data[] = $row;
f93f848a 418 }
419 }
420 }
421
fa5a5b52 422
423 /// Output the page
01e38388 424 $navlinks = array();
3b27b0fe 425 $navlinks[] = array('name' => $strforums, 'link' => '', 'type' => 'activity');
e3f58dfb 426
427 print_header("$course->shortname: $strforums", $course->fullname,
3b27b0fe 428 build_navigation($navlinks),
fa5a5b52 429 "", "", true, $searchform, navmenu($course));
fa5a5b52 430
03877b04 431 if (!isguest()) {
3df15162 432 print_box_start('subscription');
03877b04 433 echo '<span class="helplink">';
434 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
3df15162 435 echo '</span><br /><span class="helplink">';
03877b04 436 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
3df15162 437 echo '</span>';
438 print_box_end();
439 print_box('&nbsp;', 'clearer');
03877b04 440 }
441
6031fe85 442 if ($generalforums) {
443 print_heading(get_string("generalforums", "forum"));
444 print_table($generaltable);
445 }
fa5a5b52 446
6031fe85 447 if ($learningforums) {
448 print_heading(get_string("learningforums", "forum"));
449 print_table($learningtable);
450 }
f93f848a 451
452 print_footer($course);
453
4067b968 454?>