Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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   * Provides the {@link tool_policy\policy_version_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  use tool_policy\api;
  32  
  33  /**
  34   * Exporter of a single policy document version.
  35   *
  36   * Note we cannot use the persistent_exporter as our super class because we want to add some properties not present in
  37   * the persistent (e.g. acceptancescount).
  38   *
  39   * @copyright 2018 David Mudrak <david@moodle.com>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class policy_version_exporter extends exporter {
  43  
  44      /**
  45       * Return the list of properties.
  46       *
  47       * @return array
  48       */
  49      protected static function define_properties() {
  50  
  51          return policy_version::properties_definition() + [
  52              'acceptancescount' => [
  53                  'type' => PARAM_INT,
  54                  'default' => 0,
  55              ],
  56              'status' => [
  57                  'type' => PARAM_INT,
  58              ],
  59          ];
  60      }
  61  
  62      /**
  63       * Returns a list of objects that are related.
  64       *
  65       * @return array
  66       */
  67      protected static function define_related() {
  68          return [
  69              'context' => 'context',
  70          ];
  71      }
  72  
  73      /**
  74       * Return the list of additional (calculated and readonly) properties.
  75       *
  76       * @return array
  77       */
  78      protected static function define_other_properties() {
  79          return [
  80              // Human readable type of the policy document version.
  81              'typetext' => [
  82                  'type' => PARAM_TEXT,
  83              ],
  84              // Human readable audience of the policy document audience.
  85              'audiencetext' => [
  86                  'type' => PARAM_TEXT,
  87              ],
  88              // Detailed information about the number of policy acceptances.
  89              'acceptancescounttext' => [
  90                  'type' => PARAM_TEXT,
  91              ],
  92              // Link to view acceptances.
  93              'acceptancescounturl' => [
  94                  'type' => PARAM_LOCALURL,
  95              ],
  96          ];
  97      }
  98  
  99      /**
 100       * Get the additional values to inject while exporting.
 101       *
 102       * @param renderer_base $output The renderer.
 103       * @return array Keys are the property names, values are their values.
 104       */
 105      protected function get_other_values(renderer_base $output) {
 106  
 107          $othervalues = [
 108              'typetext' => get_string('policydoctype'.$this->data->type, 'tool_policy'),
 109              'audiencetext' => get_string('policydocaudience'.$this->data->audience, 'tool_policy'),
 110          ];
 111  
 112          if (!isset($this->data->acceptancescount) || $this->data->status == policy_version::STATUS_DRAFT) {
 113              // Return "N/A" for acceptances count.
 114              $othervalues['acceptancescounttext'] = get_string('useracceptancecountna', 'tool_policy');
 115              $othervalues['acceptancescounturl'] = null;
 116              return $othervalues;
 117          }
 118  
 119          $acceptancescount = empty($this->data->acceptancescount) ? 0 : $this->data->acceptancescount;
 120          $acceptancesexpected = api::count_total_users();
 121  
 122          $a = [
 123              'agreedcount' => $acceptancescount,
 124              'userscount' => $acceptancesexpected,
 125              'percent' => min(100, round($acceptancescount * 100 / max($acceptancesexpected, 1))),
 126          ];
 127  
 128          $othervalues['acceptancescounttext'] = get_string('useracceptancecount', 'tool_policy', $a);
 129          $acceptancesurl = new \moodle_url('/admin/tool/policy/acceptances.php', ['policyid' => $this->data->policyid]);
 130          if ($this->data->status != policy_version::STATUS_ACTIVE) {
 131              $acceptancesurl->param('versionid', $this->data->id);
 132          }
 133          $othervalues['acceptancescounturl'] = $acceptancesurl->out(false);
 134  
 135          return $othervalues;
 136      }
 137  
 138      /**
 139       * Get the formatting parameters for the summary field.
 140       *
 141       * @return array
 142       */
 143      protected function get_format_parameters_for_summary() {
 144          return [
 145              'component' => 'tool_policy',
 146              'filearea' => 'policydocumentsummary',
 147              'itemid' => $this->data->id
 148          ];
 149      }
 150  
 151      /**
 152       * Get the formatting parameters for the content field.
 153       *
 154       * @return array
 155       */
 156      protected function get_format_parameters_for_content() {
 157          return [
 158              'component' => 'tool_policy',
 159              'filearea' => 'policydocumentcontent',
 160              'itemid' => $this->data->id
 161          ];
 162      }
 163  }