3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->libdir/rsslib.php");
7 $id = optional_param('id', 0, PARAM_INT
); // Course id
8 $subscribe = optional_param('subscribe', null, PARAM_INT
); // Subscribe/Unsubscribe all forums
11 if (! $course = get_record("course", "id", $id)) {
12 error("Course ID is incorrect");
15 if (! $course = get_site()) {
16 error("Could not find a top-level course!");
20 require_course_login($course);
21 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
24 unset($SESSION->fromdiscussion
);
26 add_to_log($course->id
, "forum", "view forums", "index.php?id=$course->id");
28 $strforums = get_string("forums", "forum");
29 $strforum = get_string("forum", "forum");
30 $strdescription = get_string("description");
31 $strdiscussions = get_string("discussions", "forum");
32 $strsubscribed = get_string("subscribed", "forum");
33 $strunreadposts = get_string("unreadposts", "forum");
34 $strtracking = get_string('tracking', 'forum');
35 $strmarkallread = get_string('markallread', 'forum');
36 $strtrackforum = get_string('trackforum', 'forum');
37 $strnotrackforum = get_string('notrackforum', 'forum');
38 $strsubscribe = get_string('subscribe', 'forum');
39 $strunsubscribe = get_string('unsubscribe', 'forum');
40 $stryes = get_string('yes');
41 $strno = get_string('no');
42 $strrss = get_string("rss");
43 $strweek = get_string('week');
44 $strsection = get_string('section');
46 $searchform = forum_search_form($course);
49 // Start of the table for General Forums
51 $generaltable->head
= array ($strforum, $strdescription, $strdiscussions);
52 $generaltable->align
= array ('left', 'left', 'center');
54 if ($usetracking = forum_tp_can_track_forums()) {
55 $untracked = forum_tp_get_untracked_forums($USER->id
, $course->id
);
57 $generaltable->head
[] = $strunreadposts;
58 $generaltable->align
[] = 'center';
60 $generaltable->head
[] = $strtracking;
61 $generaltable->align
[] = 'center';
64 if ($can_subscribe = has_capability('moodle/course:view', $coursecontext)) {
65 $generaltable->head
[] = $strsubscribed;
66 $generaltable->align
[] = 'center';
69 if ($show_rss = (($can_subscribe ||
$course->id
== SITEID
) &&
70 isset($CFG->enablerssfeeds
) && isset($CFG->forum_enablerssfeeds
) &&
71 $CFG->enablerssfeeds
&& $CFG->forum_enablerssfeeds
)) {
72 $generaltable->head
[] = $strrss;
73 $generaltable->align
[] = 'center';
77 // Parse and organise all the forums. Most forums are course modules but
78 // some special ones are not. These get placed in the general forums
79 // category with the forums in section 0.
81 $generalforums = array(); // For now
82 $learningforums = get_all_instances_in_course("forum", $course);
84 if ($forums = get_records("forum", "course", $id, "name ASC")) { // All known forums
86 if ($learningforums) { // Copy "full" data into this complete array
87 foreach ($learningforums as $key => $learningforum) {
88 $learningforum->keyreference
= $key;
89 $forums[$learningforum->id
] = $learningforum;
93 foreach ($forums as $forum) {
95 $cm = get_coursemodule_from_instance("forum", $forum->id
, $course->id
);
96 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
98 if (!has_capability('mod/forum:viewdiscussion', $context)) {
99 if (isset($forum->keyreference
)) {
100 unset($learningforums[$forum->keyreference
]);
104 if (!isset($forum->visible
)) {
105 $forum->visible
= instance_is_visible("forum", $forum);
106 if (!$forum->visible
and !has_capability('moodle/course:viewhiddenactivities', $context)) {
107 if (isset($forum->keyreference
)) {
108 unset($learningforums[$forum->keyreference
]);
113 switch ($forum->type
) {
116 $generalforums[] = $forum;
117 if (isset($forum->keyreference
)) { // Should always be
118 unset($learningforums[$forum->keyreference
]);
122 if (($course->id
== SITEID
) or empty($forum->section
)) { // Site level or section 0
123 $generalforums[] = $forum;
124 if (isset($forum->keyreference
)) {
125 unset($learningforums[$forum->keyreference
]);
133 /// Do course wide subscribe/unsubscribe
134 if (!is_null($subscribe) && !isguest()) {
135 $allforums = array_merge($generalforums, $learningforums);
137 foreach ($allforums as $forum) {
138 if (!forum_is_forcesubscribed($forum->id
)) {
139 $subscribed = forum_is_subscribed($USER->id
, $forum->id
);
140 if ($subscribe && !$subscribed) {
141 forum_subscribe($USER->id
, $forum->id
);
142 } elseif (!$subscribe && $subscribed) {
143 forum_unsubscribe($USER->id
, $forum->id
);
148 $returnto = forum_go_back_to("index.php?id=$course->id");
150 add_to_log($course->id
, "forum", "subscribeall", "index.php?id=$course->id", $course->id
);
151 redirect($returnto, get_string("nowallsubscribed", "forum", format_string($course->shortname
)), 1);
153 add_to_log($course->id
, "forum", "unsubscribeall", "index.php?id=$course->id", $course->id
);
154 redirect($returnto, get_string("nowallunsubscribed", "forum", format_string($course->shortname
)), 1);
158 /// First, let's process the general forums and build up a display
160 if ($generalforums) {
161 foreach ($generalforums as $forum) {
163 $cm = get_coursemodule_from_instance("forum", $forum->id
, $course->id
);
164 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
166 if (!groups_course_module_visible($cm)) {
169 if (isset($forum->groupmode
)) {
170 $groupmode = groups_get_activity_groupmode($cm);
172 $groupmode = NOGROUPS
;
174 $currentgroup = groups_get_activity_group($cm);
175 $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id
);
177 // this is potentially wrong logic. could possibly check for if user has the right to hmmm
178 if ($groupmode == SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $context)) {
179 $count = count_records_select("forum_discussions", "forum = '$forum->id' AND (groupid = '$currentgroup' OR groupid = '-1')");
181 $count = count_records("forum_discussions", "forum", "$forum->id");
185 if (($forum->trackingtype
== FORUM_TRACKING_ON
) ||
!isset($untracked[$forum->id
])) {
186 $groupid = ($groupmode==SEPARATEGROUPS
&& !has_capability('moodle/site:accessallgroups', $context)) ?
$currentgroup : false;
187 $unread = forum_tp_count_forum_unread_posts($USER->id
, $forum->id
, $groupid);
189 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id
.'">'.$unread.'</a>';
190 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
191 $forum->id
.'&mark=read"><img src="'.$CFG->pixpath
.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
193 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id
.'">'.$unread.'</a></span>';
197 if ($forum->trackingtype
== FORUM_TRACKING_OPTIONAL
) {
198 $trackedlink = print_single_button($CFG->wwwroot
. '/mod/forum/settracking.php?id=' . $forum->id
, '', $stryes, 'post', '_self', true, $strnotrackforum);
201 $trackedlink = $stryes;
205 $trackedlink = print_single_button($CFG->wwwroot
. '/mod/forum/settracking.php?id=' . $forum->id
, '', $strno, 'post', '_self', true, $strtrackforum);
209 $introoptions->para
=false;
210 $forum->intro
= shorten_text(trim(format_text($forum->intro
, FORMAT_HTML
, $introoptions)), $CFG->forum_shortpost
);
212 $forumname = format_string($forum->name
,true);;
213 if ($cantaccessagroup) {
214 $forumlink = $forumname;
215 $discussionlink = $count;
217 if ($forum->visible
) {
220 $style = 'class="dimmed"';
222 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name
,true)."</a>";
223 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
226 $row = array ($forumlink, $forum->intro
, $discussionlink);
228 $row[] = $unreadlink;
229 $row[] = $trackedlink; // Tracking.
232 if ($can_subscribe) {
233 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
234 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
235 'cantsubscribe' => '-'), $cantaccessagroup, false);
238 //If this forum has RSS activated, calculate it
239 if ($show_rss and $forum->rsstype
and $forum->rssarticles
) {
240 //Calculate the tolltip text
241 if ($forum->rsstype
== 1) {
242 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name
));
244 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name
));
246 if (empty($USER->id
)) {
251 //Get html code for RSS link
252 $row[] = rss_get_link($course->id
, $userid, "forum", $forum->id
, $tooltiptext);
255 $generaltable->data
[] = $row;
260 // Start of the table for Learning Forums
261 $learningtable->head
= array ($strforum, $strdescription, $strdiscussions);
262 $learningtable->align
= array ("left", "left", "center");
265 $learningtable->head
[] = $strunreadposts;
266 $learningtable->align
[] = 'center';
268 $learningtable->head
[] = $strtracking;
269 $learningtable->align
[] = 'center';
272 if ($can_subscribe) {
273 $learningtable->head
[] = $strsubscribed;
274 $learningtable->align
[] = 'center';
277 if ($show_rss = (($can_subscribe ||
$course->id
== SITEID
) &&
278 isset($CFG->enablerssfeeds
) && isset($CFG->forum_enablerssfeeds
) &&
279 $CFG->enablerssfeeds
&& $CFG->forum_enablerssfeeds
)) {
280 $learningtable->head
[] = $strrss;
281 $learningtable->align
[] = 'center';
284 /// Now let's process the learning forums
286 if ($course->id
!= SITEID
) { // Only real courses have learning forums
287 // Add extra field for section number, at the front
288 if ($course->format
== 'weeks' or $course->format
== 'weekscss') {
289 array_unshift($learningtable->head
, $strweek);
291 array_unshift($learningtable->head
, $strsection);
293 array_unshift($learningtable->align
, "center");
296 if ($learningforums) {
297 $currentsection = "";
298 foreach ($learningforums as $key => $forum) {
299 $forum->visible
= instance_is_visible("forum", $forum)
300 ||
has_capability('moodle/course:view', $coursecontext);
302 $cm = get_coursemodule_from_instance("forum", $forum->id
, $course->id
);
303 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
305 if (!groups_course_module_visible($cm)) {
308 $currentgroup = groups_get_activity_group($cm);
309 $groupmode = groups_get_activity_groupmode($cm);
312 $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id
);
314 if ($groupmode == SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $context)) {
315 $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup);
317 $count = count_records("forum_discussions", "forum", "$forum->id");
321 if (($forum->trackingtype
== FORUM_TRACKING_ON
) ||
322 !isset($untracked[$forum->id
])) {
323 $groupid = ($groupmode==SEPARATEGROUPS
324 && !has_capability('moodle/site:accessallgroups', $context))
325 ?
$currentgroup : false;
326 $unread = forum_tp_count_forum_unread_posts($USER->id
, $forum->id
, $groupid);
328 $unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id
.'">'.$unread.'</a>';
329 $unreadlink .= '<a title="'.$strmarkallread.'" href="markposts.php?f='.
330 $forum->id
.'&mark=read"><img src="'.$CFG->pixpath
.'/t/clear.gif" alt="'.$strmarkallread.'" /></a></span>';
332 $unreadlink = '<span class="read"><a href="view.php?f='.$forum->id
.'">'.$unread.'</a></span>';
334 if ($forum->trackingtype
== FORUM_TRACKING_OPTIONAL
) {
335 $trackedlink = print_single_button($CFG->wwwroot
. '/mod/forum/settracking.php?id=' . $forum->id
, '', $stryes, 'post', '_self', true, $strnotrackforum);
339 $trackedlink = print_single_button($CFG->wwwroot
. '/mod/forum/settracking.php?id=' . $forum->id
, '', $strno, 'post', '_self', true, $strtrackforum);
343 $introoptions->para
=false;
344 $forum->intro
= shorten_text(trim(format_text($forum->intro
, FORMAT_HTML
, $introoptions)), $CFG->forum_shortpost
);
346 if ($forum->section
!= $currentsection) {
347 $printsection = $forum->section
;
348 if ($currentsection) {
349 $learningtable->data
[] = 'hr';
351 $currentsection = $forum->section
;
356 $forumname = format_string($forum->name
,true);;
357 if ($cantaccessagroup && $groupmode == SEPARATEGROUPS
) {
358 $forumlink = $forumname;
359 $discussionlink = $count;
361 if ($forum->visible
) {
364 $style = 'class="dimmed"';
366 $forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name
,true)."</a>";
367 $discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
370 $row = array ($printsection, $forumlink, $forum->intro
, $discussionlink);
372 $row[] = $unreadlink;
373 $row[] = $trackedlink; // Tracking.
376 if ($can_subscribe) {
377 $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
378 'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
379 'cantsubscribe' => '-'), $cantaccessagroup, false);
382 //If this forum has RSS activated, calculate it
383 if ($show_rss and $forum->rsstype
and $forum->rssarticles
) {
384 //Calculate the tolltip text
385 if ($forum->rsstype
== 1) {
386 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name
));
388 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name
));
390 if (empty($USER->id
)) {
395 //Get html code for RSS link
396 $row[] = rss_get_link($course->id
, $userid, "forum", $forum->id
, $tooltiptext);
399 $learningtable->data
[] = $row;
407 $navlinks[] = array('name' => $strforums, 'link' => '', 'type' => 'activity');
409 print_header("$course->shortname: $strforums", $course->fullname
,
410 build_navigation($navlinks),
411 "", "", true, $searchform, navmenu($course));
414 print_box_start('subscription');
415 echo '<span class="helplink">';
416 echo '<a href="index.php?id='.$course->id
.'&subscribe=1">'.get_string('allsubscribe', 'forum').'</a>';
417 echo '</span><br /><span class="helplink">';
418 echo '<a href="index.php?id='.$course->id
.'&subscribe=0">'.get_string('allunsubscribe', 'forum').'</a>';
421 print_box(' ', 'clearer');
424 if ($generalforums) {
425 print_heading(get_string("generalforums", "forum"));
426 print_table($generaltable);
429 if ($learningforums) {
430 print_heading(get_string("learningforums", "forum"));
431 print_table($learningtable);
434 print_footer($course);