"MDL-18486, remove php varible from file picker code, all javascript code goes into a static file"

This commit is contained in:
dongsheng 2009-04-20 08:53:21 +00:00
parent dd13271188
commit e189ec00f0
10 changed files with 1220 additions and 1224 deletions

View File

@ -92,14 +92,14 @@ $output .= <<<EOF
};
}
function moodlefilemanager(field_name, url, type, win) {
var suffix = id2suffix[tinyMCE.selectedInstance.editorId];
var client_id = id2clientid[tinyMCE.selectedInstance.editorId];
document.body.className += ' yui-skin-sam';
var picker = document.createElement('DIV');
picker.className = "file-picker";
picker.id = 'file-picker-'+suffix;
picker.id = 'file-picker-'+client_id;
document.body.appendChild(picker);
var el = win.document.getElementById(field_name);
eval('openpicker_'+suffix+'({"env":"editor","target":el, "filetype":type})');
eval('open_filepicker(client_id, {"env":"editor","target":el,"filetype":type})');
}
EOF;

View File

@ -183,14 +183,14 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
} else {
$ctx = $COURSE->context;
}
$ret = repository_get_client($ctx, array('image', 'video', 'media'), '*');
$client_id = uniqid();
$ret = repository_get_client($ctx, $client_id, array('image', 'video', 'media'), '*');
$suffix = $ret['suffix'];
$str .= $ret['css'].$ret['js'];
$str .= <<<EOD
<script type="text/javascript">
id2suffix['$id']='$suffix';
id2itemid['$id']='$draftitemid';
id2clientid['$id'] = '$client_id';
id2itemid['$id'] = '$draftitemid';
</script>
EOD;

View File

@ -171,57 +171,67 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
}
$repo_info = repository_get_client($context, $this->filetypes, $this->returnvalue);
$suffix = $repo_info['suffix'];
$client_id = uniqid();
$repo_info = repository_get_client($context, $client_id, $this->filetypes, $this->returnvalue);
$html = $this->_get_draftfiles($draftitemid, $suffix);
$html = $this->_get_draftfiles($draftitemid, $client_id);
$str = $this->_getTabs();
$str .= $html;
$str .= $repo_info['css'];
$str .= $repo_info['js'];
$str .= <<<EOD
if (empty($CFG->filemanagerjsloaded)) {
$str .= <<<EOD
<script type="text/javascript">
//<![CDATA[
var elitem = null;
var rm_cb_$suffix = {
var rm_cb = {
success: function(o) {
if(o.responseText && o.responseText == 200){
elitem.parentNode.removeChild(elitem);
}
}
}
function rm_$suffix(id, name, context) {
function rm(id, name, context) {
if (confirm('$strdelete')) {
var trans = YAHOO.util.Connect.asyncRequest('POST',
'{$CFG->httpswwwroot}/repository/ws.php?action=delete&itemid='+id,
rm_cb_$suffix,
rm_cb,
'title='+name
);
elitem = context.parentNode;
}
}
function uf_$suffix(obj) {
var list = document.getElementById('draftfiles-$suffix');
function uf(obj) {
var list = document.getElementById('draftfiles-'+obj.client_id);
var html = '<li><a href="'+obj['url']+'"><img src="'+obj['icon']+'" class="icon" /> '+obj['file']+'</a> ';
html += '<a href="###" onclick=\'rm_$suffix('+obj['id']+', "'+obj['file']+'", this)\'><img src="{$CFG->pixpath}/t/delete.gif" class="iconsmall" /></a>';;
html += '<a href="###" onclick=\'rm('+obj['id']+', "'+obj['file']+'", this)\'><img src="{$CFG->pixpath}/t/delete.gif" class="iconsmall" /></a>';;
html += '</li>';
list.innerHTML += html;
}
function callpicker_$suffix() {
function callpicker(el_id, client_id, itemid) {
document.body.className += ' yui-skin-sam';
var picker = document.createElement('DIV');
picker.id = 'file-picker-$suffix';
picker.id = 'file-picker-'+client_id;
picker.className = 'file-picker';
document.body.appendChild(picker);
var el=document.getElementById('$id');
openpicker_$suffix({'env':'filemanager', 'target':el, 'itemid': $draftitemid, 'callback':uf_$suffix})
var el=document.getElementById(el_id);
var obj = {};
obj.env = 'filemanager';
obj.itemid = itemid;
obj.target = el;
obj.callback = uf;
var fp = open_filepicker(client_id, obj);
}
//]]>
</script>
EOD;
$CFG->filemanagerjsloaded = true;
}
$str .= <<<EOD
<input value="$draftitemid" name="{$this->_attributes['name']}" type="hidden" />
<div>
<input value="$straddfile" onclick="callpicker_$suffix()" type="button" />
<input value="$straddfile" onclick="callpicker('$id', '$client_id', '$draftitemid')" type="button" />
</div>
EOD;
return $str;

View File

@ -72,8 +72,8 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
} else {
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
}
$repository_info = repository_get_client($context, $this->filetypes, $this->returnvalue);
$suffix = $repository_info['suffix'];
$client_id = uniqid();
$repository_info = repository_get_client($context, $client_id, $this->filetypes, $this->returnvalue);
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];
@ -83,21 +83,21 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
$str .= <<<EOD
<script type="text/javascript">
function updatefile_$suffix(obj) {
document.getElementById('repo_info_$suffix').innerHTML = obj['file'];
function updatefile(client_id, obj) {
document.getElementById('repo_info_'+client_id).innerHTML = obj['file'];
}
function callpicker_$suffix() {
function callpicker(client_id, id) {
document.body.className += ' yui-skin-sam';
var picker = document.createElement('DIV');
picker.id = 'file-picker-$suffix';
picker.id = 'file-picker-'+client_id;
picker.className = 'file-picker';
document.body.appendChild(picker);
var el=document.getElementById('$id');
openpicker_$suffix({'env':'filepicker', 'target':el, 'callback':updatefile_$suffix})
var el=document.getElementById(id);
open_filepicker({'env':'filepicker', 'target':el, 'callback':updatefile})
}
</script>
EOD;
$str .= '<input value="'.get_string('openpicker', 'repository').'" type="button" onclick=\'callpicker_'.$suffix.'()\' />'.'<span id="repo_info_'.$suffix.'" class="notifysuccess">'.$currentfile.'</span>'.$repository_info['css'].$repository_info['js'];
$str .= '<input value="'.get_string('openpicker', 'repository').'" type="button" onclick="callpicker(\''+$client_id+'\', \''+$id+'\')" />'.'<span id="repo_info_'.$client_id.'" class="notifysuccess">'.$currentfile.'</span>'.$repository_info['css'].$repository_info['js'];
return $str;
}

View File

@ -28,8 +28,8 @@ setTimeout('fix_column_widths()', 20);
</script>
<script type="text/javascript">
//<![CDATA[
var id2suffix = {};
var id2itemid = {};
var id2clientid = {};
var id2itemid = {};
<?php
if (!empty($focus)) {
if(($pos = strpos($focus, '.')) !== false) {

View File

@ -4932,13 +4932,14 @@ function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $v
} else {
$ctx = $COURSE->context;
}
$ret = repository_get_client($ctx, array('image', 'video', 'media'), '*');
$client_id = uniqid();
$ret = repository_get_client($ctx, $client_id, array('image', 'video', 'media'), '*');
$str .= $ret['css'].$ret['js'];
$suffix = $ret['suffix'];
$str .= '<div class="textareaicons">';
$str .= '<script type="text/javascript">
//<![CDATA[
id2suffix[\''.$id.'\']=\''.$suffix.'\';
id2clientid[\''.$id.'\']=\''.$client_id.'\';
mce_saveOnSubmit(\''.addslashes_js($id).'\');
document.write(\''.addslashes_js($str_toggle).'\');
document.write(\''.addslashes_js(editorshortcutshelpbutton()).'\');

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,6 @@ require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once(dirname(dirname(__FILE__)) . '/lib/filelib.php');
require_once(dirname(dirname(__FILE__)) . '/lib/formslib.php');
// File picker javascript code
require_once(dirname(dirname(__FILE__)) . '/repository/javascript.php');
/**
* A repository_type is a repository plug-in. It can be Box.net, Flick-r, ...
@ -1830,3 +1829,174 @@ function repository_setup_default_plugins() {
}
return true;
}
/**
* Return javascript to create file picker to browse repositories
* @global object $CFG
* @global object $USER
* @param object $context the context
* @param string $id unique id for every file picker
* @param string $accepted_filetypes
* @param string $returnvalue the return value of file picker
* @return array
*/
function repository_get_client($context, $id = '', $accepted_filetypes = '*', $returnvalue = '*') {
global $CFG, $USER;
$ft = new file_type_to_ext();
$image_file_ext = json_encode($ft->get_file_ext(array('image')));
$video_file_ext = json_encode($ft->get_file_ext(array('video')));
$accepted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
$css = '';
$js = '';
if (!isset($CFG->filepickerjsloaded)) {
$css .= <<<EOD
<style type="text/css">
@import "$CFG->httpswwwroot/lib/yui/resize/assets/skins/sam/resize.css";
@import "$CFG->httpswwwroot/lib/yui/container/assets/skins/sam/container.css";
@import "$CFG->httpswwwroot/lib/yui/layout/assets/skins/sam/layout.css";
@import "$CFG->httpswwwroot/lib/yui/button/assets/skins/sam/button.css";
@import "$CFG->httpswwwroot/lib/yui/assets/skins/sam/treeview.css";
</style>
<style type="text/css">
.file-picker{font-size:12px;}
.file-picker strong{background:#FFFFCC}
.file-picker a{color: #336699}
.file-picker a:hover{background:#003366;color:white}
.fp-panel{padding:0;margin:0; text-align:left;}
.fp-login-form{text-align:center}
.fp-searchbar{float:right}
.fp-viewbar{width:300px;float:left}
.fp-toolbar{padding: .8em;background: #FFFFCC;color:white;text-align:center}
.fp-toolbar a{padding: 0 .5em}
.fp-list{list-style-type:none;padding:0;float:left;width:100%;margin:0;}
.fp-list li{border-bottom:1px dotted gray;margin-bottom: 1em;}
.fp-repo-name{display:block;padding: .5em;margin-bottom: .5em}
.fp-pathbar{margin: .4em;border-bottom: 1px dotted gray;}
.fp-pathbar a{padding: .4em;}
.fp-rename-form{text-align:center}
.fp-rename-form p{margin: 1em;}
.fp-upload-form{margin: 2em 0;text-align:center}
.fp-upload-btn a{cursor: default;background: white;border:1px solid gray;color:black;padding: .5em}
.fp-upload-btn a:hover {background: grey;color:white}
.fp-paging{margin:1em .5em; clear:both;text-align:center;line-height: 2.5em;}
.fp-paging a{padding: .5em;border: 1px solid #CCC}
.fp-paging a.cur_page{border: 1px solid blue}
.fp-popup{text-align:center}
.fp-grid{float:left;text-align:center;}
.fp-grid div{overflow: hidden}
.fp-grid p{margin:0;padding:0;background: #FFFFCC}
.fp-grid .label{height:48px;text-align:center}
.fp-grid span{color:gray}
</style>
<!--[if IE 6]>
<style type="text/css">
/* Fix for IE6 */
.yui-skin-sam .yui-panel .hd{
}
</style>
<![endif]-->
EOD;
require_js(array(
'yui_yahoo',
'yui_dom',
'yui_event',
'yui_element',
'yui_treeview',
'yui_dragdrop',
'yui_container',
'yui_resize',
'yui_layout',
'yui_connection',
'yui_json',
'yui_button',
'yui_selector',
'repository/repository.js'
));
$lang = array();
$lang['title'] = get_string('title', 'repository');
$lang['preview'] = get_string('preview', 'repository');
$lang['add'] = get_string('add', 'repository');
$lang['back'] = get_string('back', 'repository');
$lang['cancel'] = get_string('cancel');
$lang['close'] = get_string('close', 'repository');
$lang['ccache'] = get_string('cleancache', 'repository');
$lang['copying'] = get_string('copying', 'repository');
$lang['downbtn'] = get_string('getfile', 'repository');
$lang['download'] = get_string('downloadsucc', 'repository');
$lang['date'] = get_string('date', 'repository').': ';
$lang['error'] = get_string('error', 'repository');
$lang['filenotnull'] = get_string('filenotnull', 'repository');
$lang['federatedsearch'] = get_string('federatedsearch', 'repository');
$lang['help'] = get_string('help');
$lang['refresh'] = get_string('refresh', 'repository');
$lang['invalidjson'] = get_string('invalidjson', 'repository');
$lang['listview'] = get_string('listview', 'repository');
$lang['login'] = get_string('login', 'repository');
$lang['logout'] = get_string('logout', 'repository');
$lang['loading'] = get_string('loading', 'repository');
$lang['thumbview'] = get_string('thumbview', 'repository');
$lang['title'] = get_string('title', 'repository');
$lang['noresult'] = get_string('noresult', 'repository');
$lang['mgr'] = get_string('manageurl', 'repository');
$lang['noenter'] = get_string('noenter', 'repository');
$lang['save'] = get_string('save', 'repository');
$lang['saveas'] = get_string('saveas', 'repository').': ';
$lang['saved'] = get_string('saved', 'repository');
$lang['saving'] = get_string('saving', 'repository');
$lang['size'] = get_string('size', 'repository').': ';
$lang['sync'] = get_string('sync', 'repository');
$lang['search'] = get_string('search', 'repository');
$lang['searching'] = get_string('searching', 'repository');
$lang['submit'] = get_string('submit', 'repository');
$lang['preview'] = get_string('preview', 'repository');
$lang['popup'] = get_string('popup', 'repository');
$lang['upload'] = get_string('upload', 'repository').'...';
$lang['uploading'] = get_string('uploading', 'repository');
// XXX fp_lang includes language strings
$js .= print_js_config($lang, 'fp_lang', true);
$options = array();
$context = get_system_context();
$options['contextid'] = $context->id;
// XXX fp_config includes filepicker options, including contextid
$js .= print_js_config($options, 'fp_config', true);
$accepted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
$js .= <<<EOD
<script>
file_extensions.image = $image_file_ext;
file_extensions.media = $video_file_ext;
</script>
EOD;
$CFG->filepickerjsloaded = true;
} else {
// if yui and repository javascript libs are loaded
$js = '';
}
// print instances listing
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
if (is_array($accepted_filetypes) && in_array('*', $accepted_filetypes)) {
$accepted_filetypes = '*';
}
$repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue);
$js .= <<<EOD
<script>
repository_listing['$id'] = {};
EOD;
foreach ($repos as $repo) {
$info = $repo->ajax_info();
$js .= "\r\n";
$js .= 'repository_listing[\''.$id.'\'][\''.$info->id.'\']='.json_encode($repo->ajax_info()).';';
$js .= "\n";
}
$js .= "\r\n";
$js .= "</script>";
return array('css'=>$css, 'js'=>$js);
}

974
repository/repository.js Normal file
View File

@ -0,0 +1,974 @@
// $Id$
///////////////////////////////////////////////////////////////////////////
// //
// Don't modify this file unless you know how it works //
// //
///////////////////////////////////////////////////////////////////////////
/**
* repository_client is a javascript class, it contains several static
* methods you can call it directly without creating an instance.
* If you are going to create a file picker, you need create an instance
* repo = new repository_client();
*/
var repository_listing = {};
var active_instances = {};
var file_extensions = {};
// repository_client has static functions
var repository_client = (function(){
// private static field
var version = '2.0';
var PANEL_BODY_PADDING = (10*2);
// private static methods
function help() {
alert(version);
}
// a hack to fix ie6 bug
function ie6_fix_width(id, width) {
if(YAHOO.env.ua.ie == 6){
var fp_title = document.getElementById('file-picker-'+id);
fp_title.style.width = width;
}
}
function $() {
// public methods of filepicker instance
this.create_filepicker = function(client_id) {
var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat");
var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS));
var btn_listing = {label: fp_lang.listview, value: 'l',
onclick: {fn: repository_client.view_as_list, obj:client_id}};
var btn_icons = {label: fp_lang.thumbview, value: 't',
onclick: {fn: repository_client.view_as_icons, obj:client_id}};
document.body.className += ' yui-skin-sam';
var el = document.createElement('DIV');
el.id = 'file-picker-'+client_id;
el.className = 'file-picker';
this.client_id = client_id;
document.body.appendChild(el);
this.filepicker = new YAHOO.widget.Panel('file-picker-' + client_id, {
draggable: true,
close: true,
underlay: 'none',
zindex: 666666,
xy: [50, YAHOO.util.Dom.getDocumentScrollTop()+20]
});
var layout = '';
this.filepicker.client_id = client_id;
this.filepicker.setHeader(fp_lang['title']);
this.filepicker.setBody('<div id="layout-'+client_id+'"></div>');
this.filepicker.beforeRenderEvent.subscribe(function() {
YAHOO.util.Event.onAvailable('layout-'+client_id, function() {
layout = new YAHOO.widget.Layout('layout-'+client_id, {
height: 480, width: 700,
units: [
{position: 'top', height: 32, resize: false,
body:'<div class="yui-buttongroup fp-viewbar" id="repo-viewbar-'+client_id+'"></div><div class="fp-searchbar" id="search-div-'+client_id+'"></div>', gutter: '2'},
{position: 'left', width: 200, resize: true, scroll:true,
body:'<ul class="fp-list" id="repo-list-'+client_id+'"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
{position: 'center', body: '<div class="fp-panel" id="panel-'+client_id+'"></div>',
scroll: true, gutter: '0 2 0 0' }
]
});
layout.render();
});
});
var resize = new YAHOO.util.Resize('file-picker-'+client_id, {
handles: ['br'],
autoRatio: true,
status: true,
minWidth: 680,
minHeight: 400
});
ie6_fix_width(client_id, '680px');
resize.on('resize', function(args) {
var panelHeight = args.height;
var headerHeight = this.header.offsetHeight; // Content + Padding + Border
var bodyHeight = (panelHeight - headerHeight);
var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING;
YAHOO.util.Dom.setStyle(this.body, 'height', bodyContentHeight + 'px');
ie6_fix_width(this.client_id, '680px');
if (IE_SYNC) {
this.sizeUnderlay();
this.syncIframe();
}
layout.set('height', bodyContentHeight);
layout.set('width', (args.width - PANEL_BODY_PADDING));
layout.resize();
}, this.filepicker, true);
repository_client.fp[client_id].viewbar = new YAHOO.widget.ButtonGroup({
id: 'btngroup-'+client_id,
name: 'buttons',
disabled: true,
container: 'repo-viewbar-'+client_id
});
repository_client.fp[client_id].viewbar.addButtons([btn_icons, btn_listing]);
this.print_listing();
this.filepicker.render();
}
this.print_listing = function() {
var container = new YAHOO.util.Element('repo-list-'+this.client_id);
container.set('innerHTML', '');
container.on('contentReady', function() {
for(var i in repository_listing[this.client_id]) {
var repo = repository_listing[this.client_id][i];
var support = false;
if(this.env=='editor' && this.accepted_types != '*'){
if(repo.supported_types!='*'){
for (var j in repo.supported_types){
if(mdl_in_array(repo.supported_types[j], this.accepted_types)){
support = true;
}
}
}
}else{
support = true;
}
if(repo.supported_types == '*' || support){
var li = document.createElement('li');
li.id = 'repo-'+this.client_id+'-'+repo.id;
var icon = document.createElement('img');
icon.src = repo.icon;
icon.width = '16';
icon.height = '16';
var link = document.createElement('a');
link.href = '###';
link.id = 'repo-call-'+this.client_id+'-'+repo.id;
link.appendChild(icon);
link.className = 'fp-repo-name';
link.innerHTML += ' '+repo.name;
link.onclick = function() {
var re = new RegExp("repo-call-(\\w+)-(\\d+)", "i");
var result = this.id.match(re);
var client_id = result[1];
var repo_id = result[2];
// high light currect selected repository
for(var cc in repository_listing[client_id]){
var tmp_id = 'repo-call-'+client_id+'-'+ cc;
var el = document.getElementById(tmp_id);
if(el){
el.style.background = 'transparent';
}
}
this.style.background = '#CCC';
repository_client.loading(client_id, 'load');
repository_client.req(client_id, repo_id, '');
}
li.appendChild(link);
container.appendChild(li);
repo = null;
}
}
}, this, true);
}
this.show = function(){
this.print_listing();
var panel = new YAHOO.util.Element('panel-'+this.filepicker.client_id);
panel.get('element').innerHTML = '';
this.filepicker.show();
}
this.hide = function(){
this.filepicker.hide();
}
}
// all filepicker instances
$.fp = {};
return $;
})();
// public static method
// may be called outside yui
repository_client.req = function(client_id, id, path, page) {
this.fp[client_id].viewbar.set('disabled', false);
var r = repository_client.fp[client_id];
var params = [];
params['p'] = path;
params['env']=r.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['client_id'] = client_id;
params['repo_id']=id;
if (!!page) { // convert page to boolean value
params['page']=page;
}
params['accepted_types'] = r.accepted_types;
var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=list', this.req_cb, this.postdata(params));
}
repository_client.req_cb = {
success: function(o){
try {
var data = YAHOO.lang.JSON.parse(o.responseText);
} catch(e) {
alert(fp_lang.invalidjson+' - |req_cb| -'+repository_client.stripHTML(o.responseText));
};
var repo = repository_client.fp[data.client_id];
var panel = new YAHOO.util.Element('panel-'+data.client_id);
if(data && data.e) {
panel.get('element').innerHTML = data.e;
return;
}
// save data
repo.fs = data;
if(!data) {
return;
}else if(data.msg){
repository_client.print_msg(data.msg);
}else if(data.iframe) {
repository_client.viewiframe();
}else if(data.login) {
repository_client.print_login(data.client_id, data);
}else if(data.list) {
if(repo.view_status) {
repository_client.view_as_list(data.client_id, data.list);
} else {
repository_client.view_as_icons(data.client_id, data.list);
}
}
}
}
repository_client.req_search_results = function(client_id, id, path, page) {
this.fp[client_id].viewbar.set('disabled', false);
var r = repository_client.fp[client_id];
var params = [];
params['p'] = path;
params['env']=r.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['client_id'] = client_id;
params['search_paging']='true';
params['repo_id']=id;
if (!!page) { // convert page to boolean value
params['page']=page;
}
params['accepted_types'] = r.accepted_types;
var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=search', this.req_cb, this.postdata(params));
}
repository_client.print_login = function(id, data) {
var login = data.login;
var panel = new YAHOO.util.Element('panel-'+id);
var str = '<div class="fp-login-form">';
var has_pop = false;
this.fp[id].login = login;
for(var k in login) {
if(login[k].type=='popup') {
str += '<p class="fp-popup">'+fp_lang.popup+'</p>';
str += '<p class="fp-popup"><button onclick="repository_client.popup(\''+id+'\', \''+login[k].url+'\')">'+fp_lang.login+'</button>';
str += '</p>';
has_pop = true;
}else if(login[k].type=='textarea') {
str += '<p><textarea id="'+login[k].id+'" name="'+login[k].name+'"></textarea></p>';
}else{
str += '<p>';
var label_id = '';
var field_id = '';
var field_value = '';
if(login[k].id) {
label_id = ' for="'+login[k].id+'"';
field_id = ' id="'+login[k].id+'"';
}
if (login[k].label) {
str += '<label'+label_id+'>'+login[k].label+'</label>&nbsp;';
}
if(login[k].value) {
field_value = ' value="'+login[k].value+'"';
}
str += '<input type="'+login[k].type+'"'+' name="'+login[k].name+'"'+field_id+field_value+' />';
str += '</p>';
}
}
var btn_label = login['login_btn_label']?login['login_btn_label']:fp_lang.submit;
if (data['login_search_form']) {
str += '<p><input type="button" onclick="repository_client.search(\''+id+'\', \''+data.repo_id+'\')" value="'+btn_label+'" /></p>';
} else {
if(!has_pop) {
str += '<p><input type="button" onclick="repository_client.login(\''+id+'\', \''+data.repo_id+'\')" value="'+btn_label+'" /></p>';
}
}
str += '</div>';
panel.get('element').innerHTML = str;
}
repository_client.login = function(id, repo_id) {
var params = [];
var data = this.fp[id].login;
for (var k in data) {
if(data[k].type!='popup') {
var el = document.getElementsByName(data[k].name)[0];
params[data[k].name] = '';
if(el.type == 'checkbox') {
params[data[k].name] = el.checked;
} else {
params[data[k].name] = el.value;
}
}
}
params['env'] = this.fp[id].env;
params['repo_id'] = repo_id;
params['client_id'] = id;
params['ctx_id'] = fp_config.contextid;
params['sesskey'] = moodle_cfg.sesskey;
params['accepted_types'] = this.fp[id].accepted_types;
this.loading(id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=sign', this.req_cb, this.postdata(params));
}
repository_client.search = function(id, repo_id) {
var params = [];
var data = this.fp[id].login;
for (var k in data) {
if(data[k].type!='popup') {
var el = document.getElementsByName(data[k].name)[0];
params[data[k].name] = '';
if(el.type == 'checkbox') {
params[data[k].name] = el.checked;
} else {
params[data[k].name] = el.value;
}
}
}
params['env'] = this.fp[id].env;
params['repo_id'] = repo_id;
params['client_id'] = id;
params['ctx_id'] = fp_config.contextid;
params['sesskey'] = moodle_cfg.sesskey;
params['accepted_types'] = this.fp[id].accepted_types;
this.loading(id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=search', this.req_cb, this.postdata(params));
}
repository_client.loading = function(id, type, name) {
var panel = new YAHOO.util.Element('panel-'+id);
panel.get('element').innerHTML = '';
var content = document.createElement('div');
content.style.textAlign='center';
var para = document.createElement('P');
var img = document.createElement('IMG');
if(type=='load') {
img.src = moodle_cfg.pixpath+'/i/loading.gif';
para.innerHTML = fp_lang.loading;
}else{
img.src = moodle_cfg.pixpath+'/i/progressbar.gif';
para.innerHTML = fp_lang.copying+' <strong>'+name+'</strong>';
}
content.appendChild(para);
content.appendChild(img);
panel.get('element').appendChild(content);
}
repository_client.view_as_list = function(client_id, data) {
if (typeof client_id == 'object') {
// click button
client_id = data;
list = repository_client.fp[client_id].fs.list;
} else {
// from callback
list = data;
}
var panel = new YAHOO.util.Element('panel-'+client_id);
var fp = repository_client.fp[client_id];
fp.view_status = 1;
fp.viewbar.check(1);
repository_client.print_header(client_id);
panel.get('element').innerHTML += '<div id="treediv-'+client_id+'"></div>';
var tree = new YAHOO.widget.TreeView('treediv-'+client_id);
tree.dynload = function (node, fnLoadComplete) {
var callback = {
success: function(o) {
try {
var json = YAHOO.lang.JSON.parse(o.responseText);
} catch(e) {
alert(fp_lang.invalidjson+' - |dynload| -'+repository_client.stripHTML(o.responseText));
return;
}
for(k in json.list) {
repository_client.buildtree(json.client_id, json.list[k], node);
}
o.argument.fnLoadComplete();
},
failure:function(oResponse) {
alert(fp_lang.error+' - |dynload| -');
oResponse.argument.fnLoadComplete();
},
argument:{"node":node, "fnLoadComplete": fnLoadComplete}
}
var fp = repository_client.fp[node.client_id];
var params = [];
params['p']=node.path;
params['env']=fp.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['repo_id']=fp.fs.repo_id;
params['client_id']=node.client_id;
params['accepted_types']=fp.accepted_types;
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=list',callback,repository_client.postdata(params));
}
tree.dynload.client_id = client_id;
if(fp.fs.dynload) {
tree.setDynamicLoad(tree.dynload, 1);
} else {
}
for(k in list) {
repository_client.buildtree(client_id, list[k], tree.getRoot());
}
tree.draw();
tree.subscribe('clickEvent', function(e){
if(e.node.isLeaf){
repository_client.select_file(e.node.data.filename, e.node.data.value, e.node.data.icon, client_id, e.node.repo_id);
}
});
repository_client.print_footer(client_id);
}
repository_client.buildtree = function(client_id, node, level) {
var fs = repository_client.fp[client_id].fs;
if(node.children) {
node.title = '<i><u>'+node.title+'</u></i>';
}
var info = {
label:node.title,
title:fp_lang.date+' '+node.date+fp_lang.size+' '+node.size,
filename:node.title,
value:node.source,
icon:node.thumbnail,
path:node.path
};
var tmpNode = new YAHOO.widget.TextNode(info, level, false);
var tooltip = new YAHOO.widget.Tooltip(tmpNode.labelElId, {
context:tmpNode.labelElId, text:info.title});
if(node.repo_id) {
tmpNode.repo_id=node.repo_id;
}else{
tmpNode.repo_id=fs.repo_id;
}
if(node.children) {
if(node.expanded) {
tmpNode.expand();
}
tmpNode.isLeaf = false;
tmpNode.client_id = client_id;
if (node.path) {
tmpNode.path = node.path;
} else {
tmpNode.path = '';
}
for(var c in node.children) {
this.buildtree(client_id, node.children[c], tmpNode);
}
} else {
tmpNode.isLeaf = true;
}
}
repository_client.select_file = function(oldname, url, icon, client_id, repo_id) {
var thumbnail = document.getElementById('fp-grid-panel-'+client_id);
if(thumbnail){
thumbnail.style.display = 'none';
}
var header = document.getElementById('fp-header-'+client_id);
header.style.display = 'none';
var footer = document.getElementById('fp-footer-'+client_id);
footer.style.display = 'none';
var panel = new YAHOO.util.Element('panel-'+client_id);
var html = '<div class="fp-rename-form">';
html += '<p><img src="'+icon+'" /></p>';
html += '<p><label for="newname-'+client_id+'">'+fp_lang.saveas+'</label>';
html += '<input type="text" id="newname-'+client_id+'" value="'+oldname+'" /></p>';
html += '<p><input type="hidden" id="fileurl-'+client_id+'" value="'+url+'" />';
html += '<input type="button" onclick="repository_client.download(\''+client_id+'\', \''+repo_id+'\')" value="'+fp_lang.downbtn+'" />';
html += '<input type="button" onclick="repository_client.viewfiles()" value="'+fp_lang.cancel+'" /></p>';
html += '</div>';
panel.get('element').innerHTML += html;
var tree = document.getElementById('treediv-'+client_id);
if(tree){
tree.style.display = 'none';
}
}
repository_client.paging = function(client_id, id) {
var str = '';
this.fp[client_id].view_staus = 0;
var fs = this.fp[client_id].fs;
if(fs.pages) {
str += '<div class="fp-paging" id="paging-'+id+'-"'+client_id+'>';
if(!fs.search_result){
var action = 'req';
} else {
var action = 'req_search_results';
}
str += this.get_page_btn(client_id, action, 1)+'1</a>';
if (fs.page+2>=fs.pages) {
var max = fs.pages;
} else {
var max = fs.page+2;
}
if (fs.page-2 >= 3) {
str += ' ... ';
for(var i=fs.page-2; i<max; i++) {
str += this.get_page_btn(client_id, action, i);
str += String(i);
str += '</a> ';
}
} else {
for(var i = 2; i < max; i++) {
str += this.get_page_btn(client_id, action, i);
str += String(i);
str += '</a> ';
}
}
if (max==fs.pages) {
str += this.get_page_btn(client_id, action, fs.pages)+fs.pages+'</a>';
} else {
str += repository_client.get_page_btn(client_id, action, max)+max+'</a>';
str += ' ... '+repository_client.get_page_btn(client_id, action, fs.pages)+fs.pages+'</a>';
}
str += '</div>';
}
return str;
}
repository_client.get_page_btn = function(client_id, type, page) {
var fs = this.fp[client_id].fs;
var css = '';
if (page == fs.page) {
css = 'class="cur_page"';
}
str = '<a '+css+' onclick="repository_client.'+type+'(\''+client_id+'\','+fs.repo_id+', '+page+', '+page+')" href="###">';
return str;
}
repository_client.path = function(client_id) {
var fs = this.fp[client_id].fs;
// if this is listing view
if(this.fp[client_id].view_status == 1) {
return;
}
var panel = new YAHOO.util.Element('panel-'+client_id);
var p = fs.path;
if(p && p.length!=0) {
var oDiv = document.createElement('DIV');
oDiv.id = "path-"+client_id;
oDiv.className = "fp-pathbar";
panel.get('element').appendChild(oDiv);
for(var i = 0; i < fs.path.length; i++) {
var link = document.createElement('A');
link.href = "###";
link.innerHTML = fs.path[i].name;
link.id = 'path-'+i+'-el';
var sep = document.createElement('SPAN');
sep.innerHTML = '/';
oDiv.appendChild(link);
oDiv.appendChild(sep);
var el = new YAHOO.util.Element(link.id);
el.id = fs.repo_id;
el.path = fs.path[i].path;
el.link_id = link.id;
el.client_id = client_id;
el.on('contentReady', function() {
var path_link = document.getElementById(this.link_id);
path_link.id = this.id;
path_link.path = this.path;
path_link.client_id = this.client_id;
path_link.onclick = function() {
alert('test');
repository_client.req(this.client_id, this.id, this.path);
}
});
link = null;
}
}
}
repository_client.print_header = function(client_id) {
var panel = new YAHOO.util.Element('panel-'+client_id);
var str = '<div id="fp-header-'+client_id+'">';
str += '<div class="fp-toolbar" id="repo-tb-'+client_id+'"></div>';
if(this.fp[client_id].fs.pages < 8){
str += this.paging(client_id, 'header');
}
str += '</div>';
panel.set('innerHTML', str);
this.path(client_id);
}
repository_client.view_as_icons = function(client_id, data) {
var list = null;
if (typeof client_id == 'object') {
// click button
client_id = data;
list = repository_client.fp[client_id].fs.list;
} else {
list = data;
}
var fp = repository_client.fp[client_id];
fp.view_status = 0;
fp.viewbar.check(0);
var container = document.getElementById('panel-'+client_id);
var panel = document.createElement('DIV');
panel.id = 'fp-grid-panel-'+client_id;
repository_client.print_header(client_id);
var count = 0;
for(k in list) {
// the container
var el = document.createElement('div');
el.className='fp-grid';
// the file name
var title = document.createElement('div');
title.id = 'grid-title-'+String(count);
title.className = 'label';
if (list[k].shorttitle) {
list[k].title = list[k].shorttitle;
}
title.innerHTML += '<a href="###"><span>'+list[k].title+"</span></a>";
if(list[k].thumbnail_width){
el.style.width = list[k].thumbnail_width+'px';
title.style.width = (list[k].thumbnail_width-10)+'px';
} else {
el.style.width = title.style.width = '80px';
}
var frame = document.createElement('DIV');
frame.style.textAlign='center';
if(list[k].thumbnail_height){
frame.style.height = list[k].thumbnail_height+'px';
}
var img = document.createElement('img');
img.src = list[k].thumbnail;
if(list[k].thumbnail_alt) {
img.alt = list[k].thumbnail_alt;
}
if(list[k].thumbnail_title) {
img.title = list[k].thumbnail_title;
}
var link = document.createElement('A');
link.href='###';
link.id = 'img-id-'+String(count);
if(list[k].url) {
el.innerHTML += '<p><a target="_blank" href="'+list[k].url+'">'+fp_lang.preview+'</a></p>';
}
link.appendChild(img);
frame.appendChild(link);
el.appendChild(frame);
el.appendChild(title);
panel.appendChild(el);
if(list[k].children) {
var folder = new YAHOO.util.Element(link.id);
folder.path = list[k].path;
var el_title = new YAHOO.util.Element(title.id);
folder.fs = list[k].children;
folder.on('contentReady', function() {
this.on('click', function() {
if(fp.fs.dynload) {
var fs = repository_client.fp[client_id].fs;
var params = [];
params['p'] = this.path;
params['env'] = repository_client.fp[client_id].env;
params['repo_id'] = fs.repo_id;
params['ctx_id'] = fp_config.contextid;
params['sesskey']= moodle_cfg.sesskey;
params['accepted_types'] = repository_client.fp[client_id].accepted_types;
params['client_id'] = client_id;
repository_client.loading(client_id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=list', repository_client.req_cb, repository_client.postdata(params));
}else{
repository_client.view_as_icons(client_id, this.fs);
}
});
});
el_title.on('contentReady', function() {
this.on('click', function(){
folder.fireEvent('click');
});
});
} else {
var el_title = new YAHOO.util.Element(title.id);
var file = new YAHOO.util.Element(link.id);
el_title.title = file.title = list[k].title;
el_title.value = file.value = list[k].source;
el_title.icon = file.icon = list[k].thumbnail;
if(fp.fs.repo_id) {
el_title.repo_id = file.repo_id = fp.fs.repo_id;
}else{
el_title.repo_id = file.repo_id = '';
}
file.on('contentReady', function() {
this.on('click', function() {
repository_client.select_file(this.title, this.value, this.icon, client_id, this.repo_id);
});
});
el_title.on('contentReady', function() {
this.on('click', function() {
repository_client.select_file(this.title, this.value, this.icon, client_id, this.repo_id);
});
});
}
count++;
}
container.appendChild(panel);
repository_client.print_footer(client_id);
}
repository_client.create_upload_form = function(client_id) {
var str = '';
var fs = repository_client.fp[client_id].fs;
if(fs.upload) {
str += '<div id="'+fs.upload.id+'_div" class="fp-upload-form">';
str += '<form id="'+fs.upload.id+'" onsubmit="return false">';
str += '<label for="'+fs.upload.id+'-file">'+fs.upload.label+': </label>';
str += '<input type="file" id="'+fs.upload.id+'-file" name="repo_upload_file" />';
str += '<p class="fp-upload-btn"><a href="###" onclick="return repository_client.upload(\''+client_id+'\');">'+fp_lang.upload+'</a></p>';
str += '</form>';
str += '</div>';
}
return str;
}
repository_client.print_footer = function(client_id) {
var fs = this.fp[client_id].fs;
var panel = document.getElementById('panel-'+client_id);
var footer = document.createElement('DIV');
footer.id = 'fp-footer-'+client_id;
footer.innerHTML += this.create_upload_form(client_id);
footer.innerHTML += this.paging(client_id, 'footer');
panel.appendChild(footer);
// add repository manage buttons here
var oDiv = document.getElementById('repo-tb-'+client_id);
if(!fs.nosearch) {
var search = document.createElement('A');
search.href = '###';
search.innerHTML = '<img src="'+moodle_cfg.pixpath+'/a/search.png" /> '+fp_lang.search;
oDiv.appendChild(search);
search.onclick = function() {
repository_client.search_form(client_id,repository_client.repositoryid);
}
}
// weather we use cache for this instance, this button will reload listing anyway
if(!fs.norefresh) {
var ccache = document.createElement('A');
ccache.href = '###';
ccache.innerHTML = '<img src="'+moodle_cfg.pixpath+'/a/refresh.png" /> '+fp_lang.refresh;
oDiv.appendChild(ccache);
ccache.onclick = function() {
var params = [];
params['env']=fs.env;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['repo_id']=fs.repo_id;
repository_client.loading(client_id, 'load');
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=ccache', repository_client.req_cb, repository_client.postdata(params));
}
}
if(fs.manage) {
var mgr = document.createElement('A');
mgr.innerHTML = '<img src="'+moodle_cfg.pixpath+'/a/setting.png" /> '+fp_lang.mgr;
mgr.href = fs.manage;
mgr.target = "_blank";
oDiv.appendChild(mgr);
}
if(!fs.nologin) {
var logout = document.createElement('A');
logout.href = '###';
logout.innerHTML = '<img src="'+moodle_cfg.pixpath+'/a/logout.png" /> '+fp_lang.logout;
oDiv.appendChild(logout);
logout.onclick = function() {
repository_client.logout(client_id, fs.repo_id);
}
}
if(fs.help) {
var help = document.createElement('A');
help.href = fs.help;
help.target = "_blank";
help.innerHTML = '<img src="'+moodle_cfg.pixpath+'/a/help.png" /> '+fp_lang['help'];
oDiv.appendChild(help);
}
}
repository_client.postdata = function(obj) {
var str = '';
for(k in obj) {
if(obj[k] instanceof Array) {
for(i in obj[k]) {
str += (encodeURIComponent(k) +'[]='+encodeURIComponent(obj[k][i]));
str += '&';
}
} else {
str += encodeURIComponent(k) +'='+encodeURIComponent(obj[k]);
str += '&';
}
}
return str;
}
repository_client.stripHTML = function(str){
var re= /<\S[^><]*>/g
var ret = str.replace(re, "")
return ret;
}
repository_client.popup = function(client_id, url) {
repository_client.win = window.open(url,'repo_auth', 'location=0,status=0,scrollbars=0,width=500,height=300');
repository_client.win.client_id = client_id;
return false;
}
function repository_callback(id) {
repository_client.req(repository_client.win.client_id, id, '');
repository_client.win = null;
}
repository_client.logout = function(client_id, repo_id) {
var params = [];
params['repo_id'] = repo_id;
params['client_id'] = client_id;
var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=logout',
repository_client.req_cb, repository_client.postdata(params));
}
repository_client.download = function(client_id, repo_id) {
var fp = repository_client.fp[client_id];
var title = document.getElementById('newname-'+client_id).value;
var file = document.getElementById('fileurl-'+client_id).value;
repository_client.loading(client_id, 'download', title);
var params = [];
if(fp.itemid){
params['itemid']=fp.itemid;
}
params['env']=fp.env;
params['file']=file;
params['title']=title;
params['sesskey']=moodle_cfg.sesskey;
params['ctx_id']=fp_config.contextid;
params['repo_id']=repo_id;
params['client_id']=client_id;
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=download',
repository_client.download_cb,
repository_client.postdata(params));
}
repository_client.download_cb = {
success: function(o) {
try {
var data = YAHOO.lang.JSON.parse(o.responseText);
} catch(e) {
alert(fp_lang.invalidjson+' - |download_cb| -'+repository_client.stripHTML(o.responseText));
}
var panel = new YAHOO.util.Element('panel-'+data.client_id);
if(data && data.e) {
panel.get('element').innerHTML = data.e;
return;
}
repository_client.end(data.client_id, data);
}
}
repository_client.end = function(client_id, obj) {
var fp = repository_client.fp[client_id];
if(fp.env=='filepicker') {
fp.target.value = obj['id'];
}else if(fp.env=='editor'){
fp.target.value = obj['url'];
fp.target.onchange();
}
fp.formcallback(obj);
fp.hide();
repository_client.viewfiles();
}
repository_client.viewfiles = function() {
alert('Not available yet.');
}
repository_client.search_form = function(client_id, id) {
var fp = repository_client.fp[client_id];
var params = [];
params['env']=fp.env;
params['sesskey']=moodle_cfg.sesskey;
params['client_id'] = client_id;
params['ctx_id']=fp_config.contextid;
params['repo_id']=fp.fs.id;
var trans = YAHOO.util.Connect.asyncRequest('POST',
moodle_cfg.wwwroot+'/repository/ws.php?action=searchform',
repository_client.search_form_cb,
repository_client.postdata(params));
}
repository_client.search_form_cb = {
success: function(o) {
var el = document.getElementById('fp-search-dlg');
var _r = repository_client;
if(el) {
el.innerHTML = '';
} else {
var el = document.createElement('DIV');
el.id = 'fp-search-dlg';
}
var div1 = document.createElement('DIV');
div1.className = 'hd';
div1.innerHTML = fp_lang.searching+"\"" + _r.repos[_r.repositoryid].name + '"';
var div2 = document.createElement('DIV');
div2.className = 'bd';
var sform = document.createElement('FORM');
sform.method = 'POST';
sform.id = "fp-search-form";
sform.action = '$CFG->httpswwwroot/repository/ws.php?action=search';
sform.innerHTML = o.responseText;
div2.appendChild(sform);
el.appendChild(div1);
el.appendChild(div2);
document.body.appendChild(el);
var dlg = new YAHOO.widget.Dialog("fp-search-dlg",{
postmethod: 'async',
draggable: true,
width : "30em",
fixedcenter : true,
zindex: 766667,
visible : false,
constraintoviewport : true,
buttons : [
{ text:fp_lang.submit,handler: function() {
repository_client.viewbar.set('disabled', false); repository_client.loading('load');
YAHOO.util.Connect.setForm('fp-search-form', false, false);
this.cancel();
var trans = YAHOO.util.Connect.asyncRequest('POST',
'$CFG->httpswwwroot/repository/ws.php?action=search&env='+repository_client.env, repository_client.req_cb);
},isDefault:true},
{text:fp_lang.cancel,handler:function() {this.cancel()}}
]
});
dlg.render();
dlg.show();
}
}
var mdl_in_array = function(el, arr) {
for(var i = 0, l = arr.length; i < l; i++) {
if(arr[i] == el) {
return true;
}
}
return false;
}
// will be called by filemanager or htmleditor
function open_filepicker(id, params) {
var r = repository_client.fp[id];
if(!r) {
// passing parameters
r = new repository_client();
r.env = params.env;
r.target = params.target;
if(params.itemid){
r.itemid = params.itemid;
} else if(tinyMCE && id2itemid[tinyMCE.selectedInstance.editorId]){
r.itemid = id2itemid[tinyMCE.selectedInstance.editorId];
}
// setup callback function
if(params.callback) {
r.formcallback = params.callback;
} else {
r.formcallback = function() {};
}
// write back
repository_client.fp[id] = r;
// create file picker (html elements and events)
r.create_filepicker(id);
} else {
r.target = params.target;
r.show();
}
if(params.filetype) {
if(params.filetype == 'image') {
r.accepted_types = file_extensions.image;
} else if(params.filetype == 'video' || params.filetype== 'media') {
r.accepted_types = file_extensions.media;
} else if(params.filetype == 'file') {
r.accepted_types = '*';
}
} else {
r.accepted_types = '*';
}
return r;
}

View File

@ -8,6 +8,7 @@
/// Parameters
$page = optional_param('page', '', PARAM_RAW); // page
$client_id = optional_param('client_id', SITEID, PARAM_RAW); // client ID
$env = optional_param('env', 'filepicker', PARAM_ALPHA);// opened in editor or moodleform
$file = optional_param('file', '', PARAM_RAW); // file to download
$title = optional_param('title', '', PARAM_FILE); // new file name
@ -22,10 +23,11 @@
/// Headers to make it not cacheable
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
$err = new stdclass;
$err->client_id = $client_id;
/// Check permissions
if (! (isloggedin() && repository::check_context($ctx_id)) ) {
$err = new stdclass;
$err->e = get_string('nopermissiontoaccess', 'repository');
die(json_encode($err));
}
@ -50,7 +52,6 @@
}
exit;
} catch (repository_exception $e) {
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
@ -66,17 +67,18 @@
$tmp = array_merge($list, $ret['list']);
$list = $tmp;
} catch (repository_exception $e) {
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
}
}
die(json_encode(array('list'=>$list)));
$listing = array('list'=>$list);
$listing['client_id'] = $client_id;
die(json_encode($listing));
break;
case 'ccache': // Clean cache
$cache = new curl_cache('repository');
$cache = new curl_cache;
$cache->refresh();
$action = 'list';
break;
@ -86,7 +88,6 @@
$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
'WHERE i.id=? AND i.typeid=r.id';
if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
$err = new stdclass;
$err->e = get_string('invalidrepositoryid', 'repository');
die(json_encode($err));
} else {
@ -99,12 +100,10 @@
try {
$repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name));
} catch (repository_exception $e){
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
} else {
$err = new stdclass;
$err->e = get_string('invalidplugin', 'repository', $type);
die(json_encode($err));
}
@ -114,6 +113,8 @@
// the callback url should be something like this:
// http://xx.moodle.com/repository/ws.php?callback=yes&repo_id=1&sid=xxx
// sid is the attached auth token from external source
echo_fb($client_id);
echo_fb($repo_id);
$js =<<<EOD
<html><head><script type="text/javascript">
window.opener.repository_callback($repo_id);
@ -131,9 +132,11 @@ EOD;
case 'list':
if ($repo->check_login()) {
try {
echo json_encode($repo->get_listing($req_path, $page));
$listing = $repo->get_listing($req_path, $page);
$listing['client_id'] = $client_id;
$listing['repo_id'] = $repo_id;
echo json_encode($listing);
} catch (repository_exception $e) {
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
@ -143,15 +146,19 @@ EOD;
}
case 'login':
try {
echo json_encode($repo->print_login());
$listing = $repo->print_login();
$listing['client_id'] = $client_id;
$listing['repo_id'] = $repo_id;
echo json_encode($listing);
} catch (repository_exception $e){
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'logout':
echo json_encode($repo->logout());
$logout = $repo->logout();
$logout['client_id'] = $client_id;
echo json_encode($logout);
break;
case 'searchform':
$repo->print_search();
@ -159,10 +166,11 @@ EOD;
case 'search':
try {
$search_result = $repo->search($search_text);
$search_result['search_result'] = true;
$search_result['search_result'] = 'test';
$search_result['client_id'] = $client_id;
$search_result['repo_id'] = $repo_id;
echo json_encode($search_result);
} catch (repository_exception $e) {
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
@ -174,9 +182,10 @@ EOD;
}
try {
if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path));
echo json_encode(array('client_id'=>$client_id, 'url'=>$path, 'id'=>$path, 'file'=>$path));
} else {
$info = repository::move_to_filepool($path, $title, $itemid);
$info['client_id'] = $client_id;
if ($env == 'filepicker' || $env == 'filemanager'){
echo json_encode($info);
} else if ($env == 'editor') {
@ -184,20 +193,19 @@ EOD;
}
}
} catch (repository_exception $e){
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
} catch (Exception $e) {
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'upload':
try {
echo json_encode($repo->get_listing());
$upload = $repo->get_listing();
$upload['client_id'] = $client_id;
echo json_encode($upload);
} catch (repository_exception $e){
$err = new stdclass;
$err->e = $e->getMessage();
die(json_encode($err));
}