MDL-64820 forum: fix behat tests
[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();
33
bda290ee
RW
34$cmid = optional_param('id', 0, PARAM_INT);
35$forumid = optional_param('f', 0, PARAM_INT);
54d38a73
MG
36$pageno = optional_param('p', 0, PARAM_INT);
37$pagesize = optional_param('s', 0, PARAM_INT);
38$sortorder = optional_param('o', null, PARAM_INT);
39$mode = optional_param('mode', 0, PARAM_INT);
40
bda290ee
RW
41if (!$cmid && !$forumid) {
42 print_error('missingparameter');
43}
44
45$forum = $forumid ? $forumvault->get_from_id($forumid) : $forumvault->get_from_course_module_id($cmid);
54d38a73
MG
46if (!$forum) {
47 throw new \moodle_exception('Unable to find forum with id ' . $forumid);
48}
49
50$urlfactory = mod_forum\local\container::get_url_factory();
51$capabilitymanager = $managerfactory->get_capability_manager($forum);
52
bda290ee 53$url = $urlfactory->get_forum_view_url_from_forum($forum);
54d38a73
MG
54$PAGE->set_url($url);
55
56$course = $forum->get_course_record();
57$coursemodule = $forum->get_course_module_record();
58$cm = \cm_info::create($coursemodule);
59
60require_course_login($course, true, $cm);
61
62$PAGE->set_context($forum->get_context());
63$PAGE->set_title($forum->get_name());
64$PAGE->add_body_class('forumtype-' . $forum->get_type());
65$PAGE->set_heading($course->fullname);
66$PAGE->set_button(forum_search_form($course));
67
bda290ee 68if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) {
54d38a73
MG
69 redirect(
70 $urlfactory->get_course_url_from_forum($forum),
71 get_string('activityiscurrentlyhidden'),
72 \core\output\notification::NOTIFY_WARNING
73 );
74}
75
76if (!$capabilitymanager->can_view_discussions($USER, $forum)) {
77 redirect(
78 $urlfactory->get_course_url_from_forum($forum),
79 get_string('noviewdiscussionspermission', 'fourm'),
80 \core\output\notification::NOTIFY_WARNING
81 );
82}
83
84// Mark viewed and trigger the course_module_viewed event.
85$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
86forum_view(
87 $forumdatamapper->to_legacy_object($forum),
88 $forum->get_course_record(),
89 $forum->get_course_module_record(),
90 $forum->get_context()
91);
92
bda290ee
RW
93// Return here if we post or set subscription etc.
94$SESSION->fromdiscussion = qualified_me();
95
54d38a73
MG
96if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->get_rss_type() && $forum->get_rss_articles()) {
97 require_once("{$CFG->libdir}/rsslib.php");
98
99 $rsstitle = format_string($course->shortname, true, [
100 'context' => context_course::instance($course->id),
101 ]) . ': ' . format_string($forum->get_name());
102 rss_add_http_header($forum->get_context(), 'mod_forum', $forum, $rsstitle);
103}
104
105echo $OUTPUT->header();
106echo $OUTPUT->heading(format_string($forum->get_name()), 2);
107
108if ('single' !== $forum->get_type() && !empty($forum->get_intro())) {
109 $legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
110 $forumdbdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
111 $forumrecord = $forumdbdatamapper->to_legacy_object($forum);
112
113 echo $OUTPUT->box(format_module_intro('forum', $forumrecord, $cm->id), 'generalbox', 'intro');
114}
115
116if ($mode) {
117 set_user_preference('forum_displaymode', $mode);
118}
119
120$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
121
122// Fetch the current groupid.
123$groupid = groups_get_activity_group($cm, true) ?: null;
124$rendererfactory = mod_forum\local\container::get_renderer_factory();
125switch ($forum->get_type()) {
126 case 'single':
127 $discussion = $discussionvault->get_last_discussion_in_forum($forum);
128 $discussioncount = $discussionvault->get_count_discussions_in_forum($forum);
129 $hasmultiplediscussions = $discussioncount > 1 ? true : false;
130 $discussionsrenderer = $rendererfactory->get_single_discussion_list_renderer($forum, $discussion,
131 $hasmultiplediscussions, $displaymode);
132 $post = $postvault->get_from_id($discussion->get_first_post_id());
133 $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
134 $replies = $postvault->get_replies_to_post($post, $orderpostsby);
135 echo $discussionsrenderer->render($USER, $post, $replies);
136 break;
137 case 'blog':
138 $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum);
94552394
RW
139 $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
140 // Blog forums always show discussions newest first.
141 echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC, $pageno, $pagesize);
54d38a73
MG
142 break;
143 default:
144 $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum);
145 echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize);
146}
147
148echo $OUTPUT->footer();