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 * Posts 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\builders\exported_posts as exported_posts_builder;
30use renderer_base;
31use stdClass;
32
33/**
34 * Posts renderer class.
35 *
36 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
37 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class posts {
40    /** @var renderer_base $renderer Renderer base */
41    private $renderer;
42    /** @var exported_posts_builder $exportedpostsbuilder Builder for building exported posts */
43    private $exportedpostsbuilder;
44    /** @var string $template The template to render */
45    private $template;
46    /** @var callable $postprocessfortemplate Function to process exported posts before template rendering */
47    private $postprocessfortemplate;
48
49    /**
50     * Constructor.
51     *
52     * @param renderer_base $renderer Renderer base
53     * @param exported_posts_builder $exportedpostsbuilder Builder for building exported posts
54     * @param string $template The template to render
55     * @param callable $postprocessfortemplate Function to process exported posts before template rendering
56     */
57    public function __construct(
58        renderer_base $renderer,
59        exported_posts_builder $exportedpostsbuilder,
60        string $template,
61        callable $postprocessfortemplate = null
62    ) {
63        $this->renderer = $renderer;
64        $this->exportedpostsbuilder = $exportedpostsbuilder;
65        $this->template = $template;
66        $this->postprocessfortemplate = $postprocessfortemplate;
67    }
68
69    /**
70     * Render the given posts for the forums and discussions.
71     *
72     * @param stdClass $user The user viewing the posts
73     * @param forum_entity[] $forums A list of all forums for these posts
74     * @param discussion_entity[] $discussions A list of all discussions for these posts
75     * @param post_entity[] $posts The posts to render
76     * @return string
77     */
78    public function render(
79        stdClass $user,
80        array $forums,
81        array $discussions,
82        array $posts
83    ) : string {
84        // Format the forums and discussion to make them more easily accessed later.
85        $forums = array_reduce($forums, function($carry, $forum) {
86            $carry[$forum->get_id()] = $forum;
87            return $carry;
88        }, []);
89        $discussions = array_reduce($discussions, function($carry, $discussion) {
90            $carry[$discussion->get_id()] = $discussion;
91            return $carry;
92        }, []);
93
94        $exportedposts = $this->exportedpostsbuilder->build(
95            $user,
96            $forums,
97            $discussions,
98            $posts
99        );
100
101        if ($this->postprocessfortemplate !== null) {
102            // We've got some post processing to do!
103            $exportedposts = ($this->postprocessfortemplate)($exportedposts, $forums, $discussions, $user);
104        }
105
106        return $this->renderer->render_from_template(
107            $this->template,
108            ['posts' => array_values($exportedposts)]
109        );
110    }
111}
112