]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-15351, use new file api handle draft files and final files in local plugin"
authordongsheng <dongsheng>
Fri, 22 May 2009 09:19:58 +0000 (09:19 +0000)
committerdongsheng <dongsheng>
Fri, 22 May 2009 09:19:58 +0000 (09:19 +0000)
repository/local/repository.class.php
repository/ws.php

index 8deb959fac52d50dae7ce9914bd9c82c94abb555..38d2f0efd4e33da6101a92d69d555023356857a9 100755 (executable)
@@ -46,27 +46,60 @@ class repository_local extends repository {
      * @return mixed
      */
     public function get_listing($encodedpath = '', $page = '', $search = '') {
-        global $CFG;
+        global $CFG, $USER;
         $ret = array();
         $ret['dynload'] = true;
         $list = array();
+        if (!empty($encodedpath)) {
+            $params = unserialize(base64_decode($encodedpath));
+            if (is_array($params)) {
+                $itemid   = $params['itemid'];
+                $filename = $params['filename'];
+                $filearea = $params['filearea'];
+                $filepath = $params['filepath'];
+                $context  = get_context_instance_by_id($params['contextid']);
+            }
+        } else {
+            $itemid   = null;
+            $filename = null;
+            $filearea = null;
+            $filepath = null;
+            $context  = get_system_context();
+            // append draft files directory
+            $node = array(
+                'title' => 'Current use files',
+                'size' => 0,
+                'date' => '',
+                'path' => 'draft',
+                'children'=>array(),
+                'thumbnail' => $CFG->wwwroot .'/pix/f/folder-32.png'
+            );
+            $list[] = $node;
+        }
+
+        // list draft files
+        if ($encodedpath == 'draft') {
+            $fs = get_file_storage();
+            $context = get_context_instance(CONTEXT_USER, $USER->id);
+            $files = $fs->get_area_files($context->id, 'user_draft');
+            foreach ($files as $file) {
+                if ($file->get_filename()!='.') {
+                    $node = array(
+                        'title' => $file->get_filename(),
+                        'size' => 0,
+                        'date' => '',
+                        'source'=> $file->get_id(),
+                        'thumbnail' => $CFG->wwwroot .'/pix/f/text-32.png'
+                    );
+                    $list[] = $node;
+                }
+            }
+            $ret['list'] = $list;
+            return $ret;
+        }
 
         try {
             $browser = get_file_browser();
-            if (!empty($encodedpath)) {
-                $decodedpath = unserialize(base64_decode($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();
@@ -121,33 +154,33 @@ class repository_local extends repository {
      * @return string the location of the file
      * @see curl package
      */
-    public function get_file($encoded, $title = '', $itemid = '', $ctx_id) {
-        global $USER;
+    public function get_file($encoded, $title = '', $itemid = '') {
+        global $USER, $DB;
+        $ret = array();
+        $browser = get_file_browser();
         $params = unserialize(base64_decode($encoded));
-        $contextid = $params['contextid'];
-        $filearea  = $params['filearea'];
-        $filepath  = $params['filepath'];
-        $filename  = $params['filename'];
-        $fileitemid = $params['itemid'];
-        $fs = get_file_storage();
-        $oldfile = $fs->get_file($contextid, $filearea, $fileitemid, $filepath, $filename);
-
-        $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;
+        $user_context = get_context_instance(CONTEXT_USER, $USER->id);
+        if (!$params) {
+            $fs = get_file_storage();
+            // change draft file itemid
+            $file_record = array('contextid'=>$user_context->id, 'filearea'=>'user_draft', 'itemid'=>$itemid);
+            $fs->create_file_from_storedfile($file_record, $encoded);
+        } else {
+            // the final file
+            $contextid = $params['contextid'];
+            $filearea  = $params['filearea'];
+            $filepath  = $params['filepath'];
+            $filename  = $params['filename'];
+            $fileitemid = $params['itemid'];
+            $context  = get_context_instance_by_id($contextid);
+            $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
+            $file_info->copy_to_storage($user_context->id, 'user_draft', $itemid, '/', $title);
         }
-        $newfile = $fs->create_file_from_storedfile($record, $oldfile->get_id());
-        return $newfile;
+        $ret['itemid'] = $itemid;
+        $ret['title']  = $title;
+        $ret['contextid'] = $user_context->id;
+
+        return $ret;
     }
 
     /**
index a38c1278f4123ad5b84e28f05681f486dd5fd38b..fc41538b2e56eab0afdaefe0a86b9602e1ba0026 100644 (file)
@@ -207,15 +207,13 @@ EOD;
                                 'file'=>$url
                                 )
                             );
-                } else if ($filepath instanceof stored_file) {
-                    $sf = $filepath;
-                    $browser = get_file_browser();
-                    $context  = get_context_instance_by_id($sf->get_contextid());
+                } else if (is_array($filepath)) {
+                    $fileinfo = $filepath;
                     $info = array();
                     $info['client_id'] = $client_id;
-                    $info['file'] = $sf->get_filename();
+                    $info['file'] = $fileinfo['title'];
                     $info['id'] = $itemid;
-                    $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$sf->get_contextid().'/user_draft/'.$itemid.'/'.$sf->get_filename();
+                    $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
                     echo json_encode($info);
                 } else {
                     // normal file path name