+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+
+defined('MOODLE_INTERNAL') || die();
+
+class block_private_files_renderer extends plugin_renderer_base {
+
+ /**
+ * Prints private files tree view
+ * @return string
+ */
+ public function private_files_tree() {
+ return $this->render(new private_files_tree);
+ }
+
+ public function render_private_files_tree(private_files_tree $tree) {
+ $module = array('name'=>'block_private_files', 'fullpath'=>'/blocks/private_files/module.js', 'requires'=>array('yui2-treeview'));
+ $htmlid = 'private_files_tree_'.uniqid();
+ $this->page->requires->js_init_call('M.block_private_files.init_tree', array(true, $htmlid));
+ $html = '';
+ $html .= $this->htmllize_tree($tree, $tree->dir);
+ $html .= '
';
+ return $html;
+ }
+
+ /**
+ * Internal function - creates htmls structure suitable for YUI tree.
+ */
+ protected function htmllize_tree($tree, $dir) {
+ global $CFG;
+
+ if (empty($dir['subdirs']) and empty($dir['files'])) {
+ return '';
+ }
+ $result = '';
+ foreach ($dir['subdirs'] as $subdir) {
+ $result .= '- '.s($subdir['dirname']).' '.$this->htmllize_tree($tree, $subdir).'
';
+ }
+ foreach ($dir['files'] as $file) {
+ $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/user/private'.$file->get_filepath().$file->get_filename(), true);
+ $filename = $file->get_filename();
+ $result .= '- '.html_writer::link($url, $filename).'
';
+ }
+ $result .= '
';
+
+ return $result;
+ }
+}
+
+class private_files_tree implements renderable {
+ public $context;
+ public $dir;
+ public function __construct() {
+ global $USER;
+ $this->context = get_context_instance(CONTEXT_USER, $USER->id);
+ $fs = get_file_storage();
+ $this->dir = $fs->get_area_tree($this->context->id, 'user', 'private', 0);
+ }
+}