PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/coursefiles/lib.php

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