Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Print private files tree
  19   *
  20   * @package    block_private_files
  21   * @copyright  2010 Dongsheng Cai <dongsheng@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  class block_private_files_renderer extends plugin_renderer_base {
  28  
  29      /**
  30       * Prints private files tree view
  31       * @return string
  32       */
  33      public function private_files_tree() {
  34          return $this->render(new private_files_tree);
  35      }
  36  
  37      public function render_private_files_tree(private_files_tree $tree) {
  38          $module = array('name'=>'block_private_files', 'fullpath'=>'/blocks/private_files/module.js', 'requires'=>array('yui2-treeview'));
  39          if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
  40              $html = $this->output->box(get_string('nofilesavailable', 'repository'));
  41          } else {
  42              $htmlid = 'private_files_tree_'.uniqid();
  43              $this->page->requires->js_init_call('M.block_private_files.init_tree', array(false, $htmlid));
  44              $html = '<div id="'.$htmlid.'">';
  45              $html .= $this->htmllize_tree($tree, $tree->dir);
  46              $html .= '</div>';
  47          }
  48  
  49          return $html;
  50      }
  51  
  52      /**
  53       * Internal function - creates htmls structure suitable for YUI tree.
  54       */
  55      protected function htmllize_tree($tree, $dir) {
  56          global $CFG;
  57          $yuiconfig = array();
  58          $yuiconfig['type'] = 'html';
  59  
  60          if (empty($dir['subdirs']) and empty($dir['files'])) {
  61              return '';
  62          }
  63          $result = '<ul>';
  64          foreach ($dir['subdirs'] as $subdir) {
  65              $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
  66              $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
  67          }
  68          foreach ($dir['files'] as $file) {
  69              $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
  70              $filename = $file->get_filename();
  71              $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
  72              $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.html_writer::link($url, $image.$filename).'</div></li>';
  73          }
  74          $result .= '</ul>';
  75  
  76          return $result;
  77      }
  78  }
  79  
  80  class private_files_tree implements renderable {
  81      public $context;
  82      public $dir;
  83      public function __construct() {
  84          global $USER;
  85          $this->context = context_user::instance($USER->id);
  86          $fs = get_file_storage();
  87          $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
  88      }
  89  }