Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   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 field plugin
  19   *
  20   * @package   customfield_text
  21   * @copyright 2018 Daniel Neis Araujo <daniel@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  use core_customfield\api;
  30  
  31  /**
  32   * Class data
  33   *
  34   * @package customfield_text
  35   * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class data_controller extends \core_customfield\data_controller {
  39  
  40      /**
  41       * Return the name of the field where the information is stored
  42       * @return string
  43       */
  44      public function datafield() : string {
  45          return 'charvalue';
  46      }
  47  
  48      /**
  49       * Add fields for editing a text field.
  50       *
  51       * @param \MoodleQuickForm $mform
  52       */
  53      public function instance_form_definition(\MoodleQuickForm $mform) {
  54          $field = $this->get_field();
  55          $config = $field->get('configdata');
  56          $type = $config['ispassword'] ? 'password' : 'text';
  57          $elementname = $this->get_form_element_name();
  58          $mform->addElement($type, $elementname, $this->get_field()->get_formatted_name(), 'size=' . (int)$config['displaysize']);
  59          $mform->setType($elementname, PARAM_TEXT);
  60          if (!empty($config['defaultvalue'])) {
  61              $mform->setDefault($elementname, $config['defaultvalue']);
  62          }
  63          if ($field->get_configdata_property('required')) {
  64              $mform->addRule($elementname, null, 'required', null, 'client');
  65          }
  66      }
  67  
  68      /**
  69       * Validates data for this field.
  70       *
  71       * @param array $data
  72       * @param array $files
  73       * @return array
  74       */
  75      public function instance_form_validation(array $data, array $files) : array {
  76  
  77          $errors = parent::instance_form_validation($data, $files);
  78          $maxlength = $this->get_field()->get_configdata_property('maxlength');
  79          $elementname = $this->get_form_element_name();
  80          if (($maxlength > 0) && ($maxlength < \core_text::strlen($data[$elementname]))) {
  81              $errors[$elementname] = get_string('errormaxlength', 'customfield_text', $maxlength);
  82          }
  83          return $errors;
  84      }
  85  
  86      /**
  87       * Returns the default value as it would be stored in the database (not in human-readable format).
  88       *
  89       * @return mixed
  90       */
  91      public function get_default_value() {
  92          return $this->get_field()->get_configdata_property('defaultvalue');
  93      }
  94  
  95      /**
  96       * Returns value in a human-readable format
  97       *
  98       * @return mixed|null value or null if empty
  99       */
 100      public function export_value() {
 101          $value = parent::export_value();
 102          if ($value === null) {
 103              return null;
 104          }
 105  
 106          $link = $this->get_field()->get_configdata_property('link');
 107          if ($link) {
 108              $linktarget = $this->get_field()->get_configdata_property('linktarget');
 109              $url = str_replace('$$', urlencode($this->get_value()), $link);
 110              $attributes = $linktarget ? ['target' => $linktarget] : [];
 111              $value = \html_writer::link($url, $value, $attributes);
 112          }
 113  
 114          return $value;
 115      }
 116  }