Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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   * Customfields text plugin
  19   *
  20   * @package   customfield_text
  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 customfield_text;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Class field
  31   *
  32   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @package customfield_text
  35   */
  36  class field_controller extends \core_customfield\field_controller {
  37      /**
  38       * Plugin type text
  39       */
  40      const TYPE = 'text';
  41  
  42      /**
  43       * Add fields for editing a text field.
  44       *
  45       * @param \MoodleQuickForm $mform
  46       */
  47      public function config_form_definition(\MoodleQuickForm $mform) {
  48  
  49          $mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_text'));
  50          $mform->setExpanded('header_specificsettings', true);
  51  
  52          $mform->addElement('text', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'),
  53              ['size' => 50]);
  54          $mform->setType('configdata[defaultvalue]', PARAM_TEXT);
  55  
  56          $mform->addElement('text', 'configdata[displaysize]', get_string('displaysize', 'customfield_text'), ['size' => 6]);
  57          $mform->setType('configdata[displaysize]', PARAM_INT);
  58          if (!$this->get_configdata_property('displaysize')) {
  59              $mform->setDefault('configdata[displaysize]', 50);
  60          }
  61          $mform->addRule('configdata[displaysize]', null, 'numeric', null, 'client');
  62  
  63          $mform->addElement('text', 'configdata[maxlength]', get_string('maxlength', 'customfield_text'), ['size' => 6]);
  64          $mform->setType('configdata[maxlength]', PARAM_INT);
  65          if (!$this->get_configdata_property('maxlength')) {
  66              $mform->setDefault('configdata[maxlength]', 1333);
  67          }
  68          $mform->addRule('configdata[maxlength]', null, 'numeric', null, 'client');
  69  
  70          $mform->addElement('selectyesno', 'configdata[ispassword]', get_string('ispassword', 'customfield_text'));
  71          $mform->setType('configdata[ispassword]', PARAM_INT);
  72  
  73          $mform->addElement('text', 'configdata[link]', get_string('islink', 'customfield_text'), ['size' => 50]);
  74          $mform->setType('configdata[link]', PARAM_RAW_TRIMMED);
  75          $mform->addHelpButton('configdata[link]', 'islink', 'customfield_text');
  76  
  77          $mform->disabledIf('configdata[link]', 'configdata[ispassword]', 'eq', 1);
  78  
  79          $linkstargetoptions = array(
  80              ''       => get_string('none', 'customfield_text'),
  81              '_blank' => get_string('newwindow', 'customfield_text'),
  82              '_self'  => get_string('sameframe', 'customfield_text'),
  83              '_top'   => get_string('samewindow', 'customfield_text')
  84          );
  85          $mform->addElement('select', 'configdata[linktarget]', get_string('linktarget', 'customfield_text'),
  86              $linkstargetoptions);
  87  
  88          $mform->disabledIf('configdata[linktarget]', 'configdata[link]', 'eq', '');
  89      }
  90  
  91      /**
  92       * Validate the data on the field configuration form
  93       *
  94       * @param array $data from the add/edit profile field form
  95       * @param array $files
  96       * @return array associative array of error messages
  97       */
  98      public function config_form_validation(array $data, $files = array()) : array {
  99          global $CFG;
 100          $errors = parent::config_form_validation($data, $files);
 101  
 102          $maxlength = (int)$data['configdata']['maxlength'];
 103          if ($maxlength < 1 || $maxlength > 1333) {
 104              $errors['configdata[maxlength]'] = get_string('errorconfigmaxlen', 'customfield_text');
 105          }
 106  
 107          $displaysize = (int)$data['configdata']['displaysize'];
 108          if ($displaysize < 1 || $displaysize > 200) {
 109              $errors['configdata[displaysize]'] = get_string('errorconfigdisplaysize', 'customfield_text');
 110          }
 111  
 112          if (isset($data['configdata']['link'])) {
 113              $link = $data['configdata']['link'];
 114              if (strlen($link)) {
 115                  require_once($CFG->dirroot . '/lib/validateurlsyntax.php');
 116                  if (strpos($link, '$$') === false) {
 117                      $errors['configdata[link]'] = get_string('errorconfiglinkplaceholder', 'customfield_text');
 118                  } else if (!validateUrlSyntax(str_replace('$$', 'XYZ', $link), 's+H?S?F-E-u-P-a?I?p?f?q?r?')) {
 119                      // This validation is more strict than PARAM_URL - it requires the protocol and it must be either http or https.
 120                      $errors['configdata[link]'] = get_string('errorconfiglinksyntax', 'customfield_text');
 121                  }
 122              }
 123          }
 124  
 125          return $errors;
 126      }
 127  
 128      /**
 129       * Does this custom field type support being used as part of the block_myoverview
 130       * custom field grouping?
 131       * @return bool
 132       */
 133      public function supports_course_grouping(): bool {
 134          return true;
 135      }
 136  
 137      /**
 138       * If this field supports course grouping, then this function needs overriding to
 139       * return the formatted values for this.
 140       * @param array $values the used values that need formatting
 141       * @return array
 142       */
 143      public function course_grouping_format_values($values): array {
 144          $ret = [];
 145          foreach ($values as $value) {
 146              $ret[$value] = format_string($value);
 147          }
 148          $ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
 149              $this->get_formatted_name());
 150          return $ret;
 151      }
 152  }