Merge branch 'w34_MDL-34868_m22_gmapsv2' of git://github.com/skodak/moodle into MOODL...
[moodle.git] / user / renderer.php
blob73212d5169700dd2b7d7b3a842e92797d46bff78
1 <?php
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.
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/>.
19 /**
20 * Print private files tree
22 * @package core_user
23 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 class core_user_renderer extends plugin_renderer_base {
32 /**
33 * Prints user files tree view
34 * @return string
36 public function user_files_tree() {
37 return $this->render(new user_files_tree);
40 public function render_user_files_tree(user_files_tree $tree) {
41 if (empty($tree->dir['subdirs']) && empty($tree->dir['files'])) {
42 $html = $this->output->box(get_string('nofilesavailable', 'repository'));
43 } else {
44 $htmlid = 'user_files_tree_'.uniqid();
45 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
46 $this->page->requires->js_init_call('M.core_user.init_tree', array(false, $htmlid), false, $module);
47 $html = '<div id="'.$htmlid.'">';
48 $html .= $this->htmllize_tree($tree, $tree->dir);
49 $html .= '</div>';
51 return $html;
54 /**
55 * Internal function - creates htmls structure suitable for YUI tree.
57 protected function htmllize_tree($tree, $dir) {
58 global $CFG;
59 $yuiconfig = array();
60 $yuiconfig['type'] = 'html';
62 if (empty($dir['subdirs']) and empty($dir['files'])) {
63 return '';
65 $result = '<ul>';
66 foreach ($dir['subdirs'] as $subdir) {
67 $image = $this->output->pix_icon("f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon'));
68 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
70 foreach ($dir['files'] as $file) {
71 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
72 $filename = $file->get_filename();
73 $icon = mimeinfo("icon", $filename);
74 $image = $this->output->pix_icon("f/$icon", $filename, 'moodle', array('class'=>'icon'));
75 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
77 $result .= '</ul>';
79 return $result;
83 class user_files_tree implements renderable {
84 public $context;
85 public $dir;
86 public function __construct() {
87 global $USER;
88 $this->context = get_context_instance(CONTEXT_USER, $USER->id);
89 $fs = get_file_storage();
90 $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);