Commit | Line | Data |
---|---|---|
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 |
25 | use mod_forum\grades\forum_gradeitem; |
26 | ||
54d38a73 MG |
27 | require_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 | 47 | if (!$cmid && !$forumid) { |
73d60436 | 48 | throw new \moodle_exception('missingparameter'); |
bda290ee RW |
49 | } |
50 | ||
1bfaeeb2 AN |
51 | if ($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 |
63 | if (!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 | ||
79 | require_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 | |
84 | if ($mode) { | |
667e5fd9 RW |
85 | $displaymode = $mode; |
86 | } else { | |
87 | $displaymode = $saveddisplaymode; | |
15ecfac4 RW |
88 | } |
89 | ||
667e5fd9 RW |
90 | if (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 | ||
100 | if ($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); |
dba47e86 | 108 | $PAGE->add_header_action(forum_search_form($course, $search)); |
1d709d76 | 109 | |
2602c7bf | 110 | if ($istypesingle && $displaymode == FORUM_MODE_NESTED_V2) { |
9f2a68fe | 111 | $PAGE->add_body_class('nested-v2-display-mode reset-style'); |
667e5fd9 RW |
112 | $settingstrigger = $OUTPUT->render_from_template('mod_forum/settings_drawer_trigger', null); |
113 | $PAGE->add_header_action($settingstrigger); | |
15ecfac4 RW |
114 | } |
115 | ||
bda290ee | 116 | if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) { |
54d38a73 MG |
117 | redirect( |
118 | $urlfactory->get_course_url_from_forum($forum), | |
119 | get_string('activityiscurrentlyhidden'), | |
7aa6affa | 120 | null, |
54d38a73 MG |
121 | \core\output\notification::NOTIFY_WARNING |
122 | ); | |
123 | } | |
124 | ||
7aa6affa | 125 | if (!$capabilitymanager->can_view_discussions($USER)) { |
54d38a73 MG |
126 | redirect( |
127 | $urlfactory->get_course_url_from_forum($forum), | |
2ddbc651 | 128 | get_string('noviewdiscussionspermission', 'forum'), |
7aa6affa | 129 | null, |
54d38a73 MG |
130 | \core\output\notification::NOTIFY_WARNING |
131 | ); | |
132 | } | |
133 | ||
134 | // Mark viewed and trigger the course_module_viewed event. | |
135 | $forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper(); | |
e5a501de | 136 | $forumrecord = $forumdatamapper->to_legacy_object($forum); |
90438c21 | 137 | $PAGE->set_activity_record($forumrecord); |
54d38a73 | 138 | forum_view( |
e5a501de | 139 | $forumrecord, |
54d38a73 MG |
140 | $forum->get_course_record(), |
141 | $forum->get_course_module_record(), | |
142 | $forum->get_context() | |
143 | ); | |
144 | ||
bda290ee RW |
145 | // Return here if we post or set subscription etc. |
146 | $SESSION->fromdiscussion = qualified_me(); | |
147 | ||
54d38a73 MG |
148 | if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->get_rss_type() && $forum->get_rss_articles()) { |
149 | require_once("{$CFG->libdir}/rsslib.php"); | |
150 | ||
151 | $rsstitle = format_string($course->shortname, true, [ | |
152 | 'context' => context_course::instance($course->id), | |
153 | ]) . ': ' . format_string($forum->get_name()); | |
e5a501de | 154 | rss_add_http_header($forum->get_context(), 'mod_forum', $forumrecord, $rsstitle); |
54d38a73 | 155 | } |
ab40bc59 PD |
156 | $activityheader = $PAGE->activityheader; |
157 | $pageheader = []; | |
158 | if ($activityheader->is_title_allowed()) { | |
159 | $pageheader['title'] = format_string($forum->get_name()); | |
92833547 | 160 | } |
54d38a73 | 161 | |
92833547 SH |
162 | // Fetch the current groupid. |
163 | $groupid = groups_get_activity_group($cm, true) ?: null; | |
164 | ||
ab40bc59 PD |
165 | if ($istypesingle || empty($forum->get_intro())) { |
166 | $pageheader['description'] = ''; | |
54d38a73 | 167 | } |
ab40bc59 PD |
168 | $activityheader->set_attrs($pageheader); |
169 | ||
170 | echo $OUTPUT->header(); | |
54d38a73 | 171 | |
92833547 SH |
172 | $rendererfactory = mod_forum\local\container::get_renderer_factory(); |
173 | // The elements for view action are rendered and added to the page. | |
174 | echo forum_activity_actionbar($forum, $groupid, $course, $search); | |
175 | ||
1a9c60e9 MG |
176 | if ($sortorder) { |
177 | set_user_preference('forum_discussionlistsortorder', $sortorder); | |
178 | } | |
179 | ||
180 | $sortorder = get_user_preferences('forum_discussionlistsortorder', $discussionlistvault::SORTORDER_LASTPOST_DESC); | |
181 | ||
54d38a73 MG |
182 | switch ($forum->get_type()) { |
183 | case 'single': | |
07d8669d | 184 | $forumgradeitem = forum_gradeitem::load_from_forum_entity($forum); |
2667acf1 | 185 | if ($capabilitymanager->can_grade($USER)) { |
07d8669d | 186 | |
2667acf1 RW |
187 | if ($forumgradeitem->is_grading_enabled()) { |
188 | $groupid = groups_get_activity_group($cm, true) ?: null; | |
189 | $gradeobj = (object) [ | |
190 | 'contextid' => $forum->get_context()->id, | |
191 | 'cmid' => $cmid, | |
df9eefac | 192 | 'name' => format_string($forum->get_name()), |
2667acf1 | 193 | 'courseid' => $course->id, |
df9eefac | 194 | 'coursename' => format_string($course->shortname), |
2667acf1 RW |
195 | 'experimentaldisplaymode' => $displaymode == FORUM_MODE_NESTED_V2, |
196 | 'groupid' => $groupid, | |
197 | 'gradingcomponent' => $forumgradeitem->get_grading_component_name(), | |
198 | 'gradingcomponentsubtype' => $forumgradeitem->get_grading_component_subtype(), | |
c2dc453f | 199 | 'sendstudentnotifications' => $forum->should_notify_students_default_when_grade_for_forum(), |
de4d940d | 200 | 'gradeonlyactiveusers' => $forumgradeitem->should_grade_only_active_users(), |
2667acf1 RW |
201 | ]; |
202 | echo $OUTPUT->render_from_template('mod_forum/grades/grade_button', $gradeobj); | |
203 | } | |
07d8669d MM |
204 | } else { |
205 | if ($forumgradeitem->is_grading_enabled()) { | |
206 | $groupid = groups_get_activity_group($cm, true) ?: null; | |
207 | $gradeobj = (object) [ | |
208 | 'contextid' => $forum->get_context()->id, | |
209 | 'cmid' => $cmid, | |
df9eefac | 210 | 'name' => format_string($forum->get_name()), |
07d8669d | 211 | 'courseid' => $course->id, |
df9eefac | 212 | 'coursename' => format_string($course->shortname), |
07d8669d MM |
213 | 'groupid' => $groupid, |
214 | 'userid' => $USER->id, | |
215 | 'gradingcomponent' => $forumgradeitem->get_grading_component_name(), | |
216 | 'gradingcomponentsubtype' => $forumgradeitem->get_grading_component_subtype(), | |
217 | ]; | |
974db580 | 218 | echo $OUTPUT->render_from_template('mod_forum/grades/view_grade_button', $gradeobj); |
07d8669d | 219 | } |
2667acf1 | 220 | } |
54d38a73 MG |
221 | $discussion = $discussionvault->get_last_discussion_in_forum($forum); |
222 | $discussioncount = $discussionvault->get_count_discussions_in_forum($forum); | |
1a9c60e9 | 223 | $hasmultiplediscussions = $discussioncount > 1; |
54d38a73 MG |
224 | $discussionsrenderer = $rendererfactory->get_single_discussion_list_renderer($forum, $discussion, |
225 | $hasmultiplediscussions, $displaymode); | |
226 | $post = $postvault->get_from_id($discussion->get_first_post_id()); | |
227 | $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC'; | |
bc4c7337 AN |
228 | $replies = $postvault->get_replies_to_post( |
229 | $USER, | |
230 | $post, | |
231 | $capabilitymanager->can_view_any_private_reply($USER), | |
232 | $orderpostsby | |
233 | ); | |
54d38a73 | 234 | echo $discussionsrenderer->render($USER, $post, $replies); |
be1400c6 SR |
235 | |
236 | if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) { | |
237 | $postids = array_map(function($post) { | |
238 | return $post->get_id(); | |
239 | }, array_merge([$post], array_values($replies))); | |
240 | forum_tp_mark_posts_read($USER, $postids); | |
241 | } | |
54d38a73 MG |
242 | break; |
243 | case 'blog': | |
244 | $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum); | |
94552394 | 245 | // Blog forums always show discussions newest first. |
1a9c60e9 | 246 | echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC, |
92833547 | 247 | $pageno, $pagesize, null, false); |
be1400c6 SR |
248 | |
249 | if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) { | |
250 | $discussions = mod_forum_get_discussion_summaries($forum, $USER, $groupid, null, $pageno, $pagesize); | |
251 | $firstpostids = array_map(function($discussion) { | |
252 | return $discussion->get_first_post()->get_id(); | |
253 | }, array_values($discussions)); | |
254 | forum_tp_mark_posts_read($USER, $firstpostids); | |
255 | } | |
54d38a73 MG |
256 | break; |
257 | default: | |
258 | $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum); | |
92833547 | 259 | echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize, $displaymode, false); |
54d38a73 MG |
260 | } |
261 | ||
262 | echo $OUTPUT->footer(); |