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 39 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   * Contains user badge class for displaying a badge issued to a user.
  19   *
  20   * @package   core_badges
  21   * @copyright 2018 Dani Palou <dani@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_badges\external;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core\external\exporter;
  30  use renderer_base;
  31  use moodle_url;
  32  use core_badges\external\endorsement_exporter;
  33  use core_badges\external\alignment_exporter;
  34  use core_badges\external\related_info_exporter;
  35  
  36  /**
  37   * Class for displaying a badge issued to a user.
  38   *
  39   * @package   core_badges
  40   * @copyright 2018 Dani Palou <dani@moodle.com>
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class user_badge_exporter extends exporter {
  44  
  45      /**
  46       * Return the list of properties.
  47       *
  48       * @return array
  49       */
  50      protected static function define_properties() {
  51          return [
  52              'id' => [
  53                  'type' => PARAM_INT,
  54                  'description' => 'Badge id',
  55                  'optional' => true,
  56              ],
  57              'name' => [
  58                  'type' => PARAM_TEXT,
  59                  'description' => 'Badge name',
  60              ],
  61              'description' => [
  62                  'type' => PARAM_NOTAGS,
  63                  'description' => 'Badge description',
  64                  'null' => NULL_ALLOWED,
  65              ],
  66              'timecreated' => [
  67                  'type' => PARAM_INT,
  68                  'description' => 'Time created',
  69                  'optional' => true,
  70                  'default' => 0,
  71              ],
  72              'timemodified' => [
  73                  'type' => PARAM_INT,
  74                  'description' => 'Time modified',
  75                  'optional' => true,
  76                  'default' => 0,
  77              ],
  78              'usercreated' => [
  79                  'type' => PARAM_INT,
  80                  'description' => 'User created',
  81                  'optional' => true,
  82              ],
  83              'usermodified' => [
  84                  'type' => PARAM_INT,
  85                  'description' => 'User modified',
  86                  'optional' => true,
  87              ],
  88              'issuername' => [
  89                  'type' => PARAM_TEXT,
  90                  'description' => 'Issuer name',
  91              ],
  92              'issuerurl' => [
  93                  'type' => PARAM_URL,
  94                  'description' => 'Issuer URL',
  95              ],
  96              'issuercontact' => [
  97                  'type' => PARAM_RAW,
  98                  'description' => 'Issuer contact',
  99                  'null' => NULL_ALLOWED,
 100              ],
 101              'expiredate' => [
 102                  'type' => PARAM_INT,
 103                  'description' => 'Expire date',
 104                  'optional' => true,
 105                  'null' => NULL_ALLOWED,
 106              ],
 107              'expireperiod' => [
 108                  'type' => PARAM_INT,
 109                  'description' => 'Expire period',
 110                  'optional' => true,
 111                  'null' => NULL_ALLOWED,
 112              ],
 113              'type' => [
 114                  'type' => PARAM_INT,
 115                  'description' => 'Type',
 116                  'optional' => true,
 117                  'default' => 1,
 118              ],
 119              'courseid' => [
 120                  'type' => PARAM_INT,
 121                  'description' => 'Course id',
 122                  'optional' => true,
 123                  'null' => NULL_ALLOWED,
 124              ],
 125              'message' => [
 126                  'type' => PARAM_RAW,
 127                  'description' => 'Message',
 128                  'optional' => true,
 129              ],
 130              'messagesubject' => [
 131                  'type' => PARAM_TEXT,
 132                  'description' => 'Message subject',
 133                  'optional' => true,
 134              ],
 135              'attachment' => [
 136                  'type' => PARAM_INT,
 137                  'description' => 'Attachment',
 138                  'optional' => true,
 139                  'default' => 1,
 140              ],
 141              'notification' => [
 142                  'type' => PARAM_INT,
 143                  'description' => 'Whether to notify when badge is awarded',
 144                  'optional' => true,
 145                  'default' => 1,
 146              ],
 147              'nextcron' => [
 148                  'type' => PARAM_INT,
 149                  'description' => 'Next cron',
 150                  'optional' => true,
 151                  'null' => NULL_ALLOWED,
 152              ],
 153              'status' => [
 154                  'type' => PARAM_INT,
 155                  'description' => 'Status',
 156                  'optional' => true,
 157                  'default' => 0,
 158              ],
 159              'issuedid' => [
 160                  'type' => PARAM_INT,
 161                  'description' => 'Issued id',
 162                  'optional' => true,
 163              ],
 164              'uniquehash' => [
 165                  'type' => PARAM_ALPHANUM,
 166                  'description' => 'Unique hash',
 167              ],
 168              'dateissued' => [
 169                  'type' => PARAM_INT,
 170                  'description' => 'Date issued',
 171                  'default' => 0,
 172              ],
 173              'dateexpire' => [
 174                  'type' => PARAM_INT,
 175                  'description' => 'Date expire',
 176                  'null' => NULL_ALLOWED,
 177              ],
 178              'visible' => [
 179                  'type' => PARAM_INT,
 180                  'description' => 'Visible',
 181                  'optional' => true,
 182                  'default' => 0,
 183              ],
 184              'email' => [
 185                  'type' => PARAM_TEXT,
 186                  'description' => 'User email',
 187                  'optional' => true,
 188              ],
 189              'version' => [
 190                  'type' => PARAM_TEXT,
 191                  'description' => 'Version',
 192                  'optional' => true,
 193                  'null' => NULL_ALLOWED,
 194              ],
 195              'language' => [
 196                  'type' => PARAM_NOTAGS,
 197                  'description' => 'Language',
 198                  'optional' => true,
 199                  'null' => NULL_ALLOWED,
 200              ],
 201              'imageauthorname' => [
 202                  'type' => PARAM_TEXT,
 203                  'description' => 'Name of the image author',
 204                  'optional' => true,
 205                  'null' => NULL_ALLOWED,
 206              ],
 207              'imageauthoremail' => [
 208                  'type' => PARAM_TEXT,
 209                  'description' => 'Email of the image author',
 210                  'optional' => true,
 211                  'null' => NULL_ALLOWED,
 212              ],
 213              'imageauthorurl' => [
 214                  'type' => PARAM_URL,
 215                  'description' => 'URL of the image author',
 216                  'optional' => true,
 217                  'null' => NULL_ALLOWED,
 218              ],
 219              'imagecaption' => [
 220                  'type' => PARAM_TEXT,
 221                  'description' => 'Caption of the image',
 222                  'optional' => true,
 223                  'null' => NULL_ALLOWED,
 224              ],
 225          ];
 226      }
 227  
 228      /**
 229       * Returns a list of objects that are related.
 230       *
 231       * @return array
 232       */
 233      protected static function define_related() {
 234          return array(
 235              'context' => 'context',
 236              'endorsement' => 'stdClass?',
 237              'alignment' => 'stdClass[]',
 238              'relatedbadges' => 'stdClass[]',
 239          );
 240      }
 241  
 242      /**
 243       * Return the list of additional properties.
 244       *
 245       * @return array
 246       */
 247      protected static function define_other_properties() {
 248          return [
 249              'badgeurl' => [
 250                  'type' => PARAM_URL,
 251                  'description' => 'Badge URL',
 252              ],
 253              'endorsement' => [
 254                  'type' => endorsement_exporter::read_properties_definition(),
 255                  'description' => 'Badge endorsement',
 256                  'optional' => true,
 257              ],
 258              'alignment' => [
 259                  'type' => alignment_exporter::read_properties_definition(),
 260                  'description' => 'Badge alignments',
 261                  'multiple' => true,
 262              ],
 263              'relatedbadges' => [
 264                  'type' => related_info_exporter::read_properties_definition(),
 265                  'description' => 'Related badges',
 266                  'multiple' => true,
 267              ]
 268          ];
 269      }
 270  
 271      /**
 272       * Get the additional values to inject while exporting.
 273       *
 274       * @param renderer_base $output The renderer.
 275       * @return array Keys are the property names, values are their values.
 276       */
 277      protected function get_other_values(renderer_base $output) {
 278          $context = $this->related['context'];
 279          $endorsement = $this->related['endorsement'];
 280          $alignments = $this->related['alignment'];
 281          $relatedbadges = $this->related['relatedbadges'];
 282  
 283          $values = array(
 284              'badgeurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $this->data->id, '/',
 285                  'f3')->out(false),
 286              'alignment' => array(),
 287              'relatedbadges' => array(),
 288          );
 289  
 290          if ($endorsement) {
 291              $endorsementexporter = new endorsement_exporter($endorsement, array('context' => $context));
 292              $values['endorsement'] = $endorsementexporter->export($output);
 293          }
 294  
 295          if (!empty($alignments)) {
 296              foreach ($alignments as $alignment) {
 297                  $alignmentexporter = new alignment_exporter($alignment, array('context' => $context));
 298                  $values['alignment'][] = $alignmentexporter->export($output);
 299              }
 300          }
 301  
 302          if (!empty($relatedbadges)) {
 303              foreach ($relatedbadges as $badge) {
 304                  $relatedexporter = new related_info_exporter($badge, array('context' => $context));
 305                  $values['relatedbadges'][] = $relatedexporter->export($output);
 306              }
 307          }
 308  
 309          return $values;
 310      }
 311  }