MDL-64820 forum: fix up issues from review
[moodle.git] / mod / forum / view.php
1 <?php
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/>.
17 /**
18  * Displays the list of discussions in a forum.
19  *
20  * @package   mod_forum
21  * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 require_once('../../config.php');
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();
34 $cmid = optional_param('id', 0, PARAM_INT);
35 $forumid = optional_param('f', 0, PARAM_INT);
36 $mode = optional_param('mode', 0, PARAM_INT);
37 $showall = optional_param('showall', '', PARAM_INT);
38 $pageno = optional_param('page', 0, PARAM_INT);
39 $search = optional_param('search', '', PARAM_CLEAN);
40 $pageno = optional_param('p', $pageno, PARAM_INT);
41 $pagesize = optional_param('s', 0, PARAM_INT);
42 $sortorder = optional_param('o', null, PARAM_INT);
44 if (!$cmid && !$forumid) {
45     print_error('missingparameter');
46 }
48 $forum = $forumid ? $forumvault->get_from_id($forumid) : $forumvault->get_from_course_module_id($cmid);
49 if (!$forum) {
50     throw new \moodle_exception('Unable to find forum with id ' . $forumid);
51 }
53 if (!empty($showall)) {
54     // The user wants to see all discussions.
55     $pageno = 0;
56     $pagesize = 0;
57 }
59 $urlfactory = mod_forum\local\container::get_url_factory();
60 $capabilitymanager = $managerfactory->get_capability_manager($forum);
62 $url = $urlfactory->get_forum_view_url_from_forum($forum);
63 $PAGE->set_url($url);
65 $course = $forum->get_course_record();
66 $coursemodule = $forum->get_course_module_record();
67 $cm = \cm_info::create($coursemodule);
69 require_course_login($course, true, $cm);
71 $PAGE->set_context($forum->get_context());
72 $PAGE->set_title($forum->get_name());
73 $PAGE->add_body_class('forumtype-' . $forum->get_type());
74 $PAGE->set_heading($course->fullname);
75 $PAGE->set_button(forum_search_form($course, $search));
77 if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) {
78     redirect(
79         $urlfactory->get_course_url_from_forum($forum),
80         get_string('activityiscurrentlyhidden'),
81         \core\output\notification::NOTIFY_WARNING
82     );
83 }
85 if (!$capabilitymanager->can_view_discussions($USER, $forum)) {
86     redirect(
87         $urlfactory->get_course_url_from_forum($forum),
88         get_string('noviewdiscussionspermission', 'fourm'),
89         \core\output\notification::NOTIFY_WARNING
90     );
91 }
93 // Mark viewed and trigger the course_module_viewed event.
94 $forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
95 forum_view(
96     $forumdatamapper->to_legacy_object($forum),
97     $forum->get_course_record(),
98     $forum->get_course_module_record(),
99     $forum->get_context()
100 );
102 // Return here if we post or set subscription etc.
103 $SESSION->fromdiscussion = qualified_me();
105 if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->get_rss_type() && $forum->get_rss_articles()) {
106     require_once("{$CFG->libdir}/rsslib.php");
108     $rsstitle = format_string($course->shortname, true, [
109             'context' => context_course::instance($course->id),
110         ]) . ': ' . format_string($forum->get_name());
111     rss_add_http_header($forum->get_context(), 'mod_forum', $forum, $rsstitle);
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading(format_string($forum->get_name()), 2);
117 if ('single' !== $forum->get_type() && !empty($forum->get_intro())) {
118     $legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
119     $forumdbdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
120     $forumrecord = $forumdbdatamapper->to_legacy_object($forum);
122     echo $OUTPUT->box(format_module_intro('forum', $forumrecord, $cm->id), 'generalbox', 'intro');
125 if ($mode) {
126     set_user_preference('forum_displaymode', $mode);
129 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
131 // Fetch the current groupid.
132 $groupid = groups_get_activity_group($cm, true) ?: null;
133 $rendererfactory = mod_forum\local\container::get_renderer_factory();
134 switch ($forum->get_type()) {
135     case 'single':
136         $discussion = $discussionvault->get_last_discussion_in_forum($forum);
137         $discussioncount = $discussionvault->get_count_discussions_in_forum($forum);
138         $hasmultiplediscussions = $discussioncount > 1 ? true : false;
139         $discussionsrenderer = $rendererfactory->get_single_discussion_list_renderer($forum, $discussion,
140             $hasmultiplediscussions, $displaymode);
141         $post = $postvault->get_from_id($discussion->get_first_post_id());
142         $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
143         $replies = $postvault->get_replies_to_post($post, $orderpostsby);
144         echo $discussionsrenderer->render($USER, $post, $replies);
145         break;
146     case 'blog':
147         $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum);
148         $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
149         // Blog forums always show discussions newest first.
150         echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC, $pageno, $pagesize);
151         break;
152     default:
153         $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum);
154         echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize);
157 echo $OUTPUT->footer();