Commit | Line | Data |
---|---|---|
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 | ||
25 | namespace mod_forum\local\exporters; | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | use mod_forum\local\entities\discussion as discussion_entity; | |
30 | use mod_forum\local\exporters\post as post_exporter; | |
31 | use mod_forum\local\factories\exporter as exporter_factory; | |
32 | use core\external\exporter; | |
33 | use 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 | */ | |
41 | class 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], | |
118 | ], | |
119 | ] | |
120 | ]; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Get the additional values to inject while exporting. | |
125 | * | |
126 | * @param renderer_base $output The renderer. | |
127 | * @return array Keys are the property names, values are their values. | |
128 | */ | |
129 | protected function get_other_values(renderer_base $output) { | |
130 | $capabilitymanager = $this->related['capabilitymanager']; | |
131 | $urlfactory = $this->related['urlfactory']; | |
132 | ||
133 | $forum = $this->related['forum']; | |
134 | $forumrecord = $this->get_forum_record(); | |
135 | $user = $this->related['user']; | |
136 | $discussion = $this->discussion; | |
137 | ||
138 | $groupdata = null; | |
139 | if ($discussion->has_group() && $group = $this->related['groupsbyid'][$discussion->get_group_id()]) { | |
140 | $groupdata = [ | |
141 | 'name' => $group->name, | |
142 | 'urls' => [], | |
143 | ]; | |
144 | $canviewparticipants = $capabilitymanager->can_view_participants($user, $discussion); | |
145 | if (!$group->hidepicture) { | |
146 | $url = get_group_picture_url($group, $forum->get_course_id()); | |
147 | if (!empty($url)) { | |
148 | $groupdata['urls']['picture'] = $url; | |
149 | } | |
150 | } | |
151 | if ($canviewparticipants) { | |
152 | $groupdata['urls']['userlist'] = (new \moodle_url('/user/index.php', [ | |
153 | 'id' => $forum->get_course_id(), | |
154 | 'group' => $group->id, | |
155 | ])); | |
156 | } | |
157 | } | |
158 | ||
159 | $viewfirstunreadurl = $urlfactory->get_discussion_view_first_unread_post_url_from_discussion($discussion); | |
160 | $data = [ | |
161 | 'id' => $discussion->get_id(), | |
162 | 'forumid' => $forum->get_id(), | |
163 | 'pinned' => $discussion->is_pinned(), | |
164 | 'name' => format_string($discussion->get_name(), true, [ | |
165 | 'context' => $this->related['context'] | |
166 | ]), | |
167 | 'times' => [ | |
168 | 'modified' => $discussion->get_time_modified(), | |
169 | 'start' => $discussion->get_time_start(), | |
170 | 'end' => $discussion->get_time_end(), | |
171 | ], | |
172 | 'userstate' => [ | |
173 | 'subscribed' => \mod_forum\subscriptions::is_subscribed($user->id, $forumrecord, $discussion->get_id()), | |
174 | ], | |
175 | 'capabilities' => [ | |
176 | 'subscribe' => $capabilitymanager->can_subscribe_to_discussion($user, $discussion), | |
177 | 'move' => $capabilitymanager->can_move_discussion($user, $discussion), | |
178 | 'pin' => $capabilitymanager->can_pin_discussion($user, $discussion), | |
179 | 'post' => $capabilitymanager->can_post_in_discussion($user, $discussion) | |
180 | ], | |
181 | 'urls' => [ | |
182 | 'view' => $urlfactory->get_discussion_view_url_from_discussion($discussion)->out(false), | |
183 | 'viewfirstunread' => $viewfirstunreadurl->out(false), | |
184 | 'markasread' => $urlfactory->get_mark_discussion_as_read_url_from_discussion($forum, $discussion)->out(false), | |
185 | ] | |
186 | ]; | |
187 | ||
188 | if (!empty($this->related['latestpostid'])) { | |
189 | $data['urls']['viewlatest'] = $urlfactory->get_discussion_view_latest_post_url_from_discussion( | |
190 | $discussion, | |
191 | $this->related['latestpostid'] | |
192 | )->out(false); | |
193 | } | |
194 | ||
195 | if ($groupdata) { | |
196 | $data['group'] = $groupdata; | |
197 | } | |
198 | ||
199 | return $data; | |
200 | } | |
201 | ||
202 | /** | |
203 | * Get the legacy forum record from the forum entity. | |
204 | * | |
205 | * @return stdClass | |
206 | */ | |
207 | private function get_forum_record() { | |
208 | $forumdbdatamapper = $this->related['legacydatamapperfactory']->get_forum_data_mapper(); | |
209 | return $forumdbdatamapper->to_legacy_object($this->related['forum']); | |
210 | } | |
211 | ||
212 | /** | |
213 | * Returns a list of objects that are related. | |
214 | * | |
215 | * @return array | |
216 | */ | |
217 | protected static function define_related() { | |
218 | return [ | |
219 | 'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper', | |
220 | 'context' => 'context', | |
221 | 'forum' => 'mod_forum\local\entities\forum', | |
222 | 'capabilitymanager' => 'mod_forum\local\managers\capability', | |
223 | 'urlfactory' => 'mod_forum\local\factories\url', | |
224 | 'user' => 'stdClass', | |
225 | 'groupsbyid' => 'stdClass[]', | |
226 | 'latestpostid' => 'int?' | |
227 | ]; | |
228 | } | |
229 | } |