Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
18
/**
19
 * Utility class for browsing of module files.
20
 *
21
 * @package    core_files
22
 * @copyright  2008 Petr Skoda (http://skodak.org)
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
/**
29
 * Represents a module context in the tree navigated by {@link file_browser}.
30
 *
31
 * @package    core_files
32
 * @copyright  2008 Petr Skoda (http://skodak.org)
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class file_info_context_module extends file_info {
36
    /** @var stdClass Course object */
37
    protected $course;
38
    /** @var cm_info Course module object */
39
    protected $cm;
40
    /** @var string Module name */
41
    protected $modname;
42
    /** @var array Available file areas */
43
    protected $areas;
44
    /** @var array caches the result of last call to get_non_empty_children() */
45
    protected $nonemptychildren;
46
 
47
    /**
48
     * Constructor
49
     *
50
     * @param file_browser $browser file browser instance
51
     * @param stdClass $context context object
52
     * @param stdClass $course course object
53
     * @param stdClass $cm course module object
54
     * @param string $modname module name
55
     */
56
    public function __construct($browser, $context, $course, $cm, $modname) {
57
        global $CFG;
58
 
59
        parent::__construct($browser, $context);
60
        $this->course  = $course;
61
        $this->cm      = cm_info::create($cm);
62
        $this->modname = $this->cm->modname;
63
        $this->nonemptychildren = null;
64
 
65
        if ($functionname = component_callback_exists('mod_'.$modname, 'get_file_areas')) {
66
            $cm = $this->cm->get_course_module_record();
67
            $this->areas = $functionname($course, $cm, $context);
68
        } else {
69
            $this->areas = array();
70
        }
71
 
72
        unset($this->areas['intro']); // hardcoded, ignore attempts to override it
73
    }
74
 
75
    /**
76
     * Return information about this specific context level
77
     *
78
     * @param string $component component
79
     * @param string $filearea file area
80
     * @param int $itemid item ID
81
     * @param string $filepath file path
82
     * @param string $filename file name
83
     * @return file_info|null
84
     */
85
    public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
86
        // try to emulate require_login() tests here
87
        if (!isloggedin()) {
88
            return null;
89
        }
90
 
91
        $coursecontext = $this->context->get_course_context(true);
92
        if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
93
            return null;
94
        }
95
 
96
        if (!is_viewing($this->context) and !$this->browser->is_enrolled($this->course->id)) {
97
            // no peaking here if not enrolled or inspector
98
            return null;
99
        }
100
 
101
        if (!$this->cm->uservisible) {
102
            // activity hidden sorry
103
            return null;
104
        }
105
 
106
        if (empty($component)) {
107
            return $this;
108
        }
109
 
110
        if ($component == 'mod_'.$this->modname and $filearea === 'intro') {
111
            return $this->get_area_intro($itemid, $filepath, $filename);
112
        } else if ($component == 'backup' and $filearea === 'activity') {
113
            return $this->get_area_backup($itemid, $filepath, $filename);
114
        }
115
 
116
        if ($functionname = component_callback_exists('mod_'.$this->modname, 'get_file_info')) {
117
            $cm = $this->cm->get_course_module_record();
118
            return $functionname($this->browser, $this->areas, $this->course, $cm,
119
                $this->context, $filearea, $itemid, $filepath, $filename);
120
        }
121
 
122
        return null;
123
    }
124
 
125
    /**
126
     * Get a file from module intro area
127
     *
128
     * @param int $itemid item ID
129
     * @param string $filepath file path
130
     * @param string $filename file name
131
     * @return file_info|null
132
     */
133
    protected function get_area_intro($itemid, $filepath, $filename) {
134
        global $CFG;
135
 
136
        if (!plugin_supports('mod', $this->modname, FEATURE_MOD_INTRO, true) or !has_capability('moodle/course:managefiles', $this->context)) {
137
            return null;
138
        }
139
 
140
        $fs = get_file_storage();
141
 
142
        $filepath = is_null($filepath) ? '/' : $filepath;
143
        $filename = is_null($filename) ? '.' : $filename;
144
        if (!$storedfile = $fs->get_file($this->context->id, 'mod_'.$this->modname, 'intro', 0, $filepath, $filename)) {
145
            if ($filepath === '/' and $filename === '.') {
146
                $storedfile = new virtual_root_file($this->context->id, 'mod_'.$this->modname, 'intro', 0);
147
            } else {
148
                // not found
149
                return null;
150
            }
151
        }
152
 
153
        $urlbase = $CFG->wwwroot.'/pluginfile.php';
154
        return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('moduleintro'), false, true, true, false);
155
    }
156
 
157
    /**
158
     * Get a file from module backup area
159
     *
160
     * @param int $itemid item ID
161
     * @param string $filepath file path
162
     * @param string $filename file name
163
     * @return file_info|null
164
     */
165
    protected function get_area_backup($itemid, $filepath, $filename) {
166
        global $CFG;
167
 
168
        if (!has_capability('moodle/backup:backupactivity', $this->context)) {
169
            return null;
170
        }
171
 
172
        $fs = get_file_storage();
173
 
174
        $filepath = is_null($filepath) ? '/' : $filepath;
175
        $filename = is_null($filename) ? '.' : $filename;
176
        if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'activity', 0, $filepath, $filename)) {
177
            if ($filepath === '/' and $filename === '.') {
178
                $storedfile = new virtual_root_file($this->context->id, 'backup', 'activity', 0);
179
            } else {
180
                // not found
181
                return null;
182
            }
183
        }
184
 
185
        $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
186
        $uploadable   = has_capability('moodle/restore:uploadfile', $this->context);
187
 
188
        $urlbase = $CFG->wwwroot.'/pluginfile.php';
189
        return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('activitybackup', 'repository'), false, $downloadable, $uploadable, false);
190
    }
191
 
192
    /**
193
     * Returns localised visible name.
194
     *
195
     * @return string
196
     */
197
    public function get_visible_name() {
198
        return $this->cm->get_formatted_name().' ('.$this->cm->get_module_type_name().')';
199
    }
200
 
201
    /**
202
     * Whether or not files or directories can be added
203
     *
204
     * @return bool
205
     */
206
    public function is_writable() {
207
        return false;
208
    }
209
 
210
    /**
211
     * Whether or not this is an emtpy area
212
     *
213
     * @return bool
214
     */
215
    public function is_empty_area() {
216
        if ($child = $this->get_area_backup(0, '/', '.')) {
217
            if (!$child->is_empty_area()) {
218
                return false;
219
            }
220
        }
221
        if ($child = $this->get_area_intro(0, '/', '.')) {
222
            if (!$child->is_empty_area()) {
223
                return false;
224
            }
225
        }
226
 
227
        foreach ($this->areas as $area=>$desctiption) {
228
            if ($child = $this->get_file_info('mod_'.$this->modname, $area, null, null, null)) {
229
                if (!$child->is_empty_area()) {
230
                    return false;
231
                }
232
            }
233
        }
234
 
235
        return true;
236
    }
237
 
238
    /**
239
     * Whether or not this is a directory
240
     *
241
     * @return bool
242
     */
243
    public function is_directory() {
244
        return true;
245
    }
246
 
247
    /**
248
     * Returns list of children.
249
     *
250
     * @return array of file_info instances
251
     */
252
    public function get_children() {
253
        return $this->get_filtered_children('*', false, true);
254
    }
255
 
256
    /**
257
     * Help function to return files matching extensions or their count
258
     *
259
     * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
260
     * @param bool|int $countonly if false returns the children, if an int returns just the
261
     *    count of children but stops counting when $countonly number of children is reached
262
     * @param bool $returnemptyfolders if true returns items that don't have matching files inside
263
     * @return array|int array of file_info instances or the count
264
     */
265
    private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
266
        global $DB;
267
        // prepare list of areas including intro and backup
268
        $areas = array(
269
            array('mod_'.$this->modname, 'intro'),
270
            array('backup', 'activity')
271
        );
272
        foreach ($this->areas as $area => $desctiption) {
273
            $areas[] = array('mod_'.$this->modname, $area);
274
        }
275
 
276
        $params1 = array('contextid' => $this->context->id, 'emptyfilename' => '.');
277
        list($sql2, $params2) = $this->build_search_files_sql($extensions);
278
        $children = array();
279
        foreach ($areas as $area) {
280
            if (!$returnemptyfolders) {
281
                // fast pre-check if there are any files in the filearea
282
                $params1['component'] = $area[0];
283
                $params1['filearea'] = $area[1];
284
                if (!$DB->record_exists_sql('SELECT 1 from {files}
285
                        WHERE contextid = :contextid
286
                        AND filename <> :emptyfilename
287
                        AND component = :component
288
                        AND filearea = :filearea '.$sql2,
289
                        array_merge($params1, $params2))) {
290
                    continue;
291
                }
292
            }
293
            if ($child = $this->get_file_info($area[0], $area[1], null, null, null)) {
294
                if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
295
                    $children[] = $child;
296
                    if ($countonly !== false && count($children) >= $countonly) {
297
                        break;
298
                    }
299
                }
300
            }
301
        }
302
        if ($countonly !== false) {
303
            return count($children);
304
        }
305
        return $children;
306
    }
307
 
308
    /**
309
     * Returns list of children which are either files matching the specified extensions
310
     * or folders that contain at least one such file.
311
     *
312
     * @param string|array $extensions either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
313
     * @return array of file_info instances
314
     */
315
    public function get_non_empty_children($extensions = '*') {
316
        if ($this->nonemptychildren !== null) {
317
            return $this->nonemptychildren;
318
        }
319
        $this->nonemptychildren = $this->get_filtered_children($extensions);
320
        return $this->nonemptychildren;
321
    }
322
 
323
    /**
324
     * Returns the number of children which are either files matching the specified extensions
325
     * or folders containing at least one such file.
326
     *
327
     * @param string|array $extensions for example '*' or array('.gif','.jpg')
328
     * @param int $limit stop counting after at least $limit non-empty children are found
329
     * @return int
330
     */
331
    public function count_non_empty_children($extensions = '*', $limit = 1) {
332
        if ($this->nonemptychildren !== null) {
333
            return count($this->nonemptychildren);
334
        }
335
        return $this->get_filtered_children($extensions, $limit);
336
    }
337
 
338
    /**
339
     * Returns parent file_info instance
340
     *
341
     * @return file_info|null file_info or null for root
342
     */
343
    public function get_parent() {
344
        $parent = $this->context->get_parent_context();
345
        return $this->browser->get_file_info($parent);
346
    }
347
}