MDL-40733 forum: fix duplciate headings
[moodle.git] / mod / forum / view.php
CommitLineData
1adbd2c3 1<?php
f93f848a 2
8f685009
SH
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package mod-forum
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
49a0ba94 24 require_once('../../config.php');
25 require_once('lib.php');
516c5eca 26 require_once($CFG->libdir.'/completionlib.php');
49a0ba94 27
28 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
29 $f = optional_param('f', 0, PARAM_INT); // Forum ID
30 $mode = optional_param('mode', 0, PARAM_INT); // Display mode (for single forum)
31 $showall = optional_param('showall', '', PARAM_INT); // show all discussions on one page
32 $changegroup = optional_param('group', -1, PARAM_INT); // choose the current group
33 $page = optional_param('page', 0, PARAM_INT); // which page to show
622365d2 34 $search = optional_param('search', '', PARAM_CLEAN);// search string
49a0ba94 35
fbaaeaeb 36 $params = array();
37 if ($id) {
38 $params['id'] = $id;
39 } else {
40 $params['f'] = $f;
41 }
42 if ($page) {
43 $params['page'] = $page;
44 }
45 if ($search) {
46 $params['search'] = $search;
47 }
a6855934 48 $PAGE->set_url('/mod/forum/view.php', $params);
49a0ba94 49
f93f848a 50 if ($id) {
f9d5371b 51 if (! $cm = get_coursemodule_from_id('forum', $id)) {
dde78cb8 52 print_error('invalidcoursemodule');
f93f848a 53 }
4e445355 54 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
dde78cb8 55 print_error('coursemisconf');
f93f848a 56 }
4e445355 57 if (! $forum = $DB->get_record("forum", array("id" => $cm->instance))) {
dde78cb8 58 print_error('invalidforumid', 'forum');
f93f848a 59 }
b1627a92
DC
60 if ($forum->type == 'single') {
61 $PAGE->set_pagetype('mod-forum-discuss');
62 }
4cabf99f 63 // move require_course_login here to use forced language for course
64 // fix for MDL-6926
65 require_course_login($course, true, $cm);
59c71151 66 $strforums = get_string("modulenameplural", "forum");
67 $strforum = get_string("modulename", "forum");
f93f848a 68 } else if ($f) {
49a0ba94 69
4e445355 70 if (! $forum = $DB->get_record("forum", array("id" => $f))) {
dde78cb8 71 print_error('invalidforumid', 'forum');
f93f848a 72 }
4e445355 73 if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
dde78cb8 74 print_error('coursemisconf');
f93f848a 75 }
59c71151 76
65bcf17b 77 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
dde78cb8 78 print_error('missingparameter');
d9bf513d 79 }
4cabf99f 80 // move require_course_login here to use forced language for course
81 // fix for MDL-6926
82 require_course_login($course, true, $cm);
83 $strforums = get_string("modulenameplural", "forum");
84 $strforum = get_string("modulename", "forum");
f93f848a 85 } else {
dde78cb8 86 print_error('missingparameter');
f93f848a 87 }
88
385219cb 89 if (!$PAGE->button) {
90 $PAGE->set_button(forum_search_form($course, $search));
680afe2e 91 }
31d160d3 92
bf0f06b1 93 $context = context_module::instance($cm->id);
385219cb 94 $PAGE->set_context($context);
49a0ba94 95
9e86f2e7
AD
96 if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->rsstype && $forum->rssarticles) {
97 require_once("$CFG->libdir/rsslib.php");
98
7a241d81 99 $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': ' . format_string($forum->name);
43b92251 100 rss_add_http_header($context, 'mod_forum', $forum, $rsstitle);
9e86f2e7
AD
101 }
102
6553cda7 103 // Mark viewed if required
104 $completion = new completion_info($course);
105 $completion->set_module_viewed($cm);
106
49a0ba94 107/// Print header.
95b320e5 108
385219cb 109 $PAGE->set_title(format_string($forum->name));
6606c00f 110 $PAGE->add_body_class('forumtype-'.$forum->type);
385219cb 111 $PAGE->set_heading(format_string($course->fullname));
6606c00f 112
393f1615 113 echo $OUTPUT->header();
49a0ba94 114
83da3d28 115/// Some capability checks.
f0da6b85 116 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
3a4c2fd0 117 notice(get_string("activityiscurrentlyhidden"));
118 }
65bcf17b 119
b32c8dda 120 if (!has_capability('mod/forum:viewdiscussion', $context)) {
49a0ba94 121 notice(get_string('noviewdiscussionspermission', 'forum'));
122 }
65bcf17b 123
66e2b9f8
AD
124 echo $OUTPUT->heading(format_string($forum->name), 2);
125 if (!empty($forum->intro) && $forum->type != 'single' && $forum->type != 'teacher') {
126 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
127 }
128
3b27b0fe 129/// find out current groups mode
f1035deb 130 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/forum/view.php?id=' . $cm->id);
9197e147 131
49a0ba94 132/// Okay, we can show the discussions. Log the forum view.
133 if ($cm->id) {
134 add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id);
135 } else {
136 add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id");
137 }
138
f0202ae9 139 $SESSION->fromdiscussion = qualified_me(); // Return here if we post or set subscription etc
49a0ba94 140
141
83da3d28 142/// Print settings and things across the top
143
144 // If it's a simple single discussion forum, we need to print the display
145 // mode control.
146 if ($forum->type == 'single') {
35b81f27
RT
147 $discussion = NULL;
148 $discussions = $DB->get_records('forum_discussions', array('forum'=>$forum->id), 'timemodified ASC');
149 if (!empty($discussions)) {
150 $discussion = array_pop($discussions);
83da3d28 151 }
152 if ($discussion) {
153 if ($mode) {
154 set_user_preference("forum_displaymode", $mode);
155 }
156 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
157 forum_print_mode_form($forum->id, $displaymode, $forum->type);
158 }
159 }
160
a4f495bf 161 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
4a8a7265 162 $a = new stdClass();
a4f495bf 163 $a->blockafter = $forum->blockafter;
164 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
4a8a7265 165 echo $OUTPUT->notification(get_string('thisforumisthrottled', 'forum', $a));
a4f495bf 166 }
167
0468976c 168 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
9146b979 169 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 170 }
171
e07635f4 172 switch ($forum->type) {
9197e147 173 case 'single':
35b81f27
RT
174 if (!empty($discussions) && count($discussions) > 1) {
175 echo $OUTPUT->notification(get_string('warnformorepost', 'forum'));
501cdbd8 176 }
11b0c469 177 if (! $post = forum_get_post_full($discussion->firstpost)) {
dde78cb8 178 print_error('cannotfindfirstpost', 'forum');
501cdbd8 179 }
5476a93d 180 if ($mode) {
181 set_user_preference("forum_displaymode", $mode);
182 }
62b04e08 183
8b79a625 184 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $context);
185 $canrate = has_capability('mod/forum:rate', $context);
5476a93d 186 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
8b79a625 187
8675df6f 188 echo '&nbsp;'; // this should fix the floating in FF
62b04e08 189 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
501cdbd8 190 break;
191
9197e147 192 case 'eachuser':
a6e13ccc 193 echo '<p class="mdl-align">';
89d35c49 194 if (forum_user_can_post_discussion($forum, null, -1, $cm)) {
31d160d3 195 print_string("allowsdiscussions", "forum");
e07635f4 196 } else {
e746e4dd 197 echo '&nbsp;';
e07635f4 198 }
9197e147 199 echo '</p>';
c6ee38d0 200 if (!empty($showall)) {
90f4745c 201 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
c6ee38d0 202 } else {
90f4745c 203 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
c6ee38d0 204 }
205 break;
206
9197e147 207 case 'teacher':
3c8a606d 208 if (!empty($showall)) {
90f4745c 209 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
3c8a606d 210 } else {
90f4745c 211 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
3c8a606d 212 }
e07635f4 213 break;
214
1c7b8b93 215 case 'blog':
1c7b8b93
NC
216 echo '<br />';
217 if (!empty($showall)) {
218 forum_print_latest_discussions($course, $forum, 0, 'plain', '', -1, -1, -1, 0, $cm);
219 } else {
220 forum_print_latest_discussions($course, $forum, -1, 'plain', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
221 }
222 break;
223
e07635f4 224 default:
61242972 225 echo '<br />';
3c8a606d 226 if (!empty($showall)) {
90f4745c 227 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
3c8a606d 228 } else {
90f4745c 229 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
3c8a606d 230 }
65bcf17b 231
232
e07635f4 233 break;
234 }
6553cda7 235
385219cb 236 echo $OUTPUT->footer($course);
f93f848a 237
1adbd2c3 238