Upgraded to cover things like new upload files class
[moodle.git] / mod / forum / index.php
CommitLineData
41905731 1<?php // $Id$
f93f848a 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
33f48f03 5 require_once("$CFG->dirroot/rss/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
68ddf8bc 19 if ($CFG->forcelogin) {
20 require_login();
21 }
22
f93f848a 23 if ($course->category) {
24 require_login($course->id);
25 }
26
ecc8403e 27 $currentgroup = get_current_group($course->id);
28
501cdbd8 29 unset($SESSION->fromdiscussion);
f93f848a 30
f781b794 31 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
32
33 $strforums = get_string("forums", "forum");
97485d07 34 $strforum = get_string("forum", "forum");
35 $strdescription = get_string("description");
36 $strdiscussions = get_string("discussions", "forum");
37 $strsubscribed = get_string("subscribed", "forum");
33f48f03 38 $strrss = get_string("rss");
97485d07 39
40 $searchform = forum_print_search_form($course, "", true, "plain");
f93f848a 41
f781b794 42
fa5a5b52 43 // Build up the tables
f781b794 44
fa5a5b52 45 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
839f2456 46 $generaltable->align = array ("left", "left", "center");
f93f848a 47
fa5a5b52 48 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
49 $generaltable->head[] = $strsubscribed;
839f2456 50 $generaltable->align[] = "center";
f93f848a 51 }
f93f848a 52
33f48f03 53 if ($show_rss = ($can_subscribe && isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
54 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds)) {
55 $generaltable->head[] = $strrss;
839f2456 56 $generaltable->align[] = "center";
33f48f03 57 }
58
fa5a5b52 59 $learningtable = $generaltable; // Headers etc are the same
60
ecc8403e 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
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;
95 case "teacher":
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 }
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 }
122
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 }
128
129 $forum->intro = forum_shorten_post($forum->intro);
130 replace_smilies($forum->intro);
131 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";;
132
133 if ($forum->visible) {
134 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
135 } else {
136 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
137 }
138
139 if ($can_subscribe) {
140 if (forum_is_forcesubscribed($forum->id)) {
141 $sublink = get_string("yes");
142 } else {
143 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
144 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
145 $forumlink = $forum->name;
146 } else {
147 if (forum_is_subscribed($USER->id, $forum->id)) {
148 $subscribed = get_string("yes");
149 $subtitle = get_string("unsubscribe", "forum");
150 } else {
151 $subscribed = get_string("no");
152 $subtitle = get_string("subscribe", "forum");
153 }
154 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
155 }
156 }
33f48f03 157 //If this forum has RSS activated, calculate it
158 if ($show_rss) {
159 $rsslink = '';
160 if ($forum->rsstype and $forum->rssarticles) {
161 //Calculate the tolltip text
162 if ($forum->rsstype == 1) {
163 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",$forum->name);
164 } else {
165 $tooltiptext = get_string("rsssubscriberssposts","forum",$forum->name);
166 }
167 //Get html code for RSS link
168 $rsslink = rss_get_link($course->id, $USER->id, "forum", $forum->id, $tooltiptext);
169 }
170 //Save data
171 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count", $sublink,$rsslink);
172 } else {
173 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count", $sublink);
174 }
ecc8403e 175 } else {
176 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count");
177 }
178 }
179 }
180
bd83d0a3 181 /// Now let's process the learning forums
ecc8403e 182
94361e02 183 if ($course->category) { // Only real courses have learning forums
184 // Add extra field for section number, at the front
fa5a5b52 185 array_unshift($learningtable->head, "");
186 array_unshift($learningtable->align, "center");
94361e02 187
a0b9571f 188
86c5e69a 189 if ($learningforums) {
a0b9571f 190 $currentsection = "";
191
1390f203 192 foreach ($learningforums as $key => $forum) {
ecc8403e 193 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
194
195 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 196 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 197 } else {
198 $count = count_records("forum_discussions", "forum", "$forum->id");
199 }
94361e02 200
4b2b7a71 201 $forum->intro = forum_shorten_post($forum->intro);
202 replace_smilies($forum->intro);
ef6a649b 203 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";
94361e02 204
a0b9571f 205 if ($forum->section != $currentsection) {
206 $printsection = $forum->section;
ef6a649b 207 if ($currentsection) {
208 $learningtable->data[] = 'hr';
209 }
a0b9571f 210 $currentsection = $forum->section;
211 } else {
212 $printsection = "";
213 }
214
580f2fbc 215 if ($forum->visible) {
216 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 217 } else {
580f2fbc 218 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 219 }
580f2fbc 220
94361e02 221 if ($can_subscribe) {
222 if (forum_is_forcesubscribed($forum->id)) {
223 $sublink = get_string("yes");
501cdbd8 224 } else {
ecc8403e 225 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
a0b9571f 226 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
bd83d0a3 227 if ($groupmode == SEPARATEGROUPS) {
228 $forumlink = $forum->name;
229 }
94361e02 230 } else {
a0b9571f 231 if (forum_is_subscribed($USER->id, $forum->id)) {
232 $subscribed = get_string("yes");
233 $subtitle = get_string("unsubscribe", "forum");
234 } else {
235 $subscribed = get_string("no");
236 $subtitle = get_string("subscribe", "forum");
237 }
238 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 239 }
501cdbd8 240 }
33f48f03 241 //If this forum has RSS activated, calculate it
242 if ($show_rss) {
243 $rsslink = '';
244 if ($forum->rsstype and $forum->rssarticles) {
245 //Calculate the tolltip text
246 if ($forum->rsstype == 1) {
247 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",$forum->name);
248 } else {
249 $tooltiptext = get_string("rsssubscriberssposts","forum",$forum->name);
250 }
251 //Get html code for RSS link
252 $rsslink = rss_get_link($course->id, $USER->id, "forum", $forum->id, $tooltiptext);
253 }
254 //Save data
255 $learningtable->data[] = array ($printsection,$forumlink, "$forum->intro", "$count", $sublink,$rsslink);
256 } else {
257 $learningtable->data[] = array ($printsection,$forumlink, "$forum->intro", "$count", $sublink);
258 }
94361e02 259 } else {
a0b9571f 260 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count");
f93f848a 261 }
f93f848a 262 }
263 }
264 }
265
fa5a5b52 266
267 /// Output the page
268
269 if ($course->category) {
270 print_header("$course->shortname: $strforums", "$course->fullname",
41905731 271 "<a href=../../course/view.php?id=$course->id>$course->shortname</a> -> $strforums",
fa5a5b52 272 "", "", true, $searchform, navmenu($course));
273 } else {
274 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
275 "", "", true, $searchform, navmenu($course));
276 }
277
6031fe85 278 if ($generalforums) {
279 print_heading(get_string("generalforums", "forum"));
280 print_table($generaltable);
281 }
fa5a5b52 282
6031fe85 283 if ($learningforums) {
284 print_heading(get_string("learningforums", "forum"));
285 print_table($learningtable);
286 }
f93f848a 287
288 print_footer($course);
289
290?>