MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / mod / forum / view.php
CommitLineData
41905731 1<?php // $Id$
f93f848a 2
49a0ba94 3 require_once('../../config.php');
4 require_once('lib.php');
48b29ba4 5 require_once("$CFG->libdir/rsslib.php");
f93f848a 6
49a0ba94 7
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
622365d2 14 $search = optional_param('search', '', PARAM_CLEAN);// search string
49a0ba94 15
fbaaeaeb 16 $params = array();
17 if ($id) {
18 $params['id'] = $id;
19 } else {
20 $params['f'] = $f;
21 }
22 if ($page) {
23 $params['page'] = $page;
24 }
25 if ($search) {
26 $params['search'] = $search;
27 }
28 $PAGE->set_url('mod/forum/view.php', $params);
49a0ba94 29
f93f848a 30 if ($id) {
f9d5371b 31 if (! $cm = get_coursemodule_from_id('forum', $id)) {
dde78cb8 32 print_error('invalidcoursemodule');
f93f848a 33 }
4e445355 34 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
dde78cb8 35 print_error('coursemisconf');
f93f848a 36 }
4e445355 37 if (! $forum = $DB->get_record("forum", array("id" => $cm->instance))) {
dde78cb8 38 print_error('invalidforumid', 'forum');
f93f848a 39 }
4cabf99f 40 // move require_course_login here to use forced language for course
41 // fix for MDL-6926
42 require_course_login($course, true, $cm);
59c71151 43 $strforums = get_string("modulenameplural", "forum");
44 $strforum = get_string("modulename", "forum");
f93f848a 45 } else if ($f) {
49a0ba94 46
4e445355 47 if (! $forum = $DB->get_record("forum", array("id" => $f))) {
dde78cb8 48 print_error('invalidforumid', 'forum');
f93f848a 49 }
4e445355 50 if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
dde78cb8 51 print_error('coursemisconf');
f93f848a 52 }
59c71151 53
65bcf17b 54 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
dde78cb8 55 print_error('missingparameter');
d9bf513d 56 }
4cabf99f 57 // move require_course_login here to use forced language for course
58 // fix for MDL-6926
59 require_course_login($course, true, $cm);
60 $strforums = get_string("modulenameplural", "forum");
61 $strforum = get_string("modulename", "forum");
f93f848a 62 } else {
dde78cb8 63 print_error('missingparameter');
f93f848a 64 }
65
385219cb 66 if (!$PAGE->button) {
67 $PAGE->set_button(forum_search_form($course, $search));
680afe2e 68 }
31d160d3 69
83da3d28 70 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
385219cb 71 $PAGE->set_context($context);
49a0ba94 72
73
74/// Print header.
385219cb 75 $PAGE->set_title(format_string($forum->name));
76 $PAGE->set_heading(format_string($course->fullname));
393f1615 77 echo $OUTPUT->header();
49a0ba94 78
83da3d28 79/// Some capability checks.
f0da6b85 80 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
3a4c2fd0 81 notice(get_string("activityiscurrentlyhidden"));
82 }
65bcf17b 83
b32c8dda 84 if (!has_capability('mod/forum:viewdiscussion', $context)) {
49a0ba94 85 notice(get_string('noviewdiscussionspermission', 'forum'));
86 }
65bcf17b 87
3b27b0fe 88/// find out current groups mode
13534ef7
ML
89 groups_print_activity_menu($cm, 'view.php?id=' . $cm->id);
90 $currentgroup = groups_get_activity_group($cm);
91 $groupmode = groups_get_activity_groupmode($cm);
9197e147 92
49a0ba94 93/// Okay, we can show the discussions. Log the forum view.
94 if ($cm->id) {
95 add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id);
96 } else {
97 add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id");
98 }
99
100
101
83da3d28 102/// Print settings and things across the top
103
104 // If it's a simple single discussion forum, we need to print the display
105 // mode control.
106 if ($forum->type == 'single') {
4e445355 107 if (! $discussion = $DB->get_record("forum_discussions", array("forum" => $forum->id))) {
108 if ($discussions = $DB->get_records("forum_discussions", array("forum", $forum->id), "timemodified ASC")) {
83da3d28 109 $discussion = array_pop($discussions);
110 }
111 }
112 if ($discussion) {
113 if ($mode) {
114 set_user_preference("forum_displaymode", $mode);
115 }
116 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
117 forum_print_mode_form($forum->id, $displaymode, $forum->type);
118 }
119 }
120
a4f495bf 121 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
122 $a->blockafter = $forum->blockafter;
123 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
9146b979 124 echo $OUTPUT->notification(get_string('thisforumisthrottled','forum',$a));
a4f495bf 125 }
126
0468976c 127 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
9146b979 128 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 129 }
130
e07635f4 131 switch ($forum->type) {
9197e147 132 case 'single':
4e445355 133 if (! $discussion = $DB->get_record("forum_discussions", array("forum" => $forum->id))) {
134 if ($discussions = $DB->get_records("forum_discussions", array("forum" => $forum->id), "timemodified ASC")) {
9146b979 135 echo $OUTPUT->notification("Warning! There is more than one discussion in this forum - using the most recent");
501cdbd8 136 $discussion = array_pop($discussions);
137 } else {
dde78cb8 138 print_error('nodiscussions', 'forum');
501cdbd8 139 }
140 }
11b0c469 141 if (! $post = forum_get_post_full($discussion->firstpost)) {
dde78cb8 142 print_error('cannotfindfirstpost', 'forum');
501cdbd8 143 }
5476a93d 144 if ($mode) {
145 set_user_preference("forum_displaymode", $mode);
146 }
62b04e08 147
8b79a625 148 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $context);
149 $canrate = has_capability('mod/forum:rate', $context);
5476a93d 150 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
8b79a625 151
8675df6f 152 echo '&nbsp;'; // this should fix the floating in FF
62b04e08 153 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
501cdbd8 154 break;
155
9197e147 156 case 'eachuser':
f064a3df 157 if (!empty($forum->intro)) {
698c90d0 158 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
c6ee38d0 159 }
a6e13ccc 160 echo '<p class="mdl-align">';
89d35c49 161 if (forum_user_can_post_discussion($forum, null, -1, $cm)) {
31d160d3 162 print_string("allowsdiscussions", "forum");
e07635f4 163 } else {
e746e4dd 164 echo '&nbsp;';
e07635f4 165 }
9197e147 166 echo '</p>';
c6ee38d0 167 if (!empty($showall)) {
90f4745c 168 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
c6ee38d0 169 } else {
90f4745c 170 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
c6ee38d0 171 }
172 break;
173
9197e147 174 case 'teacher':
3c8a606d 175 if (!empty($showall)) {
90f4745c 176 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
3c8a606d 177 } else {
90f4745c 178 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
3c8a606d 179 }
e07635f4 180 break;
181
1c7b8b93
NC
182 case 'blog':
183 if (!empty($forum->intro)) {
184 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
185 }
186 echo '<br />';
187 if (!empty($showall)) {
188 forum_print_latest_discussions($course, $forum, 0, 'plain', '', -1, -1, -1, 0, $cm);
189 } else {
190 forum_print_latest_discussions($course, $forum, -1, 'plain', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
191 }
192 break;
193
e07635f4 194 default:
f064a3df 195 if (!empty($forum->intro)) {
698c90d0 196 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
c6ee38d0 197 }
61242972 198 echo '<br />';
3c8a606d 199 if (!empty($showall)) {
90f4745c 200 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
3c8a606d 201 } else {
90f4745c 202 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
3c8a606d 203 }
65bcf17b 204
205
e07635f4 206 break;
207 }
4e781c7b 208 $completion=new completion_info($course);
209 $completion->set_module_viewed($cm);
385219cb 210 echo $OUTPUT->footer($course);
f93f848a 211
b32c8dda 212?>