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.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Provides {@link tool_policy\output\page_managedocs_list} class.
  19   *
  20   * @package     tool_policy
  21   * @category    output
  22   * @copyright   2018 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_policy\output;
  27  
  28  use html_writer;
  29  use tool_policy\api;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  use action_menu;
  34  use action_menu_link;
  35  use moodle_url;
  36  use pix_icon;
  37  use renderable;
  38  use renderer_base;
  39  use single_button;
  40  use templatable;
  41  use tool_policy\policy_version;
  42  
  43  /**
  44   * Represents a management page with the list of policy documents.
  45   *
  46   * The page displays all policy documents in their sort order, together with draft future versions.
  47   *
  48   * @copyright 2018 David Mudrak <david@moodle.com>
  49   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  50   */
  51  class page_managedocs_list implements renderable, templatable {
  52  
  53      /** @var int  */
  54      protected $policyid = null;
  55      /** @var moodle_url */
  56      protected $returnurl = null;
  57  
  58      /**
  59       * page_managedocs_list constructor.
  60       * @param int $policyid when specified only archived versions of this policy will be displayed.
  61       */
  62      public function __construct($policyid = null) {
  63          $this->policyid = $policyid;
  64          $this->returnurl = new moodle_url('/admin/tool/policy/managedocs.php');
  65          if ($this->policyid) {
  66              $this->returnurl->param('archived', $this->policyid);
  67          }
  68      }
  69  
  70      /**
  71       * Export the page data for the mustache template.
  72       *
  73       * @param renderer_base $output renderer to be used to render the page elements.
  74       * @return stdClass
  75       */
  76      public function export_for_template(renderer_base $output) {
  77  
  78          $data = (object) [];
  79          $data->pluginbaseurl = (new moodle_url('/admin/tool/policy'))->out(false);
  80          $data->canmanage = has_capability('tool/policy:managedocs', \context_system::instance());
  81          $data->canaddnew = $data->canmanage && !$this->policyid;
  82          $data->canviewacceptances = has_capability('tool/policy:viewacceptances', \context_system::instance());
  83          $data->title = get_string('policiesagreements', 'tool_policy');
  84          $data->policies = [];
  85  
  86          if ($this->policyid) {
  87              // We are only interested in the archived versions of the given policy.
  88              $data->backurl = (new moodle_url('/admin/tool/policy/managedocs.php'))->out(false);
  89              $policy = api::list_policies([$this->policyid], true)[0];
  90              if ($firstversion = $policy->currentversion ?: (reset($policy->draftversions) ?: reset($policy->archivedversions))) {
  91                  $data->title = get_string('previousversions', 'tool_policy', format_string($firstversion->name));
  92              }
  93  
  94              foreach ($policy->archivedversions as $i => $version) {
  95                  $data->versions[] = $this->export_version_for_template($output, $policy, $version,
  96                      false, false, false);
  97              }
  98              return $data;
  99          }
 100  
 101          // List all policies. Display current and all draft versions of each policy in this list.
 102          // If none found, then show only one archived version.
 103          $policies = api::list_policies(null, true);
 104          foreach ($policies as $i => $policy) {
 105  
 106              if (empty($policy->currentversion) && empty($policy->draftversions)) {
 107                  // There is no current and no draft versions, display the first archived version.
 108                  $firstpolicy = array_shift($policy->archivedversions);
 109                  $data->versions[] = $this->export_version_for_template($output, $policy, $firstpolicy,
 110                      false, $i > 0, $i < count($policies) - 1);
 111              }
 112  
 113              if (!empty($policy->currentversion)) {
 114  
 115                  // Current version of the policy.
 116                  $data->versions[] = $this->export_version_for_template($output, $policy, $policy->currentversion,
 117                      false, $i > 0, $i < count($policies) - 1);
 118  
 119              } else if ($policy->draftversions) {
 120  
 121                  // There is no current version, display the first draft version as the current.
 122                  $firstpolicy = array_shift($policy->draftversions);
 123                  $data->versions[] = $this->export_version_for_template($output, $policy, $firstpolicy,
 124                      false, $i > 0, $i < count($policies) - 1);
 125              }
 126  
 127              foreach ($policy->draftversions as $draft) {
 128                  // Show all [other] draft policies indented.
 129                  $data->versions[] = $this->export_version_for_template($output, $policy, $draft,
 130                      true, false, false);
 131              }
 132  
 133          }
 134  
 135          return $data;
 136      }
 137  
 138      /**
 139       * Exports one version for the list of policies
 140       *
 141       * @param \renderer_base $output
 142       * @param \stdClass $policy
 143       * @param \stdClass $version
 144       * @param bool $isindented display indented (normally drafts of the current version)
 145       * @param bool $moveup can move up
 146       * @param bool $movedown can move down
 147       * @return \stdClass
 148       */
 149      protected function export_version_for_template($output, $policy, $version, $isindented, $moveup, $movedown) {
 150  
 151          $status = $version->status;
 152          $version->statustext = get_string('status' . $status, 'tool_policy');
 153  
 154          if ($status == policy_version::STATUS_ACTIVE) {
 155              $version->statustext = html_writer::span($version->statustext, 'badge badge-success');
 156          } else if ($status == policy_version::STATUS_DRAFT) {
 157              $version->statustext = html_writer::span($version->statustext, 'badge badge-warning');
 158          } else {
 159              $version->statustext = html_writer::span($version->statustext, 'label');
 160          }
 161  
 162          if ($version->optional == policy_version::AGREEMENT_OPTIONAL) {
 163              $version->optionaltext = get_string('policydocoptionalyes', 'tool_policy');
 164          } else {
 165              $version->optionaltext = get_string('policydocoptionalno', 'tool_policy');
 166          }
 167  
 168          $version->indented = $isindented;
 169  
 170          $editbaseurl = new moodle_url('/admin/tool/policy/editpolicydoc.php', [
 171              'sesskey' => sesskey(),
 172              'policyid' => $policy->id,
 173              'returnurl' => $this->returnurl->out_as_local_url(false),
 174          ]);
 175  
 176          $viewurl = new moodle_url('/admin/tool/policy/view.php', [
 177              'policyid' => $policy->id,
 178              'versionid' => $version->id,
 179              'manage' => 1,
 180              'returnurl' => $this->returnurl->out_as_local_url(false),
 181          ]);
 182  
 183          $actionmenu = new action_menu();
 184          $actionmenu->set_menu_trigger(get_string('actions', 'tool_policy'));
 185          $actionmenu->set_alignment(action_menu::TL, action_menu::BL);
 186          $actionmenu->prioritise = true;
 187          if ($moveup) {
 188              $actionmenu->add(new action_menu_link(
 189                  new moodle_url($editbaseurl, ['moveup' => $policy->id]),
 190                  new pix_icon('t/up', get_string('moveup', 'tool_policy')),
 191                  get_string('moveup', 'tool_policy'),
 192                  true
 193              ));
 194          }
 195          if ($movedown) {
 196              $actionmenu->add(new action_menu_link(
 197                  new moodle_url($editbaseurl, ['movedown' => $policy->id]),
 198                  new pix_icon('t/down', get_string('movedown', 'tool_policy')),
 199                  get_string('movedown', 'tool_policy'),
 200                  true
 201              ));
 202          }
 203          $actionmenu->add(new action_menu_link(
 204              $viewurl,
 205              null,
 206              get_string('view'),
 207              false
 208          ));
 209          if ($status != policy_version::STATUS_ARCHIVED) {
 210              $actionmenu->add(new action_menu_link(
 211                  new moodle_url($editbaseurl, ['versionid' => $version->id]),
 212                  null,
 213                  get_string('edit'),
 214                  false
 215              ));
 216          }
 217          if ($status == policy_version::STATUS_ACTIVE) {
 218              $actionmenu->add(new action_menu_link(
 219                  new moodle_url($editbaseurl, ['inactivate' => $policy->id]),
 220                  null,
 221                  get_string('inactivate', 'tool_policy'),
 222                  false,
 223                  ['data-action' => 'inactivate']
 224              ));
 225          }
 226          if ($status == policy_version::STATUS_DRAFT) {
 227              $actionmenu->add(new action_menu_link(
 228                  new moodle_url($editbaseurl, ['makecurrent' => $version->id]),
 229                  null,
 230                  get_string('activate', 'tool_policy'),
 231                  false,
 232                  ['data-action' => 'makecurrent']
 233              ));
 234          }
 235          if (api::can_delete_version($version)) {
 236              $actionmenu->add(new action_menu_link(
 237                  new moodle_url($editbaseurl, ['delete' => $version->id]),
 238                  null,
 239                  get_string('delete'),
 240                  false,
 241                  ['data-action' => 'delete']
 242              ));
 243          }
 244          if ($status == policy_version::STATUS_ARCHIVED) {
 245              $actionmenu->add(new action_menu_link(
 246                  new moodle_url($editbaseurl, ['versionid' => $version->id]),
 247                  null,
 248                  get_string('settodraft', 'tool_policy'),
 249                  false
 250              ));
 251          }
 252          if (!$this->policyid && !$isindented && $policy->archivedversions &&
 253                  ($status != policy_version::STATUS_ARCHIVED || count($policy->archivedversions) > 1)) {
 254              $actionmenu->add(new action_menu_link(
 255                  new moodle_url('/admin/tool/policy/managedocs.php', ['archived' => $policy->id]),
 256                  null,
 257                  get_string('viewarchived', 'tool_policy'),
 258                  false
 259              ));
 260          }
 261  
 262          $version->actionmenu = $actionmenu->export_for_template($output);
 263          return $version;
 264      }
 265  }