MDL-65557 forum: increase default image size of author profile url
[moodle.git] / mod / forum / classes / local / factories / url.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 * A URL factory for the forum.
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 */
24
25namespace mod_forum\local\factories;
26
27defined('MOODLE_INTERNAL') || die();
28
29use mod_forum\local\entities\author as author_entity;
30use mod_forum\local\entities\forum as forum_entity;
31use mod_forum\local\entities\discussion as discussion_entity;
32use mod_forum\local\entities\post as post_entity;
33use mod_forum\local\factories\legacy_data_mapper as legacy_data_mapper_factory;
34use moodle_url;
35use stored_file;
36use user_picture;
37
38require_once($CFG->dirroot . '/mod/forum/lib.php');
39
40/**
41 * A URL factory for the forum.
42 *
43 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class url {
47 /** @var legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory */
48 private $legacydatamapperfactory;
49
50 /**
51 * Constructor.
52 *
53 * @param legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory
54 */
55 public function __construct(legacy_data_mapper_factory $legacydatamapperfactory) {
56 $this->legacydatamapperfactory = $legacydatamapperfactory;
57 }
58
59 /**
60 * Get the course url from the given course id.
61 *
62 * @param int $courseid The course id
63 * @return moodle_url
64 */
65 public function get_course_url_from_courseid(int $courseid) : moodle_url {
66 return new moodle_url('/course/view.php', [
67 'id' => $courseid,
68 ]);
69 }
70
71 /**
72 * Get the course url from the given forum entity.
73 *
74 * @param forum_entity $forum The forum entity
75 * @return moodle_url
76 */
77 public function get_course_url_from_forum(forum_entity $forum) : moodle_url {
78 return $this->get_course_url_from_courseid($forum->get_course_id());
79 }
80
81 /**
82 * Get the create discussion url for the given forum.
83 *
84 * @param forum_entity $forum The forum entity
85 * @return moodle_url
86 */
87 public function get_discussion_create_url(forum_entity $forum) : moodle_url {
88 return new moodle_url('/mod/forum/post.php', [
89 'forum' => $forum->get_id(),
90 ]);
91 }
92
93 /**
94 * Get the view forum url for the given forum and optionally a page number.
95 *
96 * @param forum_entity $forum The forum entity
97 * @param int|null $pageno The page number
1a9c60e9 98 * @param int|null $sortorder The sorting order
47d38303
RW
99 * @return moodle_url
100 */
1a9c60e9
MG
101 public function get_forum_view_url_from_forum(forum_entity $forum, ?int $pageno = null,
102 ?int $sortorder = null) : moodle_url {
103
104 return $this->get_forum_view_url_from_course_module_id($forum->get_course_module_record()->id, $pageno, $sortorder);
47d38303
RW
105 }
106
107 /**
108 * Get the view forum url for the given course module id and optionally a page number.
109 *
110 * @param int $coursemoduleid The course module id
111 * @param int|null $pageno The page number
1a9c60e9 112 * @param int|null $sortorder The sorting order
47d38303
RW
113 * @return moodle_url
114 */
1a9c60e9
MG
115 public function get_forum_view_url_from_course_module_id(int $coursemoduleid, ?int $pageno = null,
116 ?int $sortorder = null) : moodle_url {
117
54d38a73 118 $url = new moodle_url('/mod/forum/view.php', [
47d38303
RW
119 'id' => $coursemoduleid,
120 ]);
121
122 if (null !== $pageno) {
1a9c60e9
MG
123 $url->param('p', $pageno);
124 }
125
126 if (null !== $sortorder) {
127 $url->param('o', $sortorder);
47d38303
RW
128 }
129
130 return $url;
131 }
132
133 /**
134 * Get the view discussion url from the given discussion id.
135 *
136 * @param int $discussionid The discussion id
137 * @return moodle_url
138 */
139 public function get_discussion_view_url_from_discussion_id(int $discussionid) : moodle_url {
140 return new moodle_url('/mod/forum/discuss.php', [
141 'd' => $discussionid
142 ]);
143 }
144
145 /**
146 * Get the view discussion url from the given discussion.
147 *
148 * @param discussion_entity $discussion The discussion
149 * @return moodle_url
150 */
151 public function get_discussion_view_url_from_discussion(discussion_entity $discussion) : moodle_url {
152 return $this->get_discussion_view_url_from_discussion_id($discussion->get_id());
153 }
154
155 /**
156 * Get the url to view the first unread post in a discussion.
157 *
158 * @param discussion_entity $discussion The discussion
159 * @return moodle_url
160 */
161 public function get_discussion_view_first_unread_post_url_from_discussion(discussion_entity $discussion) {
162 $viewurl = $this->get_discussion_view_url_from_discussion_id($discussion->get_id());
163 $viewurl->set_anchor('unread');
164
165 return $viewurl;
166 }
167
168 /**
169 * Get the url to view the latest post in a discussion.
170 *
171 * @param discussion_entity $discussion The discussion
172 * @param int|null $latestpost The id of the latest post
173 * @return moodle_url
174 */
175 public function get_discussion_view_latest_post_url_from_discussion(discussion_entity $discussion, ?int $latestpost) {
176 $viewurl = $this->get_discussion_view_url_from_discussion_id($discussion->get_id());
177 if (null === $latestpost) {
178 return $viewurl;
179 } else {
180 return new moodle_url($viewurl, ['parent' => $latestpost]);
181 }
182 }
183
184 /**
185 * Get the url to view a discussion from a post.
186 *
187 * @param post_entity $post The post
188 * @return moodle_url
189 */
190 public function get_discussion_view_url_from_post(post_entity $post) : moodle_url {
191 return $this->get_discussion_view_url_from_discussion_id($post->get_discussion_id());
192 }
193
194 /**
195 * Get the url to view a discussion from a discussion id and post id.
196 *
197 * @param int $discussionid The discussion id
198 * @param int $postid The post id
199 * @return moodle_url
200 */
201 public function get_view_post_url_from_post_id(int $discussionid, int $postid) : moodle_url {
202 $url = $this->get_discussion_view_url_from_discussion_id($discussionid);
203 $url->set_anchor('p' . $postid);
204 return $url;
205 }
206
207 /**
208 * Get the url to view a post in the context of the rest of the discussion.
209 *
210 * @param post_entity $post The post
211 * @return moodle_url
212 */
213 public function get_view_post_url_from_post(post_entity $post) : moodle_url {
214 return $this->get_view_post_url_from_post_id($post->get_discussion_id(), $post->get_id());
215 }
216
217 /**
218 * Get the url to view a post and it's replies in isolation without the rest of the
219 * discussion.
220 *
221 * @param int $discussionid The discussion id
222 * @param int $postid The post id
223 * @return moodle_url
224 */
225 public function get_view_isolated_post_url_from_post_id(int $discussionid, int $postid) : moodle_url {
226 $url = $this->get_discussion_view_url_from_discussion_id($discussionid);
227 $url->params(['parent' => $postid]);
228 return $url;
229 }
230
231 /**
232 * Get the url to view a post and it's replies in isolation without the rest of the
233 * discussion.
234 *
235 * @param post_entity $post The post
236 * @return moodle_url
237 */
238 public function get_view_isolated_post_url_from_post(post_entity $post) : moodle_url {
239 return $this->get_view_isolated_post_url_from_post_id($post->get_discussion_id(), $post->get_id());
240 }
241
242 /**
243 * Get the url to edit a post.
244 *
245 * @param forum_entity $forum The forum the post belongs to
246 * @param post_entity $post The post
247 * @return moodle_url
248 */
249 public function get_edit_post_url_from_post(forum_entity $forum, post_entity $post) : moodle_url {
250 if ($forum->get_type() == 'single') {
251 return new moodle_url('/course/modedit.php', [
252 'update' => $forum->get_course_module_record()->id,
253 'sesskey' => sesskey(),
254 'return' => 1
255 ]);
256 } else {
257 return new moodle_url('/mod/forum/post.php', [
258 'edit' => $post->get_id()
259 ]);
260 }
261 }
262
263 /**
264 * Get the url to split a discussion at a post.
265 *
266 * @param post_entity $post The post
267 * @return moodle_url
268 */
269 public function get_split_discussion_at_post_url_from_post(post_entity $post) : moodle_url {
270 return new moodle_url('/mod/forum/post.php', [
271 'prune' => $post->get_id()
272 ]);
273 }
274
275 /**
276 * Get the url to delete a post.
277 *
278 * @param post_entity $post The post
279 * @return moodle_url
280 */
281 public function get_delete_post_url_from_post(post_entity $post) : moodle_url {
282 return new moodle_url('/mod/forum/post.php', [
283 'delete' => $post->get_id()
284 ]);
285 }
286
287 /**
288 * Get the url to reply to a post.
289 *
290 * @param post_entity $post The post
291 * @return moodle_url
292 */
293 public function get_reply_to_post_url_from_post(post_entity $post) : moodle_url {
294 return new moodle_url('/mod/forum/post.php#mformforum', [
295 'reply' => $post->get_id()
296 ]);
297 }
298
299 /**
300 * Get the url to export (see portfolios) a post.
301 *
302 * @param post_entity $post The post
303 * @return moodle_url
304 */
305 public function get_export_post_url_from_post(post_entity $post) : ?moodle_url {
306 global $CFG;
307
308 require_once($CFG->libdir . '/portfoliolib.php');
309 $button = new \portfolio_add_button();
310 $button->set_callback_options('forum_portfolio_caller', ['postid' => $post->get_id()], 'mod_forum');
311 if ($post->has_attachments()) {
312 $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
313 } else {
314 $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
315 }
316
317 $url = $button->to_html(PORTFOLIO_ADD_MOODLE_URL);
318 return $url ?: null;
319 }
320
321 /**
322 * Get the url to mark a post as read.
323 *
324 * @param post_entity $post The post
325 * @param int $displaymode The display mode to show the forum in after marking as read
326 * @return moodle_url
327 */
328 public function get_mark_post_as_read_url_from_post(post_entity $post, int $displaymode = FORUM_MODE_THREADED) : moodle_url {
329 $params = [
330 'd' => $post->get_discussion_id(),
331 'postid' => $post->get_id(),
332 'mark' => 'read'
333 ];
334
335 $url = new moodle_url('/mod/forum/discuss.php', $params);
336
337 if ($displaymode == FORUM_MODE_THREADED) {
338 $url->param('parent', $post->get_parent_id());
339 } else {
340 $url->set_anchor('p' . $post->get_id());
341 }
342
343 return $url;
344 }
345
346 /**
347 * Get the url to mark a post as unread.
348 *
349 * @param post_entity $post The post
350 * @param int $displaymode The display mode to show the forum in after marking as unread
351 * @return moodle_url
352 */
353 public function get_mark_post_as_unread_url_from_post(post_entity $post, int $displaymode = FORUM_MODE_THREADED) : moodle_url {
354 $params = [
355 'd' => $post->get_discussion_id(),
356 'postid' => $post->get_id(),
357 'mark' => 'unread'
358 ];
359
360 $url = new moodle_url('/mod/forum/discuss.php', $params);
361
362 if ($displaymode == FORUM_MODE_THREADED) {
363 $url->param('parent', $post->get_parent_id());
364 } else {
365 $url->set_anchor('p' . $post->get_id());
366 }
367
368 return $url;
369 }
370
371 /**
372 * Get the url to export attachments for a post.
373 *
374 * @param post_entity $post The post
375 * @param stored_file $attachment
376 * @return moodle_url|null
377 */
378 public function get_export_attachment_url_from_post_and_attachment(post_entity $post, stored_file $attachment) : ?moodle_url {
379 global $CFG;
380
381 require_once($CFG->libdir . '/portfoliolib.php');
382 $button = new \portfolio_add_button();
383 $button->set_callback_options(
384 'forum_portfolio_caller',
385 ['postid' => $post->get_id(), 'attachment' => $attachment->get_id()],
386 'mod_forum'
387 );
388 $button->set_format_by_file($attachment);
389 $url = $button->to_html(PORTFOLIO_ADD_MOODLE_URL);
390 return $url ?: null;
391 }
392
393 /**
394 * Get the url to view an author's profile.
395 *
396 * @param author_entity $author The author
397 * @return moodle_url
398 */
399 public function get_author_profile_url(author_entity $author) : moodle_url {
400 return new moodle_url('/user/view.php', [
401 'id' => $author->get_id()
402 ]);
403 }
404
405 /**
83537707
RW
406 * Get the url to view the author's profile image. The author's context id should be
407 * provided to prevent the code from needing to load it.
47d38303
RW
408 *
409 * @param author_entity $author The author
83537707 410 * @param int|null $authorcontextid The author context id
6489aa2a 411 * @param int $size The size of the image to return
47d38303
RW
412 * @return moodle_url
413 */
6489aa2a
RW
414 public function get_author_profile_image_url(
415 author_entity $author,
416 int $authorcontextid = null,
417 int $size = 100
418 ) : moodle_url {
47d38303
RW
419 global $PAGE;
420
421 $datamapper = $this->legacydatamapperfactory->get_author_data_mapper();
422 $record = $datamapper->to_legacy_object($author);
83537707 423 $record->contextid = $authorcontextid;
47d38303 424 $userpicture = new user_picture($record);
6489aa2a 425 $userpicture->size = $size;
47d38303
RW
426
427 return $userpicture->get_url($PAGE);
428 }
429
430 /**
431 * Get the url to mark a discussion as read.
432 *
433 * @param forum_entity $forum The forum that the discussion belongs to
434 * @param discussion_entity $discussion The discussion
435 * @return moodle_url
436 */
437 public function get_mark_discussion_as_read_url_from_discussion(
438 forum_entity $forum,
439 discussion_entity $discussion
440 ) : moodle_url {
441 return new moodle_url('/mod/forum/markposts.php', [
442 'f' => $discussion->get_forum_id(),
443 'd' => $discussion->get_id(),
444 'mark' => 'read',
445 'sesskey' => sesskey(),
446 'return' => $this->get_forum_view_url_from_forum($forum)->out(),
447 ]);
448 }
449
450 /**
451 * Get the url to mark all discussions as read.
452 *
453 * @param forum_entity $forum The forum that the discussions belong to
454 * @return moodle_url
455 */
456 public function get_mark_all_discussions_as_read_url(forum_entity $forum) : moodle_url {
457 return new moodle_url('/mod/forum/markposts.php', [
458 'f' => $forum->get_id(),
459 'mark' => 'read',
460 'sesskey' => sesskey(),
461 'return' => $this->get_forum_view_url_from_forum($forum)->out(),
462 ]);
463 }
b4d47dec
RW
464
465 /**
466 * Get the url to subscribe to a discussion.
467 *
468 * @param discussion_entity $discussion The discussion
469 * @return moodle_url
470 */
471 public function get_discussion_subscribe_url(discussion_entity $discussion) : moodle_url {
472 return new moodle_url('/mod/forum/subscribe.php', [
473 'sesskey' => sesskey(),
474 'id' => $discussion->get_forum_id(),
475 'd' => $discussion->get_id()
309e8698 476 ]);
e7e6a721
P
477 }
478
479 /**
480 * Generate the pinned discussion link
481 *
482 * @param discussion_entity $discussion
483 * @return moodle_url
484 * @throws \moodle_exception
485 */
486 public function get_pin_discussion_url_from_discussion(discussion_entity $discussion) : moodle_url {
487 return new moodle_url('discuss.php', [
488 'sesskey' => sesskey(),
489 'd' => $discussion->get_id(),
490 'pin' => $discussion->is_pinned() ? FORUM_DISCUSSION_UNPINNED : FORUM_DISCUSSION_PINNED
b4d47dec
RW
491 ]);
492 }
47d38303 493}