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   * This page lets users manage purposes.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 David Monllao
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  
  27  require_login(null, false);
  28  
  29  $id = optional_param('id', 0, PARAM_INT);
  30  
  31  $url = new \moodle_url('/admin/tool/dataprivacy/editpurpose.php', array('id' => $id));
  32  if ($id) {
  33      $title = get_string('editpurpose', 'tool_dataprivacy');
  34  } else {
  35      $title = get_string('addpurpose', 'tool_dataprivacy');
  36  }
  37  
  38  \tool_dataprivacy\page_helper::setup($url, $title, 'dataregistry');
  39  
  40  $purpose = new \tool_dataprivacy\purpose($id);
  41  $form = new \tool_dataprivacy\form\purpose($PAGE->url->out(false),
  42      array('persistent' => $purpose, 'showbuttons' => true));
  43  
  44  $returnurl = new \moodle_url('/admin/tool/dataprivacy/purposes.php');
  45  if ($form->is_cancelled()) {
  46      redirect($returnurl);
  47  } else if ($alldata = $form->get_data()) {
  48      $data = $form->filter_data_for_persistent($alldata);
  49  
  50      if (empty($data->id)) {
  51          $purpose = \tool_dataprivacy\api::create_purpose($data);
  52          $messagesuccess = get_string('purposecreated', 'tool_dataprivacy');
  53      } else {
  54          $purpose = \tool_dataprivacy\api::update_purpose($data);
  55          $messagesuccess = get_string('purposeupdated', 'tool_dataprivacy');
  56      }
  57  
  58      $currentoverrides = [];
  59      foreach ($purpose->get_purpose_overrides() as $override) {
  60          $currentoverrides[$override->get('id')] = $override;
  61      }
  62  
  63      $overrides = $form->get_role_overrides_from_data($alldata);
  64      $submittedoverrides = [];
  65      $tosave = [];
  66  
  67      foreach ($overrides as $overridedata) {
  68          $overridedata->purposeid = $purpose->get('id');
  69          $override = new \tool_dataprivacy\purpose_override($overridedata->id, $overridedata);
  70  
  71          $tosave[] = $override;
  72  
  73          if (!empty($overridedata->id)) {
  74              $submittedoverrides[$overridedata->id] = true;
  75          }
  76      }
  77  
  78      foreach ($currentoverrides as $id => $override) {
  79          if (!isset($submittedoverrides[$id])) {
  80              $override->delete();
  81          }
  82      }
  83  
  84      foreach ($tosave as $override) {
  85          $override->save();
  86      }
  87  
  88      redirect($returnurl, $messagesuccess, 0, \core\output\notification::NOTIFY_SUCCESS);
  89  }
  90  
  91  $output = $PAGE->get_renderer('tool_dataprivacy');
  92  echo $output->header();
  93  $form->display();
  94  echo $output->footer();