MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / forum / view.php
blobe417684d046d4f8f8f4854d7c6d3e9243c84ad0b
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 $navlinks = array();
68 $navlinks[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
69 $navlinks[] = array('name' => format_string($forum->name), 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
71 $navigation = build_navigation($navlinks);
73 print_header_simple(format_string($forum->name), "",
74 $navigation, "", "", true, $buttontext, navmenu($course, $cm));
77 /// Some capability checks.
78 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
79 notice(get_string("activityiscurrentlyhidden"));
82 if (!has_capability('mod/forum:viewdiscussion', $context)) {
83 notice(get_string('noviewdiscussionspermission', 'forum'));
86 /// find out current groups mode
87 groups_print_activity_menu($cm, 'view.php?id=' . $cm->id);
88 $currentgroup = groups_get_activity_group($cm);
89 $groupmode = groups_get_activity_groupmode($cm);
91 /// Okay, we can show the discussions. Log the forum view.
92 if ($cm->id) {
93 add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id);
94 } else {
95 add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id");
100 /// Print settings and things across the top
102 // If it's a simple single discussion forum, we need to print the display
103 // mode control.
104 if ($forum->type == 'single') {
105 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
106 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
107 $discussion = array_pop($discussions);
110 if ($discussion) {
111 if ($mode) {
112 set_user_preference("forum_displaymode", $mode);
114 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
115 forum_print_mode_form($forum->id, $displaymode, $forum->type);
120 print_box_start('forumcontrol');
122 print_box_start('subscription');
124 if (!empty($USER->id) && !has_capability('moodle/legacy:guest', $context, NULL, false)) {
125 $SESSION->fromdiscussion = "$FULLME";
126 if (forum_is_forcesubscribed($forum->id)) {
127 $streveryoneisnowsubscribed = get_string('everyoneisnowsubscribed', 'forum');
128 $strallowchoice = get_string('allowchoice', 'forum');
129 echo '<span class="helplink">' . get_string("forcessubscribe", 'forum') . '</span><br />';
130 helpbutton("subscription", $strallowchoice, "forum");
131 echo '&nbsp;<span class="helplink">';
132 if (has_capability('moodle/course:manageactivities', $context)) {
133 echo "<a title=\"$strallowchoice\" href=\"subscribe.php?id=$forum->id&amp;force=no\">$strallowchoice</a>";
134 } else {
135 echo $streveryoneisnowsubscribed;
137 echo '</span><br />';
139 } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
140 $strsubscriptionsoff = get_string('disallowsubscribe','forum');
141 echo $strsubscriptionsoff;
142 helpbutton("subscription", $strsubscriptionsoff, "forum");
143 } else {
144 $streveryonecannowchoose = get_string("everyonecannowchoose", "forum");
145 $strforcesubscribe = get_string("forcesubscribe", "forum");
146 $strshowsubscribers = get_string("showsubscribers", "forum");
147 echo '<span class="helplink">' . get_string("allowsallsubscribe", 'forum') . '</span><br />';
148 helpbutton("subscription", $strforcesubscribe, "forum");
149 echo '&nbsp;';
150 if (has_capability('moodle/course:manageactivities', $context)) {
151 echo "<span class=\"helplink\"><a title=\"$strforcesubscribe\" href=\"subscribe.php?id=$forum->id&amp;force=yes\">$strforcesubscribe</a></span>";
152 echo "<br />";
153 echo "<span class=\"helplink\"><a href=\"subscribers.php?id=$forum->id\">$strshowsubscribers</a></span>";
154 } else {
155 echo '<span class="helplink">'.$streveryonecannowchoose.'</span>';
158 echo '<div class="helplink" id="subscriptionlink">', forum_get_subscribe_link($forum, $context,
159 array('forcesubscribed' => '', 'cantsubscribe' => '')), '</div>';
162 if (($forum->trackingtype == FORUM_TRACKING_OPTIONAL) && forum_tp_can_track_forums($forum)) {
163 echo '<div class="helplink" id="trackinglink">'. forum_get_tracking_link($forum). '</div';
168 /// If rss are activated at site and forum level and this forum has rss defined, show link
169 if (isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
170 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds && $forum->rsstype and $forum->rssarticles) {
172 if ($forum->rsstype == 1) {
173 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
174 } else {
175 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
177 if (empty($USER->id)) {
178 $userid = 0;
179 } else {
180 $userid = $USER->id;
182 print_box_start('rsslink');
183 rss_print_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
184 print_box_end(); // subscription
187 print_box_end(); // subscription
189 print_box_end(); // forumcontrol
191 print_box('&nbsp;', 'clearer');
194 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
195 $a->blockafter = $forum->blockafter;
196 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
197 notify(get_string('thisforumisthrottled','forum',$a));
200 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
201 notify(get_string('qandanotify','forum'));
204 $forum->intro = trim($forum->intro);
206 switch ($forum->type) {
207 case 'single':
208 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
209 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
210 notify("Warning! There is more than one discussion in this forum - using the most recent");
211 $discussion = array_pop($discussions);
212 } else {
213 error("Could not find the discussion in this forum");
216 if (! $post = forum_get_post_full($discussion->firstpost)) {
217 error("Could not find the first post in this forum");
219 if ($mode) {
220 set_user_preference("forum_displaymode", $mode);
222 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
223 $canrate = has_capability('mod/forum:rate', $context);
224 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, NULL, $canrate);
225 break;
227 case 'eachuser':
228 if (!empty($forum->intro)) {
229 print_box(format_text($forum->intro), 'generalbox', 'intro');
231 echo '<p align="center">';
232 if (forum_user_can_post_discussion($forum)) {
233 print_string("allowsdiscussions", "forum");
234 } else {
235 echo '&nbsp;';
237 echo '</p>';
238 if (!empty($showall)) {
239 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
240 } else {
241 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
243 break;
245 case 'teacher':
246 if (!empty($showall)) {
247 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
248 } else {
249 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
251 break;
253 default:
254 if (!empty($forum->intro)) {
255 print_box(format_text($forum->intro), 'generalbox', 'intro');
257 echo '<br />';
258 if (!empty($showall)) {
259 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
260 } else {
261 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
265 break;
267 print_footer($course);