Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  
  25  namespace mod_forum\local\exporters;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  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;
  33  
  34  require_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   */
  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;
  51  
  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      }
  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              'isdeleted' => [
  95                  'type' => PARAM_BOOL,
  96                  'optional' => true,
  97                  'default' => null,
  98                  'null' => NULL_ALLOWED
  99              ],
 100              'groups' => [
 101                  'multiple' => true,
 102                  'optional' => true,
 103                  'type' => [
 104                      'id' => ['type' => PARAM_INT],
 105                      'name' => ['type' => PARAM_TEXT],
 106                      'urls' => [
 107                          'type' => [
 108                              'image' => [
 109                                  'type' => PARAM_URL,
 110                                  'optional' => true,
 111                                  'default' => null,
 112                                  'null' => NULL_ALLOWED
 113                              ]
 114                          ]
 115                      ]
 116                  ]
 117              ],
 118              'urls' => [
 119                  'type' => [
 120                      'profile' => [
 121                          'description' => 'The URL for the use profile page',
 122                          'type' => PARAM_URL,
 123                          'optional' => true,
 124                          'default' => null,
 125                          'null' => NULL_ALLOWED
 126                      ],
 127                      'profileimage' => [
 128                          'description' => 'The URL for the use profile image',
 129                          'type' => PARAM_URL,
 130                          'optional' => true,
 131                          'default' => null,
 132                          'null' => NULL_ALLOWED
 133                      ],
 134                  ]
 135              ]
 136          ];
 137      }
 138  
 139      /**
 140       * Get the additional values to inject while exporting.
 141       *
 142       * @param renderer_base $output The renderer.
 143       * @return array Keys are the property names, values are their values.
 144       */
 145      protected function get_other_values(renderer_base $output) {
 146          $author = $this->author;
 147          $authorcontextid = $this->authorcontextid;
 148          $urlfactory = $this->related['urlfactory'];
 149          $context = $this->related['context'];
 150          $forum = $this->related['forum'];
 151  
 152          if ($this->canview) {
 153              if ($author->is_deleted()) {
 154                  return [
 155                      'id' => $author->get_id(),
 156                      'fullname' => get_string('deleteduser', 'mod_forum'),
 157                      'isdeleted' => true,
 158                      'groups' => [],
 159                      'urls' => [
 160                          'profile' => ($urlfactory->get_author_profile_url($author, $forum->get_course_id()))->out(false),
 161                          'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
 162                      ]
 163                  ];
 164              } else {
 165                  $groups = array_map(function($group) use ($urlfactory, $context, $output) {
 166                      $imageurl = null;
 167                      $groupurl = null;
 168  
 169                      if (!$group->hidepicture) {
 170                          $imageurl = get_group_picture_url($group, $group->courseid, true);
 171                      }
 172  
 173                      if (course_can_view_participants($context)) {
 174                          $groupurl = $urlfactory->get_author_group_url($group);
 175                      }
 176  
 177                      return [
 178                          'id' => $group->id,
 179                          'name' => $group->name,
 180                          'urls' => [
 181                              'image' => $imageurl ? $imageurl->out(false) : null,
 182                              'group' => $groupurl ? $groupurl->out(false) : null
 183  
 184                          ]
 185                      ];
 186                  }, $this->authorgroups);
 187  
 188                  return [
 189                      'id' => $author->get_id(),
 190                      'fullname' => $author->get_full_name(),
 191                      'isdeleted' => false,
 192                      'groups' => $groups,
 193                      'urls' => [
 194                          'profile' => ($urlfactory->get_author_profile_url($author, $forum->get_course_id()))->out(false),
 195                          'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
 196                      ]
 197                  ];
 198              }
 199          } else {
 200              // The author should be anonymised.
 201              return [
 202                  'id' => null,
 203                  'fullname' => get_string('forumauthorhidden', 'mod_forum'),
 204                  'isdeleted' => null,
 205                  'groups' => [],
 206                  'urls' => [
 207                      'profile' => null,
 208                      'profileimage' => null
 209                  ]
 210              ];
 211          }
 212      }
 213  
 214      /**
 215       * Returns a list of objects that are related.
 216       *
 217       * @return array
 218       */
 219      protected static function define_related() {
 220          return [
 221              'urlfactory' => 'mod_forum\local\factories\url',
 222              'context' => 'context',
 223              'forum' => 'mod_forum\local\entities\forum',
 224          ];
 225      }
 226  }