From: dongsheng Date: Mon, 1 Sep 2008 08:19:28 +0000 (+0000) Subject: MDL-13766 X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=5a3b9db98c2dac62ad0abad538a2fccebbd0bab4;p=moodle.git MDL-13766 Upload plugin added --- diff --git a/repository/lib.php b/repository/lib.php index abbad134f6..9924566f17 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -606,6 +606,51 @@ function repository_move_to_filepool($path, $name, $itemid, $filearea = 'user_dr } } +/** + * Save file to local filesystem pool + * @param string $elname name of element + * @param int $contextid + * @param string $filearea + * @param string $filepath + * @param string $filename - use specified filename, if not specified name of uploaded file used + * @param bool $override override file if exists + * @param int $userid + * @return mixed stored_file object or false if error; may throw exception if duplicate found + */ +function repository_store_to_filepool($elname, $filearea='user_draft', $filepath='/') { + global $USER; + if (!isset($_FILES[$elname])) { + return false; + } + + $filename = $_FILES[$elname]['name']; + $context = get_context_instance(CONTEXT_USER, $USER->id); + $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); + $fs = get_file_storage(); + $browser = get_file_browser(); + + if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if ($override) { + $file->delete(); + } else { + return false; + } + } + + $file_record = new object(); + $file_record->contextid = $context->id; + $file_record->filearea = $filearea; + $file_record->itemid = $itemid; + $file_record->filepath = $filepath; + $file_record->filename = $filename; + $file_record->userid = $USER->id; + + $file = $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']); + $info = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); + $ret = array('url'=>$info->get_url(),'id'=>$itemid, 'file'=>$file->get_filename()); + return $ret; +} + /** * Return javascript to create file picker to browse repositories * @global object $CFG @@ -1060,17 +1105,29 @@ _client.upload = function(){ loading.id = u.id+'_loading'; parent.appendChild(loading); YAHOO.util.Connect.setForm(aform, true, true); - var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->wwwroot/repository/ws.php?action=upload&sesskey=$sesskey&ctx_id=$context->id&repo_id='+_client.repositoryid, _client.upload_cb); + var trans = YAHOO.util.Connect.asyncRequest('POST', + '$CFG->wwwroot/repository/ws.php?action=upload&sesskey=$sesskey&ctx_id=$context->id&repo_id=' + +_client.repositoryid, + _client.upload_cb); } _client.upload_cb = { upload: function(o){ - var u = _client.ds.upload; - var aform = document.getElementById(u.id); - aform.reset(); - var loading = document.getElementById(u.id+'_loading'); - loading.innerHTML = '$strsaved'; - alert('$strsaved'); - //_client.req(_client.repositoryid, '', 0); + try { + var ret = YAHOO.lang.JSON.parse(o.responseText); + } catch(e) { + alert('$strinvalidjson - '+o.responseText); + } + if(ret && ret.e){ + var panel = new YAHOO.util.Element('panel-$suffix'); + panel.get('element').innerHTML = ret.e; + return; + } + if(ret){ + alert('$strsaved'); + repository_client_$suffix.end(ret); + }else{ + alert('$strinvalidjson'); + } } } _client.uploadcontrol = function() { @@ -1078,12 +1135,11 @@ _client.uploadcontrol = function() { if(_client.ds.upload){ str += '
'; str += '
'; - str += ''; - str += ''; + str += ''; + str += ''; str += '

$strupload

'; str += '
'; str += '
'; - str += '
'; } return str; } diff --git a/repository/ws.php b/repository/ws.php index 7654436d50..5d50732360 100644 --- a/repository/ws.php +++ b/repository/ws.php @@ -26,19 +26,21 @@ $repo_id = optional_param('repo_id', 1, PARAM_INT); $ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); $userid = $USER->id; -if(!$repository = repository_get_instance($repo_id)) -{ +$sql = 'SELECT i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id='.$repo_id.' AND i.typeid=r.id'; +if(!$repository = $DB->get_record_sql($sql)) { $err = new stdclass; $err->e = get_string('invalidrepositoryid', 'repository'); die(json_encode($err)); +} else { + $type = $repository->type; } if(file_exists($CFG->dirroot.'/repository/'. - $repository->type.'/repository.class.php')) + $type.'/repository.class.php')) { require_once($CFG->dirroot.'/repository/'. - $repository->type.'/repository.class.php'); - $classname = 'repository_' . $repository->type; + $type.'/repository.class.php'); + $classname = 'repository_' . $type; try{ $repo = new $classname($repo_id, $ctx_id, array('ajax'=>true)); } catch (repository_exception $e){ @@ -95,4 +97,12 @@ if ($action == 'list' || $action == 'search') { $err->e = $e->getMessage(); die(json_encode($err)); } +} elseif ($action == 'upload') { + try { + echo json_encode($repo->get_listing()); + } catch (repository_exception $e){ + $err = new stdclass; + $err->e = $e->getMessage(); + die(json_encode($err)); + } }