"MDL-15351, rewrote repository local repository"
authordongsheng <dongsheng>
Wed, 20 May 2009 05:24:53 +0000 (05:24 +0000)
committerdongsheng <dongsheng>
Wed, 20 May 2009 05:24:53 +0000 (05:24 +0000)
repository/local/repository.class.php
repository/local/version.php
repository/ws.php

index ae56f7da70d63cc913c8ec73c752757b79d5f555..576805d029a9cbb29e8da58237eed4c1e9d55ea5 100755 (executable)
 class repository_local extends repository {
 
     /**
-     *
-     * @global <type> $SESSION
-     * @global <type> $action
-     * @global <type> $CFG
-     * @param <type> $repositoryid
-     * @param <type> $context
-     * @param <type> $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 <type> $SESSION
-     * @param <type> $ajax
-     * @return <type>
+     * @param boolean $ajax
+     * @return mixed
      */
     public function print_login($ajax = true) {
-        global $SESSION;
-        // TODO
-        // Return file list in moodle
         return $this->get_listing();
     }
 
     /**
      *
-     * @param <type> $path
-     * @return <type>
-     */
-    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 <type> $search_text
-     * @return <type>
+     * @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 <type> $CFG
-     * @param <type> $encodedpath
-     * @param <type> $search
-     * @return <type>
+     * @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('&amp;', $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 <type>
+     * @return string
      */
     public function get_name(){
         return get_string('repositoryname', 'repository_local');;
index bcba2404ed89042f48d2536f2534e09524e06788..5ef0c2c4300f72704251f89e680803acb20987d6 100644 (file)
@@ -1,2 +1,2 @@
 <?php
-$plugin->version = 2009031000;
+$plugin->version = 2009052000;
index d99e13d4b255d143ae9e856a7a36b7f286610b48..a38c1278f4123ad5b84e28f05681f486dd5fd38b 100644 (file)
@@ -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();