MDL-64820 forum: add missing classes and tweaks to templates
[moodle.git] / mod / forum / classes / local / exporters / 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 exporter class.
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\exporters;
26
27defined('MOODLE_INTERNAL') || die();
28
29use mod_forum\local\entities\discussion as discussion_entity;
30use mod_forum\local\exporters\post as post_exporter;
31use mod_forum\local\factories\exporter as exporter_factory;
32use core\external\exporter;
33use renderer_base;
34
35/**
36 * Discussion exporter class.
37 *
38 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class discussion extends exporter {
42 /** @var discussion_entity $discussion Discussion to export */
43 private $discussion;
44
45 /**
46 * Constructor.
47 *
48 * @param discussion_entity $discussion Discussion to export
49 * @param array $related The related export data
50 */
51 public function __construct(discussion_entity $discussion, array $related = []) {
52 $this->discussion = $discussion;
53
54 return parent::__construct([], $related);
55 }
56
57 /**
58 * Return the list of additional properties.
59 *
60 * @return array
61 */
62 protected static function define_other_properties() {
63 return [
64 'id' => ['type' => PARAM_INT],
65 'forumid' => ['type' => PARAM_INT],
66 'pinned' => ['type' => PARAM_BOOL],
67 'name' => ['type' => PARAM_TEXT],
68 'group' => [
69 'optional' => true,
70 'type' => [
71 'name' => ['type' => PARAM_TEXT],
72 'urls' => [
73 'type' => [
74 'picture' => [
75 'optional' => true,
76 'type' => PARAM_URL,
77 ],
78 'userlist' => [
79 'optional' => true,
80 'type' => PARAM_URL,
81 ],
82 ],
83 ],
84 ],
85 ],
86 'times' => [
87 'type' => [
88 'modified' => ['type' => PARAM_INT],
89 'start' => ['type' => PARAM_INT],
90 'end' => ['type' => PARAM_INT],
91 ],
92 ],
93 'userstate' => [
94 'type' => [
95 'subscribed' => ['type' => PARAM_BOOL],
96 ],
97 ],
98 'capabilities' => [
99 'type' => [
100 'subscribe' => ['type' => PARAM_BOOL],
101 'move' => ['type' => PARAM_BOOL],
102 'pin' => ['type' => PARAM_BOOL],
103 'post' => ['type' => PARAM_BOOL]
104 ]
105 ],
106 'urls' => [
107 'type' => [
108 'view' => ['type' => PARAM_URL],
109 'viewlatest' => [
110 'optional' => true,
111 'type' => PARAM_URL
112 ],
113 'viewfirstunread' => [
114 'optional' => true,
115 'type' => PARAM_URL,
116 ],
117 'markasread' => ['type' => PARAM_URL],
b4d47dec 118 'subscribe' => ['type' => PARAM_URL]
47d38303
RW
119 ],
120 ]
121 ];
122 }
123
124 /**
125 * Get the additional values to inject while exporting.
126 *
127 * @param renderer_base $output The renderer.
128 * @return array Keys are the property names, values are their values.
129 */
130 protected function get_other_values(renderer_base $output) {
131 $capabilitymanager = $this->related['capabilitymanager'];
132 $urlfactory = $this->related['urlfactory'];
133
134 $forum = $this->related['forum'];
135 $forumrecord = $this->get_forum_record();
136 $user = $this->related['user'];
137 $discussion = $this->discussion;
138
139 $groupdata = null;
140 if ($discussion->has_group() && $group = $this->related['groupsbyid'][$discussion->get_group_id()]) {
141 $groupdata = [
142 'name' => $group->name,
143 'urls' => [],
144 ];
145 $canviewparticipants = $capabilitymanager->can_view_participants($user, $discussion);
146 if (!$group->hidepicture) {
147 $url = get_group_picture_url($group, $forum->get_course_id());
148 if (!empty($url)) {
149 $groupdata['urls']['picture'] = $url;
150 }
151 }
152 if ($canviewparticipants) {
153 $groupdata['urls']['userlist'] = (new \moodle_url('/user/index.php', [
154 'id' => $forum->get_course_id(),
155 'group' => $group->id,
156 ]));
157 }
158 }
159
160 $viewfirstunreadurl = $urlfactory->get_discussion_view_first_unread_post_url_from_discussion($discussion);
161 $data = [
162 'id' => $discussion->get_id(),
163 'forumid' => $forum->get_id(),
164 'pinned' => $discussion->is_pinned(),
165 'name' => format_string($discussion->get_name(), true, [
166 'context' => $this->related['context']
167 ]),
168 'times' => [
169 'modified' => $discussion->get_time_modified(),
170 'start' => $discussion->get_time_start(),
171 'end' => $discussion->get_time_end(),
172 ],
173 'userstate' => [
174 'subscribed' => \mod_forum\subscriptions::is_subscribed($user->id, $forumrecord, $discussion->get_id()),
175 ],
176 'capabilities' => [
177 'subscribe' => $capabilitymanager->can_subscribe_to_discussion($user, $discussion),
178 'move' => $capabilitymanager->can_move_discussion($user, $discussion),
179 'pin' => $capabilitymanager->can_pin_discussion($user, $discussion),
180 'post' => $capabilitymanager->can_post_in_discussion($user, $discussion)
181 ],
182 'urls' => [
183 'view' => $urlfactory->get_discussion_view_url_from_discussion($discussion)->out(false),
184 'viewfirstunread' => $viewfirstunreadurl->out(false),
185 'markasread' => $urlfactory->get_mark_discussion_as_read_url_from_discussion($forum, $discussion)->out(false),
b4d47dec 186 'subscribe' => $urlfactory->get_discussion_subscribe_url($discussion)->out(false)
47d38303
RW
187 ]
188 ];
189
190 if (!empty($this->related['latestpostid'])) {
191 $data['urls']['viewlatest'] = $urlfactory->get_discussion_view_latest_post_url_from_discussion(
192 $discussion,
193 $this->related['latestpostid']
194 )->out(false);
195 }
196
197 if ($groupdata) {
198 $data['group'] = $groupdata;
199 }
200
201 return $data;
202 }
203
204 /**
205 * Get the legacy forum record from the forum entity.
206 *
207 * @return stdClass
208 */
209 private function get_forum_record() {
210 $forumdbdatamapper = $this->related['legacydatamapperfactory']->get_forum_data_mapper();
211 return $forumdbdatamapper->to_legacy_object($this->related['forum']);
212 }
213
214 /**
215 * Returns a list of objects that are related.
216 *
217 * @return array
218 */
219 protected static function define_related() {
220 return [
221 'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
222 'context' => 'context',
223 'forum' => 'mod_forum\local\entities\forum',
224 'capabilitymanager' => 'mod_forum\local\managers\capability',
225 'urlfactory' => 'mod_forum\local\factories\url',
226 'user' => 'stdClass',
227 'groupsbyid' => 'stdClass[]',
228 'latestpostid' => 'int?'
229 ];
230 }
231}