From cd3122334dba8e111d178668c9a467233791d00b Mon Sep 17 00:00:00 2001 From: dongsheng Date: Wed, 20 May 2009 05:24:53 +0000 Subject: [PATCH] "MDL-15351, rewrote repository local repository" --- repository/local/repository.class.php | 162 +++++++++++++++----------- repository/local/version.php | 2 +- repository/ws.php | 32 +++-- 3 files changed, 116 insertions(+), 80 deletions(-) diff --git a/repository/local/repository.class.php b/repository/local/repository.class.php index ae56f7da70..576805d029 100755 --- a/repository/local/repository.class.php +++ b/repository/local/repository.class.php @@ -13,56 +13,26 @@ class repository_local extends repository { /** - * - * @global $SESSION - * @global $action - * @global $CFG - * @param $repositoryid - * @param $context - * @param $options + * @param int $repositoryid + * @param int $context + * @param array $options */ public function __construct($repositoryid, $context = SITEID, $options = array()) { - global $SESSION, $action, $CFG; parent::__construct($repositoryid, $context, $options); - // TODO: - // get the parameter from client side - // $this->context can be used here. - // When user upload a file, $action == 'upload' - // You can use $_FILES to find that file } /** - * - * @global $SESSION - * @param $ajax - * @return + * @param boolean $ajax + * @return mixed */ public function print_login($ajax = true) { - global $SESSION; - // TODO - // Return file list in moodle return $this->get_listing(); } /** * - * @param $path - * @return - */ - private function _decode_path($path) { - $filearea = ''; - $path = ''; - if (($file = unserialize($path)) !== false) { - $filearea = $file[0]; - $path = $file[1]; - } - return array('filearea' => $filearea, 'path' => $path); - } - - /** - * - * @param $search_text - * @return + * @param string $search_text + * @return mixed */ public function search($search_text) { return $this->get_listing('', '', $search_text); @@ -70,20 +40,77 @@ class repository_local extends repository { /** * - * @global $CFG - * @param $encodedpath - * @param $search - * @return + * @param string $encodedpath + * @param string $path not used by this plugin + * @param string $search + * @return mixed */ public function get_listing($encodedpath = '', $page = '', $search = '') { global $CFG; + $ret = array(); + $ret['dynload'] = true; + $list = array(); try { - return repository::get_user_file_tree($search); + $browser = get_file_browser(); + if (!empty($encodedpath)) { + $decodedpath = unserialize($encodedpath); + $itemid = $decodedpath['itemid']; + $filename = $decodedpath['filename']; + $filearea = $decodedpath['filearea']; + $filepath = $decodedpath['filepath']; + $context = get_context_instance_by_id($decodedpath['contextid']); + } else { + $itemid = null; + $filename = null; + $filearea = null; + $filepath = null; + $context = get_system_context(); + } + + if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, '/', $filename)) { + $level = $fileinfo->get_parent(); + while ($level) { + $params = $level->get_params_rawencoded(); + $params = implode('&', $params); + $params = serialize($level->get_params()); + $path[] = array('name'=>$level->get_visible_name(), 'path'=>$params); + $level = $level->get_parent(); + } + $path = array_reverse($path); + $ret['path'] = $path; + $children = $fileinfo->get_children(); + foreach ($children as $child) { + if ($child->is_directory()) { + $params = serialize($child->get_params()); + $node = array( + 'title' => $child->get_visible_name(), + 'size' => 0, + 'date' => '', + 'path' => $params, + 'children'=>array(), + 'thumbnail' => $CFG->wwwroot .'/pix/f/folder-32.png' + ); + $list[] = $node; + } else { + $params = base64_encode(serialize($child->get_params())); + $node = array( + 'title' => $child->get_visible_name(), + 'size' => 0, + 'date' => '', + 'source'=> $params, + 'thumbnail' => $CFG->wwwroot .'/pix/f/text-32.png' + ); + $list[] = $node; + } + } + } } catch (Exception $e) { throw new repository_exception('emptyfilelist', 'repository_local'); } + $ret['list'] = $list; + return $ret; } /** @@ -96,37 +123,38 @@ class repository_local extends repository { * @return string the location of the file * @see curl package */ - public function get_file($url, $file = '') { - global $CFG; - $path = $this->prepare_file($file); - - ///retrieve the file - $fileparams = unserialize(base64_decode($url)); - $contextid = $fileparams[0]; - $filearea = $fileparams[1]; - $itemid = $fileparams[2]; - $filepath = $fileparams[3]; - $filename = $fileparams[4]; + public function get_file($encoded, $title = '', $itemid = '', $ctx_id) { + global $USER; + $params = unserialize(base64_decode($encoded)); + $contextid = $params['contextid']; + $filearea = $params['filearea']; + $filepath = $params['filepath']; + $filename = $params['filename']; + $fileitemid = $params['itemid']; $fs = get_file_storage(); - $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename); - $contents = $sf->get_content(); - $fp = fopen($path, 'w'); - fwrite($fp,$contents); - fclose($fp); - - return $path; - } + $oldfile = $fs->get_file($contextid, $filearea, $fileitemid, $filepath, $filename); - /** - * - */ - public function print_listing() { - // will be used in non-javascript file picker + $now = time(); + $context = get_context_instance(CONTEXT_USER, $USER->id); + $recored = new stdclass; + $record->filearea = 'user_draft'; + $record->contextid = $context->id; + $record->filename = $title; + $record->filepath = '/'; + $record->timecreated = $now; + $record->timemodified = $now; + $record->userid = $USER->id; + $record->mimetype = $oldfile->get_mimetype(); + if (!empty($itemid)) { + $record->itemid = $itemid; + } + $newfile = $fs->create_file_from_storedfile($record, $oldfile->get_id()); + return $newfile; } /** * - * @return + * @return string */ public function get_name(){ return get_string('repositoryname', 'repository_local');; diff --git a/repository/local/version.php b/repository/local/version.php index bcba2404ed..5ef0c2c430 100644 --- a/repository/local/version.php +++ b/repository/local/version.php @@ -1,2 +1,2 @@ version = 2009031000; +$plugin->version = 2009052000; diff --git a/repository/ws.php b/repository/ws.php index d99e13d4b2..a38c1278f4 100644 --- a/repository/ws.php +++ b/repository/ws.php @@ -186,34 +186,42 @@ EOD; break; case 'download': try { - $path = $repo->get_file($file, $title); - if ($path === false) { + $filepath = $repo->get_file($file, $title, $itemid); + if ($filepath === false) { $err->e = get_string('cannotdownload', 'repository'); die(json_encode($err)); } if (empty($itemid)) { $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); } - if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) { + if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $filepath)) { + $url = $filepath; echo json_encode(array( /* File picker need to know this is a link * in order to attach title to url */ 'type'=>'link', 'client_id'=>$client_id, - 'url'=>$path, - 'id'=>$path, - 'file'=>$path + 'url'=>$url, + 'id'=>$url, + 'file'=>$url ) ); + } else if ($filepath instanceof stored_file) { + $sf = $filepath; + $browser = get_file_browser(); + $context = get_context_instance_by_id($sf->get_contextid()); + $info = array(); + $info['client_id'] = $client_id; + $info['file'] = $sf->get_filename(); + $info['id'] = $itemid; + $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$sf->get_contextid().'/user_draft/'.$itemid.'/'.$sf->get_filename(); + echo json_encode($info); } else { - $info = repository::move_to_filepool($path, $title, $itemid); + // normal file path name + $info = repository::move_to_filepool($filepath, $title, $itemid); $info['client_id'] = $client_id; - if ($env == 'filepicker' || $env == 'filemanager'){ - echo json_encode($info); - } else if ($env == 'editor') { - echo json_encode($info); - } + echo json_encode($info); } } catch (repository_exception $e){ $err->e = $e->getMessage(); -- 2.39.5