From d8eb6e180c35284b4f426a42c551a68c2f1b331c Mon Sep 17 00:00:00 2001 From: dongsheng Date: Mon, 4 Aug 2008 05:53:53 +0000 Subject: [PATCH] "MDL-13766, use file api to move files" --- lib/editor/tinymce.js.php | 3 ++- lib/form/filepicker.php | 13 +++++++++++- repository/lib.php | 44 +++++++++++++++++++++++++++++++++++---- repository/ws.php | 31 +++++++-------------------- 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/lib/editor/tinymce.js.php b/lib/editor/tinymce.js.php index 42e6a591e6..f677d87312 100644 --- a/lib/editor/tinymce.js.php +++ b/lib/editor/tinymce.js.php @@ -197,7 +197,8 @@ $output .= <<getFrozenHtml(); } else { $ret = get_repository_client(); - return $this->_getTabs() . '_getAttrString($this->_attributes) . ' onclick=\'openpicker({"env":"form"})\' />'.$ret['html'].$ret['js']; + $str = $this->_getTabs(); + $str .= ''; + $str .= << +function callpicker(){ + var el=document.getElementById('repo_value'); + openpicker({"env":"form", 'target':el}) +} + +EOD; + $str .= '_getAttrString($this->_attributes) . ' onclick=\'callpicker()\' />'.$ret['html'].$ret['js']; + return $str; } } } diff --git a/repository/lib.php b/repository/lib.php index 75b0cda63a..d3ba659c12 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -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 = '

Download Successfully!

'; - html += 'Back'; + //html += 'Back'; + html += 'Add!'; 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 { diff --git a/repository/ws.php b/repository/ws.php index 0593927789..aec4ecde1e 100644 --- a/repository/ws.php +++ b/repository/ws.php @@ -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; -- 2.39.5