Added message string for when a user does not have permissions to view forum discussi...
[moodle.git] / mod / forum / view.php
CommitLineData
41905731 1<?php // $Id$
f93f848a 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
48b29ba4 5 require_once("$CFG->libdir/rsslib.php");
f93f848a 6
c7e65290 7 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
8 $f = optional_param('f', 0, PARAM_INT); // Forum ID
9 $mode = optional_param('mode', 0, PARAM_INT); // Display mode (for single forum)
10 $showall = optional_param('showall', '', PARAM_INT); // show all discussions on one page
fac01360 11 $changegroup = optional_param('group', -1, PARAM_INT); // choose the current group
c7e65290 12 $page = optional_param('page', 0, PARAM_INT); // which page to show
13 $search = optional_param('search', ''); // search string
f93f848a 14
15 if ($id) {
f9d5371b 16 if (! $cm = get_coursemodule_from_id('forum', $id)) {
f93f848a 17 error("Course Module ID was incorrect");
18 }
19 if (! $course = get_record("course", "id", $cm->course)) {
20 error("Course is misconfigured");
21 }
22 if (! $forum = get_record("forum", "id", $cm->instance)) {
23 error("Forum ID was incorrect");
24 }
59c71151 25
26 $strforums = get_string("modulenameplural", "forum");
27 $strforum = get_string("modulename", "forum");
28
09aebc94 29 $buttontext = update_module_button($cm->id, $course->id, $strforum);
f93f848a 30
31 } else if ($f) {
32 if (! $forum = get_record("forum", "id", $f)) {
94361e02 33 error("Forum ID was incorrect or no longer exists");
f93f848a 34 }
35 if (! $course = get_record("course", "id", $forum->course)) {
36 error("Forum is misconfigured - don't know what course it's from");
37 }
59c71151 38
39 $strforums = get_string("modulenameplural", "forum");
40 $strforum = get_string("modulename", "forum");
41
d9bf513d 42 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
09aebc94 43 $buttontext = update_module_button($cm->id, $course->id, $strforum);
d9bf513d 44 } else {
67bbc9b7 45 $cm->id = 0;
56c0715e 46 $cm->visible = 1;
47 $cm->course = $course->id;
d9bf513d 48 $buttontext = "";
49 }
f93f848a 50
51 } else {
52 error("Must specify a course module or a forum ID");
53 }
54
97485d07 55 if (!$buttontext) {
6f1cc8d6 56 $buttontext = forum_search_form($course, $search);
680afe2e 57 }
31d160d3 58
ec81373f 59 require_course_login($course, true, $cm);
68ddf8bc 60
90fcc576 61 $navigation = "<a href=\"index.php?id=$course->id\">$strforums</a> ->";
f93f848a 62
82aa0e8d 63
f0da6b85 64/// Check whether the user should be able to view this forum.
bbbf2d40 65 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
66
0468976c 67 if (!has_capability('mod/forum:viewforum', $context)) {
bbbf2d40 68 error('You do not have the permission to view this forum');
69 }
70
67bbc9b7 71 if ($cm->id) {
69d79bc3 72 add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id);
e2c4f8e4 73 } else {
74 add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id");
75 }
f93f848a 76
3849dae8 77 print_header_simple(format_string($forum->name), "",
78 "$navigation ".format_string($forum->name), "", "", true, $buttontext, navmenu($course, $cm));
f93f848a 79
f0da6b85 80 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
3a4c2fd0 81 notice(get_string("activityiscurrentlyhidden"));
82 }
83
9197e147 84
45c38eef 85 $groupmode = groupmode($course, $cm);
fa22fd5f 86
2ab76925 87 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
fa22fd5f 88
bbbf2d40 89 if ($groupmode and ($currentgroup === false) and
0468976c 90 !has_capability('moodle/site:accessallgroups', $context)) {
bbbf2d40 91
2ab76925 92 print_heading(get_string("notingroup", "forum"));
93 print_footer($course);
9197e147 94 exit;
95 }
96
97
98/// Print settings and things in a table across the top
99
100 echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top">';
101
fa22fd5f 102 ///2 ways to do this, 1. we can changed the setup_and_print_groups functions
103 ///in moodlelib, taking in 1 more parameter, and tell the function when to
104 ///allow student menus, 2, we can just use this code to explicitly print this
105 ///menu for students in forums.
106
107 //now we need a menu for separategroups as well!
bbbf2d40 108 if ($groupmode == VISIBLEGROUPS or ($groupmode and
0468976c 109 has_capability('moodle/site:accessallgroups', $context))) {
bbbf2d40 110
fa22fd5f 111 //the following query really needs to change
9197e147 112 if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
113 echo '<td>';
6ff27f32 114 print_group_menu($groups, $groupmode, $currentgroup, "view.php?id=$cm->id");
9197e147 115 echo '</td>';
116 }
117 }
118
fa22fd5f 119 //only print menus the student is in any course
120 else if ($groupmode == SEPARATEGROUPS){
121 $validgroups = array();
122 //get all the groups this guy is in in this course
123
124 if ($p = user_group($course->id,$USER->id)){
125 //extract the name and id for the group
126 foreach ($p as $index => $object){
127 $validgroups[$object->id] = $object->name;
128 }
129 //print_r($validgroups);
130 echo '<td>';
131 //print them in the menu
132 print_group_menu($validgroups, $groupmode, $currentgroup, "view.php?id=$cm->id",0);
133 echo '</td>';
134 }
135 }
9197e147 136
f4646b44 137 if (!empty($USER->id)) {
41b47dd4 138 echo '<td align="right" class="subscription">';
501cdbd8 139 $SESSION->fromdiscussion = "$FULLME";
140 if (forum_is_forcesubscribed($forum->id)) {
41b47dd4 141 $streveryoneissubscribed = get_string('everyoneissubscribed', 'forum');
142 $strallowchoice = get_string('allowchoice', 'forum');
8a387206 143 helpbutton("subscription", $streveryoneissubscribed, "forum");
144 echo '&nbsp;<span class="helplink">';
0468976c 145 if (has_capability('moodle/course:manageactivities', $context)) {
839f2456 146 echo "<a title=\"$strallowchoice\" href=\"subscribe.php?id=$forum->id&amp;force=no\">$streveryoneissubscribed</a>";
501cdbd8 147 } else {
c032b59d 148 echo $streveryoneissubscribed;
501cdbd8 149 }
41b47dd4 150 echo '</span>';
501cdbd8 151
098d27d4 152 } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
153 $strsubscriptionsoff = get_string('disallowsubscribe','forum');
154 echo $strsubscriptionsoff;
155 helpbutton("subscription", $strsubscriptionsoff, "forum");
f93f848a 156 } else {
31d160d3 157 $streveryonecanchoose = get_string("everyonecanchoose", "forum");
158 $strforcesubscribe = get_string("forcesubscribe", "forum");
159 $strshowsubscribers = get_string("showsubscribers", "forum");
c032b59d 160
8a387206 161 helpbutton("subscription", $streveryonecanchoose, "forum");
162 echo '&nbsp;';
0468976c 163 if (has_capability('moodle/course:manageactivities', $context)) {
41b47dd4 164 echo "<span class=\"helplink\"><a title=\"$strforcesubscribe\" href=\"subscribe.php?id=$forum->id&amp;force=yes\">$streveryonecanchoose</a></span>";
165 echo "<br />";
166 echo "<span class=\"helplink\"><a href=\"subscribers.php?id=$forum->id\">$strshowsubscribers</a></span>";
310da166 167 } else {
41b47dd4 168 echo '<span class="helplink">'.$streveryonecanchoose.'</span>';
501cdbd8 169 }
c032b59d 170
501cdbd8 171 if (forum_is_subscribed($USER->id, $forum->id)) {
6687be17 172 $subtexttitle = get_string("subscribestop", "forum");
31d160d3 173 $subtext = get_string("unsubscribe", "forum");
501cdbd8 174 } else {
6687be17 175 $subtexttitle = get_string("subscribestart", "forum");
31d160d3 176 $subtext = get_string("subscribe", "forum");
501cdbd8 177 }
9197e147 178 echo "<br />";
41b47dd4 179 echo "<span class=\"helplink\"><a title=\"$subtexttitle\" href=\"subscribe.php?id=$forum->id\">$subtext</a></span>";
bffb106d 180 }
8adcb49f 181
eaf50aef 182 if (forum_tp_can_track_forums($forum) && ($forum->trackingtype == FORUM_TRACKING_OPTIONAL)) {
183 if (forum_tp_is_tracked($forum, $USER->id)) {
45293cd3 184 $trtitle = get_string('notrackforum', 'forum');
185 $trackedlink = '<a title="'.get_string('notrackforum', 'forum').'" href="settracking.php?id='.
186 $forum->id.'&returnpage=view.php">'.get_string('notrackforum', 'forum').'</a>';
187 } else {
188 $trtitle = get_string('trackforum', 'forum');
189 $trackedlink = '<a title="'.get_string('trackforum', 'forum').'" href="settracking.php?id='.
190 $forum->id.'&returnpage=view.php">'.get_string('trackforum', 'forum').'</a>';
191 }
192 echo "<br />";
193 echo "<span class=\"helplink\">$trackedlink</span>";
194 }
195
f4646b44 196 echo '</td>';
197 }
8adcb49f 198
f4646b44 199 //If rss are activated at site and forum level and this forum has rss defined, show link
200 if (isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
201 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds && $forum->rsstype and $forum->rssarticles) {
202 echo '</tr><tr><td align="right">';
203 if ($forum->rsstype == 1) {
3849dae8 204 $tooltiptext = get_string("rsssubscriberssdiscussions","forum",format_string($forum->name));
680afe2e 205 } else {
3849dae8 206 $tooltiptext = get_string("rsssubscriberssposts","forum",format_string($forum->name));
f4646b44 207 }
208 if (empty($USER->id)) {
209 $userid = 0;
210 } else {
211 $userid = $USER->id;
212 }
213 rss_print_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
9197e147 214 echo '</td>';
f93f848a 215 }
216
9197e147 217 echo '</tr></table>';
218
a4f495bf 219 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
220 $a->blockafter = $forum->blockafter;
221 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
222 notify(get_string('thisforumisthrottled','forum',$a));
223 }
224
0468976c 225 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
098d27d4 226 notify(get_string('qandanotify','forum'));
227 }
228
f064a3df 229 $forum->intro = trim($forum->intro);
e07635f4 230
231 switch ($forum->type) {
9197e147 232 case 'single':
501cdbd8 233 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
234 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
235 notify("Warning! There is more than one discussion in this forum - using the most recent");
236 $discussion = array_pop($discussions);
237 } else {
238 error("Could not find the discussion in this forum");
239 }
240 }
11b0c469 241 if (! $post = forum_get_post_full($discussion->firstpost)) {
501cdbd8 242 error("Could not find the first post in this forum");
243 }
5476a93d 244 if ($mode) {
245 set_user_preference("forum_displaymode", $mode);
246 }
247 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
0468976c 248 $canrate = has_capability('mod/forum:rate', $context);
bbbf2d40 249 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, NULL, $canrate);
501cdbd8 250 break;
251
9197e147 252 case 'eachuser':
f064a3df 253 if (!empty($forum->intro)) {
b846ac39 254 print_simple_box(format_text($forum->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
c6ee38d0 255 }
9197e147 256 echo '<p align="center">';
11b0c469 257 if (forum_user_can_post_discussion($forum)) {
31d160d3 258 print_string("allowsdiscussions", "forum");
e07635f4 259 } else {
e746e4dd 260 echo '&nbsp;';
e07635f4 261 }
9197e147 262 echo '</p>';
c6ee38d0 263 if (!empty($showall)) {
af328e4b 264 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
c6ee38d0 265 } else {
af328e4b 266 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
c6ee38d0 267 }
268 break;
269
9197e147 270 case 'teacher':
3c8a606d 271 if (!empty($showall)) {
af328e4b 272 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
3c8a606d 273 } else {
af328e4b 274 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
3c8a606d 275 }
e07635f4 276 break;
277
278 default:
f064a3df 279 if (!empty($forum->intro)) {
b846ac39 280 print_simple_box(format_text($forum->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
c6ee38d0 281 }
61242972 282 echo '<br />';
3c8a606d 283 if (!empty($showall)) {
af328e4b 284 forum_print_latest_discussions($course, $forum, 0, 'header', '', $currentgroup, $groupmode);
3c8a606d 285 } else {
af328e4b 286 forum_print_latest_discussions($course, $forum, $CFG->forum_manydiscussions, 'header', '', $currentgroup, $groupmode, $page);
3c8a606d 287 }
fa22fd5f 288
289
e07635f4 290 break;
291 }
f93f848a 292 print_footer($course);
293
6adc1e97 294?>