Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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          $output = '';
  39          $folderinstances = get_fast_modinfo($folder->course)->get_instances_of('folder');
  40          if (!isset($folderinstances[$folder->id]) ||
  41                  !($cm = $folderinstances[$folder->id]) ||
  42                  !($context = context_module::instance($cm->id))) {
  43              // Some error in parameters.
  44              // Don't throw any errors in renderer, just return empty string.
  45              // Capability to view module must be checked before calling renderer.
  46              return $output;
  47          }
  48  
  49          if (trim($folder->intro)) {
  50              if ($folder->display == FOLDER_DISPLAY_INLINE && $cm->showdescription) {
  51                  // for "display inline" do not filter, filters run at display time.
  52                  $output .= format_module_intro('folder', $folder, $cm->id, false);
  53              }
  54          }
  55          $buttons = '';
  56          // Display the "Edit" button if current user can edit folder contents.
  57          // Do not display it on the course page for the teachers because there
  58          // is an "Edit settings" button right next to it with the same functionality.
  59          $canmanagefolderfiles = has_capability('mod/folder:managefiles', $context);
  60          $canmanagecourseactivities = has_capability('moodle/course:manageactivities', $context);
  61          if ($canmanagefolderfiles && ($folder->display != FOLDER_DISPLAY_INLINE || !$canmanagecourseactivities)) {
  62              $editbutton = new single_button(new moodle_url('/mod/folder/edit.php', ['id' => $cm->id]),
  63                  get_string('edit'), 'post', single_button::BUTTON_PRIMARY);
  64              $editbutton->class = 'navitem';
  65              $buttons .= $this->render($editbutton);
  66          }
  67  
  68          // Do not append the edit button on the course page.
  69          $downloadable = folder_archive_available($folder, $cm);
  70          if ($downloadable) {
  71              $downloadbutton = new single_button(new moodle_url('/mod/folder/download_folder.php', ['id' => $cm->id]),
  72                  get_string('downloadfolder', 'folder'), 'get');
  73              $downloadbutton->class = 'navitem ml-auto';
  74              $buttons .= $this->render($downloadbutton);
  75          }
  76  
  77          if ($buttons) {
  78              $output .= $this->output->container_start("container-fluid tertiary-navigation");
  79              $output .= $this->output->container_start("row");
  80              $output .= $buttons;
  81              $output .= $this->output->container_end();
  82              $output .= $this->output->container_end();
  83          }
  84  
  85          $foldertree = new folder_tree($folder, $cm);
  86          if ($folder->display == FOLDER_DISPLAY_INLINE) {
  87              // Display module name as the name of the root directory.
  88              $foldertree->dir['dirname'] = $cm->get_formatted_name(array('escape' => false));
  89          }
  90          $output .= $this->output->container_start("box generalbox pt-0 pb-3 foldertree");
  91          $output .= $this->render($foldertree);
  92          $output .= $this->output->container_end();
  93  
  94          return $output;
  95      }
  96  
  97      public function render_folder_tree(folder_tree $tree) {
  98          static $treecounter = 0;
  99  
 100          $content = '';
 101          $id = 'folder_tree'. ($treecounter++);
 102          $content .= '<div id="'.$id.'" class="filemanager">';
 103          $content .= $this->htmllize_tree($tree, array('files' => array(), 'subdirs' => array($tree->dir)));
 104          $content .= '</div>';
 105          $showexpanded = true;
 106          if (empty($tree->folder->showexpanded)) {
 107              $showexpanded = false;
 108          }
 109          $this->page->requires->js_init_call('M.mod_folder.init_tree', array($id, $showexpanded));
 110          return $content;
 111      }
 112  
 113      /**
 114       * Internal function - creates htmls structure suitable for YUI tree.
 115       */
 116      protected function htmllize_tree($tree, $dir) {
 117          global $CFG;
 118  
 119          if (empty($dir['subdirs']) and empty($dir['files'])) {
 120              return '';
 121          }
 122          $result = '<ul>';
 123          foreach ($dir['subdirs'] as $subdir) {
 124              $image = $this->output->pix_icon(file_folder_icon(24), $subdir['dirname'], 'moodle');
 125              $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
 126                      html_writer::tag('span', s($subdir['dirname']), array('class' => 'fp-filename'));
 127              $filename = html_writer::tag('div', $filename, array('class' => 'fp-filename-icon'));
 128              $result .= html_writer::tag('li', $filename. $this->htmllize_tree($tree, $subdir));
 129          }
 130          foreach ($dir['files'] as $file) {
 131              $filename = $file->get_filename();
 132              $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(),
 133                      $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $filename, false);
 134              $filenamedisplay = clean_filename($filename);
 135              if (file_extension_in_typegroup($filename, 'web_image')) {
 136                  $image = $url->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
 137                  $image = html_writer::empty_tag('img', array('src' => $image));
 138              } else {
 139                  $image = $this->output->pix_icon(file_file_icon($file, 24), $filenamedisplay, 'moodle');
 140              }
 141              $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
 142                      html_writer::tag('span', $filenamedisplay, array('class' => 'fp-filename'));
 143              $urlparams = null;
 144              if ($tree->folder->forcedownload) {
 145                  $urlparams = ['forcedownload' => 1];
 146              }
 147              $filename = html_writer::tag('span',
 148                  html_writer::link($url->out(false, $urlparams), $filename),
 149                  ['class' => 'fp-filename-icon']
 150              );
 151              $result .= html_writer::tag('li', $filename);
 152          }
 153          $result .= '</ul>';
 154  
 155          return $result;
 156      }
 157  }
 158  
 159  class folder_tree implements renderable {
 160      public $context;
 161      public $folder;
 162      public $cm;
 163      public $dir;
 164  
 165      public function __construct($folder, $cm) {
 166          $this->folder = $folder;
 167          $this->cm     = $cm;
 168  
 169          $this->context = context_module::instance($cm->id);
 170          $fs = get_file_storage();
 171          $this->dir = $fs->get_area_tree($this->context->id, 'mod_folder', 'content', 0);
 172      }
 173  }