"MDL-24032, fixed error when use bulk force password change, credits to Myles Carrick"
[moodle.git] / user / renderer.php
CommitLineData
82af55d7
MD
1<?php
2
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.
14//
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/>.
17
18
19/**
20 * Print private files tree
21 *
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
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30class core_user_renderer extends plugin_renderer_base {
31
32 /**
33 * Prints user files tree view
34 * @return string
35 */
36 public function user_files_tree() {
37 return $this->render(new user_files_tree);
38 }
39
40 public function render_user_files_tree(user_files_tree $tree) {
41 $htmlid = 'user_files_tree_'.uniqid();
59cab436
PS
42 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
43 $this->page->requires->js_init_call('M.core_user.init_tree', array(false, $htmlid), false, $module);
44
82af55d7
MD
45 $html = '<div id="'.$htmlid.'">';
46 $html .= $this->htmllize_tree($tree, $tree->dir);
47 $html .= '</div>';
48 return $html;
49 }
50
51 /**
52 * Internal function - creates htmls structure suitable for YUI tree.
53 */
54 protected function htmllize_tree($tree, $dir) {
55 global $CFG;
56 $yuiconfig = array();
57 $yuiconfig['type'] = 'html';
58
59 if (empty($dir['subdirs']) and empty($dir['files'])) {
60 return '';
61 }
62 $result = '<ul>';
63 foreach ($dir['subdirs'] as $subdir) {
ede72522 64 $image = $this->output->pix_icon("f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon'));
82af55d7
MD
65 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
66 }
67 foreach ($dir['files'] as $file) {
68 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
69 $filename = $file->get_filename();
ede72522
PS
70 $icon = mimeinfo("icon", $filename);
71 $image = $this->output->pix_icon("f/$icon", $filename, 'moodle', array('class'=>'icon'));
82af55d7
MD
72 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
73 }
74 $result .= '</ul>';
75
76 return $result;
77 }
78}
79
80class user_files_tree implements renderable {
81 public $context;
82 public $dir;
83 public function __construct() {
84 global $USER;
85 $this->context = get_context_instance(CONTEXT_USER, $USER->id);
86 $fs = get_file_storage();
87 $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
88 }
89}