Add a count of the number of activites using a particular module
[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
501cdbd8 26 unset($SESSION->fromdiscussion);
f93f848a 27
f781b794 28 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
29
30 $strforums = get_string("forums", "forum");
97485d07 31 $strforum = get_string("forum", "forum");
32 $strdescription = get_string("description");
33 $strdiscussions = get_string("discussions", "forum");
34 $strsubscribed = get_string("subscribed", "forum");
35
36 $searchform = forum_print_search_form($course, "", true, "plain");
f93f848a 37
f781b794 38
fa5a5b52 39 // Build up the tables
f781b794 40
fa5a5b52 41 $generaltable->head = array ($strforum, $strdescription, $strdiscussions);
42 $generaltable->align = array ("LEFT", "LEFT", "CENTER");
f93f848a 43
fa5a5b52 44 if ($can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin())) {
45 $generaltable->head[] = $strsubscribed;
46 $generaltable->align[] = "CENTER";
f93f848a 47 }
f93f848a 48
fa5a5b52 49 $learningtable = $generaltable; // Headers etc are the same
50
51 // Parse the forums
52
580f2fbc 53 if ($forums = get_records("forum", "course", $id, "name ASC")) {
71fb9630 54 foreach ($forums as $forum) {
580f2fbc 55 $forum->visible = true;
37b15514 56 switch ($forum->type) {
0872b023 57 case "news":
58 case "social":
580f2fbc 59 $forum->visible = instance_is_visible("forum", $forum);
0872b023 60 $generalforums[] = $forum;
37b15514 61 break;
62 case "teacher":
63 if (isteacher($course->id)) {
64 $generalforums[] = $forum;
65 }
66 break;
94361e02 67 default:
68 if (!$course->category) {
69 $generalforums[] = $forum;
70 }
71 break;
71fb9630 72 }
37b15514 73 }
74 }
71fb9630 75
94361e02 76 if ($course->category) { // Only real courses have learning forums
77 // Add extra field for section number, at the front
fa5a5b52 78 array_unshift($learningtable->head, "");
79 array_unshift($learningtable->align, "center");
94361e02 80
cccb016a 81 if ($learningforums = get_all_instances_in_course("forum", $course)) {
86c5e69a 82 foreach ($learningforums as $key => $forum) {
83 if ($forum->type == "news" or $forum->type == "social") {
84 unset($learningforums[$key]); // Remove these
85 }
86 }
87 }
a0b9571f 88
86c5e69a 89 if ($learningforums) {
a0b9571f 90 $currentsection = "";
91
1390f203 92 foreach ($learningforums as $key => $forum) {
94361e02 93 $count = count_records("forum_discussions", "forum", "$forum->id");
94
4b2b7a71 95 $forum->intro = forum_shorten_post($forum->intro);
96 replace_smilies($forum->intro);
ef6a649b 97 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";
94361e02 98
fa5a5b52 99 if (!$forum->section) { // forums in the "0" section => generaltable
100 $generalforums[] = $forum;
1390f203 101 unset($learningforums[$key]);
fa5a5b52 102 continue;
94361e02 103 }
3b0fb381 104
a0b9571f 105 if ($forum->section != $currentsection) {
106 $printsection = $forum->section;
ef6a649b 107 if ($currentsection) {
108 $learningtable->data[] = 'hr';
109 }
a0b9571f 110 $currentsection = $forum->section;
111 } else {
112 $printsection = "";
113 }
114
580f2fbc 115 if ($forum->visible) {
116 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 117 } else {
580f2fbc 118 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
3b0fb381 119 }
580f2fbc 120
94361e02 121 if ($can_subscribe) {
122 if (forum_is_forcesubscribed($forum->id)) {
123 $sublink = get_string("yes");
501cdbd8 124 } else {
c585ba8b 125 if (groupmode($course, $forum) and !isteacheredit($course->id) and !mygroupid($course->id)) {
a0b9571f 126 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
c585ba8b 127 $forumlink = $forum->name;
94361e02 128 } else {
a0b9571f 129 if (forum_is_subscribed($USER->id, $forum->id)) {
130 $subscribed = get_string("yes");
131 $subtitle = get_string("unsubscribe", "forum");
132 } else {
133 $subscribed = get_string("no");
134 $subtitle = get_string("subscribe", "forum");
135 }
136 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
94361e02 137 }
501cdbd8 138 }
a0b9571f 139 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count", "$sublink");
94361e02 140 } else {
a0b9571f 141 $learningtable->data[] = array ($printsection, $forumlink, "$forum->intro", "$count");
f93f848a 142 }
f93f848a 143 }
144 }
145 }
146
fa5a5b52 147 if ($generalforums) {
148 foreach ($generalforums as $forum) {
149 $count = count_records("forum_discussions", "forum", "$forum->id");
150
151 $forum->intro = forum_shorten_post($forum->intro);
152 replace_smilies($forum->intro);
36c23cd6 153 $forum->intro = "<span style=\"font-size:x-small;\">$forum->intro</span>";;
fa5a5b52 154
155 if ($forum->visible) {
156 $forumlink = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
157 } else {
158 $forumlink = "<a class=\"dimmed\" href=\"view.php?f=$forum->id\">$forum->name</a>";
159 }
160
161 if ($can_subscribe) {
162 if (forum_is_forcesubscribed($forum->id)) {
163 $sublink = get_string("yes");
164 } else {
c585ba8b 165 if (groupmode($course, $forum) and !isteacheredit($course->id) and !mygroupid($course->id)) {
a0b9571f 166 $sublink = get_string("no"); // Can't subscribe to a group forum (not in a group)
c585ba8b 167 $forumlink = $forum->name;
fa5a5b52 168 } else {
a0b9571f 169 if (forum_is_subscribed($USER->id, $forum->id)) {
170 $subscribed = get_string("yes");
171 $subtitle = get_string("unsubscribe", "forum");
172 } else {
173 $subscribed = get_string("no");
174 $subtitle = get_string("subscribe", "forum");
175 }
176 $sublink = "<a title=\"$subtitle\" href=\"subscribe.php?id=$forum->id\">$subscribed</a>";
fa5a5b52 177 }
fa5a5b52 178 }
179 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count", $sublink);
180 } else {
181 $generaltable->data[] = array ($forumlink, "$forum->intro", "$count");
182 }
183 }
184 }
185
186
187 /// Output the page
188
189 if ($course->category) {
190 print_header("$course->shortname: $strforums", "$course->fullname",
191 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> -> $strforums",
192 "", "", true, $searchform, navmenu($course));
193 } else {
194 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
195 "", "", true, $searchform, navmenu($course));
196 }
197
6031fe85 198 if ($generalforums) {
199 print_heading(get_string("generalforums", "forum"));
200 print_table($generaltable);
201 }
fa5a5b52 202
6031fe85 203 if ($learningforums) {
204 print_heading(get_string("learningforums", "forum"));
205 print_table($learningtable);
206 }
f93f848a 207
208 print_footer($course);
209
210?>