Some fixes to counting, still need to fix some access issues
[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
63 foreach ($learningforums as $learningforum) {
a0a4a3c9 64 $forums[$learningforum->id] = $learningforum;
ecc8403e 65 }
66 }
fa5a5b52 67
71fb9630 68 foreach ($forums as $forum) {
a0a4a3c9 69 if (!isset($forum->visible)) {
70 $forum->visible = true;
71 }
37b15514 72 switch ($forum->type) {
0872b023 73 case "news":
74 case "social":
75 $generalforums[] = $forum;
ecc8403e 76 if (isset($forum->coursemodule)) { // Should always be
77 unset($learningforums[$forum->coursemodule]);
78 }
37b15514 79 break;
80 case "teacher":
81 if (isteacher($course->id)) {
ecc8403e 82 $forum->visible = true;
37b15514 83 $generalforums[] = $forum;
84 }
85 break;
94361e02 86 default:
a0a4a3c9 87 if (!$course->category or empty($forum->section)) { // Site level or section 0
94361e02 88 $generalforums[] = $forum;
a0a4a3c9 89 if (isset($forum->coursemodule)) {
90 unset($learningforums[$forum->coursemodule]);
91 }
92 }
94361e02 93 break;
71fb9630 94 }
37b15514 95 }
96 }
71fb9630 97
ecc8403e 98 /// First, let's process the general forums and build up a display
99
100 if ($generalforums) {
101 foreach ($generalforums as $forum) {
102 if (isset($forum->groupmode)) {
103 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
104 } else {
105 $groupmode = NOGROUPS;
106 }
107
108 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 109 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 110 } else {
111 $count = count_records("forum_discussions", "forum", "$forum->id");
112 }
113
114 $forum->intro = forum_shorten_post($forum->intro);
115 replace_smilies($forum->intro);
116 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";;
117
118 if ($forum->visible) {
119 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
120 } else {
121 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
122 }
123
124 if ($can_subscribe) {
125 if (forum_is_forcesubscribed($forum->id)) {
126 $sublink = get_string("yes");
127 } else {
128 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
129 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
130 $forumlink = $forum->name;
131 } else {
132 if (forum_is_subscribed($USER->id, $forum->id)) {
133 $subscribed = get_string("yes");
134 $subtitle = get_string("unsubscribe", "forum");
135 } else {
136 $subscribed = get_string("no");
137 $subtitle = get_string("subscribe", "forum");
138 }
139 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
140 }
141 }
142 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count", $sublink);
143 } else {
144 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count");
145 }
146 }
147 }
148
149 /// Now let's process the other forums and build up a display
150
94361e02 151 if ($course->category) { // Only real courses have learning forums
152 // Add extra field for section number, at the front
fa5a5b52 153 array_unshift($learningtable->head, "");
154 array_unshift($learningtable->align, "center");
94361e02 155
a0b9571f 156
86c5e69a 157 if ($learningforums) {
a0b9571f 158 $currentsection = "";
159
1390f203 160 foreach ($learningforums as $key => $forum) {
ecc8403e 161 $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined
162
163 if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id)) {
5b5f2971 164 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
ecc8403e 165 } else {
166 $count = count_records("forum_discussions", "forum", "$forum->id");
167 }
94361e02 168
4b2b7a71 169 $forum->intro = forum_shorten_post($forum->intro);
170 replace_smilies($forum->intro);
ef6a649b 171 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";
94361e02 172
fa5a5b52 173 if (!$forum->section) { // forums in the "0" section => generaltable
174 $generalforums[] = $forum;
1390f203 175 unset($learningforums[$key]);
fa5a5b52 176 continue;
94361e02 177 }
3b0fb381 178
a0b9571f 179 if ($forum->section != $currentsection) {
180 $printsection = $forum->section;
ef6a649b 181 if ($currentsection) {
182 $learningtable->data[] = 'hr';
183 }
a0b9571f 184 $currentsection = $forum->section;
185 } else {
186 $printsection = "";
187 }
188
580f2fbc 189 if ($forum->visible) {
190 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 191 } else {
580f2fbc 192 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 193 }
580f2fbc 194
94361e02 195 if ($can_subscribe) {
196 if (forum_is_forcesubscribed($forum->id)) {
197 $sublink = get_string("yes");
501cdbd8 198 } else {
ecc8403e 199 if ($groupmode and !isteacheredit($course->id) and !mygroupid($course->id)) {
a0b9571f 200 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
c585ba8b 201 $forumlink = $forum->name;
94361e02 202 } else {
a0b9571f 203 if (forum_is_subscribed($USER->id, $forum->id)) {
204 $subscribed = get_string("yes");
205 $subtitle = get_string("unsubscribe", "forum");
206 } else {
207 $subscribed = get_string("no");
208 $subtitle = get_string("subscribe", "forum");
209 }
210 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 211 }
501cdbd8 212 }
a0b9571f 213 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count", "$sublink");
94361e02 214 } else {
a0b9571f 215 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count");
f93f848a 216 }
f93f848a 217 }
218 }
219 }
220
fa5a5b52 221
222 /// Output the page
223
224 if ($course->category) {
225 print_header("$course->shortname: $strforums", "$course->fullname",
226 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> -> $strforums",
227 "", "", true, $searchform, navmenu($course));
228 } else {
229 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
230 "", "", true, $searchform, navmenu($course));
231 }
232
6031fe85 233 if ($generalforums) {
234 print_heading(get_string("generalforums", "forum"));
235 print_table($generaltable);
236 }
fa5a5b52 237
6031fe85 238 if ($learningforums) {
239 print_heading(get_string("learningforums", "forum"));
240 print_table($learningtable);
241 }
f93f848a 242
243 print_footer($course);
244
245?>