PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/classes/local/exporters/author.php

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