]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-16596, imporve non-javascript file manager, add useful functions to filelib"
authordongsheng <dongsheng>
Thu, 3 Sep 2009 06:53:40 +0000 (06:53 +0000)
committerdongsheng <dongsheng>
Thu, 3 Sep 2009 06:53:40 +0000 (06:53 +0000)
lang/en_utf8/repository.php
lib/filelib.php
repository/filepicker.php

index ed39d8fb363b6cffcbf80404a35c128eb6a59e14..26f4de93371da00cd76a177e0c780c0e6d72b022 100644 (file)
@@ -14,7 +14,7 @@ $string['areauserpersonal'] = 'Personal';
 $string['areauserprofile'] = 'Profile';
 $string['attachment'] = 'Attachment';
 $string['attachedfiles'] = 'Attached files';
-$string['back'] = '&lt; Back';
+$string['back'] = '&laquo; Back';
 $string['cacheexpire'] = 'Cache expire';
 $string['cachecleared'] = 'Cached files are removed';
 $string['cannotaccessparentwin'] = 'If parent window is on HTTPS, then we are not allowed to access window.opener object, so we cannot refresh the repository for you automatically, but we already got your session, just go back to file picker and select the repository again, it should work now.';
@@ -35,6 +35,8 @@ $string['create'] = 'Create';
 $string['createrepository'] = 'Create a repository instance';
 $string['createinstance'] = 'Create a repository instance';
 $string['createxxinstance'] = 'Create \"$a\" instance';
+$string['createfoldersuccess'] = 'Create folder successfully';
+$string['createfolderfail'] = 'Fail to create this folder';
 $string['date'] = 'Date';
 $string['deleterepository'] = 'Delete this repository';
 $string['deleted'] = 'Repository deleted';
@@ -46,6 +48,7 @@ $string['emptylist'] = 'Empty result';
 $string['enablecourseinstances'] = 'Allow teachers to add a repository instance into the file picker';
 $string['enableuserinstances'] = 'Allow users to add a repository instance into the file picker';
 $string['enter'] = 'Enter';
+$string['entername'] = 'Please enter file/folder name';
 $string['error'] = 'An unknown error occurred!';
 $string['existingrepository'] = 'This repository already exists';
 $string['filename'] = 'Filename';
@@ -73,6 +76,7 @@ $string['manageuserrepository'] = 'Manage individual repository';
 $string['noenter'] = 'Nothing entered';
 $string['nofilesattached'] = 'No files attached';
 $string['nopermissiontoaccess'] = 'No permission to access this repository';
+$string['nopathselected'] = 'No destination path select yet (double click tree node to select)';
 $string['nomorefiles'] = 'No more attachments allowed';
 $string['notyourinstances'] = 'You can not view/edit repository instances of another user';
 $string['noresult'] = 'No search result';
@@ -112,3 +116,4 @@ $string['uploading'] = 'Uploading...';
 $string['uploadsucc'] = 'The file has been uploaded successfully';
 $string['wrongcontext'] = 'You cannot access to this context';
 $string['xhtmlerror'] = 'You are probably using XHTML strict header, some YUI Component doesn\'t work in this mode, please turn it off in moodle';
+$string['ziped'] = 'Compress folder successfully';
index 7b686eaf6e614b59be7a975c3a3d1300c46cef7e..745c8b3da72ba6bdd7677226a906cbe7df2895a6 100644 (file)
@@ -484,6 +484,113 @@ function file_get_draft_area_info($draftitemid) {
     return $results;
 }
 
+
+/**
+ * Convert any string to a valid filepath
+ * @param string $str
+ * @return string path
+ */
+function file_correct_filepath($str) {
+    return '/'.trim($str, './@#$ ').'/';
+}
+
+/**
+ * Generate a folder tree of currect draft area recursively
+ * @param int $itemid
+ * @param string $filepath
+ * @param mixed $data
+ */
+function file_get_draft_area_folders($draftitemid, $filepath, &$data) {
+    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)) {
+        foreach ($files as $file) {
+            if ($file->is_directory()) {
+                $item = new stdclass;
+                $item->filepath = $file->get_filepath();
+
+                $foldername = explode('/', trim($item->filepath, '/'));
+                $item->fullname = trim(array_pop($foldername), '/');
+
+                $item->id = uniqid();
+                file_get_draft_area_folders($draftitemid, $item->filepath, $item);
+                $data->children[] = $item;
+            } else {
+                continue;
+            }
+        }
+    }
+}
+
+/**
+ * Listing all files (including folders) in current path (draft area)
+ * used by file manager
+ * @param int $draftitemid
+ * @param string $filepath
+ * @return mixed
+ */
+function file_get_draft_area_files($draftitemid, $filepath = '/') {
+    global $USER, $OUTPUT, $CFG;
+
+    $context = get_context_instance(CONTEXT_USER, $USER->id);
+    $fs = get_file_storage();
+
+    $data = new stdclass;
+    $data->path = array();
+    $data->path[] = array('name'=>get_string('files'), 'path'=>'/');
+
+    // will be used to build breadcrumb
+    $trail = '';
+    if ($filepath !== '/') {
+        $filepath = file_correct_filepath($filepath);
+        $parts = explode('/', $filepath);
+        foreach ($parts as $part) {
+            if ($part != '' && $part != null) {
+                $trail .= ('/'.$part.'/');
+                $data->path[] = array('name'=>$part, 'path'=>$trail);
+            }
+        }
+    }
+
+    $list = array();
+    if ($files = $fs->get_directory_files($context->id, 'user_draft', $draftitemid, $filepath, false)) {
+        foreach ($files as $file) {
+            $item = new stdclass;
+            $item->filename = $file->get_filename();
+            $item->filepath = $file->get_filepath();
+            $item->fullname = trim($item->filename, '/');
+            $filesize = $file->get_filesize();
+            $item->filesize = $filesize ? display_size($filesize) : '';
+
+            $icon = mimeinfo_from_type('icon', $file->get_mimetype());
+            $icon = str_replace('.gif', '', $icon);
+            $item->icon = $OUTPUT->old_icon_url('f/' . $icon);
+
+            if ($icon == 'zip') {
+                $item->type = 'zip';
+            } else {
+                $item->type = 'file';
+            }
+
+            if ($file->is_directory()) {
+                $item->filesize = 0;
+                $item->icon = $OUTPUT->old_icon_url('f/folder');
+                $item->type = 'folder';
+                $foldername = explode('/', trim($item->filepath, '/'));
+                $item->fullname = trim(array_pop($foldername), '/');
+            } else {
+                $item->url = $CFG->wwwroot . '/draftfile.php/' . $context->id .'/user_draft/'.$draftitemid.$item->filepath.$item->fullname;
+            }
+            $list[] = $item;
+        }
+    }
+    $data->itemid = $draftitemid;
+    $data->list = $list;
+    return $data;
+}
+
 /**
  * Returns draft area itemid for a given element.
  *
index 5250764136b26b691f58c64a45ae2350fec3c793..561de8802ee5367ad9856a3ba444fe67b44751b7 100755 (executable)
@@ -73,7 +73,7 @@ if ($repository = $DB->get_record_sql($sql, array($repo_id))) {
     }
 }
 
-$url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", array('ctx_id' => $contextid, 'itemid' => $itemid));
+$url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env));
 $home_url = new moodle_url($url, array('action' => 'embedded'));
 
 switch ($action) {
@@ -263,6 +263,9 @@ case 'plugins':
         $icon->link->url = clone($url);
         $icon->link->url->params(array('action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
         $icon->linktext = $info->name;
+        if ($env == 'filemanager' && $info->type == 'draft') {
+            continue;
+        }
         echo '<li>' . $OUTPUT->action_icon($icon) . '</li>';
     }
     echo '</ul>';
@@ -272,10 +275,15 @@ case 'plugins':
 
 case 'mkdir':
     $fs = get_file_storage();
-    $fs->create_directory($user_context->id, 'user_draft', $itemid, trim_path(trim_path($draftpath).$newdirname));
+    $fs->create_directory($user_context->id, 'user_draft', $itemid, file_correct_filepath(file_correct_filepath($draftpath).trim($newdirname, '/')));
     $url->param('action', 'browse');
     $url->param('draftpath', $draftpath);
-    redirect($url, get_string('Created folder success!','repository'));
+    if (empty($newdirname)) {
+        $str = get_string('createfoldersuccess', 'repository');
+    } else {
+        $str = get_string('createfolderfail', 'repository');
+    }
+    redirect($url, $str);
     break;
 
 case 'zip':
@@ -322,7 +330,7 @@ default:
     echo '<a href="'.$url->out().'">'.'Files</a> ▶';
     $trail = '';
     if ($draftpath !== '/') {
-        $path = trim_path($draftpath);
+        $path = file_correct_filepath($draftpath);
         $parts = explode('/', $path);
         foreach ($parts as $part) {
             if (!empty($part)) {
@@ -403,12 +411,3 @@ default:
     echo $OUTPUT->footer();
     break;
 }
-
-/**
- * trim filepath, and add slash to it
- * @param string $str
- * @return string path
- */
-function trim_path($str) {
-    return '/'.trim(trim($str), './@#$').'/';
-}