MDL-30542 web service : clarified string
[moodle.git] / repository / coursefiles / lib.php
blob093e8060e11003da67497618c6f9fcbd24c750d6
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * repository_coursefiles class is used to browse course files
21 * @since 2.0
22 * @package repository
23 * @subpackage coursefiles
24 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 class repository_coursefiles extends repository {
30 /**
31 * coursefiles plugin doesn't require login, so list all files
32 * @return mixed
34 public function print_login() {
35 return $this->get_listing();
38 /**
39 * Get file listing
41 * @param string $encodedpath
42 * @return mixed
44 public function get_listing($encodedpath = '') {
45 global $CFG, $USER, $OUTPUT;
46 $ret = array();
47 $ret['dynload'] = true;
48 $ret['nosearch'] = true;
49 $ret['nologin'] = true;
50 $list = array();
51 $component = 'course';
52 $filearea = 'legacy';
53 $itemid = 0;
55 $browser = get_file_browser();
57 if (!empty($encodedpath)) {
58 $params = unserialize(base64_decode($encodedpath));
59 if (is_array($params)) {
60 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
61 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
62 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
64 } else {
65 $filename = null;
66 $filepath = null;
67 list($context, $course, $cm) = get_context_info_array($this->context->id);
68 $courseid = is_object($course) ? $course->id : SITEID;
69 $context = get_context_instance(CONTEXT_COURSE, $courseid);
72 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
73 // build path navigation
74 $pathnodes = array();
75 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
76 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
77 $level = $fileinfo->get_parent();
78 while ($level) {
79 $params = $level->get_params();
80 $encodedpath = base64_encode(serialize($params));
81 if ($params['contextid'] != $context->id) {
82 break;
84 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
85 $level = $level->get_parent();
87 if (!empty($pathnodes) && is_array($pathnodes)) {
88 $pathnodes = array_reverse($pathnodes);
89 $ret['path'] = $pathnodes;
91 // build file tree
92 $children = $fileinfo->get_children();
93 foreach ($children as $child) {
94 if ($child->is_directory()) {
95 $params = $child->get_params();
96 $subdir_children = $child->get_children();
97 $encodedpath = base64_encode(serialize($params));
98 $node = array(
99 'title' => $child->get_visible_name(),
100 'size' => 0,
101 'date' => '',
102 'path' => $encodedpath,
103 'children'=>array(),
104 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
106 $list[] = $node;
107 } else {
108 $encodedpath = base64_encode(serialize($child->get_params()));
109 $node = array(
110 'title' => $child->get_visible_name(),
111 'size' => 0,
112 'date' => '',
113 'source'=> $encodedpath,
114 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
116 $list[] = $node;
119 } else {
120 $list = array();
122 $ret['list'] = array_filter($list, array($this, 'filter'));
123 return $ret;
126 public function get_link($encoded) {
127 $info = array();
129 $browser = get_file_browser();
131 // the final file
132 $params = unserialize(base64_decode($encoded));
133 $contextid = clean_param($params['contextid'], PARAM_INT);
134 $fileitemid = clean_param($params['itemid'], PARAM_INT);
135 $filename = clean_param($params['filename'], PARAM_FILE);
136 $filepath = clean_param($params['filepath'], PARAM_PATH);;
137 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
138 $component = clean_param($params['component'], PARAM_ALPHAEXT);
139 $context = get_context_instance_by_id($contextid);
141 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
142 return $file_info->get_url();
146 * Return is the instance is visible
147 * (is the type visible ? is the context enable ?)
148 * @return boolean
150 public function is_visible() {
151 global $COURSE; //TODO: this is deprecated (skodak)
152 if ($COURSE->legacyfiles != 2) {
153 // do not show repo if legacy files disabled in this course...
154 return false;
157 return parent::is_visible();
160 public function get_name() {
161 list($context, $course, $cm) = get_context_info_array($this->context->id);
162 if (!empty($course)) {
163 return get_string('courselegacyfiles') . format_string($course->shortname, true, array('context' => get_course_context($context)));
164 } else {
165 return get_string('courselegacyfiles');
169 public function supported_returntypes() {
170 return (FILE_INTERNAL | FILE_EXTERNAL);
172 public static function get_type_option_names() {
173 return array();
177 * Does this repository used to browse moodle files?
179 * @return boolean
181 public function has_moodle_files() {
182 return true;