Upgraded to latest version and fixed some undefined variables
[moodle.git] / mod / forum / index.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
f93f848a 5
6 optional_variable($id); // course
7
8 if ($id) {
9 if (! $course = get_record("course", "id", $id)) {
10 error("Course ID is incorrect");
11 }
12 } else {
f781b794 13 if (! $course = get_site()) {
f93f848a 14 error("Could not find a top-level course!");
15 }
16 }
17
68ddf8bc 18 if ($CFG->forcelogin) {
19 require_login();
20 }
21
f93f848a 22 if ($course->category) {
23 require_login($course->id);
24 }
25
ecc8403e 26 $currentgroup = get_current_group($course->id);
27
501cdbd8 28 unset($SESSION->fromdiscussion);
f93f848a 29
f781b794 30 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
31
32 $strforums = get_string("forums", "forum");
97485d07 33 $strforum = get_string("forum", "forum");
34 $strdescription = get_string("description");
35 $strdiscussions = get_string("discussions", "forum");
36 $strsubscribed = get_string("subscribed", "forum");
37
38 $searchform = forum_print_search_form($course, "", true, "plain");
f93f848a 39
f781b794 40
fa5a5b52 41 // Build up the tables
f781b794 42
fa5a5b52 43 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
44 $generaltable->align = array ("LEFT", "LEFT", "CENTER");
f93f848a 45
fa5a5b52 46 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
47 $generaltable->head[] = $strsubscribed;
48 $generaltable->align[] = "CENTER";
f93f848a 49 }
f93f848a 50
fa5a5b52 51 $learningtable = $generaltable; // Headers etc are the same
52
ecc8403e 53 // Parse and organise all the forums. Most forums are course modules but
54 // some special ones are not. These get placed in the general forums
55 // category with the forums in section 0.
56
57 $generalforums = array(); // For now
58 $learningforums = get_all_instances_in_course("forum", $course);
59
60 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
61
62 if ($learningforums) { // Copy "full" data into this complete array
bd83d0a3 63 foreach ($learningforums as $key => $learningforum) {
64 $learningforum->keyreference = $key;
a0a4a3c9 65 $forums[$learningforum->id] = $learningforum;
ecc8403e 66 }
67 }
fa5a5b52 68
71fb9630 69 foreach ($forums as $forum) {
a0a4a3c9 70 if (!isset($forum->visible)) {
71 $forum->visible = true;
72 }
37b15514 73 switch ($forum->type) {
0872b023 74 case "news":
75 case "social":
76 $generalforums[] = $forum;
bd83d0a3 77 if (isset($forum->keyreference)) { // Should always be
78 unset($learningforums[$forum->keyreference]);
ecc8403e 79 }
37b15514 80 break;
81 case "teacher":
82 if (isteacher($course->id)) {
ecc8403e 83 $forum->visible = true;
37b15514 84 $generalforums[] = $forum;
85 }
86 break;
94361e02 87 default:
a0a4a3c9 88 if (!$course->category or empty($forum->section)) { // Site level or section 0
94361e02 89 $generalforums[] = $forum;
bd83d0a3 90 if (isset($forum->keyreference)) {
91 unset($learningforums[$forum->keyreference]);
a0a4a3c9 92 }
93 }
94361e02 94 break;
71fb9630 95 }
37b15514 96 }
97 }
71fb9630 98
ecc8403e 99 /// First, let's process the general forums and build up a display
100
101 if ($generalforums) {
102 foreach ($generalforums as $forum) {
103 if (isset($forum->groupmode)) {
104 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
105 } else {
106 $groupmode = NOGROUPS;
107 }
108
109 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 110 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 111 } else {
112 $count = count_records("forum_discussions", "forum", "$forum->id");
113 }
114
115 $forum->intro = forum_shorten_post($forum->intro);
116 replace_smilies($forum->intro);
117 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";;
118
119 if ($forum->visible) {
120 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
121 } else {
122 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
123 }
124
125 if ($can_subscribe) {
126 if (forum_is_forcesubscribed($forum->id)) {
127 $sublink = get_string("yes");
128 } else {
129 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
130 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
131 $forumlink = $forum->name;
132 } else {
133 if (forum_is_subscribed($USER->id, $forum->id)) {
134 $subscribed = get_string("yes");
135 $subtitle = get_string("unsubscribe", "forum");
136 } else {
137 $subscribed = get_string("no");
138 $subtitle = get_string("subscribe", "forum");
139 }
140 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
141 }
142 }
143 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count", $sublink);
144 } else {
145 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count");
146 }
147 }
148 }
149
bd83d0a3 150 /// Now let's process the learning forums
ecc8403e 151
94361e02 152 if ($course->category) { // Only real courses have learning forums
153 // Add extra field for section number, at the front
fa5a5b52 154 array_unshift($learningtable->head, "");
155 array_unshift($learningtable->align, "center");
94361e02 156
a0b9571f 157
86c5e69a 158 if ($learningforums) {
a0b9571f 159 $currentsection = "";
160
1390f203 161 foreach ($learningforums as $key => $forum) {
ecc8403e 162 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
163
164 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 165 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 166 } else {
167 $count = count_records("forum_discussions", "forum", "$forum->id");
168 }
94361e02 169
4b2b7a71 170 $forum->intro = forum_shorten_post($forum->intro);
171 replace_smilies($forum->intro);
ef6a649b 172 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";
94361e02 173
a0b9571f 174 if ($forum->section != $currentsection) {
175 $printsection = $forum->section;
ef6a649b 176 if ($currentsection) {
177 $learningtable->data[] = 'hr';
178 }
a0b9571f 179 $currentsection = $forum->section;
180 } else {
181 $printsection = "";
182 }
183
580f2fbc 184 if ($forum->visible) {
185 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 186 } else {
580f2fbc 187 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 188 }
580f2fbc 189
94361e02 190 if ($can_subscribe) {
191 if (forum_is_forcesubscribed($forum->id)) {
192 $sublink = get_string("yes");
501cdbd8 193 } else {
ecc8403e 194 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
a0b9571f 195 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
bd83d0a3 196 if ($groupmode == SEPARATEGROUPS) {
197 $forumlink = $forum->name;
198 }
94361e02 199 } else {
a0b9571f 200 if (forum_is_subscribed($USER->id, $forum->id)) {
201 $subscribed = get_string("yes");
202 $subtitle = get_string("unsubscribe", "forum");
203 } else {
204 $subscribed = get_string("no");
205 $subtitle = get_string("subscribe", "forum");
206 }
207 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 208 }
501cdbd8 209 }
a0b9571f 210 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count", "$sublink");
94361e02 211 } else {
a0b9571f 212 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count");
f93f848a 213 }
f93f848a 214 }
215 }
216 }
217
fa5a5b52 218
219 /// Output the page
220
221 if ($course->category) {
222 print_header("$course->shortname: $strforums", "$course->fullname",
223 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> -> $strforums",
224 "", "", true, $searchform, navmenu($course));
225 } else {
226 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
227 "", "", true, $searchform, navmenu($course));
228 }
229
6031fe85 230 if ($generalforums) {
231 print_heading(get_string("generalforums", "forum"));
232 print_table($generaltable);
233 }
fa5a5b52 234
6031fe85 235 if ($learningforums) {
236 print_heading(get_string("learningforums", "forum"));
237 print_table($learningtable);
238 }
f93f848a 239
240 print_footer($course);
241
242?>