Should be $COURSE not $course
[moodle-linuxchix.git] / mod / forum / index.php
blob068bd218c83ea1fd81ee5026b8b8812555b7c0aa
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 $currentgroup = get_and_set_current_group($course, groupmode($course));
22 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
25 unset($SESSION->fromdiscussion);
27 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
29 $strforums = get_string("forums", "forum");
30 $strforum = get_string("forum", "forum");
31 $strdescription = get_string("description");
32 $strdiscussions = get_string("discussions", "forum");
33 $strsubscribed = get_string("subscribed", "forum");
34 $strunreadposts = get_string("unreadposts", "forum");
35 $strtracking = get_string('tracking', 'forum');
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');
43 $strrss = get_string("rss");
44 $strweek = get_string('week');
45 $strsection = get_string('section');
47 $searchform = forum_search_form($course);
50 // Start of the table for General Forums
52 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
53 $generaltable->align = array ('left', 'left', 'center');
55 if ($usetracking = forum_tp_can_track_forums()) {
56 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
58 $generaltable->head[] = $strunreadposts;
59 $generaltable->align[] = 'center';
61 $generaltable->head[] = $strtracking;
62 $generaltable->align[] = 'center';
65 if ($can_subscribe = has_capability('moodle/course:view', $coursecontext)) {
66 $generaltable->head[] = $strsubscribed;
67 $generaltable->align[] = 'center';
70 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
71 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
72 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
73 $generaltable->head[] = $strrss;
74 $generaltable->align[] = 'center';
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
80 // category with the forums in section 0.
82 $generalforums = array(); // For now
83 $learningforums = get_all_instances_in_course("forum", $course);
85 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
87 if ($learningforums) { // Copy "full" data into this complete array
88 foreach ($learningforums as $key => $learningforum) {
89 $learningforum->keyreference = $key;
90 $forums[$learningforum->id] = $learningforum;
94 foreach ($forums as $forum) {
96 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
97 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
99 if (!has_capability('mod/forum:viewdiscussion', $context)) {
100 if (isset($forum->keyreference)) {
101 unset($learningforums[$forum->keyreference]);
103 continue;
105 if (!isset($forum->visible)) {
106 $forum->visible = instance_is_visible("forum", $forum);
107 if (!$forum->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
108 if (isset($forum->keyreference)) {
109 unset($learningforums[$forum->keyreference]);
111 continue;
114 switch ($forum->type) {
115 case "news":
116 case "social":
117 $generalforums[] = $forum;
118 if (isset($forum->keyreference)) { // Should always be
119 unset($learningforums[$forum->keyreference]);
121 break;
122 default:
123 if (($course->id == SITEID) or empty($forum->section)) { // Site level or section 0
124 $generalforums[] = $forum;
125 if (isset($forum->keyreference)) {
126 unset($learningforums[$forum->keyreference]);
129 break;
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);
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);
152 redirect($returnto, get_string("nowallsubscribed", "forum", format_string($course->shortname)), 1);
153 } else {
154 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
155 redirect($returnto, get_string("nowallunsubscribed", "forum", format_string($course->shortname)), 1);
159 /// First, let's process the general forums and build up a display
161 if ($generalforums) {
162 foreach ($generalforums as $forum) {
164 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
165 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
167 if (isset($forum->groupmode)) {
168 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
169 } else {
170 $groupmode = NOGROUPS;
173 $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id);
175 // this is potentially wrong logic. could possibly check for if user has the right to hmmm
176 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
177 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
178 } else {
179 $count = count_records("forum_discussions", "forum", "$forum->id");
182 if ($usetracking) {
183 if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) {
184 $groupid = ($groupmode==SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) ? $currentgroup : false;
185 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
186 if ($unread > 0) {
187 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
188 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
189 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
190 } else {
191 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
195 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
196 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
197 $forum->id.'">'.$stryes.'</a>';
199 else {
200 $trackedlink = $stryes;
202 } else {
203 $unreadlink = '-';
204 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.
205 $forum->id.'">'.$strno.'</a>';
209 $introoptions->para=false;
210 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
212 $forumname = format_string($forum->name,true);;
213 if ($cantaccessagroup) {
214 $forumlink = $forumname;
215 $discussionlink = $count;
216 } else {
217 if ($forum->visible) {
218 $style = '';
219 } else {
220 $style = 'class="dimmed"';
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>";
226 $row = array ($forumlink, $forum->intro, $discussionlink);
227 if ($usetracking) {
228 $row[] = $unreadlink;
229 $row[] = $trackedlink; // Tracking.
232 if ($can_subscribe) {
233 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
234 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
235 'cantsubscribe' => '-'), $cantaccessagroup);
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));
246 if (empty($USER->id)) {
247 $userid = 0;
248 } else {
249 $userid = $USER->id;
251 //Get html code for RSS link
252 $row[] = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
255 $generaltable->data[] = $row;
260 // Start of the table for Learning Forums
261 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
262 $learningtable->align = array ("left", "left", "center");
264 if ($usetracking) {
265 $learningtable->head[] = $strunreadposts;
266 $learningtable->align[] = 'center';
268 $learningtable->head[] = $strtracking;
269 $learningtable->align[] = 'center';
272 if ($can_subscribe) {
273 $learningtable->head[] = $strsubscribed;
274 $learningtable->align[] = 'center';
277 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
278 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
279 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
280 $learningtable->head[] = $strrss;
281 $learningtable->align[] = 'center';
284 /// Now let's process the learning forums
286 if ($course->id != SITEID) { // Only real courses have learning forums
287 // Add extra field for section number, at the front
288 if ($course->format == 'weeks' or $course->format == 'weekscss') {
289 array_unshift($learningtable->head, $strweek);
290 } else {
291 array_unshift($learningtable->head, $strsection);
293 array_unshift($learningtable->align, "center");
296 if ($learningforums) {
297 $currentsection = "";
298 foreach ($learningforums as $key => $forum) {
299 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
300 $forum->visible = instance_is_visible("forum", $forum)
301 || has_capability('moodle/course:view', $coursecontext);
303 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
304 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
306 $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id);
308 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
309 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
310 } else {
311 $count = count_records("forum_discussions", "forum", "$forum->id");
314 if ($usetracking) {
315 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
316 !isset($untracked[$forum->id])) {
317 $groupid = ($groupmode==SEPARATEGROUPS
318 && !has_capability('moodle/site:accessallgroups', $context))
319 ? $currentgroup : false;
320 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
321 if ($unread > 0) {
322 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
323 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
324 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
325 } else {
326 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
328 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
329 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
330 $forum->id.'">'.$stryes.'</a>';
332 } else {
333 $unreadlink = '-';
334 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
338 $introoptions->para=false;
339 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
341 if ($forum->section != $currentsection) {
342 $printsection = $forum->section;
343 if ($currentsection) {
344 $learningtable->data[] = 'hr';
346 $currentsection = $forum->section;
347 } else {
348 $printsection = "";
351 $forumname = format_string($forum->name,true);;
352 if ($cantaccessagroup && $groupmode == SEPARATEGROUPS) {
353 $forumlink = $forumname;
354 $discussionlink = $count;
355 } else {
356 if ($forum->visible) {
357 $style = '';
358 } else {
359 $style = 'class="dimmed"';
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>";
365 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
366 if ($usetracking) {
367 $row[] = $unreadlink;
368 $row[] = $trackedlink; // Tracking.
371 if ($can_subscribe) {
372 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
373 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
374 'cantsubscribe' => '-'), $cantaccessagroup);
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));
385 if (empty($USER->id)) {
386 $userid = 0;
387 } else {
388 $userid = $USER->id;
390 //Get html code for RSS link
391 $row[] = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
394 $learningtable->data[] = $row;
400 /// Output the page
401 $navlinks = array();
402 $navlinks[] = array('name' => $strforums, 'link' => '', 'type' => 'activity');
404 print_header("$course->shortname: $strforums", $course->fullname,
405 build_navigation($navlinks),
406 "", "", true, $searchform, navmenu($course));
408 if (!isguest()) {
409 print_box_start('subscription');
410 echo '<span class="helplink">';
411 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
412 echo '</span><br /><span class="helplink">';
413 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
414 echo '</span>';
415 print_box_end();
416 print_box('&nbsp;', 'clearer');
419 if ($generalforums) {
420 print_heading(get_string("generalforums", "forum"));
421 print_table($generaltable);
424 if ($learningforums) {
425 print_heading(get_string("learningforums", "forum"));
426 print_table($learningtable);
429 print_footer($course);