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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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   * Class file field for database activity
  19   *
  20   * @package    datafield_file
  21   * @copyright  2005 Martin Dougiamas
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  class data_field_file extends data_field_base {
  25      var $type = 'file';
  26  
  27      public function supports_preview(): bool {
  28          return true;
  29      }
  30  
  31      public function get_data_content_preview(int $recordid): stdClass {
  32          return (object)[
  33              'id' => 0,
  34              'fieldid' => $this->field->id,
  35              'recordid' => $recordid,
  36              'content' => 'samplefile.csv',
  37              'content1' => 'samplefile.csv',
  38              'content2' => null,
  39              'content3' => null,
  40              'content4' => null,
  41          ];
  42      }
  43  
  44      function display_add_field($recordid = 0, $formdata = null) {
  45          global $CFG, $DB, $OUTPUT, $PAGE;
  46  
  47          // Necessary for the constants used in args.
  48          require_once($CFG->dirroot . '/repository/lib.php');
  49  
  50          $itemid = null;
  51  
  52          // editing an existing database entry
  53          if ($formdata) {
  54              $fieldname = 'field_' . $this->field->id . '_file';
  55              $itemid = clean_param($formdata->$fieldname, PARAM_INT);
  56          } else if ($recordid) {
  57              if (!$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
  58                  // Quickly make one now!
  59                  $content = new stdClass();
  60                  $content->fieldid  = $this->field->id;
  61                  $content->recordid = $recordid;
  62                  $id = $DB->insert_record('data_content', $content);
  63                  $content = $DB->get_record('data_content', array('id' => $id));
  64              }
  65              file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
  66  
  67          } else {
  68              $itemid = file_get_unused_draft_itemid();
  69          }
  70  
  71          // database entry label
  72          $html = '<div title="' . s($this->field->description) . '">';
  73          $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name;
  74  
  75          if ($this->field->required) {
  76              $html .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
  77              $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  78              $html .= html_writer::div($image, 'inline-req');
  79          } else {
  80              $html .= '</span></legend>';
  81          }
  82  
  83          // itemid element
  84          $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.s($itemid).'" />';
  85  
  86          $options = new stdClass();
  87          $options->maxbytes = $this->field->param3;
  88          $options->maxfiles  = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future.
  89          $options->itemid    = $itemid;
  90          $options->accepted_types = '*';
  91          $options->return_types = FILE_INTERNAL | FILE_CONTROLLED_LINK;
  92          $options->context = $PAGE->context;
  93  
  94          $fm = new form_filemanager($options);
  95          // Print out file manager.
  96  
  97          $output = $PAGE->get_renderer('core', 'files');
  98          $html .= '<div class="mod-data-input">';
  99          $html .= $output->render($fm);
 100          $html .= '</div>';
 101          $html .= '</fieldset>';
 102          $html .= '</div>';
 103  
 104          return $html;
 105      }
 106  
 107      function display_search_field($value = '') {
 108          return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
 109                 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" ' .
 110                      'value="'.s($value).'" class="form-control"/>';
 111      }
 112  
 113      function generate_sql($tablealias, $value) {
 114          global $DB;
 115  
 116          static $i=0;
 117          $i++;
 118          $name = "df_file_$i";
 119          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 120      }
 121  
 122      public function parse_search_field($defaults = null) {
 123          $param = 'f_'.$this->field->id;
 124          if (empty($defaults[$param])) {
 125              $defaults = array($param => '');
 126          }
 127          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
 128      }
 129  
 130      function get_file($recordid, $content=null) {
 131          global $DB;
 132          if (empty($content)) {
 133              if (!$content = $this->get_data_content($recordid)) {
 134                  return null;
 135              }
 136          }
 137          $fs = get_file_storage();
 138          if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
 139              return null;
 140          }
 141  
 142          return $file;
 143      }
 144  
 145      function display_browse_field($recordid, $template) {
 146          global $OUTPUT;
 147  
 148          $content = $this->get_data_content($recordid);
 149  
 150          if (!$content || empty($content->content)) {
 151              return '';
 152          }
 153  
 154          $file = null;
 155          $url = '';
 156          $name = !empty($content->content1) ? $content->content1 : $content->content;
 157  
 158          if ($this->preview) {
 159              $file = (object)[
 160                  'filename' => $content->content,
 161                  'mimetype' => 'text/csv',
 162              ];
 163              $name = $content->content;
 164          } else {
 165              $file = $this->get_file($recordid, $content);
 166              if (!$file) {
 167                  return '';
 168              }
 169              $fileurl = moodle_url::make_pluginfile_url(
 170                  $file->get_contextid(),
 171                  $file->get_component(),
 172                  $file->get_filearea(),
 173                  $file->get_itemid(),
 174                  $file->get_filepath(),
 175                  $file->get_filename()
 176              );
 177              $url = $fileurl->out();
 178          }
 179  
 180          $icon = $OUTPUT->pix_icon(
 181              file_file_icon($file),
 182              get_mimetype_description($file),
 183              'moodle',
 184              ['width' => 16, 'height' => 16]
 185          );
 186  
 187          return $icon . '&nbsp;<a class="data-field-link" href="'.$url.'" >' . s($name) . '</a>';
 188      }
 189  
 190  
 191      // content: "a##b" where a is the file name, b is the display name
 192      function update_content($recordid, $value, $name='') {
 193          global $CFG, $DB, $USER;
 194          $fs = get_file_storage();
 195  
 196          // Should always be available since it is set by display_add_field before initializing the draft area.
 197          $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
 198          if (!$content) {
 199              $content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid);
 200              $content->id = $DB->insert_record('data_content', $content);
 201          }
 202  
 203          file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id);
 204  
 205          $usercontext = context_user::instance($USER->id);
 206          $files = $fs->get_area_files($this->context->id, 'mod_data', 'content', $content->id, 'itemid, filepath, filename', false);
 207  
 208          // We expect no or just one file (maxfiles = 1 option is set for the form_filemanager).
 209          if (count($files) == 0) {
 210              $content->content = null;
 211          } else {
 212              $content->content = array_values($files)[0]->get_filename();
 213              if (count($files) > 1) {
 214                  // This should not happen with a consistent database. Inform admins/developers about the inconsistency.
 215                  debugging('more then one file found in mod_data instance {$this->data->id} file field (field id: {$this->field->id}) area during update data record {$recordid} (content id: {$content->id})', DEBUG_NORMAL);
 216              }
 217          }
 218          $DB->update_record('data_content', $content);
 219      }
 220  
 221      function text_export_supported() {
 222          return false;
 223      }
 224  
 225      function file_ok($path) {
 226          return true;
 227      }
 228  
 229      /**
 230       * Custom notempty function
 231       *
 232       * @param string $value
 233       * @param string $name
 234       * @return bool
 235       */
 236      function notemptyfield($value, $name) {
 237          global $USER;
 238  
 239          $names = explode('_', $name);
 240          if ($names[2] == 'file') {
 241              $usercontext = context_user::instance($USER->id);
 242              $fs = get_file_storage();
 243              $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value);
 244              return count($files) >= 2;
 245          }
 246          return false;
 247      }
 248  
 249      /**
 250       * Return the plugin configs for external functions.
 251       *
 252       * @return array the list of config parameters
 253       * @since Moodle 3.3
 254       */
 255      public function get_config_for_external() {
 256          // Return all the config parameters.
 257          $configs = [];
 258          for ($i = 1; $i <= 10; $i++) {
 259              $configs["param$i"] = $this->field->{"param$i"};
 260          }
 261          return $configs;
 262      }
 263  }