Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  ///////////////////////////////////////////////////////////////////////////
   3  //                                                                       //
   4  // NOTICE OF COPYRIGHT                                                   //
   5  //                                                                       //
   6  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   7  //          http://moodle.org                                            //
   8  //                                                                       //
   9  // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
  10  //                                                                       //
  11  // This program is free software; you can redistribute it and/or modify  //
  12  // it under the terms of the GNU General Public License as published by  //
  13  // the Free Software Foundation; either version 2 of the License, or     //
  14  // (at your option) any later version.                                   // //                                                                       //
  15  // This program is distributed in the hope that it will be useful,       //
  16  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  17  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  18  // GNU General Public License for more details:                          //
  19  //                                                                       //
  20  //          http://www.gnu.org/copyleft/gpl.html                         //
  21  //                                                                       //
  22  ///////////////////////////////////////////////////////////////////////////
  23  
  24  require_once($CFG->dirroot.'/lib/filelib.php');
  25  require_once($CFG->dirroot.'/repository/lib.php');
  26  
  27  class data_field_textarea extends data_field_base {
  28  
  29      var $type = 'textarea';
  30      /**
  31       * priority for globalsearch indexing
  32       *
  33       * @var int
  34       */
  35      protected static $priority = self::LOW_PRIORITY;
  36  
  37      /**
  38       * Returns options for embedded files
  39       *
  40       * @return array
  41       */
  42      private function get_options() {
  43          if (!isset($this->field->param5)) {
  44              $this->field->param5 = 0;
  45          }
  46          $options = array();
  47          $options['trusttext'] = false;
  48          $options['forcehttps'] = false;
  49          $options['subdirs'] = false;
  50          $options['maxfiles'] = -1;
  51          $options['context'] = $this->context;
  52          $options['maxbytes'] = $this->field->param5;
  53          $options['changeformat'] = 0;
  54          $options['noclean'] = false;
  55          return $options;
  56      }
  57  
  58      function display_add_field($recordid = 0, $formdata = null) {
  59          global $CFG, $DB, $OUTPUT, $PAGE;
  60  
  61          $text   = '';
  62          $format = 0;
  63          $str = '<div title="' . s($this->field->description) . '" class="d-inline-flex">';
  64          $str .= '<label for="field_' . $this->field->id . '">';
  65          $str .= html_writer::span($this->field->name, 'accesshide');
  66          if ($this->field->required) {
  67              $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  68              $str .= html_writer::div($image, 'inline-req');
  69          }
  70          $str .= '</label>';
  71  
  72          editors_head_setup();
  73          $options = $this->get_options();
  74  
  75          $itemid = $this->field->id;
  76          $field = 'field_'.$itemid;
  77  
  78          if ($formdata) {
  79              $fieldname = 'field_' . $this->field->id . '_content1';
  80              if (isset($formdata->$fieldname)) {
  81                  $format = $formdata->$fieldname;
  82              } else {
  83                  $format = file_get_unused_draft_itemid();
  84              }
  85              $fieldname = 'field_' . $this->field->id . '_itemid';
  86              if (isset($formdata->$fieldname)) {
  87                  $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
  88              } else {
  89                  $draftitemid = file_get_unused_draft_itemid();
  90              }
  91              $fieldname = 'field_' . $this->field->id;
  92              if (isset($formdata->$fieldname)) {
  93                  $text = $formdata->$fieldname;
  94              }
  95          } else if ($recordid &&
  96                     $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
  97              $format = $content->content1;
  98              $text = clean_text($content->content, $format);
  99              $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
 100          } else {
 101              $draftitemid = file_get_unused_draft_itemid();
 102              $format = FORMAT_HTML;
 103          }
 104  
 105          // get filepicker info
 106          //
 107          $fpoptions = array();
 108          if ($options['maxfiles'] != 0 ) {
 109              $args = new stdClass();
 110              // need these three to filter repositories list
 111              $args->accepted_types = array('web_image');
 112              $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
 113              $args->context = $this->context;
 114              $args->env = 'filepicker';
 115              // advimage plugin
 116              $image_options = initialise_filepicker($args);
 117              $image_options->context = $this->context;
 118              $image_options->client_id = uniqid();
 119              $image_options->maxbytes = $options['maxbytes'];
 120              $image_options->env = 'editor';
 121              $image_options->itemid = $draftitemid;
 122  
 123              // moodlemedia plugin
 124              $args->accepted_types = array('video', 'audio');
 125              $media_options = initialise_filepicker($args);
 126              $media_options->context = $this->context;
 127              $media_options->client_id = uniqid();
 128              $media_options->maxbytes  = $options['maxbytes'];
 129              $media_options->env = 'editor';
 130              $media_options->itemid = $draftitemid;
 131  
 132              // advlink plugin
 133              $args->accepted_types = '*';
 134              $link_options = initialise_filepicker($args);
 135              $link_options->context = $this->context;
 136              $link_options->client_id = uniqid();
 137              $link_options->maxbytes  = $options['maxbytes'];
 138              $link_options->env = 'editor';
 139              $link_options->itemid = $draftitemid;
 140  
 141              // H5P plugin.
 142              $args->accepted_types = ['h5p'];
 143              $h5poptions = initialise_filepicker($args);
 144              $h5poptions->context = $this->context;
 145              $h5poptions->client_id = uniqid();
 146              $h5poptions->maxbytes  = $options['maxbytes'];
 147              $h5poptions->env = 'editor';
 148              $h5poptions->itemid = $draftitemid;
 149  
 150              $fpoptions['image'] = $image_options;
 151              $fpoptions['media'] = $media_options;
 152              $fpoptions['link'] = $link_options;
 153              $fpoptions['h5p'] = $h5poptions;
 154          }
 155  
 156          $editor = editors_get_preferred_editor($format);
 157          $strformats = format_text_menu();
 158          $formats =  $editor->get_supported_formats();
 159          foreach ($formats as $fid) {
 160              $formats[$fid] = $strformats[$fid];
 161          }
 162          $editor->set_text($text);
 163          $editor->use_editor($field, $options, $fpoptions);
 164          $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
 165          $str .= '<div class="mod-data-input">';
 166          $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" class="form-control" ' .
 167              'cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
 168          $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
 169          $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
 170          foreach ($formats as $key=>$desc) {
 171              $selected = ($format == $key) ? 'selected="selected"' : '';
 172              $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
 173          }
 174          $str .= '</select>';
 175  
 176          $str .= '</div>';
 177          $str .= '</div>';
 178          $str .= '</div>';
 179          return $str;
 180      }
 181  
 182  
 183      function display_search_field($value = '') {
 184          return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
 185                 '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
 186                 'value="' . s($value) . '" class="form-control"/>';
 187      }
 188  
 189      public function parse_search_field($defaults = null) {
 190          $param = 'f_'.$this->field->id;
 191          if (empty($defaults[$param])) {
 192              $defaults = array($param => '');
 193          }
 194          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
 195      }
 196  
 197      function generate_sql($tablealias, $value) {
 198          global $DB;
 199  
 200          static $i=0;
 201          $i++;
 202          $name = "df_textarea_$i";
 203          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 204      }
 205  
 206      function print_after_form() {
 207      }
 208  
 209  
 210      function update_content($recordid, $value, $name='') {
 211          global $DB;
 212  
 213          $content = new stdClass();
 214          $content->fieldid = $this->field->id;
 215          $content->recordid = $recordid;
 216  
 217          $names = explode('_', $name);
 218          if (!empty($names[2])) {
 219              if ($names[2] == 'itemid') {
 220                  // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
 221                  return true;
 222              } else {
 223                  $content->{$names[2]} = clean_param($value, PARAM_NOTAGS);  // content[1-4]
 224              }
 225          } else {
 226              $content->content = clean_param($value, PARAM_CLEAN);
 227          }
 228  
 229          if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 230              $content->id = $oldcontent->id;
 231          } else {
 232              $content->id = $DB->insert_record('data_content', $content);
 233              if (!$content->id) {
 234                  return false;
 235              }
 236          }
 237          if (!empty($content->content)) {
 238              $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
 239              $options = $this->get_options();
 240              $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
 241          }
 242          $rv = $DB->update_record('data_content', $content);
 243          return $rv;
 244      }
 245  
 246      /**
 247       * Display the content of the field in browse mode
 248       *
 249       * @param int $recordid
 250       * @param object $template
 251       * @return bool|string
 252       */
 253      function display_browse_field($recordid, $template) {
 254          global $DB;
 255  
 256          if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
 257              if (isset($content->content)) {
 258                  $options = new stdClass();
 259                  if ($this->field->param1 == '1') {  // We are autolinking this field, so disable linking within us
 260                      $options->filter = false;
 261                  }
 262                  $options->para = false;
 263                  $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
 264                  $str = format_text($str, $content->content1, $options);
 265              } else {
 266                  $str = '';
 267              }
 268              return $str;
 269          }
 270          return false;
 271      }
 272  
 273      /**
 274       * Whether this module support files
 275       *
 276       * @param string $relativepath
 277       * @return bool
 278       */
 279      function file_ok($relativepath) {
 280          return true;
 281      }
 282  
 283      /**
 284       * Only look at the first item (second is format)
 285       *
 286       * @param string $value
 287       * @param string $name
 288       * @return bool
 289       */
 290      function notemptyfield($value, $name) {
 291          $names = explode('_', $name);
 292          // Clean first.
 293          if (count($names) == 2) {
 294              // Don't assume that this is coming from a text editor with tags.
 295              return strval($value) !== '';
 296          }
 297          return false;
 298      }
 299  
 300      /**
 301       * Returns the presentable string value for a field content.
 302       *
 303       * The returned string should be plain text.
 304       *
 305       * @param stdClass $content
 306       * @return string
 307       */
 308      public static function get_content_value($content) {
 309          return content_to_text($content->content, $content->content1);
 310      }
 311  
 312      /**
 313       * Return the plugin configs for external functions.
 314       *
 315       * @return array the list of config parameters
 316       * @since Moodle 3.3
 317       */
 318      public function get_config_for_external() {
 319          // Return all the config parameters.
 320          $configs = [];
 321          for ($i = 1; $i <= 10; $i++) {
 322              $configs["param$i"] = $this->field->{"param$i"};
 323          }
 324          return $configs;
 325      }
 326  }