From 99d5265525f63dded260154218752ca3871ea8b6 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Sun, 8 Nov 2009 13:20:37 +0000 Subject: [PATCH] "MDL-20602, added link external option in file picker" --- admin/settings/plugins.php | 2 +- lang/en_utf8/repository.php | 5 +-- repository/flickr/repository.class.php | 14 +++++++++ repository/flickr_public/repository.class.php | 17 +++++++++- repository/lib.php | 26 +++++++++++++--- repository/repository.src.js | 20 ++++++++---- repository/repository_ajax.php | 31 +++++++++++++------ 7 files changed, 92 insertions(+), 23 deletions(-) diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index d35de24d1a..eff0fbc02e 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -205,7 +205,7 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext)) $temp->add(new admin_setting_managerepository()); $temp->add(new admin_setting_heading('managerepositoriescommonheading', get_string('commonsettings', 'admin'), '')); $temp->add(new admin_setting_configtext('repositorycacheexpire', get_string('cacheexpire', 'repository'), get_string('configcacheexpire', 'repository'), 120)); - $temp->add(new admin_setting_configcheckbox('repositoryuseexternallink', get_string('useexternallink', 'repository'), get_string('configuseexternallink', 'repository'), 0)); + $temp->add(new admin_setting_configcheckbox('repositoryallowexternallinks', get_string('allowexternallinks', 'repository'), get_string('configallowexternallinks', 'repository'), 1)); $ADMIN->add('repositorysettings', $temp); $ADMIN->add('repositorysettings', new admin_externalpage('repositorynew', get_string('addplugin', 'repository'), $url, 'moodle/site:config', true), diff --git a/lang/en_utf8/repository.php b/lang/en_utf8/repository.php index 586236c07d..02877542a5 100644 --- a/lang/en_utf8/repository.php +++ b/lang/en_utf8/repository.php @@ -5,6 +5,8 @@ $string['add'] = 'Add'; $string['addfile'] = 'Add...'; $string['addplugin'] = 'Add a repository plugin'; $string['activaterep'] = 'Active repositories'; +$string['allowexternallinks'] = 'Allow external links'; +$string['configallowexternallinks'] = 'Disable using external links in repository'; $string['areabackup'] = 'Backups'; $string['areacategoryintro'] = 'Category introduction'; $string['areacourseintro'] = 'Course introduction'; @@ -71,6 +73,7 @@ $string['invalidrepositoryid'] = 'Invalid repository ID'; $string['invalidplugin'] = 'Invalid repository $a plug-in'; $string['invalidjson'] = 'Invalid JSON string'; $string['listview'] = 'View as list'; +$string['linkexternal'] = 'Link external'; $string['login'] = 'Login'; $string['logout'] = 'Logout'; $string['loading'] = 'Loading...'; @@ -119,8 +122,6 @@ $string['updown'] = 'Display order'; $string['upload'] = 'Upload this file'; $string['uploading'] = 'Uploading...'; $string['uploadsucc'] = 'The file has been uploaded successfully'; -$string['useexternallink'] = 'Use external link instead downloading files'; -$string['configuseexternallink'] = 'Will return the external link to file picker instead downloading external files'; $string['wrongcontext'] = 'You cannot access to this context'; $string['xhtmlerror'] = 'You are probably using XHTML strict header, some YUI Component doesn\'t work in this mode, please turn it off in moodle'; $string['ziped'] = 'Compress folder successfully'; diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index c845584efc..65f61048e7 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -210,6 +210,20 @@ class repository_flickr extends repository { return $this->build_list($photos, $page); } + public function get_link($photo_id) { + global $CFG; + $result = $this->flickr->photos_getSizes($photo_id); + $url = ''; + if(!empty($result[4])) { + $url = $result[4]['source']; + } elseif(!empty($result[3])) { + $url = $result[3]['source']; + } elseif(!empty($result[2])) { + $url = $result[2]['source']; + } + return $url; + } + /** * * @param string $photo_id diff --git a/repository/flickr_public/repository.class.php b/repository/flickr_public/repository.class.php index 53093a669a..3e77e718dd 100644 --- a/repository/flickr_public/repository.class.php +++ b/repository/flickr_public/repository.class.php @@ -346,8 +346,9 @@ class repository_flickr_public extends repository { } /** + * Print a search form * - * @return + * @return string */ public function print_search() { $str = ''; @@ -359,6 +360,20 @@ class repository_flickr_public extends repository { return $str; } + public function get_link($photo_id) { + global $CFG; + $result = $this->flickr->photos_getSizes($photo_id); + $url = ''; + if(!empty($result[4])) { + $url = $result[4]['source']; + } elseif(!empty($result[3])) { + $url = $result[3]['source']; + } elseif(!empty($result[2])) { + $url = $result[2]['source']; + } + return $url; + } + /** * * @global object $CFG diff --git a/repository/lib.php b/repository/lib.php index 357a0065aa..469dcf18f9 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -1176,6 +1176,17 @@ abstract class repository { return $dir.$filename; } + /** + * Return file URL, for most plugins, the parameter is the original + * url, but some plugins use a file id, so we need this function to + * convert file id to original url. + * + * @param string $url the url of file + */ + public function get_link($url) { + return $url; + } + /** * Download a file, this function can be overridden by * subclass. @@ -1853,6 +1864,7 @@ function repository_get_client($context, $id = '', $accepted_filetypes = '*', $ $lang['refresh'] = get_string('refresh', 'repository'); $lang['invalidjson'] = get_string('invalidjson', 'repository'); $lang['listview'] = get_string('listview', 'repository'); + $lang['linkexternal'] = get_string('linkexternal', 'repository'); $lang['login'] = get_string('login', 'repository'); $lang['logout'] = get_string('logout', 'repository'); $lang['loading'] = get_string('loading', 'repository'); @@ -1880,13 +1892,19 @@ function repository_get_client($context, $id = '', $accepted_filetypes = '*', $ $options = array(); $sys_context = get_system_context(); $options['contextid'] = $sys_context->id; + $externallink = (int)get_config(null, 'repositoryallowexternallinks'); + if (empty($externallink)) { + $options['externallink'] = false; + } else { + $options['externallink'] = true; + } $options['icons']['loading'] = $OUTPUT->old_icon_url('i/loading'); + $options['icons']['logout'] = $OUTPUT->old_icon_url('a/logout'); + $options['icons']['help'] = $OUTPUT->old_icon_url('a/help'); $options['icons']['progressbar'] = $OUTPUT->old_icon_url('i/progressbar'); - $options['icons']['search'] = $OUTPUT->old_icon_url('a/search'); - $options['icons']['refresh'] = $OUTPUT->old_icon_url('a/refresh'); + $options['icons']['search'] = $OUTPUT->old_icon_url('a/search'); $options['icons']['setting'] = $OUTPUT->old_icon_url('a/setting'); - $options['icons']['logout'] = $OUTPUT->old_icon_url('a/logout'); - $options['icons']['help'] = $OUTPUT->old_icon_url('a/help'); + $options['icons']['refresh'] = $OUTPUT->old_icon_url('a/refresh'); $options = json_encode($options); // fp_config includes filepicker options diff --git a/repository/repository.src.js b/repository/repository.src.js index d22c547472..9d97541d48 100644 --- a/repository/repository.src.js +++ b/repository/repository.src.js @@ -602,6 +602,7 @@ repository_client.buildtree = function(client_id, node, level) { } } repository_client.select_file = function(oldname, url, icon, client_id, repo_id) { + var fp = repository_client.fp[client_id]; if (repository_client.files[client_id] == undefined) { repository_client.files[client_id] = 0; } @@ -628,7 +629,10 @@ repository_client.select_file = function(oldname, url, icon, client_id, repo_id) html += '

'; html += '

'; html += '

'; - html += '

Link external

'; + + if (fp_config.externallink && fp.env == 'editor') { + html += '

'+fp_lang.linkexternal+'

'; + } html += '

'; html += ''; html += '

'; @@ -1006,19 +1010,23 @@ repository_client.logout = function(client_id, repo_id) { repository_client.req_cb, repository_client.postdata(params)); } repository_client.download = function(client_id, repo_id) { + var params = []; var fp = repository_client.fp[client_id]; var title = document.getElementById('newname-'+client_id).value; new_filename = title; var file = document.getElementById('fileurl-'+client_id).value; - var link_external = document.getElementById('external-'+client_id).checked; + if (fp.env == 'editor') { + var link_external = document.getElementById('external_link-'+client_id).checked; + if (link_external) { + params['link_external'] = 'yes'; + } + } if (fp.env == 'url') { + params['link_external'] = 'yes'; + } repository_client.loading(client_id, 'download', title); - var params = []; if(fp.itemid){ params['itemid']=fp.itemid; } - if (link_external) { - params['link_external'] = 'yes'; - } params['env']=fp.env; params['file']=file; params['savepath']=fp.savepath; diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php index ef388efd1f..b3d6048a8e 100755 --- a/repository/repository_ajax.php +++ b/repository/repository_ajax.php @@ -228,15 +228,6 @@ EOD; break; case 'download': try { - if ($env == 'url' or $link_external === 'yes') { - if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $file)) { - die(json_encode(array('type'=>'link', 'client_id'=>$client_id, - 'url'=>$file, 'id'=>$file, 'file'=>$file))); - } else { - $err->e = get_string('invalidurl'); - die(json_encode($err)); - } - } // we have two special repoisitory type need to deal with if ($repo->options['type'] == 'local' or $repo->options['type'] == 'draft') { $fileinfo = $repo->move_to_draft($file, $title, $itemid, $save_path); @@ -248,6 +239,28 @@ EOD; die(json_encode($info)); } + $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks'); + if (!empty($allowexternallink)) { + $allowexternallink = true; + } else { + $allowexternallink = false; + } + // allow external links in url element all the time + $allowexternallink = ($allowexternallink || ($env == 'url')); + + if ($allowexternallink and $link_external === 'yes' and ($repo->supported_returntypes() || FILE_EXTERNAL)) { + try { + $link = $repo->get_link($file); + } catch (repository_exception $e){ + } + $info = array(); + $info['client_id'] = $client_id; + $info['file'] = $title; + $info['type'] = 'link'; + $info['url'] = $link; + die(json_encode($info)); + } + $filepath = $repo->get_file($file, $title, $itemid, $save_path); if ($filepath === false) { $err->e = get_string('cannotdownload', 'repository'); -- 2.39.5