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.
   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 class for displaying a assertion.
  19   *
  20   * @package   core_badges
  21   * @copyright 2019 Damyon Wiese
  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 stdClass;
  32  
  33  /**
  34   * Class for displaying a badge competency.
  35   *
  36   * @package   core_badges
  37   * @copyright 2019 Damyon Wiese
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class assertion_exporter extends exporter {
  41  
  42      /**
  43       * Constructor - saves the persistent object, and the related objects.
  44       *
  45       * @param mixed $data - Either an stdClass or an array of values.
  46       * @param array $related - An optional list of pre-loaded objects related to this object.
  47       */
  48      public function __construct($data, $related = array()) {
  49          // Having mixed $data is causing some issues. As this class is treating $data as an object everywhere, it can be converted
  50          // to object at this point, to avoid errors and get the expected behaviour always.
  51          // $data is an array when this class is a request exporter in backpack_api_mapping, but it is an object when this is
  52          // used as a response exporter.
  53          parent::__construct((object) $data, $related);
  54      }
  55  
  56      /**
  57       * Map from a request response data to the internal structure.
  58       *
  59       * @param stdClass $data The remote data.
  60       * @param string $apiversion The backpack version used to communicate remotely.
  61       * @return stdClass
  62       */
  63      public static function map_external_data($data, $apiversion) {
  64          $mapped = new \stdClass();
  65          if (isset($data->entityType)) {
  66              $mapped->type = $data->entityType;
  67          } else {
  68              $mapped->type = $data->type;
  69          }
  70          if (isset($data->entityId)) {
  71              $mapped->id = $data->entityId;
  72          } else {
  73              $mapped->id = $data->id;
  74          }
  75          if (isset($data->issuedOn)) {
  76              $mapped->issuedOn = $data->issuedOn;
  77          }
  78          if (isset($data->recipient)) {
  79              $mapped->recipient = $data->recipient;
  80          }
  81          if (isset($data->badgeclass)) {
  82              $mapped->badgeclass = $data->badgeclass;
  83          }
  84          $propname = '@context';
  85          $mapped->$propname = 'https://w3id.org/openbadges/v2';
  86          return $mapped;
  87      }
  88  
  89      /**
  90       * Return the list of additional properties.
  91       *
  92       * @return array
  93       */
  94      protected static function define_other_properties() {
  95          return array(
  96              'badge' => array(
  97                  'type' => badgeclass_exporter::read_properties_definition(),
  98                  'optional' => true
  99              ),
 100              'recipient' => array(
 101                  'type' => recipient_exporter::read_properties_definition(),
 102                  'optional' => true
 103              ),
 104              'verification' => array(
 105                  'type' => verification_exporter::read_properties_definition(),
 106                  'optional' => true
 107              )
 108          );
 109      }
 110  
 111      /**
 112       * We map from related data passed as data to this exporter to clean exportable values.
 113       *
 114       * @param renderer_base $output
 115       * @return array
 116       */
 117      protected function get_other_values(renderer_base $output) {
 118          global $DB;
 119          $result = [];
 120  
 121          if (property_exists($this->data, 'related_badge')) {
 122              $exporter = new badgeclass_exporter($this->data->related_badge, $this->related);
 123              $result['badge'] = $exporter->export($output);
 124          }
 125          if (property_exists($this->data, 'related_recipient')) {
 126              $exporter = new recipient_exporter($this->data->related_recipient, $this->related);
 127              $result['recipient'] = $exporter->export($output);
 128          }
 129          if (property_exists($this->data, 'related_verify')) {
 130              $exporter = new verification_exporter($this->data->related_verify, $this->related);
 131              $result['verification'] = $exporter->export($output);
 132          }
 133          return $result;
 134      }
 135  
 136      /**
 137       * Return the list of properties.
 138       *
 139       * @return array
 140       */
 141      protected static function define_properties() {
 142          return [
 143              'type' => [
 144                  'type' => PARAM_ALPHA,
 145                  'description' => 'Issuer',
 146              ],
 147              'id' => [
 148                  'type' => PARAM_URL,
 149                  'description' => 'Unique identifier for this assertion',
 150              ],
 151              'badgeclass' => [
 152                  'type' => PARAM_RAW,
 153                  'description' => 'Identifier of the badge for this assertion',
 154                  'optional' => true,
 155              ],
 156              'issuedOn' => [
 157                  'type' => PARAM_RAW,
 158                  'description' => 'Date this badge was issued',
 159              ],
 160              'expires' => [
 161                  'type' => PARAM_RAW,
 162                  'description' => 'Date this badge will expire',
 163                  'optional' => true,
 164              ],
 165              '@context' => [
 166                  'type' => PARAM_URL,
 167                  'description' => 'Badge version',
 168              ],
 169          ];
 170      }
 171  
 172      /**
 173       * Returns a list of objects that are related.
 174       *
 175       * @return array
 176       */
 177      protected static function define_related() {
 178          return array(
 179              'context' => 'context'
 180          );
 181      }
 182  }