"MDL-13766, use file api to move files"
authordongsheng <dongsheng>
Mon, 4 Aug 2008 05:53:53 +0000 (05:53 +0000)
committerdongsheng <dongsheng>
Mon, 4 Aug 2008 05:53:53 +0000 (05:53 +0000)
lib/editor/tinymce.js.php
lib/form/filepicker.php
repository/lib.php
repository/ws.php

index 42e6a591e64d227344985ad6f5115a41cef5a563..f677d87312e27eea01347fba107ec72688f1cc45 100644 (file)
@@ -197,7 +197,8 @@ $output .= <<<EOF
         tinyMCE.execCommand('mceToggleEditor',false,id);
     }
     function mce_moodlefilemanager(field_name, url, type, win) {
-         openpicker({'env':'editor'});
+        var el = win.document.getElementById(field_name);
+        openpicker({'env':'editor','target':el});
     }
     function mce_saveOnSubmit(id) {
         var prevOnSubmit = document.getElementById(id).form.onsubmit;
index 376515444cff56a3016b4fb69382c5eac88bfeca..e3884fccdb25c777a86f3f5cb8f25f4e3c829926 100644 (file)
@@ -45,7 +45,18 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_button
             return $this->getFrozenHtml();
         } else {
             $ret = get_repository_client();
-            return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' onclick=\'openpicker({"env":"form"})\' />'.$ret['html'].$ret['js'];
+            $str = $this->_getTabs();
+            $str .= '<input type="hidden" value="" name="repo_attachment" id="repo_value" />';
+            $str .= <<<EOD
+<script type="text/javascript">
+function callpicker(){
+    var el=document.getElementById('repo_value');
+    openpicker({"env":"form", 'target':el})
+}
+</script>
+EOD;
+            $str .= '<input' . $this->_getAttrString($this->_attributes) . ' onclick=\'callpicker()\' />'.$ret['html'].$ret['js'];
+            return $str;
         }
     }
 }
index 75b0cda63ab55989ce0c4271231ed1eac7246dad..d3ba659c12e2e80a6a9af402b9a79f98cb5e0723 100644 (file)
@@ -331,6 +331,36 @@ function repository_get_plugins(){
     }
     return $ret;
 }
+
+function move_to_filepool($path, $name) {
+    global $DB, $CFG, $USER;
+    $context = get_context_instance(CONTEXT_USER, $USER->id);
+    $entry = new object();
+    $entry->filearea  = 'user_draft';
+    $entry->contextid = $context->id;
+    $entry->filename  = $name;
+    $entry->filepath  = '/';
+    $entry->timecreated  = time();
+    $entry->timemodified = time();
+    $entry->itemid       = $USER->id;
+    $entry->mimetype     = mimeinfo('type', $path);
+    $entry->userid       = $USER->id;
+    $fs = get_file_storage();
+    $browser = get_file_browser();
+    if ($file = $fs->create_file_from_pathname($entry, $path)) {
+        $id = json_encode($file->get_itemid());
+        $ret = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
+        // TODO
+        // get_params should include id value, talk to Petr.
+        $params = $ret->get_params();
+        $params['id'] = 88;
+        return array('url'=>$ret->get_url(),'id'=>$params['id']);
+    } else {
+        return null;
+    }
+}
+
+
 function get_repository_client(){
     global $CFG;
     $strsubmit    = get_string('submit', 'repository');
@@ -632,7 +662,7 @@ function get_repository_client(){
             var file = document.getElementById('fileurl').value;
             _client.loading();
             var trans = YAHOO.util.Connect.asyncRequest('POST', 
-                '$CFG->wwwroot/repository/ws.php?id='+_client.repositoryid+'&action=download', 
+                '$CFG->wwwroot/repository/ws.php?repo_id='+_client.repositoryid+'&action=download', 
                 _client.dlfile, _client.postdata({'env':_client.env, 'file':file, 'title':title}));
         }
         _client.login = function(){
@@ -653,6 +683,10 @@ function get_repository_client(){
                 '$CFG->wwwroot/repository/ws.php', _client.callback,
                 _client.postdata(obj));
         }
+        _client.end = function(str){
+            _client.target.value = str;
+            _client.viewfiles();
+        }
         _client.callback = {
             success: function(o) {
                 var panel = new YAHOO.util.Element('panel');
@@ -690,7 +724,8 @@ function get_repository_client(){
                     return;
                 }
                 var html = '<h1>Download Successfully!</h1>';
-                html += '<a href="###" onclick="repository_client.viewfiles()">Back</a>';
+                //html += '<a href="###" onclick="repository_client.viewfiles()">Back</a>';
+                html += '<a href="###" onclick="repository_client.end(\''+ret+'\')">Add!</a>';
                 panel.get('element').innerHTML = html;
             }
         }
@@ -699,7 +734,7 @@ function get_repository_client(){
             _client.viewbar.set('disabled', false);
             _client.loading();
             _client.repositoryid = id;
-            var trans = YAHOO.util.Connect.asyncRequest('GET', '$CFG->wwwroot/repository/ws.php?id='+id+'&p='+path+'&reset='+reset+'&env='+_client.env, _client.callback);
+            var trans = YAHOO.util.Connect.asyncRequest('GET', '$CFG->wwwroot/repository/ws.php?repo_id='+id+'&p='+path+'&reset='+reset+'&env='+_client.env, _client.callback);
         }
         _client.search = function(id){
             var data = window.prompt("What are you searching for?");
@@ -709,7 +744,7 @@ function get_repository_client(){
             }
             _client.viewbar.set('disabled', false);
             _client.loading();
-            var trans = YAHOO.util.Connect.asyncRequest('GET', '$CFG->wwwroot/repository/ws.php?id='+id+'&s='+data+'&env='+_client.env, _client.callback);
+            var trans = YAHOO.util.Connect.asyncRequest('GET', '$CFG->wwwroot/repository/ws.php?repo_id='+id+'&s='+data+'&env='+_client.env, _client.callback);
         }
         return _client;
     })();
@@ -725,6 +760,7 @@ EOD;
     function openpicker(obj) {
         if(!repository_client.instance) {
             repository_client.env = obj.env;
+            repository_client.target = obj.target;
             repository_client.instance = new repository_client();
             repository_client.instance.create_picker();
         } else {
index 0593927789dd9345878ba4f8b1633e98d4816b0f..aec4ecde1e3c153ac404a1bb9b5c05a0268fd54f 100644 (file)
@@ -59,31 +59,14 @@ if($action == 'list') {
     }
 
 } elseif($action == 'download') {
-    $ret = $repo->get_file($file, $title);
-    // TODO
-    // Ask Petr how to use FILE_API here
+    $path = $repo->get_file($file, $title);
     try {
-        $pathname = $ret;
-        $entry = new object();
-        $entry->contextid = SITEID;
-        $entry->filearea  = 'repository';
-        $entry->filepath  = '/';
-        $entry->filename  = $title;
-        $entry->timecreated  = time();
-        $entry->timemodified = time();
-        $entry->itemid       = $USER->id;
-        $entry->mimetype     = mimeinfo('type', $pathname);
-        $entry->userid       = $USER->id;
-        $fs = get_file_storage();
-        if ($file = $fs->create_file_from_pathname($entry, $pathname)) {
-            if($env == 'form'){
-                // return reference id
-                echo json_encode($file->get_itemid());
-            } elseif($env == 'editor') {
-                // return url
-                // echo json_encode($file->get_content_file_location());
-            } else {
-            }
+        $info = move_to_filepool($path, $title);
+        if($env == 'form'){
+            echo json_encode($info['id']);
+        } elseif($env == 'editor') {
+            echo json_encode($info['url']);
+        } else {
         }
     } catch (repository_exception $e){
         $err = new stdclass;