Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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