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