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