When deleting a course, delete all the course files too
[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
7 optional_variable($id); // course
8
9 if ($id) {
10 if (! $course = get_record("course", "id", $id)) {
11 error("Course ID is incorrect");
12 }
13 } else {
f781b794 14 if (! $course = get_site()) {
f93f848a 15 error("Could not find a top-level course!");
16 }
17 }
18
535136bd 19 require_course_login($course);
f93f848a 20
ecc8403e 21 $currentgroup = get_current_group($course->id);
22
501cdbd8 23 unset($SESSION->fromdiscussion);
f93f848a 24
f781b794 25 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
26
27 $strforums = get_string("forums", "forum");
97485d07 28 $strforum = get_string("forum", "forum");
29 $strdescription = get_string("description");
30 $strdiscussions = get_string("discussions", "forum");
31 $strsubscribed = get_string("subscribed", "forum");
f37da850 32 $strunreadposts = get_string("unreadposts", "forum");
38994d6f 33 $strtracking = get_string('tracking', 'forum');
6de6cd5a 34 $strmarkallread = get_string('markallread', 'forum');
35 $strtrackforum = get_string('trackforum', 'forum');
36 $strnotrackforum = get_string('notrackforum', 'forum');
37 $strsubscribe = get_string('subscribe', 'forum');
38 $strunsubscribe = get_string('unsubscribe', 'forum');
39 $stryes = get_string('yes');
40 $strno = get_string('no');
33f48f03 41 $strrss = get_string("rss");
97485d07 42
6f1cc8d6 43 $searchform = forum_search_form($course);
f93f848a 44
f781b794 45
38fcc215 46 // Start of the table for General Forums
f781b794 47
fa5a5b52 48 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
9fd7165a 49 $generaltable->align = array ('left', 'left', 'center');
f37da850 50
c2468824 51 if ($usetracking = forum_tp_can_track_forums()) {
52 $untracked = forum_tp_get_untracked_forums($USER->id);
53
f37da850 54 $generaltable->head[] = $strunreadposts;
9fd7165a 55 $generaltable->align[] = 'center';
38994d6f 56
38994d6f 57 $generaltable->head[] = $strtracking;
58 $generaltable->align[] = 'center';
f37da850 59 }
f93f848a 60
f4646b44 61 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
fa5a5b52 62 $generaltable->head[] = $strsubscribed;
9fd7165a 63 $generaltable->align[] = 'center';
f93f848a 64 }
f93f848a 65
ec81373f 66 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 67 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
33f48f03 68 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
69 $generaltable->head[] = $strrss;
9fd7165a 70 $generaltable->align[] = 'center';
33f48f03 71 }
72
fa5a5b52 73
8f0cd6ef 74 // Parse and organise all the forums. Most forums are course modules but
75 // some special ones are not. These get placed in the general forums
ecc8403e 76 // category with the forums in section 0.
77
78 $generalforums = array(); // For now
79 $learningforums = get_all_instances_in_course("forum", $course);
80
81 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
82
83 if ($learningforums) { // Copy "full" data into this complete array
bd83d0a3 84 foreach ($learningforums as $key => $learningforum) {
85 $learningforum->keyreference = $key;
a0a4a3c9 86 $forums[$learningforum->id] = $learningforum;
ecc8403e 87 }
88 }
fa5a5b52 89
71fb9630 90 foreach ($forums as $forum) {
a0a4a3c9 91 if (!isset($forum->visible)) {
9c8b8eee 92 $forum->visible = instance_is_visible("forum", $forum);
93 if (!$forum->visible and !isteacher($course->id)) {
94 if (isset($forum->keyreference)) {
95 unset($learningforums[$forum->keyreference]);
96 }
97 continue;
98 }
a0a4a3c9 99 }
37b15514 100 switch ($forum->type) {
0872b023 101 case "news":
102 case "social":
103 $generalforums[] = $forum;
bd83d0a3 104 if (isset($forum->keyreference)) { // Should always be
105 unset($learningforums[$forum->keyreference]);
ecc8403e 106 }
37b15514 107 break;
8f0cd6ef 108 case "teacher":
37b15514 109 if (isteacher($course->id)) {
ecc8403e 110 $forum->visible = true;
37b15514 111 $generalforums[] = $forum;
112 }
113 break;
94361e02 114 default:
a0a4a3c9 115 if (!$course->category or empty($forum->section)) { // Site level or section 0
94361e02 116 $generalforums[] = $forum;
bd83d0a3 117 if (isset($forum->keyreference)) {
118 unset($learningforums[$forum->keyreference]);
a0a4a3c9 119 }
8f0cd6ef 120 }
94361e02 121 break;
71fb9630 122 }
37b15514 123 }
124 }
71fb9630 125
ecc8403e 126 /// First, let's process the general forums and build up a display
127
128 if ($generalforums) {
129 foreach ($generalforums as $forum) {
130 if (isset($forum->groupmode)) {
131 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
132 } else {
133 $groupmode = NOGROUPS;
134 }
8f0cd6ef 135
ecc8403e 136 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
2862b309 137 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
ecc8403e 138 } else {
139 $count = count_records("forum_discussions", "forum", "$forum->id");
140 }
8f0cd6ef 141
c2468824 142 if (forum_tp_can_track_forums()) {
143 if (forum_tp_is_tracked($forum->id) && !isset($untracked[$forum->id])) {
38994d6f 144 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
145 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
146 if ($unread > 0) {
6de6cd5a 147 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
148 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?id='.
149 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 150 } else {
151 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
152 }
6de6cd5a 153 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.$forum->id.'">'.$stryes.'</a>';
f37da850 154 } else {
6de6cd5a 155 $unreadlink = '-';
156 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 157 }
158 }
159
9a0eb679 160 $introoptions->para=false;
161 $forum->intro = format_text(shorten_text(trim($forum->intro), $CFG->forum_shortpost), FORMAT_HTML, $introoptions);
ecc8403e 162
163 if ($forum->visible) {
3849dae8 164 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 165 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 166 } else {
3849dae8 167 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 168 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 169 }
170
f4646b44 171 //If this forum has RSS activated, calculate it
172 $rsslink = '';
173 if ($show_rss) {
174 if ($forum->rsstype and $forum->rssarticles) {
175 //Calculate the tolltip text
176 if ($forum->rsstype == 1) {
3849dae8 177 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 178 } else {
3849dae8 179 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 180 }
181 if (empty($USER->id)) {
182 $userid = 0;
183 } else {
184 $userid = $USER->id;
185 }
186 //Get html code for RSS link
187 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
188 }
189 }
190
ecc8403e 191 if ($can_subscribe) {
192 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 193 $sublink = $stryes;
ecc8403e 194 } else {
195 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
6de6cd5a 196 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
3849dae8 197 $forumlink = format_string($forum->name,true);
ecc8403e 198 } else {
199 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 200 $subscribed = $stryes;
ecc8403e 201 $subtitle = get_string("unsubscribe", "forum");
202 } else {
6de6cd5a 203 $subscribed = $strno;
ecc8403e 204 $subtitle = get_string("subscribe", "forum");
205 }
206 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
207 }
208 }
9a0eb679 209 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 210 if ($usetracking) {
f37da850 211 $row[] = $unreadlink;
38994d6f 212 $row[] = $trackedlink; // Tracking.
f37da850 213 }
214 $row[] = $sublink;
f4646b44 215 if (!empty($rsslink)) {
f37da850 216 $row[] = $rsslink;
33f48f03 217 }
f37da850 218 $generaltable->data[] = $row;
ecc8403e 219 } else {
9a0eb679 220 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 221 if ($usetracking) {
f37da850 222 $row[] = $unreadlink;
38994d6f 223 $row[] = $trackedlink; // Tracking.
f37da850 224 }
f4646b44 225 if (!empty($rsslink)) {
f37da850 226 $row[] = $rsslink;
f4646b44 227 }
f37da850 228 $generaltable->data[] = $row;
ecc8403e 229 }
230 }
8f0cd6ef 231 }
ecc8403e 232
38fcc215 233
234 // Start of the table for Learning Forums
235 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
236 $learningtable->align = array ("left", "left", "center");
237
c2468824 238 if ($usetracking) {
f37da850 239 $learningtable->head[] = $strunreadposts;
9fd7165a 240 $learningtable->align[] = 'center';
38994d6f 241
242 $learningtable->head[] = $strtracking;
243 $learningtable->align[] = 'center';
f37da850 244 }
245
f4646b44 246 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
38fcc215 247 $learningtable->head[] = $strsubscribed;
9fd7165a 248 $learningtable->align[] = 'center';
38fcc215 249 }
250
ec81373f 251 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 252 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 253 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
254 $learningtable->head[] = $strrss;
9fd7165a 255 $learningtable->align[] = 'center';
38fcc215 256 }
257
bd83d0a3 258 /// Now let's process the learning forums
ecc8403e 259
94361e02 260 if ($course->category) { // Only real courses have learning forums
261 // Add extra field for section number, at the front
fa5a5b52 262 array_unshift($learningtable->head, "");
263 array_unshift($learningtable->align, "center");
8f0cd6ef 264
a0b9571f 265
86c5e69a 266 if ($learningforums) {
a0b9571f 267 $currentsection = "";
268
1390f203 269 foreach ($learningforums as $key => $forum) {
ecc8403e 270 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
8f0cd6ef 271
ecc8403e 272 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 273 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 274 } else {
275 $count = count_records("forum_discussions", "forum", "$forum->id");
276 }
8f0cd6ef 277
c2468824 278 if (forum_tp_can_track_forums()) {
279 if (!isset($untracked[$forum->id])) {
38994d6f 280 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
281 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
282 if ($unread > 0) {
6de6cd5a 283 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
284 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?id='.
285 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 286 } else {
331c512a 287 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 288 }
6de6cd5a 289 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.$forum->id.'">'.$stryes.'</a>';
f37da850 290 } else {
6de6cd5a 291 $unreadlink = '-';
292 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 293 }
294 }
295
9a0eb679 296 $introoptions->para=false;
297 $forum->intro = format_text(shorten_text(trim($forum->intro), $CFG->forum_shortpost), FORMAT_HTML, $introoptions);
8f0cd6ef 298
a0b9571f 299 if ($forum->section != $currentsection) {
300 $printsection = $forum->section;
ef6a649b 301 if ($currentsection) {
302 $learningtable->data[] = 'hr';
303 }
a0b9571f 304 $currentsection = $forum->section;
305 } else {
306 $printsection = "";
307 }
308
580f2fbc 309 if ($forum->visible) {
3849dae8 310 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 311 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 312 } else {
3849dae8 313 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 314 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 315 }
8f0cd6ef 316
f4646b44 317 //If this forum has RSS activated, calculate it
318 $rsslink = '';
319 if ($show_rss) {
320 if ($forum->rsstype and $forum->rssarticles) {
321 //Calculate the tolltip text
322 if ($forum->rsstype == 1) {
3849dae8 323 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 324 } else {
3849dae8 325 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 326 }
327 if (empty($USER->id)) {
328 $userid = 0;
329 } else {
330 $userid = $USER->id;
331 }
332 //Get html code for RSS link
333 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
334 }
335 }
ec81373f 336
94361e02 337 if ($can_subscribe) {
338 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 339 $sublink = $stryes;
501cdbd8 340 } else {
ecc8403e 341 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
6de6cd5a 342 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
bd83d0a3 343 if ($groupmode == SEPARATEGROUPS) {
3849dae8 344 $forumlink = format_string($forum->name,true);
bd83d0a3 345 }
94361e02 346 } else {
a0b9571f 347 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 348 $subscribed = $stryes;
349 $subtitle = $strunsubscribe;
a0b9571f 350 } else {
6de6cd5a 351 $subscribed = $strno;
352 $subtitle = $strsubscribe;
a0b9571f 353 }
354 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 355 }
501cdbd8 356 }
f37da850 357
9a0eb679 358 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 359 if ($usetracking) {
f37da850 360 $row[] = $unreadlink;
38994d6f 361 $row[] = $trackedlink; // Tracking.
f37da850 362 }
363 $row[] = $sublink;
f4646b44 364 if (!empty($rsslink)) {
f37da850 365 $row[] = $rsslink;
33f48f03 366 }
f37da850 367 $learningtable->data[] = $row;
368
94361e02 369 } else {
9a0eb679 370 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 371 if ($usetracking) {
f37da850 372 $row[] = $unreadlink;
38994d6f 373 $row[] = $trackedlink; // Tracking.
f37da850 374 }
f4646b44 375 if (!empty($rsslink)) {
f37da850 376 $row[] = $rsslink;
f4646b44 377 }
f37da850 378 $learningtable->data[] = $row;
f93f848a 379 }
f93f848a 380 }
381 }
382 }
383
fa5a5b52 384
385 /// Output the page
386
387 if ($course->category) {
388 print_header("$course->shortname: $strforums", "$course->fullname",
38fcc215 389 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $strforums",
fa5a5b52 390 "", "", true, $searchform, navmenu($course));
391 } else {
8f0cd6ef 392 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
fa5a5b52 393 "", "", true, $searchform, navmenu($course));
394 }
395
6031fe85 396 if ($generalforums) {
397 print_heading(get_string("generalforums", "forum"));
398 print_table($generaltable);
399 }
fa5a5b52 400
6031fe85 401 if ($learningforums) {
402 print_heading(get_string("learningforums", "forum"));
403 print_table($learningtable);
404 }
f93f848a 405
406 print_footer($course);
407
3849dae8 408?>