2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Discussion list renderer.
21 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace mod_forum\local\renderers;
27 defined('MOODLE_INTERNAL') || die();
29 use mod_forum\local\entities\forum as forum_entity;
30 use mod_forum\local\factories\legacy_data_mapper as legacy_data_mapper_factory;
31 use mod_forum\local\factories\exporter as exporter_factory;
32 use mod_forum\local\factories\vault as vault_factory;
33 use mod_forum\local\factories\url as url_factory;
34 use mod_forum\local\managers\capability as capability_manager;
35 use mod_forum\local\vaults\discussion_list as discussion_list_vault;
38 use core\output\notification;
39 use mod_forum\local\factories\builder as builder_factory;
41 require_once($CFG->dirroot . '/mod/forum/lib.php');
44 * The discussion list renderer.
47 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50 class discussion_list {
51 /** @var forum_entity The forum being rendered */
54 /** @var stdClass The DB record for the forum being rendered */
57 /** @var renderer_base The renderer used to render the view */
60 /** @var legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory */
61 private $legacydatamapperfactory;
63 /** @var exporter_factory $exporterfactory Exporter factory */
64 private $exporterfactory;
66 /** @var vault_factory $vaultfactory Vault factory */
67 private $vaultfactory;
69 /** @var capability_manager $capabilitymanager Capability manager */
70 private $capabilitymanager;
72 /** @var url_factory $urlfactory URL factory */
75 /** @var array $notifications List of notification HTML */
76 private $notifications;
78 /** @var builder_factory $builderfactory Builder factory */
79 private $builderfactory;
81 /** @var callable $postprocessfortemplate Function to process exported posts before template rendering */
82 private $postprocessfortemplate;
84 /** @var string $template The template to use when displaying */
88 * Constructor for a new discussion list renderer.
90 * @param forum_entity $forum The forum entity to be rendered
91 * @param renderer_base $renderer The renderer used to render the view
92 * @param legacy_data_mapper_factory $legacydatamapperfactory The factory used to fetch a legacy record
93 * @param exporter_factory $exporterfactory The factory used to fetch exporter instances
94 * @param vault_factory $vaultfactory The factory used to fetch the vault instances
95 * @param builder_factory $builderfactory The factory used to fetch the builder instances
96 * @param capability_manager $capabilitymanager The managed used to check capabilities on the forum
97 * @param url_factory $urlfactory The factory used to create URLs in the forum
98 * @param string $template
99 * @param notification[] $notifications A list of any notifications to be displayed within the page
100 * @param callable|null $postprocessfortemplate Callback function to process discussion lists for templates
102 public function __construct(
104 renderer_base $renderer,
105 legacy_data_mapper_factory $legacydatamapperfactory,
106 exporter_factory $exporterfactory,
107 vault_factory $vaultfactory,
108 builder_factory $builderfactory,
109 capability_manager $capabilitymanager,
110 url_factory $urlfactory,
112 array $notifications = [],
113 callable $postprocessfortemplate = null
115 $this->forum = $forum;
116 $this->renderer = $renderer;
117 $this->legacydatamapperfactory = $legacydatamapperfactory;
118 $this->exporterfactory = $exporterfactory;
119 $this->vaultfactory = $vaultfactory;
120 $this->builderfactory = $builderfactory;
121 $this->capabilitymanager = $capabilitymanager;
123 $this->urlfactory = $urlfactory;
124 $this->notifications = $notifications;
125 $this->postprocessfortemplate = $postprocessfortemplate;
126 $this->template = $template;
128 $forumdatamapper = $this->legacydatamapperfactory->get_forum_data_mapper();
129 $this->forumrecord = $forumdatamapper->to_legacy_object($forum);
133 * Render for the specified user.
135 * @param stdClass $user The user to render for
136 * @param cm_info $cm The course module info for this discussion list
137 * @param int $groupid The group to render
138 * @param int $sortorder The sort order to use when selecting the discussions in the list
139 * @param int $pageno The zero-indexed page number to use
140 * @param int $pagesize The number of discussions to show on the page
141 * @return string The rendered content for display
143 public function render(stdClass $user, \cm_info $cm, ?int $groupid, ?int $sortorder, ?int $pageno, ?int $pagesize) : string {
146 $forum = $this->forum;
148 $pagesize = $this->get_page_size($pagesize);
149 $pageno = $this->get_page_number($pageno);
151 $groupids = $this->get_groups_from_groupid($user, $groupid);
152 $forumexporter = $this->exporterfactory->get_forum_exporter(
158 // Count all forum discussion posts.
159 $alldiscussionscount = $this->get_count_all_discussions($user, $groupids);
161 // Get all forum discussions posts.
162 $discussions = $this->get_discussions($user, $groupids, $sortorder, $pageno, $pagesize);
165 'forum' => (array) $forumexporter->export($this->renderer),
166 'newdiscussionhtml' => $this->get_discussion_form($user, $cm, $groupid),
167 'groupchangemenu' => groups_print_activity_menu(
169 $this->urlfactory->get_forum_view_url_from_forum($forum),
172 'hasmore' => ($alldiscussionscount > $pagesize),
173 'notifications' => $this->get_notifications($user, $groupid),
177 return $this->renderer->render_from_template($this->template, $forumview);
180 if ($this->postprocessfortemplate !== null) {
181 // We've got some post processing to do!
182 $exportedposts = ($this->postprocessfortemplate) ($discussions, $user, $forum);
185 $forumview = array_merge(
188 'pagination' => $this->renderer->render(new \paging_bar($alldiscussionscount, $pageno, $pagesize, $PAGE->url, 'p')),
193 return $this->renderer->render_from_template($this->template, $forumview);
197 * Get the mod_forum_post_form. This is the default boiler plate from mod_forum/post_form.php with the inpage flag caveat
199 * @param stdClass $user The user the form is being generated for
200 * @param \cm_info $cm
201 * @param int $groupid The groupid if any
203 * @return string The rendered html
205 private function get_discussion_form(stdClass $user, \cm_info $cm, ?int $groupid) {
208 $forum = $this->forum;
209 $forumrecord = $this->legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($forum);
210 $modcontext = \context_module::instance($cm->id);
211 $coursecontext = \context_course::instance($forum->get_course_id());
213 'course' => $forum->get_course_id(),
214 'forum' => $forum->get_id(),
215 'discussion' => 0, // Ie discussion # not defined yet.
218 'userid' => $user->id,
220 'messageformat' => editors_get_preferred_format(),
222 'groupid' => $groupid,
224 $thresholdwarning = forum_check_throttling($forum, $cm);
226 $mformpost = new \mod_forum_post_form('post.php', array('course' => $forum->get_course_record(),
228 'coursecontext' => $coursecontext,
229 'modcontext' => $modcontext,
230 'forum' => $forumrecord,
232 'subscribe' => \mod_forum\subscriptions::is_subscribed($user->id, $forumrecord,
234 'thresholdwarning' => $thresholdwarning,
235 'inpagereply' => true,
236 'edit' => 0), 'post', '', array('id' => 'mformforum'));
238 $params = array('reply' => 0, 'forum' => $forumrecord->id, 'edit' => 0) +
239 (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
241 'userid' => $post->userid,
242 'parent' => $post->parent,
243 'discussion' => $post->discussion,
244 'course' => $forum->get_course_id()
246 $mformpost->set_data($params);
248 return $mformpost->render();
252 * Get the list of groups to show based on the current user and requested groupid.
254 * @param stdClass $user The user viewing
255 * @param int $groupid The groupid requested
256 * @return array The list of groups to show
258 private function get_groups_from_groupid(stdClass $user, ?int $groupid) : ?array {
259 $forum = $this->forum;
260 $effectivegroupmode = $forum->get_effective_group_mode();
261 if (empty($effectivegroupmode)) {
262 // This forum is not in a group mode. Show all posts always.
266 if (null == $groupid) {
267 // No group was specified.
268 $showallgroups = (VISIBLEGROUPS == $effectivegroupmode);
269 $showallgroups = $showallgroups || $this->capabilitymanager->can_access_all_groups($user);
270 if ($showallgroups) {
271 // Return null to show all groups.
274 // No group was specified. Only show the users current groups.
276 groups_get_all_groups(
277 $forum->get_course_id(),
279 $forum->get_course_module_record()->groupingid
284 // A group was specified. Just show that group.
290 * Fetch the data used to display the discussions on the current page.
292 * @param stdClass $user The user to render for
293 * @param int[]|null $groupids The group ids for this list of discussions
294 * @param int|null $sortorder The sort order to use when selecting the discussions in the list
295 * @param int|null $pageno The zero-indexed page number to use
296 * @param int|null $pagesize The number of discussions to show on the page
297 * @return stdClass The data to use for display
299 private function get_discussions(stdClass $user, ?array $groupids, ?int $sortorder, ?int $pageno, ?int $pagesize) {
300 $forum = $this->forum;
301 $discussionvault = $this->vaultfactory->get_discussions_in_forum_vault();
302 if (null === $groupids) {
303 return $discussions = $discussionvault->get_from_forum_id(
305 $this->capabilitymanager->can_view_hidden_posts($user),
308 $this->get_page_size($pagesize),
309 $this->get_page_number($pageno) * $this->get_page_size($pagesize));
311 return $discussions = $discussionvault->get_from_forum_id_and_group_id(
314 $this->capabilitymanager->can_view_hidden_posts($user),
317 $this->get_page_size($pagesize),
318 $this->get_page_number($pageno) * $this->get_page_size($pagesize));
323 * Get a count of all discussions in a forum.
325 * @param stdClass $user The user to render for
326 * @param array $groupids The array of groups to render
327 * @return int The number of discussions in a forum
329 public function get_count_all_discussions(stdClass $user, ?array $groupids) {
330 $discussionvault = $this->vaultfactory->get_discussions_in_forum_vault();
331 if (null === $groupids) {
332 return $discussionvault->get_total_discussion_count_from_forum_id(
333 $this->forum->get_id(),
334 $this->capabilitymanager->can_view_hidden_posts($user),
337 return $discussionvault->get_total_discussion_count_from_forum_id_and_group_id(
338 $this->forum->get_id(),
340 $this->capabilitymanager->can_view_hidden_posts($user),
346 * Fetch the page size to use when displaying the page.
348 * @param int $pagesize The number of discussions to show on the page
349 * @return int The normalised page size
351 private function get_page_size(?int $pagesize) : int {
352 if (null === $pagesize || $pagesize <= 0) {
353 $pagesize = discussion_list_vault::PAGESIZE_DEFAULT;
360 * Fetch the current page number (zero-indexed).
362 * @param int $pageno The zero-indexed page number to use
363 * @return int The normalised page number
365 private function get_page_number(?int $pageno) : int {
366 if (null === $pageno || $pageno < 0) {
374 * Get the list of notification for display.
376 * @param stdClass $user The viewing user
377 * @param int|null $groupid The forum's group id
380 private function get_notifications(stdClass $user, ?int $groupid) : array {
381 $notifications = $this->notifications;
382 $forum = $this->forum;
383 $renderer = $this->renderer;
384 $capabilitymanager = $this->capabilitymanager;
386 if ($forum->is_cutoff_date_reached()) {
387 $notifications[] = (new notification(
388 get_string('cutoffdatereached', 'forum'),
389 notification::NOTIFY_INFO
390 ))->set_show_closebutton();
391 } else if ($forum->is_due_date_reached()) {
392 $notifications[] = (new notification(
393 get_string('thisforumisdue', 'forum', userdate($forum->get_due_date())),
394 notification::NOTIFY_INFO
395 ))->set_show_closebutton();
396 } else if ($forum->has_due_date()) {
397 $notifications[] = (new notification(
398 get_string('thisforumhasduedate', 'forum', userdate($forum->get_due_date())),
399 notification::NOTIFY_INFO
400 ))->set_show_closebutton();
403 if ($forum->has_blocking_enabled()) {
404 $notifications[] = (new notification(
405 get_string('thisforumisthrottled', 'forum', [
406 'blockafter' => $forum->get_block_after(),
407 'blockperiod' => get_string('secondstotime' . $forum->get_block_period())
409 ))->set_show_closebutton();
412 if ($forum->is_in_group_mode() && !$capabilitymanager->can_access_all_groups($user)) {
413 if ($groupid === null) {
414 if (!$capabilitymanager->can_post_to_my_groups($user)) {
415 $notifications[] = (new notification(
416 get_string('cannotadddiscussiongroup', 'mod_forum'),
417 \core\output\notification::NOTIFY_WARNING
418 ))->set_show_closebutton();
420 $notifications[] = (new notification(
421 get_string('cannotadddiscussionall', 'mod_forum'),
422 \core\output\notification::NOTIFY_WARNING
423 ))->set_show_closebutton();
425 } else if (!$capabilitymanager->can_access_group($user, $groupid)) {
426 $notifications[] = (new notification(
427 get_string('cannotadddiscussion', 'mod_forum'),
428 \core\output\notification::NOTIFY_WARNING
429 ))->set_show_closebutton();
433 if ('qanda' === $forum->get_type() && !$capabilitymanager->can_manage_forum($user)) {
434 $notifications[] = (new notification(
435 get_string('qandanotify', 'forum'),
436 notification::NOTIFY_INFO
437 ))->set_show_closebutton();
440 if ('eachuser' === $forum->get_type()) {
441 $notifications[] = (new notification(
442 get_string('allowsdiscussions', 'forum'),
443 notification::NOTIFY_INFO)
444 )->set_show_closebutton();
447 return array_map(function($notification) {
448 return $notification->export_for_template($this->renderer);