Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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                      $groupurl = null;
 167                      $imageurl = get_group_picture_url($group, $group->courseid, true);
 168  
 169                      if (course_can_view_participants($context)) {
 170                          $groupurl = $urlfactory->get_author_group_url($group);
 171                      }
 172  
 173                      return [
 174                          'id' => $group->id,
 175                          'name' => format_string($group->name, true, ['context' => $context]),
 176                          'urls' => [
 177                              'image' => $imageurl ? $imageurl->out(false) : null,
 178                              'group' => $groupurl ? $groupurl->out(false) : null
 179  
 180                          ]
 181                      ];
 182                  }, $this->authorgroups);
 183  
 184                  return [
 185                      'id' => $author->get_id(),
 186                      'fullname' => $author->get_full_name(),
 187                      'isdeleted' => false,
 188                      'groups' => $groups,
 189                      'urls' => [
 190                          'profile' => ($urlfactory->get_author_profile_url($author, $forum->get_course_id()))->out(false),
 191                          'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false)
 192                      ]
 193                  ];
 194              }
 195          } else {
 196              // The author should be anonymised.
 197              return [
 198                  'id' => null,
 199                  'fullname' => get_string('forumauthorhidden', 'mod_forum'),
 200                  'isdeleted' => null,
 201                  'groups' => [],
 202                  'urls' => [
 203                      'profile' => null,
 204                      'profileimage' => null
 205                  ]
 206              ];
 207          }
 208      }
 209  
 210      /**
 211       * Returns a list of objects that are related.
 212       *
 213       * @return array
 214       */
 215      protected static function define_related() {
 216          return [
 217              'urlfactory' => 'mod_forum\local\factories\url',
 218              'context' => 'context',
 219              'forum' => 'mod_forum\local\entities\forum',
 220          ];
 221      }
 222  }