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.
   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   * Edit/create a policy document version.
  19   *
  20   * @package     tool_policy
  21   * @copyright   2018 David Mudrák <david@moodle.com>
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use tool_policy\api;
  26  use tool_policy\policy_version;
  27  
  28  require(__DIR__.'/../../../config.php');
  29  require_once($CFG->libdir.'/adminlib.php');
  30  
  31  $policyid = optional_param('policyid', null, PARAM_INT);
  32  $versionid = optional_param('versionid', null, PARAM_INT);
  33  $makecurrent = optional_param('makecurrent', null, PARAM_INT);
  34  $inactivate = optional_param('inactivate', null, PARAM_INT);
  35  $delete = optional_param('delete', null, PARAM_INT);
  36  $confirm = optional_param('confirm', false, PARAM_BOOL);
  37  $moveup = optional_param('moveup', null, PARAM_INT);
  38  $movedown = optional_param('movedown', null, PARAM_INT);
  39  
  40  admin_externalpage_setup('tool_policy_managedocs', '', ['policyid' => $policyid, 'versionid' => $versionid],
  41      new moodle_url('/admin/tool/policy/editpolicydoc.php'));
  42  require_capability('tool/policy:managedocs', context_system::instance());
  43  
  44  $output = $PAGE->get_renderer('tool_policy');
  45  $PAGE->navbar->add(get_string('editingpolicydocument', 'tool_policy'));
  46  
  47  if ($makecurrent) {
  48      $version = api::get_policy_version($makecurrent);
  49  
  50      if ($confirm) {
  51          require_sesskey();
  52          api::make_current($makecurrent);
  53          redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
  54      }
  55  
  56      echo $output->header();
  57      echo $output->heading(get_string('activating', 'tool_policy'));
  58      echo $output->confirm(
  59          get_string('activateconfirm', 'tool_policy', [
  60              'name' => format_string($version->name),
  61              'revision' => format_string($version->revision),
  62          ]),
  63          new moodle_url($PAGE->url, ['makecurrent' => $makecurrent, 'confirm' => 1]),
  64          new moodle_url('/admin/tool/policy/managedocs.php')
  65      );
  66      echo $output->footer();
  67      die();
  68  }
  69  
  70  if ($inactivate) {
  71      $policies = api::list_policies([$inactivate]);
  72  
  73      if (empty($policies[0]->currentversionid)) {
  74          redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
  75      }
  76  
  77      if ($confirm) {
  78          require_sesskey();
  79          api::inactivate($inactivate);
  80          redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
  81      }
  82  
  83      echo $output->header();
  84      echo $output->heading(get_string('inactivating', 'tool_policy'));
  85      echo $output->confirm(
  86          get_string('inactivatingconfirm', 'tool_policy', [
  87              'name' => format_string($policies[0]->currentversion->name),
  88              'revision' => format_string($policies[0]->currentversion->revision),
  89          ]),
  90          new moodle_url($PAGE->url, ['inactivate' => $inactivate, 'confirm' => 1]),
  91          new moodle_url('/admin/tool/policy/managedocs.php')
  92      );
  93      echo $output->footer();
  94      die();
  95  }
  96  
  97  if ($delete) {
  98      $version = api::get_policy_version($delete);
  99  
 100      if ($confirm) {
 101          require_sesskey();
 102          api::delete($delete);
 103          redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
 104      }
 105  
 106      echo $output->header();
 107      echo $output->heading(get_string('deleting', 'tool_policy'));
 108      echo $output->confirm(
 109          get_string('deleteconfirm', 'tool_policy', [
 110              'name' => format_string($version->name),
 111              'revision' => format_string($version->revision),
 112          ]),
 113          new moodle_url($PAGE->url, ['delete' => $delete, 'confirm' => 1]),
 114          new moodle_url('/admin/tool/policy/managedocs.php')
 115      );
 116      echo $output->footer();
 117      die();
 118  }
 119  
 120  if ($moveup || $movedown) {
 121      require_sesskey();
 122  
 123      if ($moveup) {
 124          api::move_up($moveup);
 125      } else {
 126          api::move_down($movedown);
 127      }
 128  
 129      redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
 130  }
 131  
 132  if (!$versionid && $policyid) {
 133      if (($policies = api::list_policies([$policyid])) && !empty($policies[0]->currentversionid)) {
 134          $policy = $policies[0];
 135          $policyversion = new policy_version($policy->currentversionid);
 136      } else {
 137          redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
 138      }
 139  } else {
 140      $policyversion = new policy_version($versionid);
 141      if ($policyversion->get('policyid')) {
 142          $policy = api::list_policies([$policyversion->get('policyid')])[0];
 143      } else {
 144          $policy = null;
 145      }
 146  }
 147  
 148  $formdata = api::form_policydoc_data($policyversion);
 149  
 150  if ($policy && $formdata->id && $policy->currentversionid == $formdata->id) {
 151      // We are editing an active version.
 152      $formdata->status = policy_version::STATUS_ACTIVE;
 153  } else {
 154      // We are editing a draft or archived version and the default next status is "draft".
 155      $formdata->status = policy_version::STATUS_DRAFT;
 156      // Archived versions can not be edited without creating a new version.
 157      $formdata->minorchange = $policyversion->get('archived') ? 0 : 1;
 158  }
 159  
 160  $form = new \tool_policy\form\policydoc($PAGE->url, ['formdata' => $formdata]);
 161  
 162  if ($form->is_cancelled()) {
 163      redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
 164  
 165  } else if ($data = $form->get_data()) {
 166  
 167      if (! $policyversion->get('id')) {
 168          $policyversion = api::form_policydoc_add($data);
 169  
 170      } else if (empty($data->minorchange)) {
 171          $data->policyid = $policyversion->get('policyid');
 172          $policyversion = api::form_policydoc_update_new($data);
 173  
 174      } else {
 175          $data->id = $policyversion->get('id');
 176          $policyversion = api::form_policydoc_update_overwrite($data);
 177      }
 178  
 179      if ($data->status == policy_version::STATUS_ACTIVE) {
 180          api::make_current($policyversion->get('id'));
 181      }
 182  
 183      redirect(new moodle_url('/admin/tool/policy/managedocs.php'));
 184  
 185  } else {
 186      echo $output->header();
 187      echo $output->heading(get_string('editingpolicydocument', 'tool_policy'));
 188      echo $form->render();
 189      echo $output->footer();
 190  }