PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/filebrowser/file_info_context_course.php

https://bitbucket.org/ngmares/moodle
PHP | 700 lines | 336 code | 94 blank | 270 comment | 56 complexity | 2d9ac47b524a3fd41ec5450be82150d7 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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 course 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. /**
  25. * Represents a course context in the tree navigated by {@link file_browser}.
  26. *
  27. * @package core_files
  28. * @copyright 2008 Petr Skoda (http://skodak.org)
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class file_info_context_course extends file_info {
  32. /** @var stdClass course object */
  33. protected $course;
  34. /**
  35. * Constructor
  36. *
  37. * @param file_browser $browser file browser instance
  38. * @param stdClass $context context object
  39. * @param stdClass $course course object
  40. */
  41. public function __construct($browser, $context, $course) {
  42. parent::__construct($browser, $context);
  43. $this->course = $course;
  44. }
  45. /**
  46. * Return information about this specific context level
  47. *
  48. * @param string $component component
  49. * @param string $filearea file area
  50. * @param int $itemid item ID
  51. * @param string $filepath file path
  52. * @param string $filename file name
  53. * @return file_info|null file_info instance or null if not found or access not allowed
  54. */
  55. public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
  56. // try to emulate require_login() tests here
  57. if (!isloggedin()) {
  58. return null;
  59. }
  60. if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
  61. return null;
  62. }
  63. if (!is_viewing($this->context) and !is_enrolled($this->context)) {
  64. // no peaking here if not enrolled or inspector
  65. return null;
  66. }
  67. if (empty($component)) {
  68. return $this;
  69. }
  70. $methodname = "get_area_{$component}_{$filearea}";
  71. if (method_exists($this, $methodname)) {
  72. return $this->$methodname($itemid, $filepath, $filename);
  73. }
  74. return null;
  75. }
  76. /**
  77. * Gets a stored file for the course summary filearea directory
  78. *
  79. * @param int $itemid item ID
  80. * @param string $filepath file path
  81. * @param string $filename file name
  82. * @return file_info|null file_info instance or null if not found or access not allowed
  83. */
  84. protected function get_area_course_summary($itemid, $filepath, $filename) {
  85. global $CFG;
  86. if (!has_capability('moodle/course:update', $this->context)) {
  87. return null;
  88. }
  89. if (is_null($itemid)) {
  90. return $this;
  91. }
  92. $fs = get_file_storage();
  93. $filepath = is_null($filepath) ? '/' : $filepath;
  94. $filename = is_null($filename) ? '.' : $filename;
  95. if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
  96. if ($filepath === '/' and $filename === '.') {
  97. $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
  98. } else {
  99. // not found
  100. return null;
  101. }
  102. }
  103. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  104. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
  105. }
  106. /**
  107. * Gets a stored file for the course section filearea directory
  108. *
  109. * @param int $itemid item ID
  110. * @param string $filepath file path
  111. * @param string $filename file name
  112. * @return file_info|null file_info instance or null if not found or access not allowed
  113. */
  114. protected function get_area_course_section($itemid, $filepath, $filename) {
  115. global $CFG, $DB;
  116. if (!has_capability('moodle/course:update', $this->context)) {
  117. return null;
  118. }
  119. if (empty($itemid)) {
  120. // list all sections
  121. return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
  122. }
  123. if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
  124. return null; // does not exist
  125. }
  126. $fs = get_file_storage();
  127. $filepath = is_null($filepath) ? '/' : $filepath;
  128. $filename = is_null($filename) ? '.' : $filename;
  129. if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
  130. if ($filepath === '/' and $filename === '.') {
  131. $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
  132. } else {
  133. // not found
  134. return null;
  135. }
  136. }
  137. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  138. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
  139. }
  140. /**
  141. * Gets a stored file for the course legacy filearea directory
  142. *
  143. * @param int $itemid item ID
  144. * @param string $filepath file path
  145. * @param string $filename file name
  146. * @return file_info|null file_info instance or null if not found or access not allowed
  147. */
  148. protected function get_area_course_legacy($itemid, $filepath, $filename) {
  149. if (!has_capability('moodle/course:managefiles', $this->context)) {
  150. return null;
  151. }
  152. if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
  153. // bad luck, legacy course files not used any more
  154. }
  155. if (is_null($itemid)) {
  156. return $this;
  157. }
  158. $fs = get_file_storage();
  159. $filepath = is_null($filepath) ? '/' : $filepath;
  160. $filename = is_null($filename) ? '.' : $filename;
  161. if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
  162. if ($filepath === '/' and $filename === '.') {
  163. $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
  164. } else {
  165. // not found
  166. return null;
  167. }
  168. }
  169. return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
  170. }
  171. /**
  172. * Gets a stored file for the backup course filearea directory
  173. *
  174. * @param int $itemid item ID
  175. * @param string $filepath file path
  176. * @param string $filename file name
  177. * @return file_info|null file_info instance or null if not found or access not allowed
  178. */
  179. protected function get_area_backup_course($itemid, $filepath, $filename) {
  180. global $CFG;
  181. if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
  182. return null;
  183. }
  184. if (is_null($itemid)) {
  185. return $this;
  186. }
  187. $fs = get_file_storage();
  188. $filepath = is_null($filepath) ? '/' : $filepath;
  189. $filename = is_null($filename) ? '.' : $filename;
  190. if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
  191. if ($filepath === '/' and $filename === '.') {
  192. $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
  193. } else {
  194. // not found
  195. return null;
  196. }
  197. }
  198. $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
  199. $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
  200. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  201. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
  202. }
  203. /**
  204. * Gets a stored file for the automated backup filearea directory
  205. *
  206. * @param int $itemid item ID
  207. * @param string $filepath file path
  208. * @param string $filename file name
  209. * @return file_info|null
  210. */
  211. protected function get_area_backup_automated($itemid, $filepath, $filename) {
  212. global $CFG;
  213. if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context)) {
  214. return null;
  215. }
  216. if (is_null($itemid)) {
  217. return $this;
  218. }
  219. $fs = get_file_storage();
  220. $filepath = is_null($filepath) ? '/' : $filepath;
  221. $filename = is_null($filename) ? '.' : $filename;
  222. if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'automated', 0, $filepath, $filename)) {
  223. if ($filepath === '/' and $filename === '.') {
  224. $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
  225. } else {
  226. // not found
  227. return null;
  228. }
  229. }
  230. $downloadable = has_capability('moodle/site:config', $this->context);
  231. $uploadable = false;
  232. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  233. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
  234. }
  235. /**
  236. * Gets a stored file for the backup section filearea directory
  237. *
  238. * @param int $itemid item ID
  239. * @param string $filepath file path
  240. * @param string $filename file name
  241. * @return file_info|null file_info instance or null if not found or access not allowed
  242. */
  243. protected function get_area_backup_section($itemid, $filepath, $filename) {
  244. global $CFG, $DB;
  245. if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
  246. return null;
  247. }
  248. if (empty($itemid)) {
  249. // list all sections
  250. return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
  251. }
  252. if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
  253. return null; // does not exist
  254. }
  255. $fs = get_file_storage();
  256. $filepath = is_null($filepath) ? '/' : $filepath;
  257. $filename = is_null($filename) ? '.' : $filename;
  258. if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
  259. if ($filepath === '/' and $filename === '.') {
  260. $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
  261. } else {
  262. // not found
  263. return null;
  264. }
  265. }
  266. $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
  267. $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
  268. $urlbase = $CFG->wwwroot.'/pluginfile.php';
  269. return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
  270. }
  271. /**
  272. * Returns localised visible name.
  273. *
  274. * @return string
  275. */
  276. public function get_visible_name() {
  277. return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname, true, array('context'=>$this->context));
  278. }
  279. /**
  280. * Whether or not new files or directories can be added
  281. *
  282. * @return bool
  283. */
  284. public function is_writable() {
  285. return false;
  286. }
  287. /**
  288. * Whether or not this is a directory
  289. *
  290. * @return bool
  291. */
  292. public function is_directory() {
  293. return true;
  294. }
  295. /**
  296. * Returns list of children.
  297. *
  298. * @return array of file_info instances
  299. */
  300. public function get_children() {
  301. $children = array();
  302. if ($child = $this->get_area_course_summary(0, '/', '.')) {
  303. $children[] = $child;
  304. }
  305. if ($child = $this->get_area_course_section(null, null, null)) {
  306. $children[] = $child;
  307. }
  308. if ($child = $this->get_area_backup_section(null, null, null)) {
  309. $children[] = $child;
  310. }
  311. if ($child = $this->get_area_backup_course(0, '/', '.')) {
  312. $children[] = $child;
  313. }
  314. if ($child = $this->get_area_backup_automated(0, '/', '.')) {
  315. $children[] = $child;
  316. }
  317. if ($child = $this->get_area_course_legacy(0, '/', '.')) {
  318. $children[] = $child;
  319. }
  320. if (!has_capability('moodle/course:managefiles', $this->context)) {
  321. // 'managefiles' capability is checked in every activity module callback.
  322. // Don't even waste time on retrieving the modules if we can't browse the files anyway
  323. return $children;
  324. }
  325. // now list all modules
  326. $modinfo = get_fast_modinfo($this->course);
  327. foreach ($modinfo->cms as $cminfo) {
  328. if (empty($cminfo->uservisible)) {
  329. continue;
  330. }
  331. $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
  332. if ($child = $this->browser->get_file_info($modcontext)) {
  333. $children[] = $child;
  334. }
  335. }
  336. return $children;
  337. }
  338. /**
  339. * Returns parent file_info instance
  340. *
  341. * @todo error checking if get_parent_contextid() returns false
  342. * @return file_info or null for root
  343. */
  344. public function get_parent() {
  345. //TODO: error checking if get_parent_contextid() returns false
  346. $pcid = get_parent_contextid($this->context);
  347. $parent = get_context_instance_by_id($pcid);
  348. return $this->browser->get_file_info($parent);
  349. }
  350. }
  351. /**
  352. * Subclass of file_info_stored for files in the course files area.
  353. *
  354. * @package core_files
  355. * @copyright 2008 Petr Skoda (http://skodak.org)
  356. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  357. */
  358. class file_info_area_course_legacy extends file_info_stored {
  359. /**
  360. * Constructor
  361. *
  362. * @param file_browser $browser file browser instance
  363. * @param stdClass $context context object
  364. * @param stored_file $storedfile stored_file instance
  365. */
  366. public function __construct($browser, $context, $storedfile) {
  367. global $CFG;
  368. $urlbase = $CFG->wwwroot.'/file.php';
  369. parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
  370. }
  371. /**
  372. * Returns file download url
  373. *
  374. * @param bool $forcedownload whether or not force download
  375. * @param bool $https whether or not force https
  376. * @return string url
  377. */
  378. public function get_url($forcedownload=false, $https=false) {
  379. if (!$this->is_readable()) {
  380. return null;
  381. }
  382. if ($this->lf->is_directory()) {
  383. return null;
  384. }
  385. $filepath = $this->lf->get_filepath();
  386. $filename = $this->lf->get_filename();
  387. $courseid = $this->context->instanceid;
  388. $path = '/'.$courseid.$filepath.$filename;
  389. return file_encode_url($this->urlbase, $path, $forcedownload, $https);
  390. }
  391. /**
  392. * Returns list of children.
  393. *
  394. * @return array of file_info instances
  395. */
  396. public function get_children() {
  397. if (!$this->lf->is_directory()) {
  398. return array();
  399. }
  400. $result = array();
  401. $fs = get_file_storage();
  402. $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
  403. foreach ($storedfiles as $file) {
  404. $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
  405. }
  406. return $result;
  407. }
  408. }
  409. /**
  410. * Represents a course category context in the tree navigated by {@link file_browser}.
  411. *
  412. * @package core_files
  413. * @copyright 2008 Petr Skoda (http://skodak.org)
  414. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  415. */
  416. class file_info_area_course_section extends file_info {
  417. /** @var stdClass course object */
  418. protected $course;
  419. /** @var file_info_context_course course file info object */
  420. protected $courseinfo;
  421. /**
  422. * Constructor
  423. *
  424. * @param file_browser $browser file browser instance
  425. * @param stdClass $context context object
  426. * @param stdClass $course course object
  427. * @param file_info_context_course $courseinfo file info instance
  428. */
  429. public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
  430. parent::__construct($browser, $context);
  431. $this->course = $course;
  432. $this->courseinfo = $courseinfo;
  433. }
  434. /**
  435. * Returns list of standard virtual file/directory identification.
  436. * The difference from stored_file parameters is that null values
  437. * are allowed in all fields
  438. *
  439. * @return array with keys contextid, filearea, itemid, filepath and filename
  440. */
  441. public function get_params() {
  442. return array('contextid' => $this->context->id,
  443. 'component' => 'course',
  444. 'filearea' => 'section',
  445. 'itemid' => null,
  446. 'filepath' => null,
  447. 'filename' => null);
  448. }
  449. /**
  450. * Returns localised visible name.
  451. *
  452. * @return string
  453. */
  454. public function get_visible_name() {
  455. //$format = $this->course->format;
  456. $sectionsname = get_string("coursesectionsummaries");
  457. return $sectionsname;
  458. }
  459. /**
  460. * Return whether or not new files or directories can be added
  461. *
  462. * @return bool
  463. */
  464. public function is_writable() {
  465. return false;
  466. }
  467. /**
  468. * Return whether or not this is a empty area
  469. *
  470. * @return bool
  471. */
  472. public function is_empty_area() {
  473. $fs = get_file_storage();
  474. return $fs->is_area_empty($this->context->id, 'course', 'section');
  475. }
  476. /**
  477. * Return whether or not this is a empty area
  478. *
  479. * @return bool
  480. */
  481. public function is_directory() {
  482. return true;
  483. }
  484. /**
  485. * Returns list of children.
  486. *
  487. * @return array of file_info instances
  488. */
  489. public function get_children() {
  490. global $DB;
  491. $children = array();
  492. $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
  493. foreach ($course_sections as $section) {
  494. if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
  495. $children[] = $child;
  496. }
  497. }
  498. return $children;
  499. }
  500. /**
  501. * Returns parent file_info instance
  502. *
  503. * @return file_info|null file_info or null for root
  504. */
  505. public function get_parent() {
  506. return $this->courseinfo;
  507. }
  508. }
  509. /**
  510. * Implementation of course section backup area
  511. *
  512. * @package core_files
  513. * @copyright 2008 Petr Skoda (http://skodak.org)
  514. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  515. */
  516. class file_info_area_backup_section extends file_info {
  517. /** @var stdClass course object */
  518. protected $course;
  519. /** @var file_info_context_course course file info object */
  520. protected $courseinfo;
  521. /**
  522. * Constructor
  523. *
  524. * @param file_browser $browser file browser instance
  525. * @param stdClass $context context object
  526. * @param stdClass $course course object
  527. * @param file_info_context_course $courseinfo file info instance
  528. */
  529. public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
  530. parent::__construct($browser, $context);
  531. $this->course = $course;
  532. $this->courseinfo = $courseinfo;
  533. }
  534. /**
  535. * Returns list of standard virtual file/directory identification.
  536. * The difference from stored_file parameters is that null values
  537. * are allowed in all fields
  538. *
  539. * @return array with keys contextid, component, filearea, itemid, filepath and filename
  540. */
  541. public function get_params() {
  542. return array('contextid' => $this->context->id,
  543. 'component' => 'backup',
  544. 'filearea' => 'section',
  545. 'itemid' => null,
  546. 'filepath' => null,
  547. 'filename' => null);
  548. }
  549. /**
  550. * Returns localised visible name.
  551. *
  552. * @return string
  553. */
  554. public function get_visible_name() {
  555. return get_string('sectionbackup', 'repository');
  556. }
  557. /**
  558. * Return whether or not new files and directories can be added
  559. *
  560. * @return bool
  561. */
  562. public function is_writable() {
  563. return false;
  564. }
  565. /**
  566. * Whether or not this is an empty area
  567. *
  568. * @return bool
  569. */
  570. public function is_empty_area() {
  571. $fs = get_file_storage();
  572. return $fs->is_area_empty($this->context->id, 'backup', 'section');
  573. }
  574. /**
  575. * Return whether or not this is a directory
  576. *
  577. * @return bool
  578. */
  579. public function is_directory() {
  580. return true;
  581. }
  582. /**
  583. * Returns list of children.
  584. *
  585. * @return array of file_info instances
  586. */
  587. public function get_children() {
  588. global $DB;
  589. $children = array();
  590. $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
  591. foreach ($course_sections as $section) {
  592. if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
  593. $children[] = $child;
  594. }
  595. }
  596. return $children;
  597. }
  598. /**
  599. * Returns parent file_info instance
  600. *
  601. * @return file_info or null for root
  602. */
  603. public function get_parent() {
  604. return $this->browser->get_file_info($this->context);
  605. }
  606. }