| 1 | efrain | 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 |  * Folder module renderer
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * @package   mod_folder
 | 
        
           |  |  | 22 |  * @copyright 2009 Petr Skoda  {@link http://skodak.org}
 | 
        
           |  |  | 23 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | class mod_folder_renderer extends plugin_renderer_base {
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 |     /**
 | 
        
           |  |  | 30 |      * Returns html to display the content of mod_folder
 | 
        
           |  |  | 31 |      * (Description, folder files and optionally Edit button)
 | 
        
           |  |  | 32 |      *
 | 
        
           |  |  | 33 |      * @param stdClass $folder record from 'folder' table (please note
 | 
        
           |  |  | 34 |      *     it may not contain fields 'revision' and 'timemodified')
 | 
        
           |  |  | 35 |      * @return string
 | 
        
           |  |  | 36 |      */
 | 
        
           |  |  | 37 |     public function display_folder(stdClass $folder) {
 | 
        
           |  |  | 38 |         static $treecounter = 0;
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |         $folderinstances = get_fast_modinfo($folder->course)->get_instances_of('folder');
 | 
        
           |  |  | 41 |         if (!isset($folderinstances[$folder->id]) ||
 | 
        
           |  |  | 42 |                 !($cm = $folderinstances[$folder->id]) ||
 | 
        
           |  |  | 43 |                 !($context = context_module::instance($cm->id))) {
 | 
        
           |  |  | 44 |             // Some error in parameters.
 | 
        
           |  |  | 45 |             // Don't throw any errors in renderer, just return empty string.
 | 
        
           |  |  | 46 |             // Capability to view module must be checked before calling renderer.
 | 
        
           |  |  | 47 |             return '';
 | 
        
           |  |  | 48 |         }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         $data = [];
 | 
        
           |  |  | 51 |         if (trim($folder->intro)) {
 | 
        
           |  |  | 52 |             if ($folder->display == FOLDER_DISPLAY_INLINE && $cm->showdescription) {
 | 
        
           |  |  | 53 |                 // for "display inline" do not filter, filters run at display time.
 | 
        
           |  |  | 54 |                 $data['intro'] = format_module_intro('folder', $folder, $cm->id, false);
 | 
        
           |  |  | 55 |             }
 | 
        
           |  |  | 56 |         }
 | 
        
           |  |  | 57 |         $buttons = [];
 | 
        
           |  |  | 58 |         // Display the "Edit" button if current user can edit folder contents.
 | 
        
           |  |  | 59 |         // Do not display it on the course page for the teachers because there
 | 
        
           |  |  | 60 |         // is an "Edit settings" option in the action menu with the same functionality.
 | 
        
           |  |  | 61 |         $canmanagefolderfiles = has_capability('mod/folder:managefiles', $context);
 | 
        
           |  |  | 62 |         $canmanagecourseactivities = has_capability('moodle/course:manageactivities', $context);
 | 
        
           |  |  | 63 |         if ($canmanagefolderfiles && ($folder->display != FOLDER_DISPLAY_INLINE || !$canmanagecourseactivities)) {
 | 
        
           |  |  | 64 |             $editbutton = new single_button(new moodle_url('/mod/folder/edit.php', ['id' => $cm->id]),
 | 
        
           |  |  | 65 |                 get_string('edit'), 'post', single_button::BUTTON_PRIMARY);
 | 
        
           |  |  | 66 |             $editbutton->class = 'navitem';
 | 
        
           |  |  | 67 |             $data['edit_button'] = $editbutton->export_for_template($this->output);
 | 
        
           |  |  | 68 |             $data['hasbuttons'] = true;
 | 
        
           |  |  | 69 |         }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |         $downloadable = folder_archive_available($folder, $cm);
 | 
        
           |  |  | 72 |         if ($downloadable) {
 | 
        
           |  |  | 73 |             $downloadbutton = new single_button(new moodle_url('/mod/folder/download_folder.php', ['id' => $cm->id]),
 | 
        
           |  |  | 74 |                 get_string('downloadfolder', 'folder'), 'get');
 | 
        
           | 1441 | ariadna | 75 |             $downloadbutton->class = 'navitem ms-auto';
 | 
        
           | 1 | efrain | 76 |             $data['download_button'] = $downloadbutton->export_for_template($this->output);
 | 
        
           |  |  | 77 |             $data['hasbuttons'] = true;
 | 
        
           |  |  | 78 |         }
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         $foldertree = new folder_tree($folder, $cm);
 | 
        
           |  |  | 81 |         if ($folder->display == FOLDER_DISPLAY_INLINE) {
 | 
        
           |  |  | 82 |             // Display module name as the name of the root directory.
 | 
        
           |  |  | 83 |             $foldertree->dir['dirname'] = $cm->get_formatted_name(array('escape' => false));
 | 
        
           |  |  | 84 |         }
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         $data['id'] = 'folder_tree'. ($treecounter++);
 | 
        
           |  |  | 87 |         $data['showexpanded'] = !empty($foldertree->folder->showexpanded);
 | 
        
           |  |  | 88 |         $data['dir'] = $this->renderable_tree_elements($foldertree, ['files' => [], 'subdirs' => [$foldertree->dir]]);
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |         return $this->render_from_template('mod_folder/folder', $data);
 | 
        
           |  |  | 91 |     }
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |     /**
 | 
        
           |  |  | 94 |      * @deprecated since Moodle 4.3
 | 
        
           |  |  | 95 |      */
 | 
        
           | 1441 | ariadna | 96 |     #[\core\attribute\deprecated('renderable_tree_elements()', since: '4.3', mdl: 'MDL-78847', final: true)]
 | 
        
           |  |  | 97 |     protected function htmllize_tree() {
 | 
        
           |  |  | 98 |         \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
 | 
        
           | 1 | efrain | 99 |     }
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |     /**
 | 
        
           |  |  | 102 |      * Internal function - Creates elements structure suitable for mod_folder/folder template.
 | 
        
           |  |  | 103 |      *
 | 
        
           |  |  | 104 |      * @param folder_tree $tree The folder tree to work with.
 | 
        
           |  |  | 105 |      * @param array $dir The subdir and files structure to convert into a tree.
 | 
        
           |  |  | 106 |      * @return array The structure to be rendered by mod_folder/folder template.
 | 
        
           |  |  | 107 |      */
 | 
        
           |  |  | 108 |     protected function renderable_tree_elements(folder_tree $tree, array $dir): array {
 | 
        
           |  |  | 109 |         if (empty($dir['subdirs']) && empty($dir['files'])) {
 | 
        
           |  |  | 110 |             return [];
 | 
        
           |  |  | 111 |         }
 | 
        
           |  |  | 112 |         $elements = [];
 | 
        
           |  |  | 113 |         foreach ($dir['subdirs'] as $subdir) {
 | 
        
           |  |  | 114 |             $htmllize = $this->renderable_tree_elements($tree, $subdir);
 | 
        
           |  |  | 115 |             $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle');
 | 
        
           |  |  | 116 |             $elements[] = [
 | 
        
           |  |  | 117 |                 'name' => $subdir['dirname'],
 | 
        
           |  |  | 118 |                 'icon' => $image,
 | 
        
           |  |  | 119 |                 'subdirs' => $htmllize,
 | 
        
           |  |  | 120 |                 'hassubdirs' => !empty($htmllize),
 | 
        
           |  |  | 121 |             ];
 | 
        
           |  |  | 122 |         }
 | 
        
           |  |  | 123 |         foreach ($dir['files'] as $file) {
 | 
        
           |  |  | 124 |             $filename = $file->get_filename();
 | 
        
           |  |  | 125 |             $filenamedisplay = clean_filename($filename);
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |             $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
 | 
        
           |  |  | 128 |                 $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);
 | 
        
           |  |  | 129 |             if (file_extension_in_typegroup($filename, 'web_image')) {
 | 
        
           |  |  | 130 |                 $image = $url->out(false, ['preview' => 'tinyicon', 'oid' => $file->get_timemodified()]);
 | 
        
           |  |  | 131 |                 $image = html_writer::empty_tag('img', ['src' => $image]);
 | 
        
           |  |  | 132 |             } else {
 | 
        
           |  |  | 133 |                 $image = $this->output->pix_icon(file_file_icon($file), $filenamedisplay, 'moodle');
 | 
        
           |  |  | 134 |             }
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 |             if ($tree->folder->forcedownload) {
 | 
        
           |  |  | 137 |                 $url->param('forcedownload', 1);
 | 
        
           |  |  | 138 |             }
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |             $elements[] = [
 | 
        
           |  |  | 141 |                 'name' => $filenamedisplay,
 | 
        
           |  |  | 142 |                 'icon' => $image,
 | 
        
           |  |  | 143 |                 'url' => $url,
 | 
        
           |  |  | 144 |                 'subdirs' => null,
 | 
        
           |  |  | 145 |                 'hassubdirs' => false,
 | 
        
           |  |  | 146 |             ];
 | 
        
           |  |  | 147 |         }
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 |         return $elements;
 | 
        
           |  |  | 150 |     }
 | 
        
           |  |  | 151 | }
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 | class folder_tree implements renderable {
 | 
        
           |  |  | 154 |     public $context;
 | 
        
           |  |  | 155 |     public $folder;
 | 
        
           |  |  | 156 |     public $cm;
 | 
        
           |  |  | 157 |     public $dir;
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 |     public function __construct($folder, $cm) {
 | 
        
           |  |  | 160 |         $this->folder = $folder;
 | 
        
           |  |  | 161 |         $this->cm     = $cm;
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |         $this->context = context_module::instance($cm->id);
 | 
        
           |  |  | 164 |         $fs = get_file_storage();
 | 
        
           |  |  | 165 |         $this->dir = $fs->get_area_tree($this->context->id, 'mod_folder', 'content', 0);
 | 
        
           |  |  | 166 |     }
 | 
        
           |  |  | 167 | }
 |