2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Print private files tree
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
25 defined('MOODLE_INTERNAL') ||
die();
27 class block_private_files_renderer
extends plugin_renderer_base
{
30 * Prints private files tree view
33 public function private_files_tree() {
34 return $this->render(new private_files_tree
);
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'));
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);
52 * Internal function - creates htmls structure suitable for core/tree AMD.
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.
59 protected function htmllize_tree($tree, $dir, $isroot) {
62 if (empty($dir['subdirs']) and empty($dir['files'])) {
66 $result = '<ul role="tree" aria-label="' . s(get_string('privatefiles')) . '">';
68 $result = '<ul role="group" aria-hidden="true">';
70 foreach ($dir['subdirs'] as $subdir) {
71 $image = $this->output
->pix_icon(file_folder_icon(), '');
72 $content = $this->htmllize_tree($tree, $subdir, false);
74 $result .= '<li role="treeitem" aria-expanded="false"><p>' . $image . s($subdir['dirname']) . '</p>' .
77 $result .= '<li role="treeitem"><p>' . $image . s($subdir['dirname']) . '</p></li>';
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>';
92 class private_files_tree
implements renderable
{
95 public function __construct() {
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);