MDL-65658 mod_forum: Group images links and titles groups
[moodle.git] / mod / forum / classes / local / exporters / author.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  * 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  */
25 namespace mod_forum\local\exporters;
27 defined('MOODLE_INTERNAL') || die();
29 use mod_forum\local\entities\author as author_entity;
30 use mod_forum\local\exporters\group as group_exporter;
31 use core\external\exporter;
32 use renderer_base;
34 require_once($CFG->dirroot . '/mod/forum/lib.php');
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  */
42 class author extends exporter {
43     /** @var author_entity $author Author entity */
44     private $author;
45     /** @var int|null $authorcontextid The context id for the author entity */
46     private $authorcontextid;
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;
52     /**
53      * Constructor.
54      *
55      * @param author_entity $author The author entity to export
56      * @param int|null $authorcontextid The context id for the author entity to export (null if the user doesn't have one)
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      */
61     public function __construct(
62         author_entity $author,
63         ?int $authorcontextid,
64         array $authorgroups = [],
65         bool $canview = true,
66         array $related = []
67     ) {
68         $this->author = $author;
69         $this->authorcontextid = $authorcontextid;
70         $this->authorgroups = $authorgroups;
71         $this->canview = $canview;
72         return parent::__construct([], $related);
73     }
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,
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                 ]
111             ],
112             'urls' => [
113                 'type' => [
114                     'profile' => [
115                         'description' => 'The URL for the use profile page',
116                         'type' => PARAM_URL,
117                         'optional' => true,
118                         'default' => null,
119                         'null' => NULL_ALLOWED
120                     ],
121                     'profileimage' => [
122                         'description' => 'The URL for the use profile image',
123                         'type' => PARAM_URL,
124                         'optional' => true,
125                         'default' => null,
126                         'null' => NULL_ALLOWED
127                     ],
128                 ]
129             ]
130         ];
131     }
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;
141         $authorcontextid = $this->authorcontextid;
142         $urlfactory = $this->related['urlfactory'];
143         $context = $this->related['context'];
145         if ($this->canview) {
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                 }
156                 return [
157                     'id' => $group->id,
158                     'name' => $group->name,
159                     'urls' => [
160                         'image' => $imageurl ? $imageurl->out(false) : null,
161                         'group' => $groupurl ? $groupurl->out(false) : null
163                     ]
164                 ];
165             }, $this->authorgroups);
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),
173                     'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
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     }
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     }