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.
   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  /**
  19   * Utility class for browsing of system files.
  20   *
  21   * @package    core_files
  22   * @copyright  2008 Petr Skoda (http://skodak.org)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/filebrowser/file_info_context_coursecat.php');
  29  
  30  /**
  31   * Represents the system context in the tree navigated by {@link file_browser}.
  32   *
  33   * @package    core_files
  34   * @copyright  2008 Petr Skoda (http://skodak.org)
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class file_info_context_system extends file_info_context_coursecat {
  38  
  39      /**
  40       * Constructor
  41       *
  42       * @param file_browser $browser file_browser instance
  43       * @param stdClass $context context object
  44       */
  45      public function __construct($browser, $context) {
  46          parent::__construct($browser, $context, (object)['id' => 0, 'parent' => 0, 'visible' => 1]);
  47      }
  48  
  49      /**
  50       * Return information about this specific part of context level
  51       *
  52       * @param string $component component
  53       * @param string $filearea file area
  54       * @param int $itemid item ID
  55       * @param string $filepath file path
  56       * @param string $filename file name
  57       * @return file_info|null file_info instance or null if not found or access not allowed
  58       */
  59      public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
  60          if (empty($component)) {
  61              return $this;
  62          }
  63  
  64          $methodname = "get_area_{$component}_{$filearea}";
  65  
  66          if (method_exists($this, $methodname)) {
  67              return $this->$methodname($itemid, $filepath, $filename);
  68          }
  69  
  70          return null;
  71      }
  72  
  73      /**
  74       * Gets a stored file for the backup course filearea directory.
  75       *
  76       * @param int $itemid item ID
  77       * @param string $filepath file path
  78       * @param string $filename file name
  79       * @return file_info|null file_info instance or null if not found or access not allowed
  80       */
  81      protected function get_area_backup_course($itemid, $filepath, $filename) {
  82          global $CFG;
  83  
  84          if (!isloggedin()) {
  85              return null;
  86          }
  87  
  88          if (!has_any_capability(array('moodle/backup:backupcourse', 'moodle/restore:restorecourse'), $this->context)) {
  89              return null;
  90          }
  91  
  92          if (is_null($itemid)) {
  93              return $this;
  94          }
  95  
  96          $fs = get_file_storage();
  97  
  98          $filepath = is_null($filepath) ? '/' : $filepath;
  99          $filename = is_null($filename) ? '.' : $filename;
 100          if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
 101              if ($filepath === '/' && $filename === '.') {
 102                  $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
 103              } else {
 104                  // Not found.
 105                  return null;
 106              }
 107          }
 108  
 109          $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
 110          $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
 111  
 112          $urlbase = $CFG->wwwroot . '/pluginfile.php';
 113          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase,
 114              get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
 115      }
 116  
 117      /**
 118       * Returns localised visible name.
 119       *
 120       * @return string
 121       */
 122      public function get_visible_name() {
 123          return get_string('arearoot', 'repository');
 124      }
 125  
 126      /**
 127       * Whether or not new files or directories can be added
 128       *
 129       * @return bool
 130       */
 131      public function is_writable() {
 132          return false;
 133      }
 134  
 135      /**
 136       * Whether or not this is a directory
 137       *
 138       * @return bool
 139       */
 140      public function is_directory() {
 141          return true;
 142      }
 143  
 144      /**
 145       * Returns parent file_info instance
 146       *
 147       * @return file_info|null file_info instance or null for root
 148       */
 149      public function get_parent() {
 150          return null;
 151      }
 152  }