Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Contains the course_content_item_exporter class.
  19   *
  20   * @package    core
  21   * @subpackage course
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_course\local\exporters;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core\external\exporter;
  30  use core_course\local\entity\content_item;
  31  use core_course\local\service\content_item_service;
  32  
  33  /**
  34   * The course_content_item_exporter class.
  35   *
  36   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class course_content_item_exporter extends exporter {
  40  
  41      /** @var content_item $contentitem the content_item to export. */
  42      private $contentitem;
  43  
  44      /**
  45       * The course_content_item_exporter constructor.
  46       *
  47       * @param content_item $contentitem the content item to export.
  48       * @param array $related the array of related objects used during export.
  49       */
  50      public function __construct(content_item $contentitem, array $related = []) {
  51          $this->contentitem = $contentitem;
  52  
  53          return parent::__construct([], $related);
  54      }
  55  
  56      /**
  57       * Definition of all properties originating in the export target, \core_course\local\entity\content_item.
  58       *
  59       * @return array The array of property values, indexed by name.
  60       */
  61      protected static function define_properties() {
  62          return [
  63              'id' => ['type' => PARAM_INT, 'description' => 'The id of the content item'],
  64              'name' => ['type' => PARAM_TEXT, 'description' => 'Name of the content item'],
  65              'title' => ['type' => PARAM_TEXT, 'description' => 'The string title of the content item, human readable'],
  66              'link' => ['type' => PARAM_URL, 'description' => 'The link to the content item creation page'],
  67              'icon' => ['type' => PARAM_RAW, 'description' => 'Html containing the icon for the content item'],
  68              'help' => ['type' => PARAM_RAW, 'description' => 'Html description / help for the content item'],
  69              'archetype' => ['type' => PARAM_RAW, 'description' => 'The archetype of the module exposing the content item'],
  70              'componentname' => ['type' => PARAM_TEXT, 'description' => 'The name of the component exposing the content item'],
  71          ];
  72      }
  73  
  74      /**
  75       * Definition of all properties which are either calculated or originate in a related domain object.
  76       *
  77       * @return array The array of property values, indexed by name.
  78       */
  79      protected static function define_other_properties() {
  80          // This will hold user-dependant properties such as whether the item is starred or recommended.
  81          return [
  82              'favourite' => ['type' => PARAM_BOOL, 'description' => 'Has the user favourited the content item'],
  83              'legacyitem' => [
  84                  'type' => PARAM_BOOL,
  85                  'description' => 'If this item was pulled from the old callback and has no item id.'
  86              ],
  87              'recommended' => ['type' => PARAM_BOOL, 'description' => 'Has this item been recommended'],
  88          ];
  89      }
  90  
  91      /**
  92       * Get ALL properties for the content_item DTO being exported.
  93       *
  94       * These properties are a mix of:
  95       * - readonly properties of the primary object (content_item) being exported.
  96       * - calculated values
  97       * - properties originating from the related domain objects.
  98       *
  99       * Normally, those properties defined in get_properties() are added to the export automatically as part of the superclass code,
 100       * provided they are public properties on the export target. In this case, the export target is content_item, which doesn't
 101       * provide public access to its properties, so those are fetched via their respective getters here.
 102       *
 103       * @param \renderer_base $output
 104       * @return array The array of property values, indexed by name.
 105       */
 106      protected function get_other_values(\renderer_base $output) {
 107  
 108          $favourite = false;
 109          $itemtype = 'contentitem_' . $this->contentitem->get_component_name();
 110          if (isset($this->related['favouriteitems'])) {
 111              foreach ($this->related['favouriteitems'] as $favobj) {
 112                  if ($favobj->itemtype === $itemtype && in_array($this->contentitem->get_id(), $favobj->ids)) {
 113                      $favourite = true;
 114                  }
 115              }
 116          }
 117  
 118          $recommended = false;
 119          $itemtype = content_item_service::RECOMMENDATION_PREFIX . $this->contentitem->get_component_name();
 120          if (isset($this->related['recommended'])) {
 121              foreach ($this->related['recommended'] as $favobj) {
 122                  if ($favobj->itemtype === $itemtype && in_array($this->contentitem->get_id(), $favobj->ids)) {
 123                      $recommended = true;
 124                  }
 125              }
 126          }
 127  
 128          $properties = [
 129              'id' => $this->contentitem->get_id(),
 130              'name' => $this->contentitem->get_name(),
 131              'title' => $this->contentitem->get_title()->get_value(),
 132              'link' => $this->contentitem->get_link()->out(false),
 133              'icon' => $this->contentitem->get_icon(),
 134              'help' => format_text($this->contentitem->get_help(), FORMAT_MARKDOWN),
 135              'archetype' => $this->contentitem->get_archetype(),
 136              'componentname' => $this->contentitem->get_component_name(),
 137              'favourite' => $favourite,
 138              'legacyitem' => ($this->contentitem->get_id() == -1),
 139              'recommended' => $recommended
 140          ];
 141  
 142          return $properties;
 143      }
 144  
 145      /**
 146       * Define the list of related objects, used by this exporter.
 147       *
 148       * @return array the list of related objects.
 149       */
 150      protected static function define_related(): array {
 151          return [
 152              'context' => '\context',
 153              'favouriteitems' => '\stdClass[]?',
 154              'recommended' => '\stdClass[]?'
 155          ];
 156      }
 157  }