Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * @package    atto_managefiles
18
 * @copyright  2014 Frédéric Massart
19
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20
 */
21
 
22
/**
23
 * @module moodle-atto_managefiles-usedfiles
24
 */
25
 
26
 
27
/**
28
 * Atto text editor managefiles usedfiles plugin.
29
 *
30
 * @namespace M.atto_managefiles
31
 * @class usedfiles
32
 */
33
 
34
/**
35
 * CSS constants.
36
 *
37
 * @type {Object}
38
 */
39
var CSS = {
40
    HASMISSINGFILES: 'has-missing-files',
41
    HASUNUSEDFILES: 'has-unused-files'
42
};
43
 
44
/**
45
 * Selectors constants.
46
 *
47
 * @type {Object}
48
 */
49
var SELECTORS = {
50
    FILEANCESTOR: '.fitem',
51
    FORM: '#atto_managefiles_manageform',
52
    MISSINGFILES: '.missing-files'
53
};
54
 
55
M.atto_managefiles = M.atto_managefiles || {};
56
M.atto_managefiles.usedfiles = M.atto_managefiles.usedfiles || {
57
 
58
    /**
59
     * The user context.
60
     *
61
     * @property _usercontext
62
     * @type Number
63
     * @private
64
     */
65
    _usercontext: null,
66
 
67
    /**
68
     * Area Item ID.
69
     *
70
     * @property _itemid
71
     * @type String
72
     * @private
73
     */
74
    _itemid: null,
75
 
76
    /**
77
     * The editor elementid
78
     *
79
     * @property _elementid
80
     * @type String
81
     * @private
82
     */
83
    _elementid: null,
84
 
85
    /**
86
     * Init function.
87
     *
88
     * @param {Object} allFiles The keys are the file names, the values are the hashes.
89
     * @return {Void}
90
     */
91
    init: function(config) {
92
        this._usercontext = config.usercontext;
93
        this._itemid = config.itemid;
94
        this._elementid = config.elementid;
95
 
96
        var allFiles = config.files;
97
        var form = Y.one(SELECTORS.FORM),
98
            usedFiles,
99
            unusedFiles,
100
            missingFiles,
101
            missingFilesTxt,
102
            i;
103
 
104
        if (!form || !window.parent) {
105
            Y.log("Unable to find parent window", 'warn', 'moodle-atto_managemedia-usedfiles');
106
            return;
107
        }
108
 
109
        usedFiles = this._getUsedFiles();
110
        unusedFiles = this.findUnusedFiles(allFiles, usedFiles);
111
        missingFiles = this.findMissingFiles(allFiles, usedFiles);
112
 
113
        // There are some unused files.
114
        if (unusedFiles.length > 0) {
115
            // Loop over all the files in the form.
116
            form.all('input[type=checkbox][name^="deletefile"]').each(function(node) {
117
                // If the file is used, remove it.
118
                if (Y.Array.indexOf(unusedFiles, node.getData('filename')) === -1) {
119
                    node.ancestor(SELECTORS.FILEANCESTOR).remove();
120
                }
121
            });
122
            form.addClass(CSS.HASUNUSEDFILES);
123
        } else {
124
            // This is needed as the init may be called twice due to the double call to $PAGE->requires->yui_module().
125
            form.removeClass(CSS.HASUNUSEDFILES);
126
        }
127
 
128
        // There are some files missing.
129
        if (missingFiles.length > 0) {
130
            missingFilesTxt = '<ul>';
131
            for (i = 0; i < missingFiles.length; i++) {
132
                missingFilesTxt += '<li>' + Y.Escape.html(missingFiles[i]) + '</li>';
133
            }
134
            missingFilesTxt += '</ul>';
135
            form.one(SELECTORS.MISSINGFILES).setHTML('').append(missingFilesTxt);
136
            form.addClass(CSS.HASMISSINGFILES);
137
        } else {
138
            form.removeClass(CSS.HASMISSINGFILES);
139
        }
140
    },
141
 
142
    /**
143
     * Return the list of files used in the area.
144
     *
145
     * @method _getUsedFiles
146
     * @return {Object} List of files used where the keys are the name of the files, the value is true.
147
     * @private
148
     */
149
    _getUsedFiles: function() {
150
        var content = Y.one(window.parent.document.getElementById(this._elementid + 'editable')),
151
            baseUrl = M.cfg.wwwroot + '/draftfile.php/' + this._usercontext + '/user/draft/' + this._itemid + '/',
152
            pattern = new RegExp("[\"']" + baseUrl.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + "(.+?)[\\?\"']", 'gm'),
153
            filename = '',
154
            match = '',
155
            usedFiles = {};
156
 
157
        // The pattern matches any draftfile URL contained within quotes, e.g. 'src="<filename>"' or 'href="<filename>"'.
158
        while ((match = pattern.exec(content.get('innerHTML'))) !== null) {
159
            filename = decodeURIComponent(match[1]);
160
            usedFiles[filename] = true;
161
        }
162
 
163
        return usedFiles;
164
    },
165
 
166
    /**
167
     * Return an array of unused files.
168
     *
169
     * @param {Object} allFiles Where the keys are the file names.
170
     * @param {Object} usedFiles Where the keys are the file names.
171
     * @return {Array} Of file names.
172
     */
173
    findUnusedFiles: function(allFiles, usedFiles) {
174
        var key,
175
            list = [];
176
        for (key in allFiles) {
177
            if (!usedFiles[key]) {
178
                list.push(key);
179
            }
180
        }
181
        return list;
182
    },
183
 
184
    /**
185
     * Return an array of missing files.
186
     *
187
     * @param {Object} allFiles Where the keys are the file names.
188
     * @param {Object} usedFiles Where the keys are the file names.
189
     * @return {Array} Of file names.
190
     */
191
    findMissingFiles: function(allFiles, usedFiles) {
192
        var key,
193
            list = [];
194
        for (key in usedFiles) {
195
            if (!allFiles[key]) {
196
                list.push(key);
197
            }
198
        }
199
        return list;
200
    }
201
};