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 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   * This plugin is used to access coursefiles repository
  19   *
  20   * @since Moodle 2.0
  21   * @package    repository_coursefiles
  22   * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  require_once($CFG->dirroot . '/repository/lib.php');
  26  
  27  /**
  28   * repository_coursefiles class is used to browse course files
  29   *
  30   * @since Moodle 2.0
  31   * @package    repository_coursefiles
  32   * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class repository_coursefiles extends repository {
  36  
  37      /**
  38       * coursefiles plugin doesn't require login, so list all files
  39       *
  40       * @return mixed
  41       */
  42      public function print_login() {
  43          return $this->get_listing();
  44      }
  45  
  46      /**
  47       * Get file listing
  48       *
  49       * @param string $encodedpath
  50       * @return mixed
  51       */
  52      public function get_listing($encodedpath = '', $page = '') {
  53          global $CFG, $USER, $OUTPUT;
  54          $ret = array();
  55          $ret['dynload'] = true;
  56          $ret['nosearch'] = true;
  57          $ret['nologin'] = true;
  58          $list = array();
  59          $component = 'course';
  60          $filearea  = 'legacy';
  61          $itemid = 0;
  62  
  63          $browser = get_file_browser();
  64  
  65          if (!empty($encodedpath)) {
  66              $params = json_decode(base64_decode($encodedpath), true);
  67              if (is_array($params)) {
  68                  $filepath  = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
  69                  $filename  = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
  70                  $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
  71              }
  72          } else {
  73              $filename = null;
  74              $filepath = null;
  75              list($context, $course, $cm) = get_context_info_array($this->context->id);
  76              $courseid = is_object($course) ? $course->id : SITEID;
  77              $context = context_course::instance($courseid);
  78          }
  79  
  80          if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
  81              // build path navigation
  82              $pathnodes = array();
  83              $encodedpath = base64_encode(json_encode($fileinfo->get_params()));
  84              $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
  85              $level = $fileinfo->get_parent();
  86              while ($level) {
  87                  $params = $level->get_params();
  88                  $encodedpath = base64_encode(json_encode($params));
  89                  if ($params['contextid'] != $context->id) {
  90                      break;
  91                  }
  92                  $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
  93                  $level = $level->get_parent();
  94              }
  95              if (!empty($pathnodes) && is_array($pathnodes)) {
  96                  $pathnodes = array_reverse($pathnodes);
  97                  $ret['path'] = $pathnodes;
  98              }
  99              // build file tree
 100              $children = $fileinfo->get_children();
 101              foreach ($children as $child) {
 102                  if ($child->is_directory()) {
 103                      $params = $child->get_params();
 104                      $subdir_children = $child->get_children();
 105                      $encodedpath = base64_encode(json_encode($params));
 106                      $node = array(
 107                          'title' => $child->get_visible_name(),
 108                          'datemodified' => $child->get_timemodified(),
 109                          'datecreated' => $child->get_timecreated(),
 110                          'path' => $encodedpath,
 111                          'children'=>array(),
 112                          'thumbnail' => $OUTPUT->image_url(file_folder_icon(90))->out(false)
 113                      );
 114                      $list[] = $node;
 115                  } else {
 116                      $encodedpath = base64_encode(json_encode($child->get_params()));
 117                      $node = array(
 118                          'title' => $child->get_visible_name(),
 119                          'size' => $child->get_filesize(),
 120                          'author' => $child->get_author(),
 121                          'license' => $child->get_license(),
 122                          'datemodified' => $child->get_timemodified(),
 123                          'datecreated' => $child->get_timecreated(),
 124                          'source'=> $encodedpath,
 125                          'isref' => $child->is_external_file(),
 126                          'thumbnail' => $OUTPUT->image_url(file_file_icon($child, 90))->out(false)
 127                      );
 128                      if ($child->get_status() == 666) {
 129                          $node['originalmissing'] = true;
 130                      }
 131                      if ($imageinfo = $child->get_imageinfo()) {
 132                          $fileurl = new moodle_url($child->get_url());
 133                          $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
 134                          $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
 135                          $node['image_width'] = $imageinfo['width'];
 136                          $node['image_height'] = $imageinfo['height'];
 137                      }
 138                      $list[] = $node;
 139                  }
 140              }
 141          } else {
 142              $list = array();
 143          }
 144          $ret['list'] = array_filter($list, array($this, 'filter'));
 145          return $ret;
 146      }
 147  
 148      public function get_link($encoded) {
 149          $info = array();
 150  
 151          $browser = get_file_browser();
 152  
 153          // the final file
 154          $params = unserialize(base64_decode($encoded));
 155          $contextid  = clean_param($params['contextid'], PARAM_INT);
 156          $fileitemid = clean_param($params['itemid'], PARAM_INT);
 157          $filename = clean_param($params['filename'], PARAM_FILE);
 158          $filepath = clean_param($params['filepath'], PARAM_PATH);
 159          $filearea = clean_param($params['filearea'], PARAM_AREA);
 160          $component = clean_param($params['component'], PARAM_COMPONENT);
 161          $context = context::instance_by_id($contextid);
 162  
 163          $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
 164          return $file_info->get_url();
 165      }
 166  
 167      /**
 168       * Return is the instance is visible
 169       * (is the type visible ? is the context enable ?)
 170       *
 171       * @return boolean
 172       */
 173      public function is_visible() {
 174          global $COURSE; //TODO: this is deprecated (skodak)
 175          if ($COURSE->legacyfiles != 2) {
 176              // do not show repo if legacy files disabled in this course...
 177              return false;
 178          }
 179  
 180          return parent::is_visible();
 181      }
 182  
 183      /**
 184       * Return the repository name.
 185       *
 186       * @return string
 187       */
 188      public function get_name() {
 189          $context = $this->context->get_course_context(false);
 190          if ($context) {
 191              return get_string('courselegacyfilesofcourse', 'moodle', $context->get_context_name(false, true));
 192          } else {
 193              return get_string('courselegacyfiles');
 194          }
 195      }
 196  
 197      public function supported_returntypes() {
 198          return (FILE_INTERNAL | FILE_REFERENCE);
 199      }
 200  
 201      public static function get_type_option_names() {
 202          return array();
 203      }
 204  
 205      /**
 206       * Does this repository used to browse moodle files?
 207       *
 208       * @return boolean
 209       */
 210      public function has_moodle_files() {
 211          return true;
 212      }
 213  
 214      /**
 215       * Is this repository accessing private data?
 216       *
 217       * @return bool
 218       */
 219      public function contains_private_data() {
 220          return false;
 221      }
 222  }