Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is 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   * Provides the {@link tool_policy\policy_exporter} class.
  19   *
  20   * @package   tool_policy
  21   * @copyright 2018 David Mudrak <david@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_policy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core\external\exporter;
  30  use renderer_base;
  31  
  32  /**
  33   * Exporter of a policy document model.
  34   *
  35   * @copyright 2018 David Mudrak <david@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class policy_exporter extends exporter {
  39  
  40      /**
  41       * Return the list of properties.
  42       *
  43       * @return array
  44       */
  45      protected static function define_properties() {
  46          return [
  47              'id' => [
  48                  'type' => PARAM_INT,
  49              ],
  50              'sortorder' => [
  51                  'type' => PARAM_INT,
  52                  'default' => 999,
  53              ],
  54              'currentversionid' => [
  55                  'type' => PARAM_INT,
  56                  'null' => NULL_ALLOWED,
  57              ],
  58          ];
  59      }
  60  
  61      /**
  62       * Returns a list of objects that are related.
  63       *
  64       * @return array
  65       */
  66      protected static function define_related() {
  67          return [
  68              'versions' => 'tool_policy\policy_version_exporter[]',
  69          ];
  70      }
  71  
  72      /**
  73       * Return the list of additional, generated dynamically from the given properties.
  74       *
  75       * @return array
  76       */
  77      protected static function define_other_properties() {
  78          return [
  79              'currentversion' => [
  80                  'type' => policy_version_exporter::read_properties_definition(),
  81                  'null' => NULL_ALLOWED,
  82              ],
  83              'draftversions' => [
  84                  'type' => policy_version_exporter::read_properties_definition(),
  85                  'multiple' => true,
  86              ],
  87              'archivedversions' => [
  88                  'type' => policy_version_exporter::read_properties_definition(),
  89                  'multiple' => true,
  90              ],
  91          ];
  92      }
  93  
  94      /**
  95       * Get the additional values to inject while exporting.
  96       *
  97       * @param renderer_base $output The renderer.
  98       * @return array Keys are the property names, values are their values.
  99       */
 100      protected function get_other_values(renderer_base $output) {
 101  
 102          $othervalues = [
 103              'currentversion' => null,
 104              'draftversions' => [],
 105              'archivedversions' => [],
 106          ];
 107  
 108          foreach ($this->related['versions'] as $exporter) {
 109              $data = $exporter->export($output);
 110  
 111              if ($data->id == $this->data->currentversionid) {
 112                  $othervalues['currentversion'] = $data;
 113  
 114              } else if ($data->archived) {
 115                  $othervalues['archivedversions'][] = $data;
 116  
 117              } else {
 118                  $othervalues['draftversions'][] = $data;
 119              }
 120          }
 121  
 122          return $othervalues;
 123      }
 124  }