PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/coursefiles/lib.php

https://github.com/kpike/moodle
PHP | 184 lines | 120 code | 17 blank | 47 comment | 16 complexity | 8bc6d03bc6b81405ac8013d36309746b MD5 | raw file
  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. * repository_coursefiles class is used to browse course files
  18. *
  19. * @since 2.0
  20. * @package repository
  21. * @subpackage coursefiles
  22. * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. class repository_coursefiles extends repository {
  26. /**
  27. * coursefiles plugin doesn't require login, so list all files
  28. * @return mixed
  29. */
  30. public function print_login() {
  31. return $this->get_listing();
  32. }
  33. /**
  34. * Get file listing
  35. *
  36. * @param string $encodedpath
  37. * @return mixed
  38. */
  39. public function get_listing($encodedpath = '') {
  40. global $CFG, $USER, $OUTPUT;
  41. $ret = array();
  42. $ret['dynload'] = true;
  43. $ret['nosearch'] = true;
  44. $ret['nologin'] = true;
  45. $list = array();
  46. $component = 'course';
  47. $filearea = 'legacy';
  48. $itemid = 0;
  49. $browser = get_file_browser();
  50. if (!empty($encodedpath)) {
  51. $params = unserialize(base64_decode($encodedpath));
  52. if (is_array($params)) {
  53. $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
  54. $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
  55. $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
  56. }
  57. } else {
  58. $filename = null;
  59. $filepath = null;
  60. list($context, $course, $cm) = get_context_info_array($this->context->id);
  61. $courseid = is_object($course) ? $course->id : SITEID;
  62. $context = get_context_instance(CONTEXT_COURSE, $courseid);
  63. }
  64. if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
  65. // build path navigation
  66. $pathnodes = array();
  67. $encodedpath = base64_encode(serialize($fileinfo->get_params()));
  68. $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
  69. $level = $fileinfo->get_parent();
  70. while ($level) {
  71. $params = $level->get_params();
  72. $encodedpath = base64_encode(serialize($params));
  73. if ($params['contextid'] != $context->id) {
  74. break;
  75. }
  76. $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
  77. $level = $level->get_parent();
  78. }
  79. if (!empty($pathnodes) && is_array($pathnodes)) {
  80. $pathnodes = array_reverse($pathnodes);
  81. $ret['path'] = $pathnodes;
  82. }
  83. // build file tree
  84. $children = $fileinfo->get_children();
  85. foreach ($children as $child) {
  86. if ($child->is_directory()) {
  87. $params = $child->get_params();
  88. $subdir_children = $child->get_children();
  89. $encodedpath = base64_encode(serialize($params));
  90. $node = array(
  91. 'title' => $child->get_visible_name(),
  92. 'size' => 0,
  93. 'date' => '',
  94. 'path' => $encodedpath,
  95. 'children'=>array(),
  96. 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
  97. );
  98. $list[] = $node;
  99. } else {
  100. $encodedpath = base64_encode(serialize($child->get_params()));
  101. $node = array(
  102. 'title' => $child->get_visible_name(),
  103. 'size' => 0,
  104. 'date' => '',
  105. 'source'=> $encodedpath,
  106. 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
  107. );
  108. $list[] = $node;
  109. }
  110. }
  111. } else {
  112. $list = array();
  113. }
  114. $ret['list'] = array_filter($list, array($this, 'filter'));
  115. return $ret;
  116. }
  117. public function get_link($encoded) {
  118. $info = array();
  119. $browser = get_file_browser();
  120. // the final file
  121. $params = unserialize(base64_decode($encoded));
  122. $contextid = clean_param($params['contextid'], PARAM_INT);
  123. $fileitemid = clean_param($params['itemid'], PARAM_INT);
  124. $filename = clean_param($params['filename'], PARAM_FILE);
  125. $filepath = clean_param($params['filepath'], PARAM_PATH);;
  126. $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
  127. $component = clean_param($params['component'], PARAM_ALPHAEXT);
  128. $context = get_context_instance_by_id($contextid);
  129. $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
  130. return $file_info->get_url();
  131. }
  132. /**
  133. * Return is the instance is visible
  134. * (is the type visible ? is the context enable ?)
  135. * @return boolean
  136. */
  137. public function is_visible() {
  138. global $COURSE; //TODO: this is deprecated (skodak)
  139. if ($COURSE->legacyfiles != 2) {
  140. // do not show repo if legacy files disabled in this course...
  141. return false;
  142. }
  143. return parent::is_visible();
  144. }
  145. public function get_name() {
  146. list($context, $course, $cm) = get_context_info_array($this->context->id);
  147. if (!empty($course)) {
  148. return get_string('courselegacyfiles') . $course->shortname;
  149. } else {
  150. return get_string('courselegacyfiles');
  151. }
  152. }
  153. public function supported_returntypes() {
  154. return (FILE_INTERNAL | FILE_EXTERNAL);
  155. }
  156. public static function get_type_option_names() {
  157. return array();
  158. }
  159. /**
  160. * Does this repository used to browse moodle files?
  161. *
  162. * @return boolean
  163. */
  164. public function has_moodle_files() {
  165. return true;
  166. }
  167. }