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