PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/filebrowser/file_info_context_system.php

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