MDL-31355 mod_forum: WS should return duedate and cutoffdate.
[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
200 if ($capabilities['pin']) {
201 $exporteddiscussion['html']['pindiscussion'] = $this->get_pin_discussion_html();
202 }
203
204 return $this->renderer->render_from_template('mod_forum/forum_discussion', $exporteddiscussion);
205 }
206
207 /**
208 * Get the groups details for all groups available to the forum.
209 *
210 * @return stdClass[]
211 */
212 private function get_groups_available_in_forum() : array {
213 $course = $this->forum->get_course_record();
214 $coursemodule = $this->forum->get_course_module_record();
215
216 return groups_get_all_groups($course->id, 0, $coursemodule->groupingid);
217 }
218
219 /**
220 * Get the exported discussion.
221 *
222 * @param stdClass $user The user viewing the discussion
223 * @return array
224 */
225 private function get_exported_discussion(stdClass $user) : array {
226 $discussionexporter = $this->exporterfactory->get_discussion_exporter(
227 $user,
228 $this->forum,
229 $this->discussion,
230 $this->get_groups_available_in_forum()
231 );
232
233 return (array) $discussionexporter->export($this->renderer);
234 }
235
236 /**
237 * Get the HTML for the display mode selector.
238 *
239 * @param int $displaymode The current display mode
240 * @return string
241 */
242 private function get_display_mode_selector_html(int $displaymode) : string {
243 $baseurl = $this->baseurl;
244 $select = new single_select(
245 $baseurl,
246 'mode',
247 forum_get_layout_modes(),
248 $displaymode,
249 null,
250 'mode'
251 );
252 $select->set_label(get_string('displaymode', 'forum'), ['class' => 'accesshide']);
253
254 return $this->renderer->render($select);
255 }
256
257 /**
258 * Get the HTML to render the subscription button.
259 *
260 * @return string
261 */
262 private function get_subscription_button_html() : string {
263 global $PAGE;
264
265 $forumrecord = $this->forumrecord;
266 $discussion = $this->discussion;
267 $html = html_writer::div(
268 forum_get_discussion_subscription_icon($forumrecord, $discussion->get_id(), null, true),
269 'discussionsubscription'
270 );
271 $html .= forum_get_discussion_subscription_icon_preloaders();
272 // Add the subscription toggle JS.
273 $PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
274 return $html;
275 }
276
277 /**
278 * Get the HTML to render the move discussion selector and button.
279 *
280 * @return string
281 */
282 private function get_move_discussion_html() : string {
283 global $DB;
284
285 $forum = $this->forum;
286 $discussion = $this->discussion;
287 $courseid = $forum->get_course_id();
288
289 // Popup menu to move discussions to other forums. The discussion in a
290 // single discussion forum can't be moved.
291 $modinfo = get_fast_modinfo($courseid);
292 if (isset($modinfo->instances['forum'])) {
293 $forummenu = [];
294 // Check forum types and eliminate simple discussions.
295 $forumcheck = $DB->get_records('forum', ['course' => $courseid], '', 'id, type');
296 foreach ($modinfo->instances['forum'] as $forumcm) {
297 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
298 context_module::instance($forumcm->id))) {
299 continue;
300 }
301 $section = $forumcm->sectionnum;
302 $sectionname = get_section_name($courseid, $section);
303 if (empty($forummenu[$section])) {
304 $forummenu[$section] = [$sectionname => []];
305 }
306 $forumidcompare = $forumcm->instance != $forum->get_id();
307 $forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
308
309 if ($forumidcompare and $forumtypecheck) {
310 $url = "/mod/forum/discuss.php?d={$discussion->get_id()}&move=$forumcm->instance&sesskey=".sesskey();
311 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
312 }
313 }
314 if (!empty($forummenu)) {
315 $html = '<div class="movediscussionoption">';
316 $select = new url_select($forummenu, '',
317 ['/mod/forum/discuss.php?d=' . $discussion->get_id() => get_string("movethisdiscussionto", "forum")],
318 'forummenu', get_string('move'));
319 $html .= $this->renderer->render($select);
320 $html .= "</div>";
321 return $html;
322 }
323 }
324
325 return null;
326 }
327
328 /**
329 * Get the HTML to render the pin discussion button.
330 *
331 * @return string
332 */
333 private function get_pin_discussion_html() : string {
334 $discussion = $this->discussion;
335
336 if ($discussion->is_pinned()) {
337 $pinlink = FORUM_DISCUSSION_UNPINNED;
338 $pintext = get_string('discussionunpin', 'forum');
339 } else {
340 $pinlink = FORUM_DISCUSSION_PINNED;
341 $pintext = get_string('discussionpin', 'forum');
342 }
343
344 $button = new single_button(
345 new moodle_url('discuss.php', ['pin' => $pinlink, 'd' => $discussion->get_id()]),
346 $pintext,
347 'post'
348 );
349 return $this->renderer->render($button);
350 }
351
352 /**
353 * Get the HTML to render the export discussion button.
354 *
355 * @return string|null
356 */
357 private function get_export_discussion_html() : ?string {
358 global $CFG;
359
360 require_once($CFG->libdir . '/portfoliolib.php');
361 $discussion = $this->discussion;
362 $button = new \portfolio_add_button();
363 $button->set_callback_options('forum_portfolio_caller', ['discussionid' => $discussion->get_id()], 'mod_forum');
364 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
365 return $button ?: null;
366 }
367
368 /**
369 * Get a list of notification HTML to render in the page.
370 *
54d38a73 371 * @param stdClass $user The user viewing the discussion
47d38303
RW
372 * @return string[]
373 */
54d38a73 374 private function get_notifications($user) : array {
47d38303
RW
375 $notifications = $this->notifications;
376 $discussion = $this->discussion;
377 $forum = $this->forum;
378 $renderer = $this->renderer;
379
380 if ($forum->is_discussion_locked($discussion)) {
381 $notifications[] = (new notification(
382 get_string('discussionlocked', 'forum'),
383 notification::NOTIFY_INFO
384 ))
385 ->set_extra_classes(['discussionlocked'])
386 ->set_show_closebutton();
387 }
54d38a73
MG
388
389 if ($forum->get_type() == 'qanda') {
390 if ($this->capabilitymanager->must_post_before_viewing_discussion($user, $discussion)) {
391 $notifications[] = (new notification(
392 get_string('qandanotify', 'forum')
393 ))->set_show_closebutton(true);
394 }
395 }
47d38303
RW
396
397 if ($forum->has_blocking_enabled()) {
398 $notifications[] = (new notification(
399 get_string('thisforumisthrottled', 'forum', [
400 'blockafter' => $forum->get_block_after(),
401 'blockperiod' => get_string('secondstotime' . $forum->get_block_period())
402 ])
403 ))->set_show_closebutton();
404 }
405
406 return array_map(function($notification) {
407 return $notification->export_for_template($this->renderer);
408 }, $notifications);
409 }
410
411 /**
412 * Get HTML to display the neighbour links.
413 *
414 * @return string
415 */
416 private function get_neighbour_links_html() : string {
417 $forum = $this->forum;
418 $coursemodule = $forum->get_course_module_record();
419 $neighbours = forum_get_discussion_neighbours($coursemodule, $this->discussionrecord, $this->forumrecord);
420 return $this->renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
421 }
422}