Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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');
  75              $downloadbutton->class = 'navitem ml-auto';
  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       * Internal function - creates htmls structure suitable for YUI tree.
  95       *
  96       * @deprecated since Moodle 4.3
  97       */
  98      protected function htmllize_tree($tree, $dir) {
  99          global $CFG;
 100  
 101          debugging(
 102              'Method htmllize_tree() is deprecated. Please use renderable_tree_elements instead',
 103              DEBUG_DEVELOPER
 104          );
 105  
 106          if (empty($dir['subdirs']) and empty($dir['files'])) {
 107              return '';
 108          }
 109          $result = '<ul>';
 110          foreach ($dir['subdirs'] as $subdir) {
 111              $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle');
 112              $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
 113                  html_writer::tag('span', s($subdir['dirname']), array('class' => 'fp-filename'));
 114              $filename = html_writer::tag('div', $filename, array('class' => 'fp-filename-icon'));
 115              $result .= html_writer::tag('li', $filename. $this->htmllize_tree($tree, $subdir));
 116          }
 117          foreach ($dir['files'] as $file) {
 118              $filename = $file->get_filename();
 119              $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
 120                  $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);
 121              $filenamedisplay = clean_filename($filename);
 122              if (file_extension_in_typegroup($filename, 'web_image')) {
 123                  $image = $url->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
 124                  $image = html_writer::empty_tag('img', array('src' => $image));
 125              } else {
 126                  $image = $this->output->pix_icon(file_file_icon($file), $filenamedisplay, 'moodle');
 127              }
 128              $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
 129                  html_writer::tag('span', $filenamedisplay, array('class' => 'fp-filename'));
 130              $urlparams = null;
 131              if ($tree->folder->forcedownload) {
 132                  $urlparams = ['forcedownload' => 1];
 133              }
 134              $filename = html_writer::tag('span',
 135                  html_writer::link($url->out(false, $urlparams), $filename),
 136                  ['class' => 'fp-filename-icon']
 137              );
 138              $result .= html_writer::tag('li', $filename);
 139          }
 140          $result .= '</ul>';
 141  
 142          return $result;
 143      }
 144  
 145      /**
 146       * Internal function - Creates elements structure suitable for mod_folder/folder template.
 147       *
 148       * @param folder_tree $tree The folder tree to work with.
 149       * @param array $dir The subdir and files structure to convert into a tree.
 150       * @return array The structure to be rendered by mod_folder/folder template.
 151       */
 152      protected function renderable_tree_elements(folder_tree $tree, array $dir): array {
 153          if (empty($dir['subdirs']) && empty($dir['files'])) {
 154              return [];
 155          }
 156          $elements = [];
 157          foreach ($dir['subdirs'] as $subdir) {
 158              $htmllize = $this->renderable_tree_elements($tree, $subdir);
 159              $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle');
 160              $elements[] = [
 161                  'name' => $subdir['dirname'],
 162                  'icon' => $image,
 163                  'subdirs' => $htmllize,
 164                  'hassubdirs' => !empty($htmllize),
 165              ];
 166          }
 167          foreach ($dir['files'] as $file) {
 168              $filename = $file->get_filename();
 169              $filenamedisplay = clean_filename($filename);
 170  
 171              $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
 172                  $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);
 173              if (file_extension_in_typegroup($filename, 'web_image')) {
 174                  $image = $url->out(false, ['preview' => 'tinyicon', 'oid' => $file->get_timemodified()]);
 175                  $image = html_writer::empty_tag('img', ['src' => $image]);
 176              } else {
 177                  $image = $this->output->pix_icon(file_file_icon($file), $filenamedisplay, 'moodle');
 178              }
 179  
 180              if ($tree->folder->forcedownload) {
 181                  $url->param('forcedownload', 1);
 182              }
 183  
 184              $elements[] = [
 185                  'name' => $filenamedisplay,
 186                  'icon' => $image,
 187                  'url' => $url,
 188                  'subdirs' => null,
 189                  'hassubdirs' => false,
 190              ];
 191          }
 192  
 193          return $elements;
 194      }
 195  }
 196  
 197  class folder_tree implements renderable {
 198      public $context;
 199      public $folder;
 200      public $cm;
 201      public $dir;
 202  
 203      public function __construct($folder, $cm) {
 204          $this->folder = $folder;
 205          $this->cm     = $cm;
 206  
 207          $this->context = context_module::instance($cm->id);
 208          $fs = get_file_storage();
 209          $this->dir = $fs->get_area_tree($this->context->id, 'mod_folder', 'content', 0);
 210      }
 211  }