MDL-66481 mod_forum: Implement additional column sorting
[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                     'sortdiscussionasc' => ['type' => PARAM_URL],
93                     'sortdiscussiondesc' => ['type' => PARAM_URL],
94                     'sortstarterasc' => ['type' => PARAM_URL],
95                     'sortstarterdesc' => ['type' => PARAM_URL],
96                     'sortgroupasc' => ['type' => PARAM_URL],
97                     'sortgroupdesc' => ['type' => PARAM_URL],
98                 ],
99             ],
100         ];
101     }
103     /**
104      * Get the additional values to inject while exporting.
105      *
106      * @param renderer_base $output The renderer.
107      * @return array Keys are the property names, values are their values.
108      */
109     protected function get_other_values(renderer_base $output) {
110         $capabilitymanager = $this->related['capabilitymanager'];
111         $urlfactory = $this->related['urlfactory'];
112         $user = $this->related['user'];
113         $currentgroup = $this->related['currentgroup'];
114         $vaultfactory = $this->related['vaultfactory'];
115         $discussionvault = $vaultfactory->get_discussions_in_forum_vault();
117         return [
118             'id' => $this->forum->get_id(),
119             'state' => [
120                 'groupmode' => $this->forum->get_effective_group_mode(),
121             ],
122             'userstate' => [
123                 'tracked' => forum_tp_is_tracked($this->get_forum_record(), $this->related['user']),
124             ],
125             'capabilities' => [
126                 'viewdiscussions' => $capabilitymanager->can_view_discussions($user),
127                 'create' => $capabilitymanager->can_create_discussions($user, $currentgroup),
128                 'selfenrol' => $capabilitymanager->can_self_enrol($user),
129                 'subscribe' => $capabilitymanager->can_subscribe_to_forum($user),
130             ],
131             'urls' => [
132                 'create' => $urlfactory->get_discussion_create_url($this->forum)->out(false),
133                 'markasread' => $urlfactory->get_mark_all_discussions_as_read_url($this->forum)->out(false),
134                 'view' => $urlfactory->get_forum_view_url_from_forum($this->forum)->out(false),
135                 'sortrepliesasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
136                     $discussionvault::SORTORDER_REPLIES_ASC)->out(false),
137                 'sortrepliesdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
138                     $discussionvault::SORTORDER_REPLIES_DESC)->out(false),
139                 'sortlastpostasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
140                     $discussionvault::SORTORDER_LASTPOST_ASC)->out(false),
141                 'sortlastpostdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
142                     $discussionvault::SORTORDER_LASTPOST_DESC)->out(false),
143                 'sortcreatedasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
144                     $discussionvault::SORTORDER_CREATED_ASC)->out(false),
145                 'sortcreateddesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
146                     $discussionvault::SORTORDER_CREATED_DESC)->out(false),
147                 'sortdiscussionasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
148                     $discussionvault::SORTORDER_DISCUSSION_ASC)->out(false),
149                 'sortdiscussiondesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
150                     $discussionvault::SORTORDER_DISCUSSION_DESC)->out(false),
151                 'sortstarterasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
152                     $discussionvault::SORTORDER_STARTER_ASC)->out(false),
153                 'sortstarterdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
154                     $discussionvault::SORTORDER_STARTER_DESC)->out(false),
155                 'sortgroupasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
156                     $discussionvault::SORTORDER_GROUP_ASC)->out(false),
157                 'sortgroupdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
158                     $discussionvault::SORTORDER_GROUP_DESC)->out(false),
159             ],
160         ];
161     }
163     /**
164      * Returns a list of objects that are related.
165      *
166      * @return array
167      */
168     protected static function define_related() {
169         return [
170             'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
171             'capabilitymanager' => 'mod_forum\local\managers\capability',
172             'urlfactory' => 'mod_forum\local\factories\url',
173             'user' => 'stdClass',
174             'currentgroup' => 'int?',
175             'vaultfactory' => 'mod_forum\local\factories\vault'
176         ];
177     }
179     /**
180      * Get the legacy forum record for this forum.
181      *
182      * @return  stdClass
183      */
184     private function get_forum_record() : stdClass {
185         $forumdbdatamapper = $this->related['legacydatamapperfactory']->get_forum_data_mapper();
186         return $forumdbdatamapper->to_legacy_object($this->forum);
187     }