]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-20602, added link external option in file picker"
authorDongsheng Cai <unoter@gmail.com>
Sun, 8 Nov 2009 13:20:37 +0000 (13:20 +0000)
committerDongsheng Cai <unoter@gmail.com>
Sun, 8 Nov 2009 13:20:37 +0000 (13:20 +0000)
admin/settings/plugins.php
lang/en_utf8/repository.php
repository/flickr/repository.class.php
repository/flickr_public/repository.class.php
repository/lib.php
repository/repository.src.js
repository/repository_ajax.php

index d35de24d1ad620718c656d92521ae65226d86aff..eff0fbc02e4fb3dee5378a769d336ebf918125a6 100644 (file)
@@ -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),
index 586236c07db90dc607bb1b7dd76f9865a3e887e8..02877542a5dbde642c2a1d06b4589ee9a1e76231 100644 (file)
@@ -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';
index c845584efc7d62ab7dc3346224415fb535087962..65f61048e744c0fec3399861da8edf1202bdede7 100755 (executable)
@@ -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
index 53093a669a8499fa457c2147786de327770ebed5..3e77e718dd82b74d1ef18e81e2f9bc17042ee518 100644 (file)
@@ -346,8 +346,9 @@ class repository_flickr_public extends repository {
     }
 
     /**
+     * Print a search form
      *
-     * @return <type>
+     * @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
index 357a0065aa596ac3adb7796bff7a4731a1283237..469dcf18f9a4f207c8889007284452b890503797 100644 (file)
@@ -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
 
index d22c5474728e72a0f17620aa6c73afe2bd018eac..9d97541d489e4a6ecf4ea11a33e78680b43e0646 100644 (file)
@@ -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 += '<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="" /></p>';
-    html += '<p><input type="checkbox" id="external-'+client_id+'" value="" /> Link external</p>';
+
+    if (fp_config.externallink && fp.env == 'editor') {
+        html += '<p><input type="checkbox" id="external_link-'+client_id+'" value="" />'+fp_lang.linkexternal+'</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(\''+client_id+'\')" value="'+fp_lang.cancel+'" /></p>';
@@ -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;
index ef388efd1f3ce19bf9412fd6ae69138403d16129..b3d6048a8e98eec921c86592e18d857517a0c177 100755 (executable)
@@ -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');