MDL-66893 mod_forum: update navigation bar in grader UI
[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
1d709d76
MM
25use mod_forum\grades\forum_gradeitem;
26
54d38a73
MG
27require_once('../../config.php');
28
29$managerfactory = mod_forum\local\container::get_manager_factory();
30$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
31$vaultfactory = mod_forum\local\container::get_vault_factory();
32$forumvault = $vaultfactory->get_forum_vault();
33$discussionvault = $vaultfactory->get_discussion_vault();
34$postvault = $vaultfactory->get_post_vault();
1a9c60e9 35$discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
54d38a73 36
bda290ee
RW
37$cmid = optional_param('id', 0, PARAM_INT);
38$forumid = optional_param('f', 0, PARAM_INT);
f30f46db
RW
39$mode = optional_param('mode', 0, PARAM_INT);
40$showall = optional_param('showall', '', PARAM_INT);
41$pageno = optional_param('page', 0, PARAM_INT);
42$search = optional_param('search', '', PARAM_CLEAN);
43$pageno = optional_param('p', $pageno, PARAM_INT);
54d38a73
MG
44$pagesize = optional_param('s', 0, PARAM_INT);
45$sortorder = optional_param('o', null, PARAM_INT);
54d38a73 46
bda290ee
RW
47if (!$cmid && !$forumid) {
48 print_error('missingparameter');
49}
50
1bfaeeb2
AN
51if ($cmid) {
52 $forum = $forumvault->get_from_course_module_id($cmid);
53 if (empty($forum)) {
54 throw new \moodle_exception('Unable to find forum with cmid ' . $cmid);
55 }
56} else {
57 $forum = $forumvault->get_from_id($forumid);
58 if (empty($forum)) {
59 throw new \moodle_exception('Unable to find forum with id ' . $forumid);
60 }
54d38a73
MG
61}
62
f30f46db
RW
63if (!empty($showall)) {
64 // The user wants to see all discussions.
65 $pageno = 0;
66 $pagesize = 0;
67}
68
54d38a73
MG
69$urlfactory = mod_forum\local\container::get_url_factory();
70$capabilitymanager = $managerfactory->get_capability_manager($forum);
71
bda290ee 72$url = $urlfactory->get_forum_view_url_from_forum($forum);
54d38a73
MG
73$PAGE->set_url($url);
74
75$course = $forum->get_course_record();
76$coursemodule = $forum->get_course_module_record();
77$cm = \cm_info::create($coursemodule);
78
79require_course_login($course, true, $cm);
80
667e5fd9
RW
81$istypesingle = $forum->get_type() === 'single';
82$saveddisplaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
15ecfac4
RW
83
84if ($mode) {
667e5fd9
RW
85 $displaymode = $mode;
86} else {
87 $displaymode = $saveddisplaymode;
15ecfac4
RW
88}
89
667e5fd9
RW
90if (get_user_preferences('forum_useexperimentalui', false)) {
91 if ($displaymode == FORUM_MODE_NESTED) {
92 $displaymode = FORUM_MODE_NESTED_V2;
93 }
94} else {
95 if ($displaymode == FORUM_MODE_NESTED_V2) {
96 $displaymode = FORUM_MODE_NESTED;
97 }
98}
99
100if ($displaymode != $saveddisplaymode) {
101 set_user_preference('forum_displaymode', $displaymode);
102}
15ecfac4 103
54d38a73
MG
104$PAGE->set_context($forum->get_context());
105$PAGE->set_title($forum->get_name());
7a2d4f54 106$PAGE->add_body_class('forumtype-' . $forum->get_type());
54d38a73 107$PAGE->set_heading($course->fullname);
54d38a73 108
1d709d76
MM
109$buttons = [];
110if ($capabilitymanager->can_grade($USER)) {
111 $forumgradeitem = forum_gradeitem::load_from_forum_entity($forum);
112 if ($forumgradeitem->is_grading_enabled()) {
113 $groupid = groups_get_activity_group($cm, true) ?: null;
114 $gradeobj = (object) [
115 'contextid' => $forum->get_context()->id,
116 'cmid' => $cmid,
117 'name' => $forum->get_name(),
4c98e56c
RW
118 'courseid' => $course->id,
119 'coursename' => $course->shortname,
1d709d76
MM
120 'groupid' => $groupid,
121 'gradingcomponent' => $forumgradeitem->get_grading_component_name(),
122 'gradingcomponentsubtype' => $forumgradeitem->get_grading_component_subtype(),
123 ];
124 $buttons[] = $OUTPUT->render_from_template('mod_forum/grades/grade_button', $gradeobj);
125 }
126}
127$buttons[] = forum_search_form($course, $search);
128$PAGE->set_button(implode('', $buttons));
129
2602c7bf 130if ($istypesingle && $displaymode == FORUM_MODE_NESTED_V2) {
667e5fd9
RW
131 $PAGE->add_body_class('reset-style');
132 $settingstrigger = $OUTPUT->render_from_template('mod_forum/settings_drawer_trigger', null);
133 $PAGE->add_header_action($settingstrigger);
15ecfac4
RW
134}
135
bda290ee 136if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) {
54d38a73
MG
137 redirect(
138 $urlfactory->get_course_url_from_forum($forum),
139 get_string('activityiscurrentlyhidden'),
7aa6affa 140 null,
54d38a73
MG
141 \core\output\notification::NOTIFY_WARNING
142 );
143}
144
7aa6affa 145if (!$capabilitymanager->can_view_discussions($USER)) {
54d38a73
MG
146 redirect(
147 $urlfactory->get_course_url_from_forum($forum),
2ddbc651 148 get_string('noviewdiscussionspermission', 'forum'),
7aa6affa 149 null,
54d38a73
MG
150 \core\output\notification::NOTIFY_WARNING
151 );
152}
153
154// Mark viewed and trigger the course_module_viewed event.
155$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
e5a501de 156$forumrecord = $forumdatamapper->to_legacy_object($forum);
54d38a73 157forum_view(
e5a501de 158 $forumrecord,
54d38a73
MG
159 $forum->get_course_record(),
160 $forum->get_course_module_record(),
161 $forum->get_context()
162);
163
bda290ee
RW
164// Return here if we post or set subscription etc.
165$SESSION->fromdiscussion = qualified_me();
166
54d38a73
MG
167if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->get_rss_type() && $forum->get_rss_articles()) {
168 require_once("{$CFG->libdir}/rsslib.php");
169
170 $rsstitle = format_string($course->shortname, true, [
171 'context' => context_course::instance($course->id),
172 ]) . ': ' . format_string($forum->get_name());
e5a501de 173 rss_add_http_header($forum->get_context(), 'mod_forum', $forumrecord, $rsstitle);
54d38a73
MG
174}
175
176echo $OUTPUT->header();
177echo $OUTPUT->heading(format_string($forum->get_name()), 2);
178
15ecfac4 179if (!$istypesingle && !empty($forum->get_intro())) {
54d38a73
MG
180 echo $OUTPUT->box(format_module_intro('forum', $forumrecord, $cm->id), 'generalbox', 'intro');
181}
182
1a9c60e9
MG
183if ($sortorder) {
184 set_user_preference('forum_discussionlistsortorder', $sortorder);
185}
186
187$sortorder = get_user_preferences('forum_discussionlistsortorder', $discussionlistvault::SORTORDER_LASTPOST_DESC);
188
54d38a73
MG
189// Fetch the current groupid.
190$groupid = groups_get_activity_group($cm, true) ?: null;
191$rendererfactory = mod_forum\local\container::get_renderer_factory();
192switch ($forum->get_type()) {
193 case 'single':
194 $discussion = $discussionvault->get_last_discussion_in_forum($forum);
195 $discussioncount = $discussionvault->get_count_discussions_in_forum($forum);
1a9c60e9 196 $hasmultiplediscussions = $discussioncount > 1;
54d38a73
MG
197 $discussionsrenderer = $rendererfactory->get_single_discussion_list_renderer($forum, $discussion,
198 $hasmultiplediscussions, $displaymode);
199 $post = $postvault->get_from_id($discussion->get_first_post_id());
200 $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
bc4c7337
AN
201 $replies = $postvault->get_replies_to_post(
202 $USER,
203 $post,
204 $capabilitymanager->can_view_any_private_reply($USER),
205 $orderpostsby
206 );
54d38a73 207 echo $discussionsrenderer->render($USER, $post, $replies);
be1400c6
SR
208
209 if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) {
210 $postids = array_map(function($post) {
211 return $post->get_id();
212 }, array_merge([$post], array_values($replies)));
213 forum_tp_mark_posts_read($USER, $postids);
214 }
54d38a73
MG
215 break;
216 case 'blog':
217 $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum);
94552394 218 // Blog forums always show discussions newest first.
1a9c60e9
MG
219 echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC,
220 $pageno, $pagesize);
be1400c6
SR
221
222 if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) {
223 $discussions = mod_forum_get_discussion_summaries($forum, $USER, $groupid, null, $pageno, $pagesize);
224 $firstpostids = array_map(function($discussion) {
225 return $discussion->get_first_post()->get_id();
226 }, array_values($discussions));
227 forum_tp_mark_posts_read($USER, $firstpostids);
228 }
54d38a73
MG
229 break;
230 default:
231 $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum);
232 echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize);
233}
234
235echo $OUTPUT->footer();