Merge branch 'MDL-61498-master' of https://github.com/lucaboesch/moodle
[moodle.git] / blocks / private_files / renderer.php
blobd3570536aa896e99ad1c0a932b2ad3467602b465
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.
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/>.
17 /**
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 {
29 /**
30 * Prints private files tree view
31 * @return string
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 $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>';
49 return $html;
52 /**
53 * Internal function - creates htmls structure suitable for YUI tree.
55 protected function htmllize_tree($tree, $dir) {
56 global $CFG;
57 $yuiconfig = array();
58 $yuiconfig['type'] = 'html';
60 if (empty($dir['subdirs']) and empty($dir['files'])) {
61 return '';
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>';
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>';
74 $result .= '</ul>';
76 return $result;
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);