| 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 |  * Tiny Media Manager usedfiles.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module      tiny_media/usedfiles
 | 
        
           |  |  | 20 |  * @copyright   2022, Stevani Andolo <stevani@hotmail.com.au>
 | 
        
           |  |  | 21 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  */
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | import * as Templates from 'core/templates';
 | 
        
           |  |  | 25 | import Config from 'core/config';
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | class UsedFileManager {
 | 
        
           | 1441 | ariadna | 28 |     constructor(userContext, itemId, elementId) {
 | 
        
           |  |  | 29 |         this.files = this.getFiles();
 | 
        
           | 1 | efrain | 30 |         this.userContext = userContext;
 | 
        
           |  |  | 31 |         this.itemId = itemId;
 | 
        
           |  |  | 32 |         this.elementId = elementId;
 | 
        
           |  |  | 33 |     }
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     getElementId() {
 | 
        
           |  |  | 36 |         return this.elementId;
 | 
        
           |  |  | 37 |     }
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     getUsedFiles() {
 | 
        
           |  |  | 40 |         const editor = window.parent.tinymce.EditorManager.get(this.getElementId());
 | 
        
           |  |  | 41 |         if (!editor) {
 | 
        
           |  |  | 42 |             window.console.error(`Editor not found for ${this.getElementId()}`);
 | 
        
           |  |  | 43 |             return [];
 | 
        
           |  |  | 44 |         }
 | 
        
           |  |  | 45 |         const content = editor.getContent();
 | 
        
           |  |  | 46 |         const baseUrl = `${Config.wwwroot}/draftfile.php/${this.userContext}/user/draft/${this.itemId}/`;
 | 
        
           |  |  | 47 |         const pattern = new RegExp("[\"']" + baseUrl.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + "(?<filename>.+?)[\\?\"']", 'gm');
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |         const usedFiles = [...content.matchAll(pattern)].map((match) => decodeURIComponent(match.groups.filename));
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |         return usedFiles;
 | 
        
           |  |  | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     // Return an array of unused files.
 | 
        
           |  |  | 55 |     findUnusedFiles(usedFiles) {
 | 
        
           |  |  | 56 |         return Object.entries(this.files)
 | 
        
           |  |  | 57 |             .filter(([filename]) => !usedFiles.includes(filename))
 | 
        
           |  |  | 58 |             .map(([filename]) => filename);
 | 
        
           |  |  | 59 |     }
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     // Return an array of missing files.
 | 
        
           |  |  | 62 |     findMissingFiles(usedFiles) {
 | 
        
           |  |  | 63 |         return usedFiles.filter((filename) => !this.files.hasOwnProperty(filename));
 | 
        
           |  |  | 64 |     }
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |     updateFiles() {
 | 
        
           |  |  | 67 |         const form = document.querySelector('form');
 | 
        
           |  |  | 68 |         const usedFiles = this.getUsedFiles();
 | 
        
           |  |  | 69 |         const unusedFiles = this.findUnusedFiles(usedFiles);
 | 
        
           |  |  | 70 |         const missingFiles = this.findMissingFiles(usedFiles);
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         form.querySelectorAll('input[type=checkbox][name^="deletefile"]').forEach((checkbox) => {
 | 
        
           |  |  | 73 |             if (!unusedFiles.includes(checkbox.dataset.filename)) {
 | 
        
           |  |  | 74 |                 checkbox.closest('.fitem').remove();
 | 
        
           |  |  | 75 |             }
 | 
        
           |  |  | 76 |         });
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         form.classList.toggle('has-missing-files', !!missingFiles.length);
 | 
        
           |  |  | 79 |         form.classList.toggle('has-unused-files', !!unusedFiles.length);
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |         return Templates.renderForPromise('tiny_media/missingfiles', {
 | 
        
           |  |  | 82 |             missingFiles,
 | 
        
           |  |  | 83 |         }).then(({html, js}) => {
 | 
        
           |  |  | 84 |             Templates.replaceNodeContents(form.querySelector('.missing-files'), html, js);
 | 
        
           |  |  | 85 |             return;
 | 
        
           |  |  | 86 |         });
 | 
        
           |  |  | 87 |     }
 | 
        
           | 1441 | ariadna | 88 |   | 
        
           |  |  | 89 |     /**
 | 
        
           |  |  | 90 |      * Retrieves a list of existing files selected for deletion.
 | 
        
           |  |  | 91 |      *
 | 
        
           |  |  | 92 |      * @returns {Object} An object where the keys are filenames and the values are file hashes.
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      */
 | 
        
           |  |  | 95 |      getFiles() {
 | 
        
           |  |  | 96 |         const files = {};
 | 
        
           |  |  | 97 |         document.querySelectorAll('input[type=checkbox][name^="deletefile"]').forEach(input => {
 | 
        
           |  |  | 98 |             files[input.dataset.filename] = input.dataset.filehash;
 | 
        
           |  |  | 99 |         });
 | 
        
           |  |  | 100 |         return files;
 | 
        
           |  |  | 101 |     }
 | 
        
           | 1 | efrain | 102 | }
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 | export const init = (files, usercontext, itemid, elementid) => {
 | 
        
           |  |  | 105 |     const manager = new UsedFileManager(files, usercontext, itemid, elementid);
 | 
        
           |  |  | 106 |     manager.updateFiles();
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 |     return manager;
 | 
        
           |  |  | 109 | };
 |