Merge branch 'MDL-48779-28' of https://github.com/vadimonus/moodle into MOODLE_28_STABLE
[moodle.git] / repository / coursefiles / lib.php
blobad4fa83faaa6ea0d957f7cd85c5bc57e3013296e
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.
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/>.
17 /**
18 * This plugin is used to access coursefiles repository
20 * @since Moodle 2.0
21 * @package repository_coursefiles
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
27 /**
28 * repository_coursefiles class is used to browse course files
30 * @since Moodle 2.0
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
35 class repository_coursefiles extends repository {
37 /**
38 * coursefiles plugin doesn't require login, so list all files
40 * @return mixed
42 public function print_login() {
43 return $this->get_listing();
46 /**
47 * Get file listing
49 * @param string $encodedpath
50 * @return mixed
52 public function get_listing($encodedpath = '', $page = '') {
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;
63 $browser = get_file_browser();
65 if (!empty($encodedpath)) {
66 $params = json_decode(base64_decode($encodedpath), true);
67 if (is_array($params)) {
68 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
69 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
70 $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
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;
77 $context = context_course::instance($courseid);
80 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
81 // build path navigation
82 $pathnodes = array();
83 $encodedpath = base64_encode(json_encode($fileinfo->get_params()));
84 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
85 $level = $fileinfo->get_parent();
86 while ($level) {
87 $params = $level->get_params();
88 $encodedpath = base64_encode(json_encode($params));
89 if ($params['contextid'] != $context->id) {
90 break;
92 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
93 $level = $level->get_parent();
95 if (!empty($pathnodes) && is_array($pathnodes)) {
96 $pathnodes = array_reverse($pathnodes);
97 $ret['path'] = $pathnodes;
99 // build file tree
100 $children = $fileinfo->get_children();
101 foreach ($children as $child) {
102 if ($child->is_directory()) {
103 $params = $child->get_params();
104 $subdir_children = $child->get_children();
105 $encodedpath = base64_encode(json_encode($params));
106 $node = array(
107 'title' => $child->get_visible_name(),
108 'datemodified' => $child->get_timemodified(),
109 'datecreated' => $child->get_timecreated(),
110 'path' => $encodedpath,
111 'children'=>array(),
112 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
114 $list[] = $node;
115 } else {
116 $encodedpath = base64_encode(json_encode($child->get_params()));
117 $node = array(
118 'title' => $child->get_visible_name(),
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(),
124 'source'=> $encodedpath,
125 'isref' => $child->is_external_file(),
126 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
128 if ($child->get_status() == 666) {
129 $node['originalmissing'] = true;
131 if ($imageinfo = $child->get_imageinfo()) {
132 $fileurl = new moodle_url($child->get_url());
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()));
135 $node['image_width'] = $imageinfo['width'];
136 $node['image_height'] = $imageinfo['height'];
138 $list[] = $node;
141 } else {
142 $list = array();
144 $ret['list'] = array_filter($list, array($this, 'filter'));
145 return $ret;
148 public function get_link($encoded) {
149 $info = array();
151 $browser = get_file_browser();
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);
158 $filepath = clean_param($params['filepath'], PARAM_PATH);
159 $filearea = clean_param($params['filearea'], PARAM_AREA);
160 $component = clean_param($params['component'], PARAM_COMPONENT);
161 $context = context::instance_by_id($contextid);
163 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
164 return $file_info->get_url();
168 * Return is the instance is visible
169 * (is the type visible ? is the context enable ?)
171 * @return boolean
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;
180 return parent::is_visible();
184 * Return the repository name.
186 * @return string
188 public function get_name() {
189 $context = $this->context->get_course_context(false);
190 if ($context) {
191 return get_string('courselegacyfilesofcourse', 'moodle', $context->get_context_name(false, true));
192 } else {
193 return get_string('courselegacyfiles');
197 public function supported_returntypes() {
198 return (FILE_INTERNAL | FILE_REFERENCE);
201 public static function get_type_option_names() {
202 return array();
206 * Does this repository used to browse moodle files?
208 * @return boolean
210 public function has_moodle_files() {
211 return true;
215 * Is this repository accessing private data?
217 * @return bool
219 public function contains_private_data() {
220 return false;