"MDL-13766, improve filemanager to browse user_private area files"
authorDongsheng Cai <unoter@gmail.com>
Mon, 15 Mar 2010 06:12:39 +0000 (06:12 +0000)
committerDongsheng Cai <unoter@gmail.com>
Mon, 15 Mar 2010 06:12:39 +0000 (06:12 +0000)
files/files_ajax.php
lib/filelib.php
lib/form/filemanager.js
lib/form/filemanager.php
repository/lib.php

index 0cc00a7..f02bf63 100755 (executable)
@@ -44,7 +44,7 @@ if (!confirm_sesskey()) {
 
 $action     = optional_param('action', 'list', PARAM_ALPHA);
 $filename   = optional_param('filename', '', PARAM_FILE);
-$filearea   = optional_param('filearea', '', PARAM_ALPHAEXT);
+$filearea   = optional_param('filearea', 'user_draft', PARAM_ALPHAEXT);
 $filepath   = optional_param('filepath', '/', PARAM_PATH);
 $itemid     = optional_param('itemid', -1, PARAM_INT);
 $newfilepath = optional_param('newfilepath', '/', PARAM_PATH);
@@ -56,18 +56,18 @@ $user_context = get_context_instance(CONTEXT_USER, $USER->id);
 switch ($action) {
 case 'dir':
     $data = new stdclass;
-    file_get_draft_area_folders($itemid, $filepath, $data);
+    file_get_user_area_folders($itemid, $filepath, $data, $filearea);
     echo json_encode($data);
     break;
 
 case 'list':
-    $data = file_get_draft_area_files($itemid, $filepath);
+    $data = file_get_user_area_files($itemid, $filepath, $filearea);
     echo json_encode($data);
     break;
 
 case 'mkdir':
     $fs = get_file_storage();
-    $fs->create_directory($user_context->id, 'user_draft', $itemid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
+    $fs->create_directory($user_context->id, $filearea, $itemid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
     $return = new stdclass;
     $return->filepath = $filepath;
     echo json_encode($return);
@@ -77,7 +77,7 @@ case 'delete':
     $fs = get_file_storage();
     $filepath = file_correct_filepath($filepath);
     $return = new stdclass;
-    if ($stored_file = $fs->get_file($user_context->id, 'user_draft', $itemid, $filepath, $filename)) {
+    if ($stored_file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, $filename)) {
         $parent_path = $stored_file->get_parent_directory()->get_filepath();
         if($result = $stored_file->delete()) {
             $return->filepath = $parent_path;
@@ -94,9 +94,9 @@ case 'renamedir':
     $fs = get_file_storage();
     $fb = get_file_browser();
     $return = new stdclass;
-    $fileinfo = $fb->get_file_info($user_context, 'user_draft', $itemid, $filepath, '.');
+    $fileinfo = $fb->get_file_info($user_context, $filearea, $itemid, $filepath, '.');
     if ($result = $fileinfo->delete()) {
-        $newdir = $fs->create_directory($user_context->id, 'user_draft', $itemid, file_correct_filepath($newfilename));
+        $newdir = $fs->create_directory($user_context->id, $filearea, $itemid, file_correct_filepath($newfilename));
         $return->filepath = $newdir->get_parent_directory()->get_filepath();
         echo json_encode($return);
     } else {
@@ -106,8 +106,8 @@ case 'renamedir':
 
 case 'rename':
     $fb = get_file_browser();
-    $file = $fb->get_file_info($user_context, 'user_draft', $itemid, $filepath, $filename);
-    $file->copy_to_storage($user_context->id, 'user_draft', $itemid, $filepath, $newfilename);
+    $file = $fb->get_file_info($user_context, $filearea, $itemid, $filepath, $filename);
+    $file->copy_to_storage($user_context->id, $filearea, $itemid, $filepath, $newfilename);
     if ($file->delete()) {
         $return = new stdclass;
         $return->filepath = $filepath;
@@ -122,8 +122,8 @@ case 'movedir':
     $fb = get_file_browser();
     $return = new stdclass;
     if ($filepath != $newfilepath) {
-        $file = $fb->get_file_info($user_context, 'user_draft', $itemid, $filepath, $filename);
-        $file->copy_to_storage($user_context->id, 'user_draft', $itemid, $newfilepath, $filename);
+        $file = $fb->get_file_info($user_context, $filearea, $itemid, $filepath, $filename);
+        $file->copy_to_storage($user_context->id, $filearea, $itemid, $newfilepath, $filename);
         if ($file->delete()) {
             $return->filepath = $newfilepath;
         }
@@ -138,11 +138,11 @@ case 'zip':
     $zipper = new zip_packer();
     $fs = get_file_storage();
 
-    $file = $fs->get_file($user_context->id, 'user_draft', $itemid, $filepath, '.');
+    $file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, '.');
 
     $parent_path = $file->get_parent_directory()->get_filepath();
 
-    if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user_draft', $itemid, $parent_path, $filepath.'.zip', $USER->id)) {
+    if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, $filearea, $itemid, $parent_path, $filepath.'.zip', $USER->id)) {
         $return = new stdclass;
         $return->filepath = $parent_path;
         echo json_encode($return);
@@ -155,7 +155,7 @@ case 'downloaddir':
     $zipper = new zip_packer();
     $fs = get_file_storage();
 
-    $stored_file = $fs->get_file($user_context->id, 'user_draft', $itemid, $filepath, '.');
+    $stored_file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, '.');
     if ($filepath === '/') {
         $parent_path = '/';
         $filename = get_string('files').'.zip';
@@ -166,9 +166,9 @@ case 'downloaddir':
 
     // archive compressed file to an unused draft area
     $newdraftitemid = file_get_unused_draft_itemid();
-    if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user_draft', $newdraftitemid, '/', $filename, $USER->id)) {
+    if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, $filearea, $newdraftitemid, '/', $filename, $USER->id)) {
         $return = new stdclass;
-        $return->fileurl  = $CFG->wwwroot . '/draftfile.php/' . $user_context->id .'/user_draft/'.$newdraftitemid.'/'.$filename;
+        $return->fileurl  = $CFG->wwwroot . '/draftfile.php/' . $user_context->id .'/'.$filearea.'/'.$newdraftitemid.'/'.$filename;
         $return->filepath = $parent_path;
         echo json_encode($return);
     } else {
@@ -181,9 +181,9 @@ case 'unzip':
 
     $fs = get_file_storage();
 
-    $file = $fs->get_file($user_context->id, 'user_draft', $itemid, $filepath, $filename);
+    $file = $fs->get_file($user_context->id, $filearea, $itemid, $filepath, $filename);
 
-    if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user_draft', $itemid, $filepath, $USER->id)) {
+    if ($newfile = $file->extract_to_storage($zipper, $user_context->id, $filearea, $itemid, $filepath, $USER->id)) {
         $return = new stdclass;
         $return->filepath = $filepath;
         echo json_encode($return);
index 2cfdc9c..f5b987d 100644 (file)
@@ -500,12 +500,12 @@ function file_correct_filepath($str) {
  * @param string $filepath
  * @param mixed $data
  */
-function file_get_draft_area_folders($draftitemid, $filepath, &$data) {
+function file_get_user_area_folders($draftitemid, $filepath, &$data, $filearea = 'user_draft') {
     global $USER, $OUTPUT, $CFG;
     $data->children = array();
     $context = get_context_instance(CONTEXT_USER, $USER->id);
     $fs = get_file_storage();
-    if ($files = $fs->get_directory_files($context->id, 'user_draft', $draftitemid, $filepath, false)) {
+    if ($files = $fs->get_directory_files($context->id, $filearea, $draftitemid, $filepath, false)) {
         foreach ($files as $file) {
             if ($file->is_directory()) {
                 $item = new stdclass;
@@ -531,7 +531,7 @@ function file_get_draft_area_folders($draftitemid, $filepath, &$data) {
  * @param string $filepath
  * @return mixed
  */
-function file_get_draft_area_files($draftitemid, $filepath = '/') {
+function file_get_user_area_files($draftitemid, $filepath = '/', $filearea = 'user_draft') {
     global $USER, $OUTPUT, $CFG;
 
     $context = get_context_instance(CONTEXT_USER, $USER->id);
@@ -555,7 +555,7 @@ function file_get_draft_area_files($draftitemid, $filepath = '/') {
     }
 
     $list = array();
-    if ($files = $fs->get_directory_files($context->id, 'user_draft', $draftitemid, $filepath, false)) {
+    if ($files = $fs->get_directory_files($context->id, $filearea, $draftitemid, $filepath, false)) {
         foreach ($files as $file) {
             $item = new stdclass;
             $item->filename = $file->get_filename();
index fb5a115..1afaa45 100644 (file)
@@ -56,9 +56,18 @@ M.form_filemanager.init = function(Y, options) {
             this.options = options;
             this.client_id = options.client_id;
             this.currentpath = '/';
-            this.filepicker_options = options.filepicker;
             this.maxfiles = options.maxfiles;
             this.maxbytes = options.maxbytes;
+
+            this.filepicker_options = options.filepicker?options.filepicker:{};
+            this.filepicker_options.client_id = this.client_id;
+            this.filepicker_options.maxfiles = this.maxfiles;
+            this.filepicker_options.maxbytes = this.maxbytes;
+            this.filepicker_options.env = 'filemanager';
+            this.filepicker_options.filearea = options.filearea;
+            this.filepicker_options.itemid = options.itemid;
+
+            this.filearea = options.filearea?options.filearea:'user_draft';
             if (options.filecount) {
                 this.filecount = options.filecount;
             } else {
@@ -76,6 +85,7 @@ M.form_filemanager.init = function(Y, options) {
                 scope = args['scope'];
             }
             params['sesskey'] = M.cfg.sesskey;
+            params['filearea'] = this.filearea;
             params['client_id'] = this.client_id;
             params['filepath'] = this.currentpath;
             params['itemid'] = this.options.itemid?this.options.itemid:0;
index 4d8abe0..895e187 100644 (file)
@@ -27,7 +27,7 @@ global $CFG;
 
 require_once('HTML/QuickForm/element.php');
 require_once($CFG->dirroot.'/lib/filelib.php');
-require_once("$CFG->dirroot/repository/lib.php");
+require_once($CFG->dirroot.'/repository/lib.php');
 
 class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
     public $_helpbutton = '';
@@ -142,15 +142,13 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
         $params->context = $PAGE->context;
         $params->env = 'filemanager';
 
+        // including the repository instances list
         $filepicker_options = initialise_filepicker($params);
 
-        $filepicker_options->client_id = $client_id;
-        $filepicker_options->maxbytes = $this->_options['maxbytes'];
-        $filepicker_options->maxfiles = $this->_options['maxfiles'];
-        $filepicker_options->env      = 'filemanager';
-        $filepicker_options->itemid   = $draftitemid;
-
-        $options = file_get_draft_area_files($draftitemid);
+        // including draft files
+        $options = file_get_user_area_files($draftitemid, '/', 'user_draft');
+        // filemanager options
+        $options->filepicker = $filepicker_options;
         $options->mainfile  = $this->_options['mainfile'];
         $options->maxbytes  = $this->_options['maxbytes'];
         $options->maxfiles  = $this->getMaxfiles();
@@ -159,7 +157,6 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
         $options->itemid    = $draftitemid;
         $options->subdirs   = $this->_options['subdirs'];
         // store filepicker options
-        $options->filepicker = $filepicker_options;
         $options->target    = $id;
 
         $html = $this->_getTabs();
index 684f093..9ee4c53 100644 (file)
@@ -837,11 +837,13 @@ abstract class repository {
             $existingfile->delete();
         }
         if ($file = $fs->create_file_from_pathname($entry, $thefile)) {
+            echo_fb($file);
             if (empty($CFG->repository_no_delete)) {
                 $delete = unlink($thefile);
                 unset($CFG->repository_no_delete);
             }
             $fileinfo = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
+            echo_fb($fileinfo);
             if(!empty($fileinfo)) {
                 return array(
                     'url'=>$fileinfo->get_url(),