]> git.mjollnir.org Git - moodle.git/commitdiff
"REPOSITORY, BOXNET/MDL-17391, create a helper function to filter files in listing"
authordongsheng <dongsheng>
Tue, 9 Dec 2008 02:11:57 +0000 (02:11 +0000)
committerdongsheng <dongsheng>
Tue, 9 Dec 2008 02:11:57 +0000 (02:11 +0000)
repository/boxnet/repository.class.php
repository/javascript.php
repository/lib.php

index 1a0b698ab725298918b5fa6adf54d7f216cb4323..e4ff381d0ed775486367b407bcd515ee486ba556 100755 (executable)
@@ -181,7 +181,7 @@ class repository_boxnet extends repository {
         $ret['manage'] = 'http://www.box.net/files';
         $ret['path'] = array(array('name'=>'Root', 'path'=>0));
         if(!empty($tree)) {
-            $ret['list']   = $tree;
+            $ret['list']   = array_filter($tree, array($this, 'filter'));
         }
         return $ret;
     }
index 99de61161ddacbb438fbfba67a5691e2bb434755..b1f9b85d10fdae70a8e49646bc164857a5763f73 100644 (file)
@@ -11,7 +11,7 @@
  * @param object $context the context
  * @return array
  */
-function repository_get_client($context, $filetypes = '*', $returnvalue = '*') {
+function repository_get_client($context, $accepted_filetypes = '*', $returnvalue = '*') {
     global $CFG, $USER;
     $suffix = uniqid();
     $sesskey = sesskey();
@@ -309,9 +309,9 @@ _client.print_instances = function() {
         var repo = _client.repos[i];
         var support = false;
         if(repository_client_$suffix.env=='editor'){
-            if(repo.filetype!='*'){
-                for (var j in repo.filetype){
-                    if(mdl_in_array(repo.filetype[j], _client.filetype)){
+            if(repo.supported_types!='*'){
+                for (var j in repo.supported_types){
+                    if(mdl_in_array(repo.supported_types[j], _client.accepted_types)){
                         support = true;
                     }
                 }
@@ -319,7 +319,7 @@ _client.print_instances = function() {
         }else{
             support = true;
         }
-        if(repo.filetype == '*' || support){
+        if(repo.supported_types == '*' || support){
             var li = document.createElement('li');
             li.id = 'repo-$suffix-'+repo.id;
             var icon = document.createElement('img');
@@ -905,6 +905,7 @@ _client.req = function(id, path, logout) {
     params['sesskey']='$sesskey';
     params['ctx_id']=$context->id;
     params['repo_id']=id;
+    params['accepted_types'] = _client.accepted_types;
     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action='+action, _client.req_cb, _client.postdata(params));
 }
 _client.search_form_cb = {
@@ -1013,10 +1014,10 @@ return _client;
 EOD;
 
 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
-if (is_array($filetypes) && in_array('*', $filetypes)) {
-    $filetypes = '*';
+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, $filetypes, $returnvalue);
+$repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue);
 $js .= "\r\n".'repository_client_'.$suffix.'.repos=[];'."\r\n";
 foreach ($repos as $repo) {
     $info = $repo->ajax_info();
@@ -1028,16 +1029,17 @@ $js .= "\r\n";
 $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')));
+$accpeted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
 $js .= <<<EOD
 function openpicker_$suffix(params) {
     if(params.filetype) {
         if(params.filetype == 'image') {
-            repository_client_$suffix.filetype = $image_file_ext;
+            repository_client_$suffix.accepted_types = $image_file_ext;
         } else if(params.filetype == 'video' || params.filetype== 'media') {
-            repository_client_$suffix.filetype = $video_file_ext;
+            repository_client_$suffix.accepted_types = $video_file_ext;
         }
     } else {
-        repository_client_$suffix.filetype = '*';
+        repository_client_$suffix.accepted_types = $accpeted_file_ext;
     }
     if(!repository_client_$suffix.instance) {
         repository_client_$suffix.env = params.env;
index b65fe9b9e1563aa6311c059424de001f4e36b042..e6fdeab2ccd60d24e58f207265972a44bbfda2ea 100644 (file)
@@ -557,7 +557,7 @@ abstract class repository {
      * @param string $type a type name to retrieve
      * @return array repository instances
      */
-    public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $filetypes = '*', $returnvalue = '*') {
+    public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $accepted_types = '*', $returnvalue = '*') {
         global $DB, $CFG, $USER;
 
         $params = array();
@@ -598,23 +598,25 @@ abstract class repository {
         }
 
         $ret = array();
+        $ft = new file_type_to_ext();
         foreach ($repos as $repo) {
             require_once($CFG->dirroot . '/repository/'. $repo->repositorytype.'/repository.class.php');
             $options['visible'] = $repo->visible;
             $options['name']    = $repo->name;
             $options['type']    = $repo->repositorytype;
             $options['typeid']  = $repo->typeid;
+            // tell instance what file types will be accepted by file picker
+            $options['accepted_types'] = $ft->get_file_ext($accepted_types);
             $classname = 'repository_' . $repo->repositorytype;//
             $is_supported = true;
 
             $repository = new $classname($repo->id, $repo->contextid, $options, $repo->readonly);
-            $ft = new file_type_to_ext();
-            if ($filetypes !== '*' and $repository->supported_filetypes() !== '*') {
-                $filetypes = $ft->get_file_ext($filetypes);
+            if ($accepted_types !== '*' and $repository->supported_filetypes() !== '*') {
+                $accepted_types = $ft->get_file_ext($accepted_types);
                 $supported_filetypes = $ft->get_file_ext($repository->supported_filetypes());
                 $is_supported = false;
                 foreach  ($supported_filetypes as $type) {
-                    if (in_array($type, $filetypes)) {
+                    if (in_array($type, $accepted_types)) {
                         $is_supported = true;
                     }
                 }
@@ -1274,7 +1276,8 @@ abstract class repository {
         $repo->name = $this->get_name();
         $repo->type = $this->options['type'];
         $repo->icon = $CFG->httpswwwroot.'/repository/'.$repo->type.'/icon.png';
-        $repo->filetype = $ft->get_file_ext($this->supported_filetypes());
+        $repo->supported_types = $ft->get_file_ext($this->supported_filetypes());
+        $repo->accepted_types = $this->accepted_types;
         return $repo;
     }
 
@@ -1455,6 +1458,28 @@ abstract class repository {
         }
     }
 
+    public function filter(&$value) {
+        $pass = false;
+        $accepted_types = optional_param('accepted_types', '', PARAM_RAW);
+        $ft = new file_type_to_ext;
+        $ext = $ft->get_file_ext($this->supported_filetypes());
+        if (isset($value->children)) {
+            $pass = true;
+            $value->children = array_filter($value->children, array($this, 'filter'));
+        } else {
+            if ($accepted_types == '*') {
+                $pass = true;
+            } elseif (is_array($accepted_types)) {
+                foreach ($accepted_types as $type) {
+                    if (preg_match('#'.$type.'$#', $value['title'])) {
+                        $pass = true;
+                    }
+                }
+            }
+        }
+        return $pass;
+    }
+
     /**
      * Given a path, and perhaps a search, get a list of files.
      *