MDL-66770 mod_forum: rename modern display mode to nested v2
[moodle.git] / mod / forum / classes / local / renderers / discussion.php
CommitLineData
47d38303
RW
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/>.
16
17/**
18 * Discussion renderer.
19 *
20 * @package mod_forum
21 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace mod_forum\local\renderers;
26
27defined('MOODLE_INTERNAL') || die();
28
29use mod_forum\local\entities\discussion as discussion_entity;
30use mod_forum\local\entities\forum as forum_entity;
31use mod_forum\local\entities\post as post_entity;
32use mod_forum\local\entities\sorter as sorter_entity;
fe8e8ad4 33use mod_forum\local\factories\entity as entity_factory;
47d38303
RW
34use mod_forum\local\factories\legacy_data_mapper as legacy_data_mapper_factory;
35use mod_forum\local\factories\exporter as exporter_factory;
fe8e8ad4 36use mod_forum\local\factories\url as url_factory;
47d38303
RW
37use mod_forum\local\factories\vault as vault_factory;
38use mod_forum\local\managers\capability as capability_manager;
39use mod_forum\local\renderers\posts as posts_renderer;
40use core\output\notification;
41use context;
42use context_module;
43use html_writer;
44use moodle_exception;
45use moodle_page;
46use moodle_url;
47use rating_manager;
48use renderer_base;
49use single_button;
50use single_select;
51use stdClass;
52use url_select;
53
54require_once($CFG->dirroot . '/mod/forum/lib.php');
55
56/**
57 * Discussion renderer class.
58 *
59 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
60 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61 */
62class discussion {
54d38a73
MG
63 /** @var forum_entity $forum The forum that the discussion belongs to */
64 private $forum;
65 /** @var discussion_entity $discussion The discussion entity */
47d38303
RW
66 private $discussion;
67 /** @var stdClass $discussionrecord Legacy discussion record */
68 private $discussionrecord;
47d38303
RW
69 /** @var stdClass $forumrecord Legacy forum record */
70 private $forumrecord;
71 /** @var int $displaymode The display mode to render the discussion in */
72 private $displaymode;
73 /** @var renderer_base $renderer Renderer base */
74 private $renderer;
75 /** @var posts_renderer $postsrenderer A posts renderer */
76 private $postsrenderer;
77 /** @var moodle_page $page The page this discussion is being rendered for */
78 private $page;
79 /** @var legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory */
80 private $legacydatamapperfactory;
81 /** @var exporter_factory $exporterfactory Exporter factory */
82 private $exporterfactory;
83 /** @var vault_factory $vaultfactory Vault factory */
84 private $vaultfactory;
fe8e8ad4
RW
85 /** @var url_factory $urlfactory URL factory */
86 private $urlfactory;
87 /** @var entity_factory $entityfactory Entity factory */
88 private $entityfactory;
47d38303
RW
89 /** @var capability_manager $capabilitymanager Capability manager */
90 private $capabilitymanager;
91 /** @var rating_manager $ratingmanager Rating manager */
92 private $ratingmanager;
93 /** @var moodle_url $baseurl The base URL for the discussion */
94 private $baseurl;
95 /** @var array $notifications List of HTML notifications to display */
96 private $notifications;
97 /** @var sorter_entity $exportedpostsorter Sorter for the exported posts */
98 private $exportedpostsorter;
13cd05ac
P
99 /** @var callable $postprocessfortemplate Function to process exported posts before template rendering */
100 private $postprocessfortemplate;
47d38303
RW
101
102 /**
103 * Constructor.
104 *
47d38303 105 * @param forum_entity $forum The forum that the discussion belongs to
54d38a73 106 * @param discussion_entity $discussion The discussion entity
47d38303
RW
107 * @param int $displaymode The display mode to render the discussion in
108 * @param renderer_base $renderer Renderer base
109 * @param posts_renderer $postsrenderer A posts renderer
110 * @param moodle_page $page The page this discussion is being rendered for
111 * @param legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory
112 * @param exporter_factory $exporterfactory Exporter factory
113 * @param vault_factory $vaultfactory Vault factory
fe8e8ad4
RW
114 * @param url_factory $urlfactory URL factory
115 * @param entity_factory $entityfactory Entity factory
47d38303
RW
116 * @param capability_manager $capabilitymanager Capability manager
117 * @param rating_manager $ratingmanager Rating manager
118 * @param sorter_entity $exportedpostsorter Sorter for the exported posts
119 * @param moodle_url $baseurl The base URL for the discussion
120 * @param array $notifications List of HTML notifications to display
fe8e8ad4 121 * @param callable|null $postprocessfortemplate Post processing for template callback
47d38303
RW
122 */
123 public function __construct(
47d38303 124 forum_entity $forum,
54d38a73 125 discussion_entity $discussion,
47d38303
RW
126 int $displaymode,
127 renderer_base $renderer,
128 posts_renderer $postsrenderer,
129 moodle_page $page,
130 legacy_data_mapper_factory $legacydatamapperfactory,
131 exporter_factory $exporterfactory,
132 vault_factory $vaultfactory,
fe8e8ad4
RW
133 url_factory $urlfactory,
134 entity_factory $entityfactory,
47d38303
RW
135 capability_manager $capabilitymanager,
136 rating_manager $ratingmanager,
137 sorter_entity $exportedpostsorter,
138 moodle_url $baseurl,
13cd05ac
P
139 array $notifications = [],
140 callable $postprocessfortemplate = null
47d38303 141 ) {
47d38303 142 $this->forum = $forum;
54d38a73 143 $this->discussion = $discussion;
47d38303
RW
144 $this->displaymode = $displaymode;
145 $this->renderer = $renderer;
146 $this->postsrenderer = $postsrenderer;
147 $this->page = $page;
148 $this->baseurl = $baseurl;
149 $this->legacydatamapperfactory = $legacydatamapperfactory;
150 $this->exporterfactory = $exporterfactory;
151 $this->vaultfactory = $vaultfactory;
fe8e8ad4
RW
152 $this->urlfactory = $urlfactory;
153 $this->entityfactory = $entityfactory;
47d38303
RW
154 $this->capabilitymanager = $capabilitymanager;
155 $this->ratingmanager = $ratingmanager;
156 $this->notifications = $notifications;
54d38a73 157
47d38303 158 $this->exportedpostsorter = $exportedpostsorter;
13cd05ac 159 $this->postprocessfortemplate = $postprocessfortemplate;
47d38303
RW
160
161 $forumdatamapper = $this->legacydatamapperfactory->get_forum_data_mapper();
162 $this->forumrecord = $forumdatamapper->to_legacy_object($forum);
163
164 $discussiondatamapper = $this->legacydatamapperfactory->get_discussion_data_mapper();
165 $this->discussionrecord = $discussiondatamapper->to_legacy_object($discussion);
166 }
167
168 /**
169 * Render the discussion for the given user in the specified display mode.
170 *
171 * @param stdClass $user The user viewing the discussion
172 * @param post_entity $firstpost The first post in the discussion
173 * @param array $replies List of replies to the first post
174 * @return string HTML for the discussion
175 */
176 public function render(
177 stdClass $user,
178 post_entity $firstpost,
179 array $replies
180 ) : string {
181 global $CFG;
182
183 $displaymode = $this->displaymode;
184 $capabilitymanager = $this->capabilitymanager;
fe8e8ad4
RW
185 $urlfactory = $this->urlfactory;
186 $entityfactory = $this->entityfactory;
47d38303
RW
187
188 // Make sure we can render.
189 if (!$capabilitymanager->can_view_discussions($user)) {
190 throw new moodle_exception('noviewdiscussionspermission', 'mod_forum');
191 }
192
193 $posts = array_merge([$firstpost], array_values($replies));
194
13cd05ac
P
195 if ($this->postprocessfortemplate !== null) {
196 $exporteddiscussion = ($this->postprocessfortemplate) ($this->discussion, $user, $this->forum);
197 } else {
198 $exporteddiscussion = $this->get_exported_discussion($user);
199 }
200
4b6e0328
AN
201 $hasanyactions = false;
202 $hasanyactions = $hasanyactions || $capabilitymanager->can_favourite_discussion($user);
203 $hasanyactions = $hasanyactions || $capabilitymanager->can_pin_discussions($user);
204 $hasanyactions = $hasanyactions || $capabilitymanager->can_manage_forum($user);
205
47d38303 206 $exporteddiscussion = array_merge($exporteddiscussion, [
54d38a73 207 'notifications' => $this->get_notifications($user),
47d38303 208 'html' => [
4b6e0328 209 'hasanyactions' => $hasanyactions,
47d38303
RW
210 'posts' => $this->postsrenderer->render($user, [$this->forum], [$this->discussion], $posts),
211 'modeselectorform' => $this->get_display_mode_selector_html($displaymode),
212 'subscribe' => null,
213 'movediscussion' => null,
214 'pindiscussion' => null,
215 'neighbourlinks' => $this->get_neighbour_links_html(),
216 'exportdiscussion' => !empty($CFG->enableportfolios) ? $this->get_export_discussion_html() : null
217 ]
218 ]);
219
220 $capabilities = (array) $exporteddiscussion['capabilities'];
221
47d38303
RW
222 if ($capabilities['move']) {
223 $exporteddiscussion['html']['movediscussion'] = $this->get_move_discussion_html();
224 }
225
38d96b65
RW
226 if (!empty($user->id)) {
227 $loggedinuser = $entityfactory->get_author_from_stdClass($user);
228 $exporteddiscussion['loggedinuser'] = [
229 'firstname' => $loggedinuser->get_first_name(),
230 'fullname' => $loggedinuser->get_full_name(),
231 'profileimageurl' => ($urlfactory->get_author_profile_image_url($loggedinuser, null))->out(false)
232 ];
233 }
234
2602c7bf
RW
235 if ($this->displaymode === FORUM_MODE_NESTED_V2) {
236 $template = 'mod_forum/forum_discussion_nested_v2';
fe8e8ad4
RW
237 } else {
238 $template = 'mod_forum/forum_discussion';
239 }
240
241 return $this->renderer->render_from_template($template, $exporteddiscussion);
47d38303
RW
242 }
243
244 /**
245 * Get the groups details for all groups available to the forum.
246 *
247 * @return stdClass[]
248 */
249 private function get_groups_available_in_forum() : array {
250 $course = $this->forum->get_course_record();
251 $coursemodule = $this->forum->get_course_module_record();
252
253 return groups_get_all_groups($course->id, 0, $coursemodule->groupingid);
254 }
255
256 /**
257 * Get the exported discussion.
258 *
259 * @param stdClass $user The user viewing the discussion
260 * @return array
261 */
262 private function get_exported_discussion(stdClass $user) : array {
263 $discussionexporter = $this->exporterfactory->get_discussion_exporter(
264 $user,
265 $this->forum,
266 $this->discussion,
267 $this->get_groups_available_in_forum()
268 );
269
270 return (array) $discussionexporter->export($this->renderer);
271 }
272
273 /**
274 * Get the HTML for the display mode selector.
275 *
276 * @param int $displaymode The current display mode
277 * @return string
278 */
279 private function get_display_mode_selector_html(int $displaymode) : string {
280 $baseurl = $this->baseurl;
281 $select = new single_select(
282 $baseurl,
283 'mode',
284 forum_get_layout_modes(),
285 $displaymode,
286 null,
287 'mode'
288 );
289 $select->set_label(get_string('displaymode', 'forum'), ['class' => 'accesshide']);
290
291 return $this->renderer->render($select);
292 }
293
47d38303
RW
294 /**
295 * Get the HTML to render the move discussion selector and button.
296 *
297 * @return string
298 */
14572292 299 private function get_move_discussion_html() : ?string {
47d38303
RW
300 global $DB;
301
302 $forum = $this->forum;
303 $discussion = $this->discussion;
304 $courseid = $forum->get_course_id();
305
306 // Popup menu to move discussions to other forums. The discussion in a
307 // single discussion forum can't be moved.
308 $modinfo = get_fast_modinfo($courseid);
309 if (isset($modinfo->instances['forum'])) {
310 $forummenu = [];
311 // Check forum types and eliminate simple discussions.
312 $forumcheck = $DB->get_records('forum', ['course' => $courseid], '', 'id, type');
313 foreach ($modinfo->instances['forum'] as $forumcm) {
314 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
315 context_module::instance($forumcm->id))) {
316 continue;
317 }
318 $section = $forumcm->sectionnum;
319 $sectionname = get_section_name($courseid, $section);
320 if (empty($forummenu[$section])) {
321 $forummenu[$section] = [$sectionname => []];
322 }
323 $forumidcompare = $forumcm->instance != $forum->get_id();
324 $forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
325
326 if ($forumidcompare and $forumtypecheck) {
327 $url = "/mod/forum/discuss.php?d={$discussion->get_id()}&move=$forumcm->instance&sesskey=".sesskey();
328 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
329 }
330 }
331 if (!empty($forummenu)) {
332 $html = '<div class="movediscussionoption">';
959b996c
JP
333
334 $movebutton = get_string('move');
2602c7bf 335 if ($this->displaymode === FORUM_MODE_NESTED_V2) {
959b996c
JP
336 // Move discussion selector will be rendered on the settings drawer. We won't output the button in this mode.
337 $movebutton = null;
338 }
47d38303
RW
339 $select = new url_select($forummenu, '',
340 ['/mod/forum/discuss.php?d=' . $discussion->get_id() => get_string("movethisdiscussionto", "forum")],
959b996c 341 'forummenu', $movebutton);
47d38303
RW
342 $html .= $this->renderer->render($select);
343 $html .= "</div>";
344 return $html;
345 }
346 }
347
348 return null;
349 }
350
47d38303
RW
351 /**
352 * Get the HTML to render the export discussion button.
353 *
354 * @return string|null
355 */
356 private function get_export_discussion_html() : ?string {
357 global $CFG;
358
359 require_once($CFG->libdir . '/portfoliolib.php');
360 $discussion = $this->discussion;
361 $button = new \portfolio_add_button();
362 $button->set_callback_options('forum_portfolio_caller', ['discussionid' => $discussion->get_id()], 'mod_forum');
363 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
364 return $button ?: null;
365 }
366
367 /**
368 * Get a list of notification HTML to render in the page.
369 *
54d38a73 370 * @param stdClass $user The user viewing the discussion
47d38303
RW
371 * @return string[]
372 */
54d38a73 373 private function get_notifications($user) : array {
47d38303
RW
374 $notifications = $this->notifications;
375 $discussion = $this->discussion;
376 $forum = $this->forum;
377 $renderer = $this->renderer;
378
cbf63d8e
SR
379 if ($forum->is_cutoff_date_reached()) {
380 $notifications[] = (new notification(
381 get_string('cutoffdatereached', 'forum'),
82482e3d
SR
382 notification::NOTIFY_INFO
383 ))->set_show_closebutton();
384 } else if ($forum->is_due_date_reached()) {
385 $notifications[] = (new notification(
386 get_string('thisforumisdue', 'forum', userdate($forum->get_due_date())),
cbf63d8e
SR
387 notification::NOTIFY_INFO
388 ))->set_show_closebutton();
389 } else if ($forum->has_due_date()) {
390 $notifications[] = (new notification(
391 get_string('thisforumhasduedate', 'forum', userdate($forum->get_due_date())),
392 notification::NOTIFY_INFO
393 ))->set_show_closebutton();
394 }
395
47d38303
RW
396 if ($forum->is_discussion_locked($discussion)) {
397 $notifications[] = (new notification(
398 get_string('discussionlocked', 'forum'),
399 notification::NOTIFY_INFO
400 ))
401 ->set_extra_classes(['discussionlocked'])
402 ->set_show_closebutton();
403 }
54d38a73
MG
404
405 if ($forum->get_type() == 'qanda') {
406 if ($this->capabilitymanager->must_post_before_viewing_discussion($user, $discussion)) {
407 $notifications[] = (new notification(
408 get_string('qandanotify', 'forum')
409 ))->set_show_closebutton(true);
410 }
411 }
47d38303
RW
412
413 if ($forum->has_blocking_enabled()) {
414 $notifications[] = (new notification(
415 get_string('thisforumisthrottled', 'forum', [
416 'blockafter' => $forum->get_block_after(),
417 'blockperiod' => get_string('secondstotime' . $forum->get_block_period())
418 ])
419 ))->set_show_closebutton();
420 }
421
422 return array_map(function($notification) {
423 return $notification->export_for_template($this->renderer);
424 }, $notifications);
425 }
426
427 /**
428 * Get HTML to display the neighbour links.
429 *
430 * @return string
431 */
432 private function get_neighbour_links_html() : string {
433 $forum = $this->forum;
434 $coursemodule = $forum->get_course_module_record();
435 $neighbours = forum_get_discussion_neighbours($coursemodule, $this->discussionrecord, $this->forumrecord);
436 return $this->renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
437 }
438}