import of ADODB v4.91 and general cleanup, removed some unused files - see readme_moo...
[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
70e3da07 7 $id = optional_param('id',0,PARAM_INT); // course
03877b04 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);
f93f848a 21
ecc8403e 22 $currentgroup = get_current_group($course->id);
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");
97485d07 43
6f1cc8d6 44 $searchform = forum_search_form($course);
f93f848a 45
f781b794 46
38fcc215 47 // Start of the table for General Forums
f781b794 48
fa5a5b52 49 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
9fd7165a 50 $generaltable->align = array ('left', 'left', 'center');
f37da850 51
c2468824 52 if ($usetracking = forum_tp_can_track_forums()) {
eaf50aef 53 $untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
c2468824 54
f37da850 55 $generaltable->head[] = $strunreadposts;
9fd7165a 56 $generaltable->align[] = 'center';
38994d6f 57
38994d6f 58 $generaltable->head[] = $strtracking;
59 $generaltable->align[] = 'center';
f37da850 60 }
f93f848a 61
f4646b44 62 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
fa5a5b52 63 $generaltable->head[] = $strsubscribed;
9fd7165a 64 $generaltable->align[] = 'center';
f93f848a 65 }
f93f848a 66
ec81373f 67 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 68 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
33f48f03 69 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
70 $generaltable->head[] = $strrss;
9fd7165a 71 $generaltable->align[] = 'center';
33f48f03 72 }
73
fa5a5b52 74
8f0cd6ef 75 // Parse and organise all the forums. Most forums are course modules but
76 // some special ones are not. These get placed in the general forums
ecc8403e 77 // category with the forums in section 0.
78
79 $generalforums = array(); // For now
80 $learningforums = get_all_instances_in_course("forum", $course);
81
82 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
83
84 if ($learningforums) { // Copy "full" data into this complete array
bd83d0a3 85 foreach ($learningforums as $key => $learningforum) {
86 $learningforum->keyreference = $key;
a0a4a3c9 87 $forums[$learningforum->id] = $learningforum;
ecc8403e 88 }
89 }
fa5a5b52 90
71fb9630 91 foreach ($forums as $forum) {
a0a4a3c9 92 if (!isset($forum->visible)) {
9c8b8eee 93 $forum->visible = instance_is_visible("forum", $forum);
bbbf2d40 94 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
95 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
0468976c 96 if (!$forum->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
9c8b8eee 97 if (isset($forum->keyreference)) {
98 unset($learningforums[$forum->keyreference]);
99 }
100 continue;
101 }
a0a4a3c9 102 }
37b15514 103 switch ($forum->type) {
0872b023 104 case "news":
105 case "social":
106 $generalforums[] = $forum;
bd83d0a3 107 if (isset($forum->keyreference)) { // Should always be
108 unset($learningforums[$forum->keyreference]);
ecc8403e 109 }
37b15514 110 break;
bbbf2d40 111 /*
8f0cd6ef 112 case "teacher":
37b15514 113 if (isteacher($course->id)) {
ecc8403e 114 $forum->visible = true;
37b15514 115 $generalforums[] = $forum;
116 }
117 break;
bbbf2d40 118 */
94361e02 119 default:
a0a4a3c9 120 if (!$course->category or empty($forum->section)) { // Site level or section 0
94361e02 121 $generalforums[] = $forum;
bd83d0a3 122 if (isset($forum->keyreference)) {
123 unset($learningforums[$forum->keyreference]);
a0a4a3c9 124 }
8f0cd6ef 125 }
94361e02 126 break;
71fb9630 127 }
37b15514 128 }
129 }
71fb9630 130
03877b04 131 /// Do course wide subscribe/unsubscribe
132 if (!is_null($subscribe) && !isguest()) {
133 $allforums = array_merge($generalforums, $learningforums);
134 if ($allforums) {
135 foreach ($allforums as $forum) {
136 if (!forum_is_forcesubscribed($forum->id)) {
137 $subscribed = forum_is_subscribed($USER->id, $forum->id);
138 if ($subscribe && !$subscribed) {
139 forum_subscribe($USER->id, $forum->id);
140 } elseif (!$subscribe && $subscribed) {
141 forum_unsubscribe($USER->id, $forum->id);
142 }
143 }
144 }
145 }
146 $returnto = forum_go_back_to("index.php?id=$course->id");
147 if ($subscribe) {
148 add_to_log($course->id, "forum", "subscribeall", "index.php?id=$course->id", $course->id);
149 redirect($returnto, get_string("nowallsubscribed", "forum", $course->shortname), 1);
150 } else {
151 add_to_log($course->id, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id);
152 redirect($returnto, get_string("nowallunsubscribed", "forum", $course->shortname), 1);
153 }
154 }
155
ecc8403e 156 /// First, let's process the general forums and build up a display
157
158 if ($generalforums) {
159 foreach ($generalforums as $forum) {
bbbf2d40 160
161 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
162 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
163
164 if (isset($forum->groupmode)) {
ecc8403e 165 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
166 } else {
167 $groupmode = NOGROUPS;
168 }
8f0cd6ef 169
bbbf2d40 170
171 // this is potentially wrong logic. could possibly check for if user has the right to hmmm
0468976c 172 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2862b309 173 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
ecc8403e 174 } else {
175 $count = count_records("forum_discussions", "forum", "$forum->id");
176 }
8f0cd6ef 177
eaf50aef 178 if ($usetracking) {
179 if (($forum->trackingtype == FORUM_TRACKING_ON) || !isset($untracked[$forum->id])) {
0468976c 180 $groupid = ($groupmode==SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) ? $currentgroup : false;
38994d6f 181 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
182 if ($unread > 0) {
6de6cd5a 183 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 184 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 185 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 186 } else {
187 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
188 }
630bd96b 189
bbbf2d40 190
eaf50aef 191 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
192 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
193 $forum->id.'">'.$stryes.'</a>';
194 }
630bd96b 195 else {
196 $trackedlink = $stryes;
197 }
f37da850 198 } else {
6de6cd5a 199 $unreadlink = '-';
eaf50aef 200 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.
201 $forum->id.'">'.$strno.'</a>';
f37da850 202 }
203 }
204
9a0eb679 205 $introoptions->para=false;
91121c89 206 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
ecc8403e 207
208 if ($forum->visible) {
3849dae8 209 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 210 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 211 } else {
3849dae8 212 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 213 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
ecc8403e 214 }
215
f4646b44 216 //If this forum has RSS activated, calculate it
217 $rsslink = '';
218 if ($show_rss) {
219 if ($forum->rsstype and $forum->rssarticles) {
220 //Calculate the tolltip text
221 if ($forum->rsstype == 1) {
3849dae8 222 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 223 } else {
3849dae8 224 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 225 }
226 if (empty($USER->id)) {
227 $userid = 0;
228 } else {
229 $userid = $USER->id;
230 }
231 //Get html code for RSS link
232 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
233 }
234 }
235
ecc8403e 236 if ($can_subscribe) {
237 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 238 $sublink = $stryes;
ecc8403e 239 } else {
0468976c 240 if ($groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id)) {
6de6cd5a 241 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
3849dae8 242 $forumlink = format_string($forum->name,true);
ecc8403e 243 } else {
244 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 245 $subscribed = $stryes;
ecc8403e 246 $subtitle = get_string("unsubscribe", "forum");
247 } else {
6de6cd5a 248 $subscribed = $strno;
ecc8403e 249 $subtitle = get_string("subscribe", "forum");
250 }
098d27d4 251 if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !isteacher($forum->course)) {
252 $sublink = '-';
253 } else {
254 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
255 }
ecc8403e 256 }
257 }
9a0eb679 258 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 259 if ($usetracking) {
f37da850 260 $row[] = $unreadlink;
38994d6f 261 $row[] = $trackedlink; // Tracking.
f37da850 262 }
263 $row[] = $sublink;
a9022f9e 264 if ($show_rss) {
f37da850 265 $row[] = $rsslink;
33f48f03 266 }
f37da850 267 $generaltable->data[] = $row;
ecc8403e 268 } else {
9a0eb679 269 $row = array ($forumlink, $forum->intro, $discussionlink);
c2468824 270 if ($usetracking) {
f37da850 271 $row[] = $unreadlink;
38994d6f 272 $row[] = $trackedlink; // Tracking.
f37da850 273 }
a9022f9e 274 if ($show_rss) {
f37da850 275 $row[] = $rsslink;
f4646b44 276 }
f37da850 277 $generaltable->data[] = $row;
ecc8403e 278 }
279 }
8f0cd6ef 280 }
ecc8403e 281
38fcc215 282
283 // Start of the table for Learning Forums
284 $learningtable->head = array ($strforum, $strdescription, $strdiscussions);
285 $learningtable->align = array ("left", "left", "center");
286
c2468824 287 if ($usetracking) {
f37da850 288 $learningtable->head[] = $strunreadposts;
9fd7165a 289 $learningtable->align[] = 'center';
38994d6f 290
291 $learningtable->head[] = $strtracking;
292 $learningtable->align[] = 'center';
f37da850 293 }
294
f4646b44 295 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
38fcc215 296 $learningtable->head[] = $strsubscribed;
9fd7165a 297 $learningtable->align[] = 'center';
38fcc215 298 }
299
ec81373f 300 if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
f4646b44 301 isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
38fcc215 302 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
303 $learningtable->head[] = $strrss;
9fd7165a 304 $learningtable->align[] = 'center';
38fcc215 305 }
306
bd83d0a3 307 /// Now let's process the learning forums
ecc8403e 308
94361e02 309 if ($course->category) { // Only real courses have learning forums
310 // Add extra field for section number, at the front
fa5a5b52 311 array_unshift($learningtable->head, "");
312 array_unshift($learningtable->align, "center");
8f0cd6ef 313
a0b9571f 314
86c5e69a 315 if ($learningforums) {
a0b9571f 316 $currentsection = "";
1390f203 317 foreach ($learningforums as $key => $forum) {
ecc8403e 318 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
bbbf2d40 319 $forum->visible = instance_is_visible("forum", $forum);
320 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
321
0468976c 322 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
5b5f2971 323 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 324 } else {
325 $count = count_records("forum_discussions", "forum", "$forum->id");
326 }
8f0cd6ef 327
eaf50aef 328 if ($usetracking) {
329 if (($forum->trackingtype == FORUM_TRACKING_ON) ||
330 !isset($untracked[$forum->id])) {
38994d6f 331 $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false;
332 $unread = forum_tp_count_forum_unread_posts($USER->id, $forum->id, $groupid);
333 if ($unread > 0) {
6de6cd5a 334 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'">'.$unread.'</a>';
c39748f4 335 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
6de6cd5a 336 $forum->id.'&amp;mark=read"><img src="'.$CFG->pixpath.'/t/clear.gif" alt="" /></a></span>';
38994d6f 337 } else {
331c512a 338 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id.'">'.$unread.'</a></span>';
38994d6f 339 }
eaf50aef 340 if ($forum->trackingtype == FORUM_TRACKING_OPTIONAL) {
341 $trackedlink = '<a title="'.$strnotrackforum.'" href="settracking.php?id='.
342 $forum->id.'">'.$stryes.'</a>';
343 }
f37da850 344 } else {
6de6cd5a 345 $unreadlink = '-';
346 $trackedlink = '<a title="'.$strtrackforum.'" href="settracking.php?id='.$forum->id.'">'.$strno.'</a>';
f37da850 347 }
348 }
349
9a0eb679 350 $introoptions->para=false;
91121c89 351 $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost);
8f0cd6ef 352
a0b9571f 353 if ($forum->section != $currentsection) {
354 $printsection = $forum->section;
ef6a649b 355 if ($currentsection) {
356 $learningtable->data[] = 'hr';
357 }
a0b9571f 358 $currentsection = $forum->section;
359 } else {
360 $printsection = "";
361 }
362
580f2fbc 363 if ($forum->visible) {
3849dae8 364 $forumlink = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 365 $discussionlink = "<a href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 366 } else {
3849dae8 367 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
9a0eb679 368 $discussionlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">".$count."</a>";
3b0fb381 369 }
8f0cd6ef 370
f4646b44 371 //If this forum has RSS activated, calculate it
372 $rsslink = '';
373 if ($show_rss) {
374 if ($forum->rsstype and $forum->rssarticles) {
375 //Calculate the tolltip text
376 if ($forum->rsstype == 1) {
3849dae8 377 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
f4646b44 378 } else {
3849dae8 379 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 380 }
381 if (empty($USER->id)) {
382 $userid = 0;
383 } else {
384 $userid = $USER->id;
385 }
386 //Get html code for RSS link
387 $rsslink = rss_get_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
388 }
389 }
ec81373f 390
94361e02 391 if ($can_subscribe) {
392 if (forum_is_forcesubscribed($forum->id)) {
6de6cd5a 393 $sublink = $stryes;
501cdbd8 394 } else {
ecc8403e 395 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
6de6cd5a 396 $sublink = $strno; // Can't subscribe to a group forum (not in a group)
bd83d0a3 397 if ($groupmode == SEPARATEGROUPS) {
3849dae8 398 $forumlink = format_string($forum->name,true);
bd83d0a3 399 }
94361e02 400 } else {
a0b9571f 401 if (forum_is_subscribed($USER->id, $forum->id)) {
6de6cd5a 402 $subscribed = $stryes;
403 $subtitle = $strunsubscribe;
a0b9571f 404 } else {
6de6cd5a 405 $subscribed = $strno;
406 $subtitle = $strsubscribe;
a0b9571f 407 }
408 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 409 }
501cdbd8 410 }
f37da850 411
9a0eb679 412 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 413 if ($usetracking) {
f37da850 414 $row[] = $unreadlink;
38994d6f 415 $row[] = $trackedlink; // Tracking.
f37da850 416 }
417 $row[] = $sublink;
a9022f9e 418 if ($show_rss) {
f37da850 419 $row[] = $rsslink;
33f48f03 420 }
f37da850 421 $learningtable->data[] = $row;
422
94361e02 423 } else {
9a0eb679 424 $row = array ($printsection, $forumlink, $forum->intro, $discussionlink);
c2468824 425 if ($usetracking) {
f37da850 426 $row[] = $unreadlink;
38994d6f 427 $row[] = $trackedlink; // Tracking.
f37da850 428 }
a9022f9e 429 if ($show_rss) {
f37da850 430 $row[] = $rsslink;
f4646b44 431 }
f37da850 432 $learningtable->data[] = $row;
f93f848a 433 }
f93f848a 434 }
435 }
436 }
437
fa5a5b52 438
439 /// Output the page
440
441 if ($course->category) {
442 print_header("$course->shortname: $strforums", "$course->fullname",
38fcc215 443 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $strforums",
fa5a5b52 444 "", "", true, $searchform, navmenu($course));
445 } else {
8f0cd6ef 446 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
fa5a5b52 447 "", "", true, $searchform, navmenu($course));
448 }
449
03877b04 450 if (!isguest()) {
451 echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top"><td align="right" class="subscription">';
452 echo '<span class="helplink">';
453 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
454 echo '<br />';
455 echo '<a href="index.php?id='.$course->id.'&amp;subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
456 echo '</span></td></tr></table>';
457 }
458
6031fe85 459 if ($generalforums) {
460 print_heading(get_string("generalforums", "forum"));
461 print_table($generaltable);
462 }
fa5a5b52 463
6031fe85 464 if ($learningforums) {
465 print_heading(get_string("learningforums", "forum"));
466 print_table($learningtable);
467 }
f93f848a 468
469 print_footer($course);
470
3849dae8 471?>