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]

   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              'purpose' => ['type' => PARAM_TEXT, 'description' => 'The purpose of the component exposing the content item'],
  72          ];
  73      }
  74  
  75      /**
  76       * Definition of all properties which are either calculated or originate in a related domain object.
  77       *
  78       * @return array The array of property values, indexed by name.
  79       */
  80      protected static function define_other_properties() {
  81          // This will hold user-dependant properties such as whether the item is starred or recommended.
  82          return [
  83              'favourite' => ['type' => PARAM_BOOL, 'description' => 'Has the user favourited the content item'],
  84              'legacyitem' => [
  85                  'type' => PARAM_BOOL,
  86                  'description' => 'If this item was pulled from the old callback and has no item id.'
  87              ],
  88              'recommended' => ['type' => PARAM_BOOL, 'description' => 'Has this item been recommended'],
  89          ];
  90      }
  91  
  92      /**
  93       * Get ALL properties for the content_item DTO being exported.
  94       *
  95       * These properties are a mix of:
  96       * - readonly properties of the primary object (content_item) being exported.
  97       * - calculated values
  98       * - properties originating from the related domain objects.
  99       *
 100       * Normally, those properties defined in get_properties() are added to the export automatically as part of the superclass code,
 101       * provided they are public properties on the export target. In this case, the export target is content_item, which doesn't
 102       * provide public access to its properties, so those are fetched via their respective getters here.
 103       *
 104       * @param \renderer_base $output
 105       * @return array The array of property values, indexed by name.
 106       */
 107      protected function get_other_values(\renderer_base $output) {
 108  
 109          $favourite = false;
 110          $itemtype = 'contentitem_' . $this->contentitem->get_component_name();
 111          if (isset($this->related['favouriteitems'])) {
 112              foreach ($this->related['favouriteitems'] as $favobj) {
 113                  if ($favobj->itemtype === $itemtype && in_array($this->contentitem->get_id(), $favobj->ids)) {
 114                      $favourite = true;
 115                  }
 116              }
 117          }
 118  
 119          $recommended = false;
 120          $itemtype = content_item_service::RECOMMENDATION_PREFIX . $this->contentitem->get_component_name();
 121          if (isset($this->related['recommended'])) {
 122              foreach ($this->related['recommended'] as $favobj) {
 123                  if ($favobj->itemtype === $itemtype && in_array($this->contentitem->get_id(), $favobj->ids)) {
 124                      $recommended = true;
 125                  }
 126              }
 127          }
 128  
 129          $properties = [
 130              'id' => $this->contentitem->get_id(),
 131              'name' => $this->contentitem->get_name(),
 132              'title' => $this->contentitem->get_title()->get_value(),
 133              'link' => $this->contentitem->get_link()->out(false),
 134              'icon' => $this->contentitem->get_icon(),
 135              'help' => format_text($this->contentitem->get_help(), FORMAT_MARKDOWN),
 136              'archetype' => $this->contentitem->get_archetype(),
 137              'componentname' => $this->contentitem->get_component_name(),
 138              'favourite' => $favourite,
 139              'legacyitem' => ($this->contentitem->get_id() == -1),
 140              'recommended' => $recommended,
 141              'purpose' => $this->contentitem->get_purpose()
 142          ];
 143  
 144          return $properties;
 145      }
 146  
 147      /**
 148       * Define the list of related objects, used by this exporter.
 149       *
 150       * @return array the list of related objects.
 151       */
 152      protected static function define_related(): array {
 153          return [
 154              'context' => '\context',
 155              'favouriteitems' => '\stdClass[]?',
 156              'recommended' => '\stdClass[]?'
 157          ];
 158      }
 159  }