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