Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

   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          if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
  39              $html = $this->output->box(get_string('nofilesavailable', 'repository'));
  40          } else {
  41              $htmlid = 'private_files_tree_'.uniqid();
  42              $this->page->requires->js_call_amd('block_private_files/files_tree', 'init', [$htmlid]);
  43              $html = '<div id="'.$htmlid.'">';
  44              $html .= $this->htmllize_tree($tree, $tree->dir, true);
  45              $html .= '</div>';
  46          }
  47  
  48          return $html;
  49      }
  50  
  51      /**
  52       * Internal function - creates htmls structure suitable for core/tree AMD.
  53       *
  54       * @param private_files_tree $tree The renderable tree.
  55       * @param array $dir The directory in the tree
  56       * @param bool $isroot If it is the root directory in the tree.
  57       * @return string
  58       */
  59      protected function htmllize_tree($tree, $dir, $isroot) {
  60          global $CFG;
  61  
  62          if (empty($dir['subdirs']) and empty($dir['files'])) {
  63              return '';
  64          }
  65          if ($isroot) {
  66              $result = '<ul role="tree" aria-label="' . s(get_string('privatefiles')) . '">';
  67          } else {
  68              $result = '<ul role="group" aria-hidden="true">';
  69          }
  70          foreach ($dir['subdirs'] as $subdir) {
  71              $image = $this->output->pix_icon(file_folder_icon(), '');
  72              $content = $this->htmllize_tree($tree, $subdir, false);
  73              if ($content) {
  74                  $result .= '<li role="treeitem" aria-expanded="false"><p>' . $image . s($subdir['dirname']) . '</p>' .
  75                      $content . '</li>';
  76              } else {
  77                  $result .= '<li role="treeitem"><p>' . $image . s($subdir['dirname']) . '</p></li>';
  78              }
  79          }
  80          foreach ($dir['files'] as $file) {
  81              $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
  82              $filename = $file->get_filename();
  83              $image = $this->output->pix_icon(file_file_icon($file), '');
  84              $result .= '<li role="treeitem">'.html_writer::link($url, $image.$filename, ['tabindex' => -1]).'</li>';
  85          }
  86          $result .= '</ul>';
  87  
  88          return $result;
  89      }
  90  }
  91  
  92  class private_files_tree implements renderable {
  93      public $context;
  94      public $dir;
  95      public function __construct() {
  96          global $USER;
  97          $this->context = context_user::instance($USER->id);
  98          $fs = get_file_storage();
  99          $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
 100      }
 101  }