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