MDL-22805, display section_backup course_backup course_content only, hide empty direc...
authorDongsheng Cai <unoter@gmail.com>
Tue, 22 Jun 2010 09:04:46 +0000 (09:04 +0000)
committerDongsheng Cai <unoter@gmail.com>
Tue, 22 Jun 2010 09:04:46 +0000 (09:04 +0000)
files/files_ajax.php
files/index.php
lib/outputcomponents.php
lib/outputrenderers.php

index 146f1e6..88c4beb 100755 (executable)
@@ -58,6 +58,7 @@ switch ($action) {
 // used by course file tree viewer
 case 'getfiletree':
     $browser = get_file_browser();
+    $fs = get_file_storage();
     $url = new moodle_url($fileurl);
     $params = $url->params();
     // fix empty value
@@ -84,6 +85,17 @@ case 'getfiletree':
         if ($child->is_directory()) {
             $fileitem['isdir'] = true;
             $fileitem['url'] = $url->out();
+
+            // hide empty folder
+            if (!empty($params['itemid'])) {
+                $itemid = $params['itemid'];
+            } else {
+                $itemid = false;
+            }
+            $draftfiles = $fs->get_area_files($params['contextid'], $params['filearea'], $itemid, 'id', false);
+            if (count($draftfiles) == 0) {
+                continue;
+            }
         } else {
             $fileitem['url'] = $child->get_url();
         }
index fc0776b..bc9acbf 100644 (file)
@@ -90,8 +90,10 @@ $PAGE->set_title("$SITE->shortname: $strfiles");
 $PAGE->set_heading($SITE->fullname);
 echo $OUTPUT->header();
 
+$options = array();
+$options['enabled_fileareas'] = array('section_backup', 'course_backup', 'course_content', 'user_backup');
 echo $OUTPUT->box_start();
-echo $OUTPUT->moodle_file_tree_viewer($context->id, $filearea, $itemid, $filepath);
+echo $OUTPUT->moodle_file_tree_viewer($context->id, $filearea, $itemid, $filepath, $options);
 echo $OUTPUT->box_end();
 
 echo $OUTPUT->footer();
index 5d0e247..6c0f656 100644 (file)
@@ -96,6 +96,7 @@ class area_file_tree_viewer implements renderable {
 class moodle_file_tree_viewer implements renderable {
     public $tree;
     public $path;
+    private $enabled_fileareas;
     /**
      * Constructor of moodle_file_tree_viewer class
      * @param int $contextid
@@ -103,18 +104,29 @@ class moodle_file_tree_viewer implements renderable {
      * @param int $itemid
      * @param string $urlbase, file serving url base
      */
-    public function __construct($contextid, $filearea, $itemid, $filepath) {
+    public function __construct($contextid, $filearea, $itemid, $filepath, $options=array()) {
         global $CFG, $OUTPUT;
         $this->tree = array();
         $browser = get_file_browser();
+        $fs = get_file_storage();
         $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath);
         $children = $fileinfo->get_children();
         $parent_info = $fileinfo->get_parent();
+        if (!empty($options['enabled_fileareas']) && is_array($options['enabled_fileareas'])) {
+            $this->enabled_fileareas = $options['enabled_fileareas'];
+        } else {
+            unset($this->enabled_fileareas);
+        }
 
         $level = $parent_info;
         $this->path = array();
         while ($level) {
             $params = $level->get_params();
+            $context = get_context_instance_by_id($params['contextid']);
+            // lock user in course level
+            if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) {
+                break;
+            }
             $url = new moodle_url('/files/index.php', $params);
             $this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
             $level = $level->get_parent();
@@ -137,6 +149,21 @@ class moodle_file_tree_viewer implements renderable {
             if ($child->is_directory()) {
                 $fileitem['isdir'] = true;
                 $fileitem['url'] = $url->out(false);
+                if (isset($this->enabled_fileareas)) {
+                    if (!in_array($params['filearea'], $this->enabled_fileareas)) {
+                        continue;
+                    } else {
+                        if (!empty($params['itemid'])) {
+                            $itemid = $params['itemid'];
+                        } else {
+                            $itemid = false;
+                        }
+                        $draftfiles = $fs->get_area_files($contextid, $params['filearea'], $itemid, 'id', false);
+                        if (count($draftfiles) == 0) {
+                            continue;
+                        }
+                    }
+                }
             } else {
                 $fileitem['url'] = $child->get_url();
             }
index 8de63e2..3aebd94 100644 (file)
@@ -1783,8 +1783,8 @@ class core_renderer extends renderer_base {
      * @param string $filepath
      * @return string HTML fragment
      */
-    public function moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath) {
-        $tree = new moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath);
+    public function moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath, $options = array()) {
+        $tree = new moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath, $options);
         return $this->render($tree);
     }
     public function render_moodle_file_tree_viewer(moodle_file_tree_viewer $tree) {