MDL-23116, fixed private files block
authorDongsheng Cai <unoter@gmail.com>
Wed, 7 Jul 2010 05:20:46 +0000 (05:20 +0000)
committerDongsheng Cai <unoter@gmail.com>
Wed, 7 Jul 2010 05:20:46 +0000 (05:20 +0000)
blocks/private_files/block_private_files.php
blocks/private_files/module.js [new file with mode: 0644]
blocks/private_files/renderer.php [new file with mode: 0644]

index cc8432e..eab2394 100755 (executable)
@@ -24,9 +24,6 @@
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-
-require_once($CFG->dirroot. '/repository/lib.php');
-
 class block_private_files extends block_base {
 
     function init() {
@@ -56,23 +53,13 @@ class block_private_files extends block_base {
         $this->content->text = '';
         $this->content->footer = '';
         if (isloggedin() && !isguestuser()) {   // Show the block
-
-            $options = new stdclass;
-            $options->maxbytes  = -1;
-            $options->maxfiles  = -1;
-            $options->subdirs   = true;
-            $options->accepted_types = '*';
-            $options->return_types = FILE_INTERNAL;
-            $options->context   = $PAGE->context;
-            $options->disable_types = array('user');
-
             $this->content = new object();
 
             //TODO: add capability check here!
 
-            //TODO: add list of available files here
-            $this->content->text = $OUTPUT->single_button(new moodle_url('/blocks/private_files/edit.php'), get_string('edit'), 'get');
-;
+            $renderer = $this->page->get_renderer('block_private_files');
+            $this->content->text = $renderer->private_files_tree();
+            $this->content->text .= $OUTPUT->single_button(new moodle_url('/blocks/private_files/edit.php'), get_string('edit'), 'get');
             $this->content->footer = '';
 
         }
diff --git a/blocks/private_files/module.js b/blocks/private_files/module.js
new file mode 100644 (file)
index 0000000..322903d
--- /dev/null
@@ -0,0 +1,41 @@
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Init private files treeview
+ *
+ * @package    block_private_files
+ * @copyright  2009 Petr Skoda (http://skodak.org)
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+M.block_private_files = {};
+
+M.block_private_files.init_tree = function(Y, expand_all, htmlid) {
+    Y.use('yui2-treeview', function(Y) {
+        var tree = new YAHOO.widget.TreeView(htmlid);
+
+        tree.subscribe("clickEvent", function(node, event) {
+            // we want normal clicking which redirects to url
+            return false;
+        });
+
+        if (expand_all) {
+            tree.expandAll();
+        }
+
+        tree.render();
+    });
+}
diff --git a/blocks/private_files/renderer.php b/blocks/private_files/renderer.php
new file mode 100644 (file)
index 0000000..f3a7320
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+
+/**
+ * Print private files tree
+ *
+ * @package    block_private_files
+ * @copyright  2010 Dongsheng Cai <dongsheng@moodle.com>
+ * @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 = '<div id="'.$htmlid.'">';
+        $html .= $this->htmllize_tree($tree, $tree->dir);
+        $html .= '</div>';
+        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 = '<ul>';
+        foreach ($dir['subdirs'] as $subdir) {
+            $result .= '<li>'.s($subdir['dirname']).' '.$this->htmllize_tree($tree, $subdir).'</li>';
+        }
+        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 .= '<li><span>'.html_writer::link($url, $filename).'</span></li>';
+        }
+        $result .= '</ul>';
+
+        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);
+    }
+}