MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / forum / view.php
blobe08505bcddd4bc2166842e1c00156125bd08c47a
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>';
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 '<br /><span class="helplink" id="subscriptionlink">', forum_get_subscribe_link($forum, $context,
159 array('forcesubscribed' => '', 'cantsubscribe' => '')), '</span>';
162 if (forum_tp_can_track_forums($forum) && ($forum->trackingtype == FORUM_TRACKING_OPTIONAL)) {
163 if (forum_tp_is_tracked($forum, $USER->id)) {
164 $trtitle = get_string('notrackforum', 'forum');
165 $trackedlink = '<a title="'.get_string('notrackforum', 'forum').'" href="settracking.php?id='.
166 $forum->id.'&amp;returnpage=view.php">'.get_string('forumtracked', 'forum').'</a>';
167 } else {
168 $trtitle = get_string('trackforum', 'forum');
169 $trackedlink = '<a title="'.get_string('trackforum', 'forum').'" href="settracking.php?id='.
170 $forum->id.'&amp;returnpage=view.php">'.get_string('forumtrackednot', 'forum').'</a>';
172 echo '<br />';
173 echo "<span class=\"helplink\">$trackedlink</span>";
178 /// If rss are activated at site and forum level and this forum has rss defined, show link
179 if (isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
180 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds && $forum->rsstype and $forum->rssarticles) {
182 if ($forum->rsstype == 1) {
183 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
184 } else {
185 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
187 if (empty($USER->id)) {
188 $userid = 0;
189 } else {
190 $userid = $USER->id;
192 print_box_start('rsslink');
193 rss_print_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
194 print_box_end(); // subscription
197 print_box_end(); // subscription
199 print_box_end(); // forumcontrol
201 print_box('&nbsp;', 'clearer');
204 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
205 $a->blockafter = $forum->blockafter;
206 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
207 notify(get_string('thisforumisthrottled','forum',$a));
210 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
211 notify(get_string('qandanotify','forum'));
214 $forum->intro = trim($forum->intro);
216 switch ($forum->type) {
217 case 'single':
218 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
219 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
220 notify("Warning! There is more than one discussion in this forum - using the most recent");
221 $discussion = array_pop($discussions);
222 } else {
223 error("Could not find the discussion in this forum");
226 if (! $post = forum_get_post_full($discussion->firstpost)) {
227 error("Could not find the first post in this forum");
229 if ($mode) {
230 set_user_preference("forum_displaymode", $mode);
232 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
233 $canrate = has_capability('mod/forum:rate', $context);
234 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, NULL, $canrate);
235 break;
237 case 'eachuser':
238 if (!empty($forum->intro)) {
239 print_box(format_text($forum->intro), 'generalbox', 'intro');
241 echo '<p align="center">';
242 if (forum_user_can_post_discussion($forum)) {
243 print_string("allowsdiscussions", "forum");
244 } else {
245 echo '&nbsp;';
247 echo '</p>';
248 if (!empty($showall)) {
249 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
250 } else {
251 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
253 break;
255 case 'teacher':
256 if (!empty($showall)) {
257 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
258 } else {
259 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
261 break;
263 default:
264 if (!empty($forum->intro)) {
265 print_box(format_text($forum->intro), 'generalbox', 'intro');
267 echo '<br />';
268 if (!empty($showall)) {
269 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
270 } else {
271 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
275 break;
277 print_footer($course);