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