gradebook MDL-15457 hide grade item when activity is hidden in the course
[moodle.git] / repository / coursefiles / lib.php
CommitLineData
d3959c26
DC
1<?php
2
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.
14//
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/>.
17
18/**
19 * repository_coursefiles class is used to browse course files
20 *
21 * @since 2.0
22 * @package moodlecore
23 * @subpackage repository
24 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28class repository_coursefiles extends repository {
29
30 /**
31 * coursefiles plugin doesn't require login, so list all files
32 * @return mixed
33 */
34 public function print_login() {
35 return $this->get_listing();
36 }
37
38 /**
39 * Get file listing
40 *
41 * @param string $encodedpath
42 * @return mixed
43 */
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;
54
55 $browser = get_file_browser();
56
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));
63 }
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);
70 }
71
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;
83 }
84 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
85 $level = $level->get_parent();
86 }
87 if (!empty($pathnodes) && is_array($pathnodes)) {
88 $pathnodes = array_reverse($pathnodes);
89 $ret['path'] = $pathnodes;
90 }
91 // build file tree
92 $children = $fileinfo->get_children();
93 foreach ($children as $child) {
94 $shorttitle = $this->get_short_filename($child->get_visible_name(), 12);
95 if ($child->is_directory()) {
96 $params = $child->get_params();
97 $subdir_children = $child->get_children();
98 $encodedpath = base64_encode(serialize($params));
99 $node = array(
100 'title' => $child->get_visible_name(),
101 'shorttitle'=>$shorttitle,
102 'size' => 0,
103 'date' => '',
104 'path' => $encodedpath,
105 'children'=>array(),
106 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
107 );
108 $list[] = $node;
109 } else {
110 $encodedpath = base64_encode(serialize($child->get_params()));
111 $node = array(
112 'title' => $child->get_visible_name(),
113 'shorttitle'=>$shorttitle,
114 'size' => 0,
115 'date' => '',
116 'source'=> $encodedpath,
117 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
118 );
119 $list[] = $node;
120 }
121 }
122 } else {
123 $list = array();
124 }
125 $ret['list'] = array_filter($list, array($this, 'filter'));
126 return $ret;
127 }
128
129 /**
130 * course files don't support to link to external links
131 *
132 * @return int
133 */
134 public function supported_returntypes() {
135 return FILE_INTERNAL;
136 }
137
138 /**
139 * Copy a file to file area
140 *
141 * @global object $USER
142 * @global object $DB
143 * @param string $encoded The metainfo of file, it is base64 encoded php serialized data
144 * @param string $draftitemid itemid
145 * @param string $new_filename The intended name of file
146 * @param string $new_filepath the new path in draft area
147 * @return array The information of file
148 */
149 public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
150 global $USER, $DB;
151 $info = array();
152
153 $browser = get_file_browser();
154 $fs = get_file_storage();
155 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
156
157 // the final file
158 $params = unserialize(base64_decode($encoded));
159 $contextid = clean_param($params['contextid'], PARAM_INT);
160 $fileitemid = clean_param($params['itemid'], PARAM_INT);
161 $filename = clean_param($params['filename'], PARAM_FILE);
162 $filepath = clean_param($params['filepath'], PARAM_PATH);;
163 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
164 $component = clean_param($params['component'], PARAM_ALPHAEXT);
165 $context = get_context_instance_by_id($contextid);
166
167 if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) {
168 throw new moodle_exception('fileexists');
169 }
170
171 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
172 $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename);
173
174 $info['itemid'] = $draftitemid;
175 $info['title'] = $new_filename;
176 $info['contextid'] = $user_context->id;
177 $info['filesize'] = $file_info->get_filesize();
178
179 return $info;
180 }
181}