Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  namespace tool_dataprivacy\form;
  19  
  20  use context;
  21  use context_user;
  22  use moodle_exception;
  23  use moodle_url;
  24  use core_form\dynamic_form;
  25  use tool_dataprivacy\api;
  26  use tool_dataprivacy\external;
  27  
  28  /**
  29   * Contact DPO modal form
  30   *
  31   * @package    tool_dataprivacy
  32   * @copyright  2021 Paul Holden <paulh@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class contactdpo extends dynamic_form {
  36  
  37      /**
  38       * Form definition
  39       */
  40      protected function definition() {
  41          global $USER;
  42  
  43          $mform = $this->_form;
  44  
  45          $mform->addElement('static', 'replyto', get_string('replyto', 'tool_dataprivacy'), s($USER->email));
  46  
  47          $mform->addElement('textarea', 'message', get_string('message', 'tool_dataprivacy'), 'cols="60" rows="8"');
  48          $mform->setType('message', PARAM_TEXT);
  49          $mform->addRule('message', get_string('required'), 'required', null, 'client');
  50      }
  51  
  52      /**
  53       * Return form context
  54       *
  55       * @return context
  56       */
  57      protected function get_context_for_dynamic_submission(): context {
  58          global $USER;
  59  
  60          return context_user::instance($USER->id);
  61      }
  62  
  63      /**
  64       * Check if current user has access to this form, otherwise throw exception
  65       *
  66       * @throws moodle_exception
  67       */
  68      protected function check_access_for_dynamic_submission(): void {
  69          if (!api::can_contact_dpo()) {
  70              throw new moodle_exception('errorcontactdpodisabled', 'tool_dataprivacy');
  71          }
  72      }
  73  
  74      /**
  75       * Process the form submission, used if form was submitted via AJAX
  76       *
  77       * @return array
  78       */
  79      public function process_dynamic_submission() {
  80          return external::contact_dpo($this->get_data()->message);
  81      }
  82  
  83      /**
  84       * Load in existing data as form defaults (not applicable)
  85       */
  86      public function set_data_for_dynamic_submission(): void {
  87          return;
  88      }
  89  
  90      /**
  91       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
  92       *
  93       * @return moodle_url
  94       */
  95      protected function get_page_url_for_dynamic_submission(): moodle_url {
  96          global $USER;
  97  
  98          return new moodle_url('/user/profile.php', ['id' => $USER->id]);
  99      }
 100  }