MDL-10234
[moodle-linuxchix.git] / mod / forum / view.php
blobc57f82b3c39843cd3bd0f35959e2dd82c1237f31
1 <?php // $Id$
3 require_once('../../config.php');
4 require_once('lib.php');
5 require_once("$CFG->libdir/rsslib.php");
8 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
9 $f = optional_param('f', 0, PARAM_INT); // Forum ID
10 $mode = optional_param('mode', 0, PARAM_INT); // Display mode (for single forum)
11 $showall = optional_param('showall', '', PARAM_INT); // show all discussions on one page
12 $changegroup = optional_param('group', -1, PARAM_INT); // choose the current group
13 $page = optional_param('page', 0, PARAM_INT); // which page to show
14 $search = optional_param('search', ''); // search string
18 if ($id) {
20 if (! $cm = get_coursemodule_from_id('forum', $id)) {
21 error("Course Module ID was incorrect");
23 if (! $course = get_record("course", "id", $cm->course)) {
24 error("Course is misconfigured");
26 if (! $forum = get_record("forum", "id", $cm->instance)) {
27 error("Forum ID was incorrect");
29 $strforums = get_string("modulenameplural", "forum");
30 $strforum = get_string("modulename", "forum");
31 $buttontext = update_module_button($cm->id, $course->id, $strforum);
33 } else if ($f) {
35 if (! $forum = get_record("forum", "id", $f)) {
36 error("Forum ID was incorrect or no longer exists");
38 if (! $course = get_record("course", "id", $forum->course)) {
39 error("Forum is misconfigured - don't know what course it's from");
42 $strforums = get_string("modulenameplural", "forum");
43 $strforum = get_string("modulename", "forum");
45 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
46 $buttontext = update_module_button($cm->id, $course->id, $strforum);
47 } else {
48 $cm->id = 0;
49 $cm->visible = 1;
50 $cm->course = $course->id;
51 $buttontext = "";
53 } else {
54 error('Must specify a course module or a forum ID');
57 if (!$buttontext) {
58 $buttontext = forum_search_form($course, $search);
62 require_course_login($course, true, $cm);
63 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
66 /// Print header.
67 $crumbs[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
68 $crumbs[] = array('name' => format_string($forum->name), 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
70 $navigation = build_navigation($crumbs);
72 print_header_simple(format_string($forum->name), "",
73 $navigation, "", "", true, $buttontext, navmenu($course, $cm));
76 /// Some capability checks.
77 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
78 notice(get_string("activityiscurrentlyhidden"));
81 if (!has_capability('mod/forum:viewdiscussion', $context)) {
82 notice(get_string('noviewdiscussionspermission', 'forum'));
85 $groupmode = groupmode($course, $cm);
86 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
88 if ($groupmode == SEPARATEGROUPS && ($currentgroup === false) &&
89 !has_capability('moodle/site:accessallgroups', $context)) {
90 notice(get_string('notingroup', 'forum'));
95 /// Okay, we can show the discussions. Log the forum view.
96 if ($cm->id) {
97 add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id);
98 } else {
99 add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id");
104 /// Print settings and things across the top
106 // If it's a simple single discussion forum, we need to print the display
107 // mode control.
108 if ($forum->type == 'single') {
109 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
110 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
111 $discussion = array_pop($discussions);
114 if ($discussion) {
115 if ($mode) {
116 set_user_preference("forum_displaymode", $mode);
118 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
119 forum_print_mode_form($forum->id, $displaymode, $forum->type);
124 print_box_start('forumcontrol');
126 /// 2 ways to do this, 1. we can changed the setup_and_print_groups functions
127 /// in moodlelib, taking in 1 more parameter, and tell the function when to
128 /// allow student menus, 2, we can just use this code to explicitly print this
129 /// menu for students in forums.
131 /// Now we need a menu for separategroups as well!
132 if ($groupmode == VISIBLEGROUPS || ($groupmode
133 && has_capability('moodle/site:accessallgroups', $context))) {
135 //the following query really needs to change
136 if ($groups = groups_get_groups_names($course->id)) { //TODO:
137 print_box_start('groupmenu');
138 print_group_menu($groups, $groupmode, $currentgroup, "$CFG->wwwroot/mod/forum/view.php?id=$cm->id");
139 print_box_end(); // groupmenu
143 /// Only print menus the student is in any course
144 else if ($groupmode == SEPARATEGROUPS){
145 $validgroups = array();
146 // Get all the groups this guy is in in this course
148 if ($p = user_group($course->id,$USER->id)){
149 /// Extract the name and id for the group
150 foreach ($p as $index => $object){
151 $validgroups[$object->id] = $object->name;
153 /// Print them in the menu
154 print_box_start('groupmenu');
155 print_group_menu($validgroups, $groupmode, $currentgroup, "view.php?id=$cm->id",0);
156 print_box_end(); // groupmenu
160 print_box_start('subscription');
162 if (!empty($USER->id) && !has_capability('moodle/legacy:guest', $context, NULL, false)) {
163 $SESSION->fromdiscussion = "$FULLME";
164 if (forum_is_forcesubscribed($forum->id)) {
165 $streveryoneisnowsubscribed = get_string('everyoneisnowsubscribed', 'forum');
166 $strallowchoice = get_string('allowchoice', 'forum');
167 echo '<span class="helplink">' . get_string("forcessubscribe", 'forum') . '</span><br />';
168 helpbutton("subscription", $strallowchoice, "forum");
169 echo '&nbsp;<span class="helplink">';
170 if (has_capability('moodle/course:manageactivities', $context)) {
171 echo "<a title=\"$strallowchoice\" href=\"subscribe.php?id=$forum->id&amp;force=no\">$strallowchoice</a>";
172 } else {
173 echo $streveryoneisnowsubscribed;
175 echo '</span>';
177 } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
178 $strsubscriptionsoff = get_string('disallowsubscribe','forum');
179 echo $strsubscriptionsoff;
180 helpbutton("subscription", $strsubscriptionsoff, "forum");
181 } else {
182 $streveryonecannowchoose = get_string("everyonecannowchoose", "forum");
183 $strforcesubscribe = get_string("forcesubscribe", "forum");
184 $strshowsubscribers = get_string("showsubscribers", "forum");
185 echo '<span class="helplink">' . get_string("allowsallsubscribe", 'forum') . '</span><br />';
186 helpbutton("subscription", $strforcesubscribe, "forum");
187 echo '&nbsp;';
188 if (has_capability('moodle/course:manageactivities', $context)) {
189 echo "<span class=\"helplink\"><a title=\"$strforcesubscribe\" href=\"subscribe.php?id=$forum->id&amp;force=yes\">$strforcesubscribe</a></span>";
190 echo "<br />";
191 echo "<span class=\"helplink\"><a href=\"subscribers.php?id=$forum->id\">$strshowsubscribers</a></span>";
192 } else {
193 echo '<span class="helplink">'.$streveryonecannowchoose.'</span>';
196 if (forum_is_subscribed($USER->id, $forum->id)) {
197 $subtexttitle = get_string("subscribestop", "forum");
198 $subtext = get_string("unsubscribe", "forum");
199 } else {
200 $subtexttitle = get_string("subscribestart", "forum");
201 $subtext = get_string("subscribe", "forum");
203 echo "<br />";
204 echo "<span class=\"helplink\"><a title=\"$subtexttitle\" href=\"subscribe.php?id=$forum->id\">$subtext</a></span>";
207 if (forum_tp_can_track_forums($forum) && ($forum->trackingtype == FORUM_TRACKING_OPTIONAL)) {
208 if (forum_tp_is_tracked($forum, $USER->id)) {
209 $trtitle = get_string('notrackforum', 'forum');
210 $trackedlink = '<a title="'.get_string('notrackforum', 'forum').'" href="settracking.php?id='.
211 $forum->id.'&amp;returnpage=view.php">'.get_string('forumtracked', 'forum').'</a>';
212 } else {
213 $trtitle = get_string('trackforum', 'forum');
214 $trackedlink = '<a title="'.get_string('trackforum', 'forum').'" href="settracking.php?id='.
215 $forum->id.'&amp;returnpage=view.php">'.get_string('forumtrackednot', 'forum').'</a>';
217 echo '<br />';
218 echo "<span class=\"helplink\">$trackedlink</span>";
223 /// If rss are activated at site and forum level and this forum has rss defined, show link
224 if (isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
225 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds && $forum->rsstype and $forum->rssarticles) {
227 if ($forum->rsstype == 1) {
228 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
229 } else {
230 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
232 if (empty($USER->id)) {
233 $userid = 0;
234 } else {
235 $userid = $USER->id;
237 print_box_start('rsslink');
238 rss_print_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
239 print_box_end(); // subscription
242 print_box_end(); // subscription
244 print_box_end(); // forumcontrol
246 print_box('&nbsp;', 'clearer');
249 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
250 $a->blockafter = $forum->blockafter;
251 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
252 notify(get_string('thisforumisthrottled','forum',$a));
255 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
256 notify(get_string('qandanotify','forum'));
259 $forum->intro = trim($forum->intro);
261 switch ($forum->type) {
262 case 'single':
263 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
264 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
265 notify("Warning! There is more than one discussion in this forum - using the most recent");
266 $discussion = array_pop($discussions);
267 } else {
268 error("Could not find the discussion in this forum");
271 if (! $post = forum_get_post_full($discussion->firstpost)) {
272 error("Could not find the first post in this forum");
274 if ($mode) {
275 set_user_preference("forum_displaymode", $mode);
277 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
278 $canrate = has_capability('mod/forum:rate', $context);
279 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, NULL, $canrate);
280 break;
282 case 'eachuser':
283 if (!empty($forum->intro)) {
284 print_box(format_text($forum->intro), 'generalbox', 'intro');
286 echo '<p align="center">';
287 if (forum_user_can_post_discussion($forum)) {
288 print_string("allowsdiscussions", "forum");
289 } else {
290 echo '&nbsp;';
292 echo '</p>';
293 if (!empty($showall)) {
294 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
295 } else {
296 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
298 break;
300 case 'teacher':
301 if (!empty($showall)) {
302 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
303 } else {
304 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
306 break;
308 default:
309 if (!empty($forum->intro)) {
310 print_box(format_text($forum->intro), 'generalbox', 'intro');
312 echo '<br />';
313 if (!empty($showall)) {
314 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
315 } else {
316 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
320 break;
322 print_footer($course);