Skip to content

Commit

Permalink
"MDL-16596, imporve non-javascript file manager, add useful functions…
Browse files Browse the repository at this point in the history
… to filelib"
  • Loading branch information
dongsheng committed Sep 3, 2009
1 parent efd89cc commit f0e5f03
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 14 deletions.
7 changes: 6 additions & 1 deletion lang/en_utf8/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$string['areauserprofile'] = 'Profile';
$string['attachment'] = 'Attachment';
$string['attachedfiles'] = 'Attached files';
$string['back'] = '< Back';
$string['back'] = '« 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.';
Expand All @@ -35,6 +35,8 @@
$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';
Expand All @@ -46,6 +48,7 @@
$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';
Expand Down Expand Up @@ -73,6 +76,7 @@
$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';
Expand Down Expand Up @@ -112,3 +116,4 @@
$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';
107 changes: 107 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
25 changes: 12 additions & 13 deletions repository/filepicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
}
}

$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) {
Expand Down Expand Up @@ -263,6 +263,9 @@
$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>';
Expand All @@ -272,10 +275,15 @@

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':
Expand Down Expand Up @@ -322,7 +330,7 @@
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)) {
Expand Down Expand Up @@ -403,12 +411,3 @@
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), './@#$').'/';
}

0 comments on commit f0e5f03

Please sign in to comment.