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