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
namespace core_files\external;
18
 
19
use coding_exception;
20
use core_text;
21
use moodle_url;
22
use renderer_base;
23
use stdClass;
24
use stored_file;
25
 
26
/**
27
 * Class for exporting stored_file data.
28
 *
29
 * @package    core_files
30
 * @copyright  2015 Frédéric Massart - FMCorz.net
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class stored_file_exporter extends \core\external\exporter {
34
 
35
    /** @var int Length of the shortened filename */
36
    protected const FILENAMESHORT_LENGTH = 25;
37
 
38
    /** @var stored_file */
39
    protected $file;
40
 
41
    public function __construct(stored_file $file, $related = array()) {
42
        $this->file = $file;
43
 
44
        $data = new stdClass();
45
        $data->contextid = $file->get_contextid();
46
        $data->component = $file->get_component();
47
        $data->filearea = $file->get_filearea();
48
        $data->itemid = $file->get_itemid();
49
        $data->filepath = $file->get_filepath();
50
        $data->filename = $file->get_filename();
51
        $data->isdir = $file->is_directory();
52
        $data->isimage = $file->is_valid_image();
53
        $data->timemodified = $file->get_timemodified();
54
        $data->timecreated = $file->get_timecreated();
55
        $data->filesize = $file->get_filesize();
56
        $data->author = $file->get_author();
57
        $data->license = $file->get_license();
58
 
59
        if ($related['context']->id != $data->contextid) {
60
            throw new coding_exception('Unexpected context ID received.');
61
        }
62
 
63
        parent::__construct($data, $related);
64
    }
65
 
66
    protected static function define_related() {
67
        return array('context' => 'context');
68
    }
69
 
70
    protected static function define_properties() {
71
        return array(
72
            'contextid' => array(
73
                'type' => PARAM_INT
74
            ),
75
            'component' => array(
76
                'type' => PARAM_COMPONENT
77
            ),
78
            'filearea' => array(
79
                'type' => PARAM_AREA
80
            ),
81
            'itemid' => array(
82
                'type' => PARAM_INT
83
            ),
84
            'filepath' => array(
85
                'type' => PARAM_PATH
86
            ),
87
            'filename' => array(
88
                'type' => PARAM_FILE
89
            ),
90
            'isdir' => array(
91
                'type' => PARAM_BOOL
92
            ),
93
            'isimage' => array(
94
                'type' => PARAM_BOOL
95
            ),
96
            'timemodified' => array(
97
                'type' => PARAM_INT
98
            ),
99
            'timecreated' => array(
100
                'type' => PARAM_INT
101
            ),
102
            'filesize' => array(
103
                'type' => PARAM_INT
104
            ),
105
            'author' => array(
106
                'type' => PARAM_TEXT
107
            ),
108
            'license' => array(
109
                'type' => PARAM_TEXT
110
            )
111
        );
112
    }
113
 
114
    protected static function define_other_properties() {
115
        return array(
116
            'filenameshort' => array(
117
                'type' => PARAM_RAW,
118
            ),
119
            'filesizeformatted' => array(
120
                'type' => PARAM_RAW
121
            ),
122
            'icon' => array(
123
                'type' => PARAM_RAW,
124
            ),
125
            'timecreatedformatted' => array(
126
                'type' => PARAM_RAW
127
            ),
128
            'timemodifiedformatted' => array(
129
                'type' => PARAM_RAW
130
            ),
131
            'url' => array(
132
                'type' => PARAM_URL
133
            ),
134
        );
135
    }
136
 
137
    protected function get_other_values(renderer_base $output) {
138
        $filename = $this->file->get_filename();
139
        $filenameshort = $filename;
140
 
141
        if (core_text::strlen($filename) > static::FILENAMESHORT_LENGTH) {
142
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
143
            $extensionlength = core_text::strlen($extension) + 1;
144
            $filenameshort = core_text::substr($filename, 0, -$extensionlength);
145
            $filenameshort = shorten_text($filenameshort, static::FILENAMESHORT_LENGTH - $extensionlength, true, '..') .
146
                ".{$extension}";
147
        }
148
 
149
        $icon = $this->file->is_directory() ? file_folder_icon() : file_file_icon($this->file);
150
 
151
        $url = moodle_url::make_pluginfile_url(
152
            $this->file->get_contextid(),
153
            $this->file->get_component(),
154
            $this->file->get_filearea(),
155
            $this->file->get_itemid(),
156
            $this->file->get_filepath(),
157
            $this->file->get_filename(),
158
            true
159
        );
160
 
161
        return array(
162
            'filenameshort' => $filenameshort,
163
            'filesizeformatted' => display_size((int) $this->file->get_filesize()),
164
            'icon' => $icon,
165
            'url' => $url->out(false),
166
            'timecreatedformatted' => userdate($this->file->get_timecreated()),
167
            'timemodifiedformatted' => userdate($this->file->get_timemodified()),
168
        );
169
    }
170
 
171
}