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.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  //                                                                       //
  16  // This program is distributed in the hope that it will be useful,       //
  17  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  18  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  19  // GNU General Public License for more details:                          //
  20  //                                                                       //
  21  //          http://www.gnu.org/copyleft/gpl.html                         //
  22  //                                                                       //
  23  ///////////////////////////////////////////////////////////////////////////
  24  
  25  class data_field_file extends data_field_base {
  26      var $type = 'file';
  27  
  28      function display_add_field($recordid = 0, $formdata = null) {
  29          global $DB, $OUTPUT, $PAGE;
  30  
  31          $itemid = null;
  32  
  33          // editing an existing database entry
  34          if ($formdata) {
  35              $fieldname = 'field_' . $this->field->id . '_file';
  36              $itemid = clean_param($formdata->$fieldname, PARAM_INT);
  37          } else if ($recordid) {
  38              if (!$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
  39                  // Quickly make one now!
  40                  $content = new stdClass();
  41                  $content->fieldid  = $this->field->id;
  42                  $content->recordid = $recordid;
  43                  $id = $DB->insert_record('data_content', $content);
  44                  $content = $DB->get_record('data_content', array('id' => $id));
  45              }
  46              file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
  47  
  48          } else {
  49              $itemid = file_get_unused_draft_itemid();
  50          }
  51  
  52          // database entry label
  53          $html = '<div title="' . s($this->field->description) . '">';
  54          $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name;
  55  
  56          if ($this->field->required) {
  57              $html .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
  58              $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  59              $html .= html_writer::div($image, 'inline-req');
  60          } else {
  61              $html .= '</span></legend>';
  62          }
  63  
  64          // itemid element
  65          $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.s($itemid).'" />';
  66  
  67          $options = new stdClass();
  68          $options->maxbytes = $this->field->param3;
  69          $options->maxfiles  = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future.
  70          $options->itemid    = $itemid;
  71          $options->accepted_types = '*';
  72          $options->return_types = FILE_INTERNAL | FILE_CONTROLLED_LINK;
  73          $options->context = $PAGE->context;
  74  
  75          $fm = new form_filemanager($options);
  76          // Print out file manager.
  77  
  78          $output = $PAGE->get_renderer('core', 'files');
  79          $html .= '<div class="mod-data-input">';
  80          $html .= $output->render($fm);
  81          $html .= '</div>';
  82          $html .= '</fieldset>';
  83          $html .= '</div>';
  84  
  85          return $html;
  86      }
  87  
  88      function display_search_field($value = '') {
  89          return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
  90                 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" ' .
  91                      'value="'.s($value).'" class="form-control"/>';
  92      }
  93  
  94      function generate_sql($tablealias, $value) {
  95          global $DB;
  96  
  97          static $i=0;
  98          $i++;
  99          $name = "df_file_$i";
 100          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 101      }
 102  
 103      public function parse_search_field($defaults = null) {
 104          $param = 'f_'.$this->field->id;
 105          if (empty($defaults[$param])) {
 106              $defaults = array($param => '');
 107          }
 108          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
 109      }
 110  
 111      function get_file($recordid, $content=null) {
 112          global $DB;
 113          if (empty($content)) {
 114              if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 115                  return null;
 116              }
 117          }
 118          $fs = get_file_storage();
 119          if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
 120              return null;
 121          }
 122  
 123          return $file;
 124      }
 125  
 126      function display_browse_field($recordid, $template) {
 127          global $CFG, $DB, $OUTPUT;
 128  
 129          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 130              return '';
 131          }
 132  
 133          if (empty($content->content)) {
 134              return '';
 135          }
 136  
 137          if (!$file = $this->get_file($recordid, $content)) {
 138              return '';
 139          }
 140  
 141          $name   = empty($content->content1) ? $file->get_filename() : $content->content1;
 142          $src    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
 143          $width  = $this->field->param1 ? ' width  = "'.s($this->field->param1).'" ':' ';
 144          $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
 145  
 146          $str = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('width' => 16, 'height' => 16)). '&nbsp;'.
 147                 '<a href="'.$src.'" >'.s($name).'</a>';
 148          return $str;
 149      }
 150  
 151  
 152      // content: "a##b" where a is the file name, b is the display name
 153      function update_content($recordid, $value, $name='') {
 154          global $CFG, $DB, $USER;
 155          $fs = get_file_storage();
 156  
 157          // Should always be available since it is set by display_add_field before initializing the draft area.
 158          $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
 159          if (!$content) {
 160              $content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid);
 161              $content->id = $DB->insert_record('data_content', $content);
 162          }
 163  
 164          file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id);
 165  
 166          $usercontext = context_user::instance($USER->id);
 167          $files = $fs->get_area_files($this->context->id, 'mod_data', 'content', $content->id, 'itemid, filepath, filename', false);
 168  
 169          // We expect no or just one file (maxfiles = 1 option is set for the form_filemanager).
 170          if (count($files) == 0) {
 171              $content->content = null;
 172          } else {
 173              $content->content = array_values($files)[0]->get_filename();
 174              if (count($files) > 1) {
 175                  // This should not happen with a consistent database. Inform admins/developers about the inconsistency.
 176                  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);
 177              }
 178          }
 179          $DB->update_record('data_content', $content);
 180      }
 181  
 182      function text_export_supported() {
 183          return false;
 184      }
 185  
 186      function file_ok($path) {
 187          return true;
 188      }
 189  
 190      /**
 191       * Custom notempty function
 192       *
 193       * @param string $value
 194       * @param string $name
 195       * @return bool
 196       */
 197      function notemptyfield($value, $name) {
 198          global $USER;
 199  
 200          $names = explode('_', $name);
 201          if ($names[2] == 'file') {
 202              $usercontext = context_user::instance($USER->id);
 203              $fs = get_file_storage();
 204              $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value);
 205              return count($files) >= 2;
 206          }
 207          return false;
 208      }
 209  
 210      /**
 211       * Return the plugin configs for external functions.
 212       *
 213       * @return array the list of config parameters
 214       * @since Moodle 3.3
 215       */
 216      public function get_config_for_external() {
 217          // Return all the config parameters.
 218          $configs = [];
 219          for ($i = 1; $i <= 10; $i++) {
 220              $configs["param$i"] = $this->field->{"param$i"};
 221          }
 222          return $configs;
 223      }
 224  }