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.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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  namespace mod_data\form;
  18  
  19  use context;
  20  use moodle_exception;
  21  use moodle_url;
  22  use core_form\dynamic_form;
  23  
  24  /**
  25   * Save database as preset form.
  26   *
  27   * @package    mod_data
  28   * @copyright  2021 Mihail Geshoski <paulh@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class save_as_preset extends dynamic_form {
  32  
  33      /**
  34       * Form definition
  35       */
  36      protected function definition() {
  37  
  38          $this->_form->addElement('hidden', 'd');
  39          $this->_form->setType('d', PARAM_INT);
  40          $this->_form->addElement('hidden', 'action', 'save2');
  41          $this->_form->setType('action', PARAM_ALPHANUM);
  42          $this->_form->addElement('text', 'name', get_string('name'), ['size' => 60]);
  43          $this->_form->setType('name', PARAM_FILE);
  44          $this->_form->addRule('name', null, 'required');
  45          $this->_form->addElement('checkbox', 'overwrite', '', get_string('overrwritedesc', 'data'));
  46      }
  47  
  48      /**
  49       * Return form context
  50       *
  51       * @return context
  52       */
  53      protected function get_context_for_dynamic_submission(): context {
  54          global $DB;
  55  
  56          $d = $this->optional_param('d', null, PARAM_INT);
  57          $data = $DB->get_record('data', array('id' => $d), '*', MUST_EXIST);
  58          $course = $DB->get_record('course', array('id' => $data->course), '*', MUST_EXIST);
  59          $cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);
  60  
  61          return \context_module::instance($cm->id, MUST_EXIST);
  62      }
  63  
  64      /**
  65       * Perform some validation.
  66       *
  67       * @param array $formdata
  68       * @param array $files
  69       * @return array
  70       */
  71      public function validation($formdata, $files): array {
  72  
  73          $errors = parent::validation($formdata, $files);
  74          $context = $this->get_context_for_dynamic_submission();
  75  
  76          if (!empty($formdata['overwrite'])) {
  77              $presets = data_get_available_presets($context);
  78              $selectedpreset = new \stdClass();
  79              foreach ($presets as $preset) {
  80                  if ($preset->name == $formdata['name']) {
  81                      $selectedpreset = $preset;
  82                      break;
  83                  }
  84              }
  85              if (isset($selectedpreset->name) && !data_user_can_delete_preset($context, $selectedpreset)) {
  86                  $errors['name'] = get_string('cannotoverwritepreset', 'data');
  87              }
  88          } else {
  89              // If the preset exists now then we need to throw an error.
  90              $sitepresets = data_get_available_site_presets($context);
  91              foreach ($sitepresets as $preset) {
  92                  if ($formdata['name'] == $preset->name) {
  93                      $errors['name'] = get_string('errorpresetexists', 'data');
  94                  }
  95              }
  96          }
  97  
  98          return $errors;
  99      }
 100  
 101      /**
 102       * Check if current user has access to this form, otherwise throw exception.
 103       *
 104       * @return void
 105       * @throws moodle_exception
 106       */
 107      protected function check_access_for_dynamic_submission(): void {
 108          global $DB;
 109  
 110          if (!has_capability('mod/data:managetemplates', $this->get_context_for_dynamic_submission())) {
 111              throw new moodle_exception('saveaspresetmissingcapability', 'data');
 112          }
 113  
 114          $d = $this->optional_param('d', null, PARAM_INT);
 115          $hasfields = $DB->record_exists('data_fields', ['dataid' => $d]);
 116  
 117          if (!$hasfields) {
 118              throw new moodle_exception('nofieldindatabase', 'data');
 119          }
 120      }
 121  
 122      /**
 123       * Process the form submission, used if form was submitted via AJAX.
 124       *
 125       * @return array
 126       */
 127      public function process_dynamic_submission(): array {
 128          global $DB, $CFG;
 129          require_once($CFG->dirroot . '/mod/data/lib.php');
 130  
 131          $result = false;
 132          $errors = [];
 133          $data = $DB->get_record('data', array('id' => $this->get_data()->d), '*', MUST_EXIST);
 134          $course = $DB->get_record('course', array('id' => $data->course), '*', MUST_EXIST);
 135          $cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);
 136          $context = \context_module::instance($cm->id, MUST_EXIST);
 137  
 138          try {
 139              if (!empty($this->get_data()->overwrite)) {
 140                  $presets = data_get_available_presets($context);
 141                  $selectedpreset = new \stdClass();
 142                  foreach ($presets as $preset) {
 143                      if ($preset->name == $this->get_data()->name) {
 144                          $selectedpreset = $preset;
 145                          break;
 146                      }
 147                  }
 148                  if (isset($selectedpreset->name) && data_user_can_delete_preset($context, $selectedpreset)) {
 149                      data_delete_site_preset($this->get_data()->name);
 150                  }
 151              }
 152              data_presets_save($course, $cm, $data, $this->get_data()->name);
 153              $result = true;
 154          } catch (\Exception $e) {
 155              $errors[] = $e->getMessage();
 156          }
 157  
 158          return [
 159              'result' => $result,
 160              'errors' => $errors,
 161          ];
 162      }
 163  
 164      /**
 165       * Load in existing data as form defaults.
 166       *
 167       * @return void
 168       */
 169      public function set_data_for_dynamic_submission(): void {
 170          $data = (object)[
 171              'd' => $this->optional_param('d', 0, PARAM_INT),
 172          ];
 173          $this->set_data($data);
 174      }
 175  
 176      /**
 177       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
 178       *
 179       * @return moodle_url
 180       */
 181      protected function get_page_url_for_dynamic_submission(): moodle_url {
 182          $d = $this->optional_param('d', null, PARAM_INT);
 183  
 184          return new moodle_url('/user/field.php', ['d' => $d]);
 185      }
 186  }