ad396159228855594ef98fbb3a2edecc562e6878
[moodle.git] / mod / forum / classes / local / exporters / forum.php
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/>.
17 /**
18  * Forum Exporter.
19  *
20  * @package     mod_forum
21  * @copyright   2019 Andrew Nicols <andrew@nicols.co.uk>
22  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 namespace mod_forum\local\exporters;
27 defined('MOODLE_INTERNAL') || die();
29 use mod_forum\local\entities\forum as forum_entity;
30 use mod_forum\local\exporters\post as post_exporter;
31 use core\external\exporter;
32 use renderer_base;
33 use stdClass;
35 /**
36  * Forum class.
37  *
38  * @copyright   2019 Andrew Nicols <andrew@nicols.co.uk>
39  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class forum extends exporter {
42     /** @var forum_entity The entity relating to the forum being displayed */
43     private $forum;
45     /**
46      * Constructor for the forum exporter.
47      *
48      * @param   forum_entity    $forum The forum being displayed
49      * @param   array           $related The related objects
50      */
51     public function __construct(forum_entity $forum, $related = []) {
52         $this->forum = $forum;
53         return parent::__construct([], $related);
54     }
56     /**
57      * Return the list of additional properties.
58      *
59      * @return array
60      */
61     protected static function define_other_properties() {
62         return [
63             'id' => ['type' => PARAM_INT],
64             'state' => [
65                 'type' => [
66                     'groupmode' => ['type' => PARAM_INT],
67                 ],
68             ],
69             'userstate' => [
70                 'type' => [
71                     'tracked' => ['type' => PARAM_INT],
72                 ],
73             ],
74             'capabilities' => [
75                 'type' => [
76                     'viewdiscussions' => ['type' => PARAM_BOOL],
77                     'create' => ['type' => PARAM_BOOL],
78                     'subscribe' => ['type' => PARAM_BOOL],
79                 ]
80             ],
81             'urls' => [
82                 'type' => [
83                     'create' => ['type' => PARAM_URL],
84                     'markasread' => ['type' => PARAM_URL],
85                     'view' => ['type' => PARAM_URL],
86                     'sortrepliesasc' => ['type' => PARAM_URL],
87                     'sortrepliesdesc' => ['type' => PARAM_URL],
88                     'sortlastpostasc' => ['type' => PARAM_URL],
89                     'sortlastpostdesc' => ['type' => PARAM_URL],
90                     'sortcreatedasc' => ['type' => PARAM_URL],
91                     'sortcreateddesc' => ['type' => PARAM_URL],
92                 ],
93             ],
94         ];
95     }
97     /**
98      * Get the additional values to inject while exporting.
99      *
100      * @param renderer_base $output The renderer.
101      * @return array Keys are the property names, values are their values.
102      */
103     protected function get_other_values(renderer_base $output) {
104         $capabilitymanager = $this->related['capabilitymanager'];
105         $urlfactory = $this->related['urlfactory'];
106         $user = $this->related['user'];
107         $currentgroup = $this->related['currentgroup'];
108         $vaultfactory = $this->related['vaultfactory'];
109         $discussionvault = $vaultfactory->get_discussions_in_forum_vault();
111         return [
112             'id' => $this->forum->get_id(),
113             'state' => [
114                 'groupmode' => $this->forum->get_effective_group_mode(),
115             ],
116             'userstate' => [
117                 'tracked' => forum_tp_is_tracked($this->get_forum_record(), $this->related['user']),
118             ],
119             'capabilities' => [
120                 'viewdiscussions' => $capabilitymanager->can_view_discussions($user),
121                 'create' => $capabilitymanager->can_create_discussions($user, $currentgroup),
122                 'selfenrol' => $capabilitymanager->can_self_enrol($user),
123                 'subscribe' => $capabilitymanager->can_subscribe_to_forum($user),
124             ],
125             'urls' => [
126                 'create' => $urlfactory->get_discussion_create_url($this->forum)->out(false),
127                 'markasread' => $urlfactory->get_mark_all_discussions_as_read_url($this->forum)->out(false),
128                 'view' => $urlfactory->get_forum_view_url_from_forum($this->forum)->out(false),
129                 'sortrepliesasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
130                     $discussionvault::SORTORDER_REPLIES_ASC)->out(false),
131                 'sortrepliesdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
132                     $discussionvault::SORTORDER_REPLIES_DESC)->out(false),
133                 'sortlastpostasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
134                     $discussionvault::SORTORDER_LASTPOST_ASC)->out(false),
135                 'sortlastpostdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
136                     $discussionvault::SORTORDER_LASTPOST_DESC)->out(false),
137                 'sortcreatedasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
138                     $discussionvault::SORTORDER_CREATED_ASC)->out(false),
139                 'sortcreateddesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
140                     $discussionvault::SORTORDER_CREATED_DESC)->out(false)
141             ],
142         ];
143     }
145     /**
146      * Returns a list of objects that are related.
147      *
148      * @return array
149      */
150     protected static function define_related() {
151         return [
152             'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
153             'capabilitymanager' => 'mod_forum\local\managers\capability',
154             'urlfactory' => 'mod_forum\local\factories\url',
155             'user' => 'stdClass',
156             'currentgroup' => 'int?',
157             'vaultfactory' => 'mod_forum\local\factories\vault'
158         ];
159     }
161     /**
162      * Get the legacy forum record for this forum.
163      *
164      * @return  stdClass
165      */
166     private function get_forum_record() : stdClass {
167         $forumdbdatamapper = $this->related['legacydatamapperfactory']->get_forum_data_mapper();
168         return $forumdbdatamapper->to_legacy_object($this->forum);
169     }