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 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 for exporting stored_file data.
  19   *
  20   * @package    core_files
  21   * @copyright  2015 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_files\external;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use coding_exception;
  28  use core_text;
  29  use moodle_url;
  30  use renderer_base;
  31  use stdClass;
  32  use stored_file;
  33  
  34  /**
  35   * Class for exporting stored_file data.
  36   *
  37   * @package    core_files
  38   * @copyright  2015 Frédéric Massart - FMCorz.net
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class stored_file_exporter extends \core\external\exporter {
  42  
  43      /** @var stored_file */
  44      protected $file;
  45  
  46      public function __construct(stored_file $file, $related = array()) {
  47          $this->file = $file;
  48  
  49          $data = new stdClass();
  50          $data->contextid = $file->get_contextid();
  51          $data->component = $file->get_component();
  52          $data->filearea = $file->get_filearea();
  53          $data->itemid = $file->get_itemid();
  54          $data->filepath = $file->get_filepath();
  55          $data->filename = $file->get_filename();
  56          $data->isdir = $file->is_directory();
  57          $data->isimage = $file->is_valid_image();
  58          $data->timemodified = $file->get_timemodified();
  59          $data->timecreated = $file->get_timecreated();
  60          $data->filesize = $file->get_filesize();
  61          $data->author = $file->get_author();
  62          $data->license = $file->get_license();
  63  
  64          if ($related['context']->id != $data->contextid) {
  65              throw new coding_exception('Unexpected context ID received.');
  66          }
  67  
  68          parent::__construct($data, $related);
  69      }
  70  
  71      protected static function define_related() {
  72          return array('context' => 'context');
  73      }
  74  
  75      protected static function define_properties() {
  76          return array(
  77              'contextid' => array(
  78                  'type' => PARAM_INT
  79              ),
  80              'component' => array(
  81                  'type' => PARAM_COMPONENT
  82              ),
  83              'filearea' => array(
  84                  'type' => PARAM_AREA
  85              ),
  86              'itemid' => array(
  87                  'type' => PARAM_INT
  88              ),
  89              'filepath' => array(
  90                  'type' => PARAM_PATH
  91              ),
  92              'filename' => array(
  93                  'type' => PARAM_FILE
  94              ),
  95              'isdir' => array(
  96                  'type' => PARAM_BOOL
  97              ),
  98              'isimage' => array(
  99                  'type' => PARAM_BOOL
 100              ),
 101              'timemodified' => array(
 102                  'type' => PARAM_INT
 103              ),
 104              'timecreated' => array(
 105                  'type' => PARAM_INT
 106              ),
 107              'filesize' => array(
 108                  'type' => PARAM_INT
 109              ),
 110              'author' => array(
 111                  'type' => PARAM_TEXT
 112              ),
 113              'license' => array(
 114                  'type' => PARAM_TEXT
 115              )
 116          );
 117      }
 118  
 119      protected static function define_other_properties() {
 120          return array(
 121              'filenameshort' => array(
 122                  'type' => PARAM_RAW,
 123              ),
 124              'filesizeformatted' => array(
 125                  'type' => PARAM_RAW
 126              ),
 127              'icon' => array(
 128                  'type' => PARAM_RAW,
 129              ),
 130              'timecreatedformatted' => array(
 131                  'type' => PARAM_RAW
 132              ),
 133              'timemodifiedformatted' => array(
 134                  'type' => PARAM_RAW
 135              ),
 136              'url' => array(
 137                  'type' => PARAM_URL
 138              ),
 139          );
 140      }
 141  
 142      protected function get_other_values(renderer_base $output) {
 143          $filename = $this->file->get_filename();
 144          $filenameshort = $filename;
 145          if (core_text::strlen($filename) > 25) {
 146              $filenameshort = shorten_text(substr($filename, 0, -4), 21, true, '..');
 147              $filenameshort .= substr($filename, -4);
 148          }
 149  
 150          $icon = $this->file->is_directory() ? file_folder_icon(128) : file_file_icon($this->file, 128);
 151  
 152          $url = moodle_url::make_pluginfile_url(
 153              $this->file->get_contextid(),
 154              $this->file->get_component(),
 155              $this->file->get_filearea(),
 156              $this->file->get_itemid(),
 157              $this->file->get_filepath(),
 158              $this->file->get_filename(),
 159              true
 160          );
 161  
 162          return array(
 163              'filenameshort' => $filenameshort,
 164              'filesizeformatted' => display_size((int) $this->file->get_filesize()),
 165              'icon' => $icon,
 166              'url' => $url->out(false),
 167              'timecreatedformatted' => userdate($this->file->get_timecreated()),
 168              'timemodifiedformatted' => userdate($this->file->get_timemodified()),
 169          );
 170      }
 171  
 172  }