Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * Form to filter export data.
  19   *
  20   * @copyright 2021 The Open University
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package tool_dataprivacy
  23   * @since Moodle 4.3
  24   */
  25  
  26  namespace tool_dataprivacy\form;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->libdir.'/formslib.php');
  31  
  32  /**
  33   * Form to filter export data.
  34   *
  35   * @copyright 2021 The Open University
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  37   * @package tool_dataprivacy
  38   * @since   Moodle 4.3
  39   */
  40  class exportfilter_form extends \moodleform {
  41      /**
  42       * Form definition.
  43       */
  44      public function definition(): void {
  45          $requestid = $this->_customdata['requestid'];
  46          $mform = $this->_form;
  47          $selectitems = [];
  48  
  49          $mform->addElement('hidden', 'requestid', $requestid);
  50          $mform->setType('requestid', PARAM_INT);
  51          $contexts = \tool_dataprivacy\api::get_course_contexts_for_view_filter($requestid);
  52          foreach ($contexts as $context) {
  53              $coursename = '';
  54              $groupname = '';
  55              $parentcontexts = $context->get_parent_contexts(true);
  56              $parentcontexts = array_reverse($parentcontexts);
  57              end($parentcontexts);
  58              $lastkey = key($parentcontexts);
  59              reset($parentcontexts);
  60              $firstkey = key($parentcontexts);
  61  
  62              foreach ($parentcontexts as $key => $parentcontext) {
  63                  if ($key !== $lastkey) {
  64                      if ($key !== $firstkey) {
  65                          $groupname .= ' > ';
  66                      }
  67                      $groupname .= $parentcontext->get_context_name(false);
  68                  } else {
  69                      $coursename = $parentcontext->get_context_name(false);
  70                  }
  71              }
  72  
  73              $selectitems[$groupname][$context->id] = $coursename;
  74          }
  75  
  76          if ($contexts) {
  77              $mform->addElement(
  78                  'selectgroups',
  79                  'coursecontextids',
  80                  get_string('selectcourses', 'tool_dataprivacy'),
  81                  $selectitems,
  82              );
  83              $mform->getElement('coursecontextids')->setMultiple(true);
  84              $mform->getElement('coursecontextids')->setSize(15);
  85          } else {
  86              $mform->addElement('html', get_string('nocoursetofilter', 'tool_dataprivacy'));
  87          }
  88      }
  89  
  90      /**
  91       * Form validation.
  92       *
  93       * @param array $data
  94       * @param array $files
  95       * @return array
  96       */
  97      public function validation($data, $files) {
  98          $errors = [];
  99  
 100          if (empty($data['coursecontextids'])) {
 101              $errors['coursecontextids'] = get_string('errornoselectedcourse', 'tool_dataprivacy');
 102          }
 103  
 104          return $errors;
 105      }
 106  }