MDL-66074 mod_forum: Make initial user id optional
[moodle.git] / mod / forum / view.php
CommitLineData
1adbd2c3 1<?php
8f685009
SH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
54d38a73
MG
18 * Displays the list of discussions in a forum.
19 *
01030f1b 20 * @package mod_forum
54d38a73 21 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
8f685009
SH
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
54d38a73
MG
25require_once('../../config.php');
26
27$managerfactory = mod_forum\local\container::get_manager_factory();
28$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
29$vaultfactory = mod_forum\local\container::get_vault_factory();
30$forumvault = $vaultfactory->get_forum_vault();
31$discussionvault = $vaultfactory->get_discussion_vault();
32$postvault = $vaultfactory->get_post_vault();
1a9c60e9 33$discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
54d38a73 34
bda290ee
RW
35$cmid = optional_param('id', 0, PARAM_INT);
36$forumid = optional_param('f', 0, PARAM_INT);
f30f46db
RW
37$mode = optional_param('mode', 0, PARAM_INT);
38$showall = optional_param('showall', '', PARAM_INT);
39$pageno = optional_param('page', 0, PARAM_INT);
40$search = optional_param('search', '', PARAM_CLEAN);
41$pageno = optional_param('p', $pageno, PARAM_INT);
54d38a73
MG
42$pagesize = optional_param('s', 0, PARAM_INT);
43$sortorder = optional_param('o', null, PARAM_INT);
54d38a73 44
bda290ee
RW
45if (!$cmid && !$forumid) {
46 print_error('missingparameter');
47}
48
1bfaeeb2
AN
49if ($cmid) {
50 $forum = $forumvault->get_from_course_module_id($cmid);
51 if (empty($forum)) {
52 throw new \moodle_exception('Unable to find forum with cmid ' . $cmid);
53 }
54} else {
55 $forum = $forumvault->get_from_id($forumid);
56 if (empty($forum)) {
57 throw new \moodle_exception('Unable to find forum with id ' . $forumid);
58 }
54d38a73
MG
59}
60
f30f46db
RW
61if (!empty($showall)) {
62 // The user wants to see all discussions.
63 $pageno = 0;
64 $pagesize = 0;
65}
66
54d38a73
MG
67$urlfactory = mod_forum\local\container::get_url_factory();
68$capabilitymanager = $managerfactory->get_capability_manager($forum);
69
bda290ee 70$url = $urlfactory->get_forum_view_url_from_forum($forum);
54d38a73
MG
71$PAGE->set_url($url);
72
73$course = $forum->get_course_record();
74$coursemodule = $forum->get_course_module_record();
75$cm = \cm_info::create($coursemodule);
76
77require_course_login($course, true, $cm);
78
15ecfac4
RW
79$istypesingle = 'single' === $forum->get_type();
80
81if ($mode) {
82 set_user_preference('forum_displaymode', $mode);
83}
84
85$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
86
54d38a73
MG
87$PAGE->set_context($forum->get_context());
88$PAGE->set_title($forum->get_name());
2b8e4f2e 89$PAGE->add_body_class('forumtype-' . $forum->get_type() . ' reset-style');
54d38a73 90$PAGE->set_heading($course->fullname);
f30f46db 91$PAGE->set_button(forum_search_form($course, $search));
c77d1ae4 92$PAGE->set_include_region_main_settings_in_header_actions(true);
54d38a73 93
15ecfac4
RW
94if ($istypesingle && $displaymode == FORUM_MODE_MODERN) {
95 $PAGE->add_body_class('modern-display-mode reset-style');
96}
97
bda290ee 98if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) {
54d38a73
MG
99 redirect(
100 $urlfactory->get_course_url_from_forum($forum),
101 get_string('activityiscurrentlyhidden'),
7aa6affa 102 null,
54d38a73
MG
103 \core\output\notification::NOTIFY_WARNING
104 );
105}
106
7aa6affa 107if (!$capabilitymanager->can_view_discussions($USER)) {
54d38a73
MG
108 redirect(
109 $urlfactory->get_course_url_from_forum($forum),
2ddbc651 110 get_string('noviewdiscussionspermission', 'forum'),
7aa6affa 111 null,
54d38a73
MG
112 \core\output\notification::NOTIFY_WARNING
113 );
114}
115
116// Mark viewed and trigger the course_module_viewed event.
117$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
e5a501de 118$forumrecord = $forumdatamapper->to_legacy_object($forum);
54d38a73 119forum_view(
e5a501de 120 $forumrecord,
54d38a73
MG
121 $forum->get_course_record(),
122 $forum->get_course_module_record(),
123 $forum->get_context()
124);
125
bda290ee
RW
126// Return here if we post or set subscription etc.
127$SESSION->fromdiscussion = qualified_me();
128
54d38a73
MG
129if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->get_rss_type() && $forum->get_rss_articles()) {
130 require_once("{$CFG->libdir}/rsslib.php");
131
132 $rsstitle = format_string($course->shortname, true, [
133 'context' => context_course::instance($course->id),
134 ]) . ': ' . format_string($forum->get_name());
e5a501de 135 rss_add_http_header($forum->get_context(), 'mod_forum', $forumrecord, $rsstitle);
54d38a73
MG
136}
137
138echo $OUTPUT->header();
139echo $OUTPUT->heading(format_string($forum->get_name()), 2);
140
15ecfac4 141if (!$istypesingle && !empty($forum->get_intro())) {
54d38a73
MG
142 echo $OUTPUT->box(format_module_intro('forum', $forumrecord, $cm->id), 'generalbox', 'intro');
143}
144
1a9c60e9
MG
145if ($sortorder) {
146 set_user_preference('forum_discussionlistsortorder', $sortorder);
147}
148
149$sortorder = get_user_preferences('forum_discussionlistsortorder', $discussionlistvault::SORTORDER_LASTPOST_DESC);
150
54d38a73
MG
151// Fetch the current groupid.
152$groupid = groups_get_activity_group($cm, true) ?: null;
153$rendererfactory = mod_forum\local\container::get_renderer_factory();
154switch ($forum->get_type()) {
155 case 'single':
156 $discussion = $discussionvault->get_last_discussion_in_forum($forum);
157 $discussioncount = $discussionvault->get_count_discussions_in_forum($forum);
1a9c60e9 158 $hasmultiplediscussions = $discussioncount > 1;
54d38a73
MG
159 $discussionsrenderer = $rendererfactory->get_single_discussion_list_renderer($forum, $discussion,
160 $hasmultiplediscussions, $displaymode);
161 $post = $postvault->get_from_id($discussion->get_first_post_id());
162 $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
bc4c7337
AN
163 $replies = $postvault->get_replies_to_post(
164 $USER,
165 $post,
166 $capabilitymanager->can_view_any_private_reply($USER),
167 $orderpostsby
168 );
54d38a73 169 echo $discussionsrenderer->render($USER, $post, $replies);
be1400c6
SR
170
171 if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) {
172 $postids = array_map(function($post) {
173 return $post->get_id();
174 }, array_merge([$post], array_values($replies)));
175 forum_tp_mark_posts_read($USER, $postids);
176 }
54d38a73
MG
177 break;
178 case 'blog':
179 $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum);
94552394 180 // Blog forums always show discussions newest first.
1a9c60e9
MG
181 echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC,
182 $pageno, $pagesize);
be1400c6
SR
183
184 if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) {
185 $discussions = mod_forum_get_discussion_summaries($forum, $USER, $groupid, null, $pageno, $pagesize);
186 $firstpostids = array_map(function($discussion) {
187 return $discussion->get_first_post()->get_id();
188 }, array_values($discussions));
189 forum_tp_mark_posts_read($USER, $firstpostids);
190 }
54d38a73
MG
191 break;
192 default:
193 $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum);
194 echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize);
195}
196
197echo $OUTPUT->footer();