MDL-50109 forum: Display blog posts in reverse chronological order
[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/**
01030f1b 19 * @package mod_forum
8f685009
SH
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
49a0ba94 103/// Print header.
95b320e5 104
8c6ca696 105 $PAGE->set_title($forum->name);
6606c00f 106 $PAGE->add_body_class('forumtype-'.$forum->type);
8c6ca696 107 $PAGE->set_heading($course->fullname);
6606c00f 108
83da3d28 109/// Some capability checks.
f0da6b85 110 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
3a4c2fd0 111 notice(get_string("activityiscurrentlyhidden"));
112 }
65bcf17b 113
b32c8dda 114 if (!has_capability('mod/forum:viewdiscussion', $context)) {
49a0ba94 115 notice(get_string('noviewdiscussionspermission', 'forum'));
116 }
65bcf17b 117
51c378b1
JL
118 // Mark viewed and trigger the course_module_viewed event.
119 forum_view($forum, $course, $cm, $context);
120
121 echo $OUTPUT->header();
122
66e2b9f8
AD
123 echo $OUTPUT->heading(format_string($forum->name), 2);
124 if (!empty($forum->intro) && $forum->type != 'single' && $forum->type != 'teacher') {
125 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
126 }
127
3b27b0fe 128/// find out current groups mode
f1035deb 129 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/forum/view.php?id=' . $cm->id);
9197e147 130
f0202ae9 131 $SESSION->fromdiscussion = qualified_me(); // Return here if we post or set subscription etc
49a0ba94 132
133
83da3d28 134/// Print settings and things across the top
135
136 // If it's a simple single discussion forum, we need to print the display
137 // mode control.
138 if ($forum->type == 'single') {
35b81f27
RT
139 $discussion = NULL;
140 $discussions = $DB->get_records('forum_discussions', array('forum'=>$forum->id), 'timemodified ASC');
141 if (!empty($discussions)) {
142 $discussion = array_pop($discussions);
83da3d28 143 }
144 if ($discussion) {
145 if ($mode) {
146 set_user_preference("forum_displaymode", $mode);
147 }
148 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
149 forum_print_mode_form($forum->id, $displaymode, $forum->type);
150 }
151 }
152
a4f495bf 153 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
4a8a7265 154 $a = new stdClass();
a4f495bf 155 $a->blockafter = $forum->blockafter;
156 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
4a8a7265 157 echo $OUTPUT->notification(get_string('thisforumisthrottled', 'forum', $a));
a4f495bf 158 }
159
0468976c 160 if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
9146b979 161 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 162 }
163
e07635f4 164 switch ($forum->type) {
9197e147 165 case 'single':
35b81f27
RT
166 if (!empty($discussions) && count($discussions) > 1) {
167 echo $OUTPUT->notification(get_string('warnformorepost', 'forum'));
501cdbd8 168 }
11b0c469 169 if (! $post = forum_get_post_full($discussion->firstpost)) {
dde78cb8 170 print_error('cannotfindfirstpost', 'forum');
501cdbd8 171 }
5476a93d 172 if ($mode) {
173 set_user_preference("forum_displaymode", $mode);
174 }
62b04e08 175
8b79a625 176 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $context);
177 $canrate = has_capability('mod/forum:rate', $context);
5476a93d 178 $displaymode = get_user_preferences("forum_displaymode", $CFG->forum_displaymode);
8b79a625 179
8675df6f 180 echo '&nbsp;'; // this should fix the floating in FF
62b04e08 181 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
501cdbd8 182 break;
183
9197e147 184 case 'eachuser':
a6e13ccc 185 echo '<p class="mdl-align">';
89d35c49 186 if (forum_user_can_post_discussion($forum, null, -1, $cm)) {
31d160d3 187 print_string("allowsdiscussions", "forum");
e07635f4 188 } else {
e746e4dd 189 echo '&nbsp;';
e07635f4 190 }
9197e147 191 echo '</p>';
c6ee38d0 192 if (!empty($showall)) {
90f4745c 193 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
c6ee38d0 194 } else {
90f4745c 195 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
c6ee38d0 196 }
197 break;
198
9197e147 199 case 'teacher':
3c8a606d 200 if (!empty($showall)) {
90f4745c 201 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
3c8a606d 202 } else {
90f4745c 203 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
3c8a606d 204 }
e07635f4 205 break;
206
1c7b8b93 207 case 'blog':
1c7b8b93
NC
208 echo '<br />';
209 if (!empty($showall)) {
8d032f59 210 forum_print_latest_discussions($course, $forum, 0, 'plain', 'p.created DESC', -1, -1, -1, 0, $cm);
1c7b8b93 211 } else {
8d032f59
DM
212 forum_print_latest_discussions($course, $forum, -1, 'plain', 'p.created DESC', -1, -1, $page,
213 $CFG->forum_manydiscussions, $cm);
1c7b8b93
NC
214 }
215 break;
216
e07635f4 217 default:
61242972 218 echo '<br />';
3c8a606d 219 if (!empty($showall)) {
90f4745c 220 forum_print_latest_discussions($course, $forum, 0, 'header', '', -1, -1, -1, 0, $cm);
3c8a606d 221 } else {
90f4745c 222 forum_print_latest_discussions($course, $forum, -1, 'header', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
3c8a606d 223 }
65bcf17b 224
225
e07635f4 226 break;
227 }
6553cda7 228
ebfb73db
AN
229 // Add the subscription toggle JS.
230 $PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
1adbd2c3 231
ebfb73db 232 echo $OUTPUT->footer($course);