MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / forum / index.php
blob2c4c969bf8f47ba00191663f529f6f669bb8d36b
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->libdir/rsslib.php");
7 $id = optional_param('id', 0, PARAM_INT); // Course id
8 $subscribe = optional_param('subscribe', null, PARAM_INT); // Subscribe/Unsubscribe all forums
10 if ($id) {
11 if (! $course = get_record("course", "id", $id)) {
12 error("Course ID is incorrect");
14 } else {
15 if (! $course = get_site()) {
16 error("Could not find a top-level course!");
20 require_course_login($course);
21 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
24 unset($SESSION->fromdiscussion);
26 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
28 $strforums = get_string("forums", "forum");
29 $strforum = get_string("forum", "forum");
30 $strdescription = get_string("description");
31 $strdiscussions = get_string("discussions", "forum");
32 $strsubscribed = get_string("subscribed", "forum");
33 $strunreadposts = get_string("unreadposts", "forum");
34 $strtracking = get_string('tracking', 'forum');
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');
42 $strrss = get_string("rss");
43 $strweek = get_string('week');
44 $strsection = get_string('section');
46 $searchform = forum_search_form($course);
49 // Start of the table for General Forums
51 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
52 $generaltable->align = array ('left', 'left', 'center');
54 if ($usetracking = forum_tp_can_track_forums()) {
55 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
57 $generaltable->head[] = $strunreadposts;
58 $generaltable->align[] = 'center';
60 $generaltable->head[] = $strtracking;
61 $generaltable->align[] = 'center';
64 if ($can_subscribe = has_capability('moodle/course:view', $coursecontext)) {
65 $generaltable->head[] = $strsubscribed;
66 $generaltable->align[] = 'center';
69 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
70 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
71 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
72 $generaltable->head[] = $strrss;
73 $generaltable->align[] = 'center';
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
79 // category with the forums in section 0.
81 $generalforums = array(); // For now
82 $learningforums = get_all_instances_in_course("forum", $course);
84 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
86 if ($learningforums) { // Copy "full" data into this complete array
87 foreach ($learningforums as $key => $learningforum) {
88 $learningforum->keyreference = $key;
89 $forums[$learningforum->id] = $learningforum;
93 foreach ($forums as $forum) {
95 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
96 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
98 if (!has_capability('mod/forum:viewdiscussion', $context)) {
99 if (isset($forum->keyreference)) {
100 unset($learningforums[$forum->keyreference]);
102 continue;
104 if (!isset($forum->visible)) {
105 $forum->visible = instance_is_visible("forum", $forum);
106 if (!$forum->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
107 if (isset($forum->keyreference)) {
108 unset($learningforums[$forum->keyreference]);
110 continue;
113 switch ($forum->type) {
114 case "news":
115 case "social":
116 $generalforums[] = $forum;
117 if (isset($forum->keyreference)) { // Should always be
118 unset($learningforums[$forum->keyreference]);
120 break;
121 default:
122 if (($course->id == SITEID) or empty($forum->section)) { // Site level or section 0
123 $generalforums[] = $forum;
124 if (isset($forum->keyreference)) {
125 unset($learningforums[$forum->keyreference]);
128 break;
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);
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);
151 redirect($returnto, get_string("nowallsubscribed", "forum", format_string($course->shortname)), 1);
152 } else {
153 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
154 redirect($returnto, get_string("nowallunsubscribed", "forum", format_string($course->shortname)), 1);
158 /// First, let's process the general forums and build up a display
160 if ($generalforums) {
161 foreach ($generalforums as $forum) {
163 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
164 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
166 if (!groups_course_module_visible($cm)) {
167 continue;
169 if (isset($forum->groupmode)) {
170 $groupmode = groups_get_activity_groupmode($cm);
171 } else {
172 $groupmode = NOGROUPS;
174 $currentgroup = groups_get_activity_group($cm);
175 $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id);
177 // this is potentially wrong logic. could possibly check for if user has the right to hmmm
178 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
179 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
180 } else {
181 $count = count_records("forum_discussions", "forum", "$forum->id");
184 if ($usetracking) {
185 if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) {
186 $groupid = ($groupmode==SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) ? $currentgroup : false;
187 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
188 if ($unread > 0) {
189 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
190 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
191 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
192 } else {
193 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
197 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
198 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
199 $forum->id.'">'.$stryes.'</a>';
201 else {
202 $trackedlink = $stryes;
204 } else {
205 $unreadlink = '-';
206 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.
207 $forum->id.'">'.$strno.'</a>';
211 $introoptions->para=false;
212 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
214 $forumname = format_string($forum->name,true);;
215 if ($cantaccessagroup) {
216 $forumlink = $forumname;
217 $discussionlink = $count;
218 } else {
219 if ($forum->visible) {
220 $style = '';
221 } else {
222 $style = 'class="dimmed"';
224 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
225 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
228 $row = array ($forumlink, $forum->intro, $discussionlink);
229 if ($usetracking) {
230 $row[] = $unreadlink;
231 $row[] = $trackedlink; // Tracking.
234 if ($can_subscribe) {
235 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
236 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
237 'cantsubscribe' => '-'), $cantaccessagroup);
240 //If this forum has RSS activated, calculate it
241 if ($show_rss and $forum->rsstype and $forum->rssarticles) {
242 //Calculate the tolltip text
243 if ($forum->rsstype == 1) {
244 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
245 } else {
246 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
248 if (empty($USER->id)) {
249 $userid = 0;
250 } else {
251 $userid = $USER->id;
253 //Get html code for RSS link
254 $row[] = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
257 $generaltable->data[] = $row;
262 // Start of the table for Learning Forums
263 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
264 $learningtable->align = array ("left", "left", "center");
266 if ($usetracking) {
267 $learningtable->head[] = $strunreadposts;
268 $learningtable->align[] = 'center';
270 $learningtable->head[] = $strtracking;
271 $learningtable->align[] = 'center';
274 if ($can_subscribe) {
275 $learningtable->head[] = $strsubscribed;
276 $learningtable->align[] = 'center';
279 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
280 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
281 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
282 $learningtable->head[] = $strrss;
283 $learningtable->align[] = 'center';
286 /// Now let's process the learning forums
288 if ($course->id != SITEID) { // Only real courses have learning forums
289 // Add extra field for section number, at the front
290 if ($course->format == 'weeks' or $course->format == 'weekscss') {
291 array_unshift($learningtable->head, $strweek);
292 } else {
293 array_unshift($learningtable->head, $strsection);
295 array_unshift($learningtable->align, "center");
298 if ($learningforums) {
299 $currentsection = "";
300 foreach ($learningforums as $key => $forum) {
301 $forum->visible = instance_is_visible("forum", $forum)
302 || has_capability('moodle/course:view', $coursecontext);
304 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
305 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
307 if (!groups_course_module_visible($cm)) {
308 continue;
310 $currentgroup = groups_get_activity_group($cm);
311 $groupmode = groups_get_activity_groupmode($cm);
314 $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id);
316 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
317 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
318 } else {
319 $count = count_records("forum_discussions", "forum", "$forum->id");
322 if ($usetracking) {
323 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
324 !isset($untracked[$forum->id])) {
325 $groupid = ($groupmode==SEPARATEGROUPS
326 && !has_capability('moodle/site:accessallgroups', $context))
327 ? $currentgroup : false;
328 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
329 if ($unread > 0) {
330 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
331 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
332 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
333 } else {
334 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
336 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
337 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
338 $forum->id.'">'.$stryes.'</a>';
340 } else {
341 $unreadlink = '-';
342 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
346 $introoptions->para=false;
347 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
349 if ($forum->section != $currentsection) {
350 $printsection = $forum->section;
351 if ($currentsection) {
352 $learningtable->data[] = 'hr';
354 $currentsection = $forum->section;
355 } else {
356 $printsection = "";
359 $forumname = format_string($forum->name,true);;
360 if ($cantaccessagroup && $groupmode == SEPARATEGROUPS) {
361 $forumlink = $forumname;
362 $discussionlink = $count;
363 } else {
364 if ($forum->visible) {
365 $style = '';
366 } else {
367 $style = 'class="dimmed"';
369 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
370 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
373 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
374 if ($usetracking) {
375 $row[] = $unreadlink;
376 $row[] = $trackedlink; // Tracking.
379 if ($can_subscribe) {
380 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
381 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
382 'cantsubscribe' => '-'), $cantaccessagroup);
385 //If this forum has RSS activated, calculate it
386 if ($show_rss and $forum->rsstype and $forum->rssarticles) {
387 //Calculate the tolltip text
388 if ($forum->rsstype == 1) {
389 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
390 } else {
391 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
393 if (empty($USER->id)) {
394 $userid = 0;
395 } else {
396 $userid = $USER->id;
398 //Get html code for RSS link
399 $row[] = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
402 $learningtable->data[] = $row;
408 /// Output the page
409 $navlinks = array();
410 $navlinks[] = array('name' => $strforums, 'link' => '', 'type' => 'activity');
412 print_header("$course->shortname: $strforums", $course->fullname,
413 build_navigation($navlinks),
414 "", "", true, $searchform, navmenu($course));
416 if (!isguest()) {
417 print_box_start('subscription');
418 echo '<span class="helplink">';
419 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
420 echo '</span><br /><span class="helplink">';
421 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
422 echo '</span>';
423 print_box_end();
424 print_box('&nbsp;', 'clearer');
427 if ($generalforums) {
428 print_heading(get_string("generalforums", "forum"));
429 print_table($generaltable);
432 if ($learningforums) {
433 print_heading(get_string("learningforums", "forum"));
434 print_table($learningtable);
437 print_footer($course);