MDL-65658 mod_forum: Group images links and titles groups
[moodle.git] / mod / forum / classes / local / exporters / author.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 * Author exporter.
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\author as author_entity;
bc4c7337 30use mod_forum\local\exporters\group as group_exporter;
47d38303
RW
31use core\external\exporter;
32use renderer_base;
33
34require_once($CFG->dirroot . '/mod/forum/lib.php');
35
36/**
37 * Author exporter.
38 *
39 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class author extends exporter {
43 /** @var author_entity $author Author entity */
44 private $author;
b6163959 45 /** @var int|null $authorcontextid The context id for the author entity */
83537707 46 private $authorcontextid;
47d38303
RW
47 /** @var array $authorgroups List of groups that the author belongs to */
48 private $authorgroups;
49 /** @var bool $canview Should the author be anonymised? */
50 private $canview;
51
52 /**
53 * Constructor.
54 *
55 * @param author_entity $author The author entity to export
b6163959 56 * @param int|null $authorcontextid The context id for the author entity to export (null if the user doesn't have one)
47d38303
RW
57 * @param stdClass[] $authorgroups The list of groups that the author belongs to
58 * @param bool $canview Can the requesting user view this author or should it be anonymised?
59 * @param array $related The related data for the export.
60 */
83537707
RW
61 public function __construct(
62 author_entity $author,
b6163959 63 ?int $authorcontextid,
83537707
RW
64 array $authorgroups = [],
65 bool $canview = true,
66 array $related = []
67 ) {
47d38303 68 $this->author = $author;
83537707 69 $this->authorcontextid = $authorcontextid;
47d38303
RW
70 $this->authorgroups = $authorgroups;
71 $this->canview = $canview;
72 return parent::__construct([], $related);
73 }
74
75 /**
76 * Return the list of additional properties.
77 *
78 * @return array
79 */
80 protected static function define_other_properties() {
81 return [
82 'id' => [
83 'type' => PARAM_INT,
84 'optional' => true,
85 'default' => null,
86 'null' => NULL_ALLOWED
87 ],
88 'fullname' => [
89 'type' => PARAM_TEXT,
90 'optional' => true,
91 'default' => null,
92 'null' => NULL_ALLOWED
93 ],
94 'groups' => [
95 'multiple' => true,
96 'optional' => true,
53d74374
RW
97 'type' => [
98 'id' => ['type' => PARAM_INT],
99 'name' => ['type' => PARAM_TEXT],
100 'urls' => [
101 'type' => [
102 'image' => [
103 'type' => PARAM_URL,
104 'optional' => true,
105 'default' => null,
106 'null' => NULL_ALLOWED
107 ]
108 ]
109 ]
110 ]
47d38303
RW
111 ],
112 'urls' => [
113 'type' => [
114 'profile' => [
bc4c7337 115 'description' => 'The URL for the use profile page',
47d38303
RW
116 'type' => PARAM_URL,
117 'optional' => true,
118 'default' => null,
119 'null' => NULL_ALLOWED
120 ],
121 'profileimage' => [
bc4c7337 122 'description' => 'The URL for the use profile image',
47d38303
RW
123 'type' => PARAM_URL,
124 'optional' => true,
125 'default' => null,
126 'null' => NULL_ALLOWED
127 ],
128 ]
129 ]
130 ];
131 }
132
133 /**
134 * Get the additional values to inject while exporting.
135 *
136 * @param renderer_base $output The renderer.
137 * @return array Keys are the property names, values are their values.
138 */
139 protected function get_other_values(renderer_base $output) {
140 $author = $this->author;
83537707 141 $authorcontextid = $this->authorcontextid;
47d38303 142 $urlfactory = $this->related['urlfactory'];
c939a043 143 $context = $this->related['context'];
47d38303
RW
144
145 if ($this->canview) {
c939a043
MM
146 $groups = array_map(function($group) use ($urlfactory, $context) {
147 $imageurl = null;
148 $groupurl = null;
149 if (!$group->hidepicture) {
150 $imageurl = get_group_picture_url($group, $group->courseid, true);
151 }
152 if (course_can_view_participants($context)) {
153 $groupurl = $urlfactory->get_author_group_url($group);
154 }
155
47d38303
RW
156 return [
157 'id' => $group->id,
53d74374 158 'name' => $group->name,
47d38303 159 'urls' => [
c939a043
MM
160 'image' => $imageurl ? $imageurl->out(false) : null,
161 'group' => $groupurl ? $groupurl->out(false) : null
162
47d38303
RW
163 ]
164 ];
165 }, $this->authorgroups);
166
167 return [
168 'id' => $author->get_id(),
169 'fullname' => $author->get_full_name(),
170 'groups' => $groups,
171 'urls' => [
172 'profile' => ($urlfactory->get_author_profile_url($author))->out(false),
83537707 173 'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
47d38303
RW
174 ]
175 ];
176 } else {
177 // The author should be anonymised.
178 return [
179 'id' => null,
180 'fullname' => get_string('forumauthorhidden', 'mod_forum'),
181 'groups' => [],
182 'urls' => [
183 'profile' => null,
184 'profileimage' => null
185 ]
186 ];
187 }
188 }
189
190 /**
191 * Returns a list of objects that are related.
192 *
193 * @return array
194 */
195 protected static function define_related() {
196 return [
197 'urlfactory' => 'mod_forum\local\factories\url',
198 'context' => 'context'
199 ];
200 }
201}