Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Customfield package
  19   *
  20   * @package   core_customfield
  21   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_customfield;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  global $CFG;
  30  require_once($CFG->libdir . '/formslib.php');
  31  
  32  /**
  33   * Class field_config_form
  34   *
  35   * @package core_customfield
  36   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class field_config_form extends \moodleform {
  40  
  41      /**
  42       * Class definition
  43       *
  44       * @throws \coding_exception
  45       */
  46      public function definition() {
  47          global $PAGE;
  48          $mform = $this->_form;
  49  
  50          $field = $this->_customdata['field'];
  51          if (!($field && $field instanceof field_controller)) {
  52              throw new \coding_exception('Field must be passed in customdata');
  53          }
  54          $handler = $field->get_handler();
  55  
  56          $mform->addElement('header', '_commonsettings', get_string('commonsettings', 'core_customfield'));
  57  
  58          $mform->addElement('text', 'name', get_string('fieldname', 'core_customfield'), 'size="50"');
  59          $mform->addRule('name', null, 'required', null, 'client');
  60          $mform->setType('name', PARAM_TEXT);
  61  
  62          // Accepted values for 'shortname' would follow [a-z0-9_] pattern,
  63          // but we are accepting any PARAM_TEXT value here,
  64          // and checking [a-zA-Z0-9_] pattern in validation() function to throw an error when needed.
  65          $mform->addElement('text', 'shortname', get_string('fieldshortname', 'core_customfield'), 'size=20');
  66          $mform->addHelpButton('shortname', 'shortname', 'core_customfield');
  67          $mform->addRule('shortname', null, 'required', null, 'client');
  68          $mform->setType('shortname', PARAM_TEXT);
  69  
  70          $desceditoroptions = $handler->get_description_text_options();
  71          $mform->addElement('editor', 'description_editor', get_string('description', 'core_customfield'), null, $desceditoroptions);
  72          $mform->addHelpButton('description_editor', 'description', 'core_customfield');
  73  
  74          // If field is required.
  75          $mform->addElement('selectyesno', 'configdata[required]', get_string('isfieldrequired', 'core_customfield'));
  76          $mform->addHelpButton('configdata[required]', 'isfieldrequired', 'core_customfield');
  77          $mform->setType('configdata[required]', PARAM_BOOL);
  78  
  79          // If field data is unique.
  80          $mform->addElement('selectyesno', 'configdata[uniquevalues]', get_string('isdataunique', 'core_customfield'));
  81          $mform->addHelpButton('configdata[uniquevalues]', 'isdataunique', 'core_customfield');
  82          $mform->setType('configdata[uniquevalues]', PARAM_BOOL);
  83  
  84          // Field specific settings from field type.
  85          $field->config_form_definition($mform);
  86  
  87          // Handler/component settings.
  88          $handler->config_form_definition($mform);
  89  
  90          // We add hidden fields.
  91          $mform->addElement('hidden', 'categoryid');
  92          $mform->setType('categoryid', PARAM_INT);
  93  
  94          $mform->addElement('hidden', 'type');
  95          $mform->setType('type', PARAM_COMPONENT);
  96  
  97          $mform->addElement('hidden', 'id');
  98          $mform->setType('id', PARAM_INT);
  99  
 100          $this->add_action_buttons(true);
 101      }
 102  
 103      /**
 104       * Field data validation
 105       *
 106       * @param array $data
 107       * @param array $files
 108       * @return array
 109       */
 110      public function validation($data, $files = array()) {
 111          global $DB;
 112  
 113          $errors = array();
 114          /** @var field_controller $field */
 115          $field = $this->_customdata['field'];
 116          $handler = $field->get_handler();
 117  
 118          // Check the shortname is specified and is unique for this component-area-itemid combination.
 119          if (!preg_match('/^[a-z0-9_]+$/', $data['shortname'])) {
 120              // Check allowed pattern (numbers, letters and underscore).
 121              $errors['shortname'] = get_string('invalidshortnameerror', 'core_customfield');
 122          } else if ($DB->record_exists_sql('SELECT 1 FROM {customfield_field} f ' .
 123              'JOIN {customfield_category} c ON c.id = f.categoryid ' .
 124              'WHERE f.shortname = ? AND f.id <> ? AND c.component = ? AND c.area = ? AND c.itemid = ?',
 125              [$data['shortname'], $data['id'],
 126                  $handler->get_component(), $handler->get_area(), $handler->get_itemid()])) {
 127              $errors['shortname'] = get_string('formfieldcheckshortname', 'core_customfield');
 128          }
 129  
 130          $errors = array_merge($errors, $field->config_form_validation($data, $files));
 131  
 132          return $errors;
 133      }
 134  }