MDL-60828 user: Reset current page when filtering/searching users
[moodle.git] / mod / forum / view.php
blobde2f2a83373deea4a64360a7671f1bc8521a1054
1 <?php
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.
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/>.
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
24 require_once('../../config.php');
25 require_once('lib.php');
26 require_once($CFG->libdir.'/completionlib.php');
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
34 $search = optional_param('search', '', PARAM_CLEAN);// search string
36 $params = array();
37 if ($id) {
38 $params['id'] = $id;
39 } else {
40 $params['f'] = $f;
42 if ($page) {
43 $params['page'] = $page;
45 if ($search) {
46 $params['search'] = $search;
48 $PAGE->set_url('/mod/forum/view.php', $params);
50 if ($id) {
51 if (! $cm = get_coursemodule_from_id('forum', $id)) {
52 print_error('invalidcoursemodule');
54 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
55 print_error('coursemisconf');
57 if (! $forum = $DB->get_record("forum", array("id" => $cm->instance))) {
58 print_error('invalidforumid', 'forum');
60 if ($forum->type == 'single') {
61 $PAGE->set_pagetype('mod-forum-discuss');
63 // move require_course_login here to use forced language for course
64 // fix for MDL-6926
65 require_course_login($course, true, $cm);
66 $strforums = get_string("modulenameplural", "forum");
67 $strforum = get_string("modulename", "forum");
68 } else if ($f) {
70 if (! $forum = $DB->get_record("forum", array("id" => $f))) {
71 print_error('invalidforumid', 'forum');
73 if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
74 print_error('coursemisconf');
77 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
78 print_error('missingparameter');
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");
85 } else {
86 print_error('missingparameter');
89 if (!$PAGE->button) {
90 $PAGE->set_button(forum_search_form($course, $search));
93 $context = context_module::instance($cm->id);
94 $PAGE->set_context($context);
96 if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->rsstype && $forum->rssarticles) {
97 require_once("$CFG->libdir/rsslib.php");
99 $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': ' . format_string($forum->name);
100 rss_add_http_header($context, 'mod_forum', $forum, $rsstitle);
103 /// Print header.
105 $PAGE->set_title($forum->name);
106 $PAGE->add_body_class('forumtype-'.$forum->type);
107 $PAGE->set_heading($course->fullname);
109 // Some capability checks.
110 $courselink = new moodle_url('/course/view.php', ['id' => $cm->course]);
112 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
113 notice(get_string("activityiscurrentlyhidden"), $courselink);
116 if (!has_capability('mod/forum:viewdiscussion', $context)) {
117 notice(get_string('noviewdiscussionspermission', 'forum'), $courselink);
120 // Mark viewed and trigger the course_module_viewed event.
121 forum_view($forum, $course, $cm, $context);
123 echo $OUTPUT->header();
125 echo $OUTPUT->heading(format_string($forum->name), 2);
126 if (!empty($forum->intro) && $forum->type != 'single' && $forum->type != 'teacher') {
127 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
130 /// find out current groups mode
131 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/forum/view.php?id=' . $cm->id);
133 $SESSION->fromdiscussion = qualified_me(); // Return here if we post or set subscription etc
136 /// Print settings and things across the top
138 // If it's a simple single discussion forum, we need to print the display
139 // mode control.
140 if ($forum->type == 'single') {
141 $discussion = NULL;
142 $discussions = $DB->get_records('forum_discussions', array('forum'=>$forum->id), 'timemodified ASC');
143 if (!empty($discussions)) {
144 $discussion = array_pop($discussions);
146 if ($discussion) {
147 if ($mode) {
148 set_user_preference("forum_displaymode", $mode);
150 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
151 forum_print_mode_form($forum->id, $displaymode, $forum->type);
155 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
156 $a = new stdClass();
157 $a->blockafter = $forum->blockafter;
158 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
159 echo $OUTPUT->notification(get_string('thisforumisthrottled', 'forum', $a));
162 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
163 echo $OUTPUT->notification(get_string('qandanotify','forum'));
166 switch ($forum->type) {
167 case 'single':
168 if (!empty($discussions) && count($discussions) > 1) {
169 echo $OUTPUT->notification(get_string('warnformorepost', 'forum'));
171 if (! $post = forum_get_post_full($discussion->firstpost)) {
172 print_error('cannotfindfirstpost', 'forum');
174 if ($mode) {
175 set_user_preference("forum_displaymode", $mode);
178 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $context);
179 $canrate = has_capability('mod/forum:rate', $context);
180 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
182 echo '&nbsp;'; // this should fix the floating in FF
183 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
184 break;
186 case 'eachuser':
187 echo '<p class="mdl-align">';
188 if (forum_user_can_post_discussion($forum, null, -1, $cm)) {
189 print_string("allowsdiscussions", "forum");
190 } else {
191 echo '&nbsp;';
193 echo '</p>';
194 if (!empty($showall)) {
195 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
196 } else {
197 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
199 break;
201 case 'teacher':
202 if (!empty($showall)) {
203 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
204 } else {
205 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
207 break;
209 case 'blog':
210 echo '<br />';
211 if (!empty($showall)) {
212 forum_print_latest_discussions($course, $forum, 0, 'plain', 'd.pinned DESC, p.created DESC', -1, -1, -1, 0, $cm);
213 } else {
214 forum_print_latest_discussions($course, $forum, -1, 'plain', 'd.pinned DESC, p.created DESC', -1, -1, $page,
215 $CFG->forum_manydiscussions, $cm);
217 break;
219 default:
220 echo '<br />';
221 if (!empty($showall)) {
222 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
223 } else {
224 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
228 break;
231 // Add the subscription toggle JS.
232 $PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
234 echo $OUTPUT->footer($course);