From: dongsheng Date: Mon, 8 Dec 2008 05:19:09 +0000 (+0000) Subject: "MDL-17391, filter repository intances by file types" X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=014c1ca037a4b84346977c5466c25235fe6eb9b2;p=moodle.git "MDL-17391, filter repository intances by file types" --- diff --git a/lib/file/file_types.mm b/lib/file/file_types.mm new file mode 100644 index 0000000000..07f969c62e --- /dev/null +++ b/lib/file/file_types.mm @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/filelib.php b/lib/filelib.php index 0a6b0aa8b5..91f3e0a3e6 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2049,3 +2049,65 @@ class curl_cache { } } } +class file_type_to_ext { + public function __construct($file = '') { + global $CFG; + if (empty($file)) { + $this->file = $CFG->libdir.'/file/file_types.mm'; + } else { + $this->file = $file; + } + $this->tree = array(); + $this->result = array(); + } + private function _browse_nodes($parent, $types) { + $key = (string)$parent['TEXT']; + if(isset($parent->node)) { + $this->tree[$key] = array(); + if (in_array((string)$parent['TEXT'], $types)) { + $this->_select_nodes($parent, $this->result); + } else { + foreach($parent->node as $v){ + $this->_browse_nodes($v, $types); + } + } + } else { + $this->tree[] = $key; + } + } + private function _select_nodes($parent){ + if(isset($parent->node)) { + foreach($parent->node as $v){ + $this->_select_nodes($v, $this->result); + } + } else { + $this->result[] = (string)$parent['TEXT']; + } + } + + public function get_file_ext($types) { + $this->result = array(); + if (in_array('*', $types)) { + return '*'; + } + foreach ($types as $key=>$value){ + if (strpos($value, '.') !== false) { + $this->result[] = $value; + unset($types[$key]); + } + } + if (file_exists($this->file)) { + $xml = simplexml_load_file($this->file); + foreach($xml->node->node as $v){ + if (in_array((string)$v['TEXT'], $types)) { + $this->_select_nodes($v); + } else { + $this->_browse_nodes($v, $types); + } + } + } else { + exit('Failed to open test.xml.'); + } + return $this->result; + } +} diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index 295a317f74..bc1ff41d92 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -16,6 +16,16 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $this->_options[$name] = $value; } } + if (!empty($options['filetypes'])) { + $this->filetypes = $options['filetypes']; + } else { + $this->filetypes = '*'; + } + if (!empty($options['returnvalue'])) { + $this->returnvalue = $options['returnvalue']; + } else { + $this->returnvalue = '*'; + } if (!empty($options['maxbytes'])) { $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); } @@ -159,7 +169,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); } - $repo_info = repository_get_client($context); + $repo_info = repository_get_client($context, $this->filetypes, $this->returnvalue); $suffix = $repo_info['suffix']; $html = $this->_get_draftfiles($draftitemid, $suffix); diff --git a/repository/boxnet/repository.class.php b/repository/boxnet/repository.class.php index 29127d66e6..1a0b698ab7 100755 --- a/repository/boxnet/repository.class.php +++ b/repository/boxnet/repository.class.php @@ -239,4 +239,4 @@ class repository_boxnet extends repository { $mform->addElement('static', null, '', get_string('information','repository_boxnet')); } } -?> \ No newline at end of file +?> diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index 1bca8df601..9ced3f1f9b 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -292,4 +292,7 @@ class repository_flickr extends repository { public static function get_type_option_names() { return array('api_key', 'secret'); } + public function supported_filetypes() { + return array('web_image'); + } } diff --git a/repository/flickr_public/repository.class.php b/repository/flickr_public/repository.class.php index fa571597e8..5b4efe8cfc 100644 --- a/repository/flickr_public/repository.class.php +++ b/repository/flickr_public/repository.class.php @@ -338,5 +338,7 @@ class repository_flickr_public extends repository { repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null),1); } + public function supported_filetypes() { + return array('web_image'); + } } - diff --git a/repository/javascript.php b/repository/javascript.php index 2e320f1f6a..33ed7bb2c5 100644 --- a/repository/javascript.php +++ b/repository/javascript.php @@ -981,7 +981,10 @@ return _client; EOD; $user_context = get_context_instance(CONTEXT_USER, $USER->id); -$repos = repository::get_instances(array($user_context, $context, get_system_context())); +if (is_array($filetypes) && in_array('*', $filetypes)) { + $filetypes = '*'; +} +$repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $filetypes, $returnvalue); $js .= "\r\n".'repository_client_'.$suffix.'repos=[];'."\r\n"; foreach ($repos as $repo) { $info = $repo->ajax_info(); diff --git a/repository/lib.php b/repository/lib.php index 832f4a3859..30321bf50b 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -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) { + public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $filetypes = '*', $returnvalue = '*') { global $DB, $CFG, $USER; $params = array(); @@ -605,10 +605,30 @@ abstract class repository { $options['type'] = $repo->repositorytype; $options['typeid'] = $repo->typeid; $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); + $supported_filetypes = $ft->get_file_ext($repository->supported_filetypes()); + $is_supported = false; + foreach ($supported_filetypes as $type) { + if (in_array($type, $filetypes)) { + $is_supported = true; + } + } + } + if ($returnvalue !== '*' and $repository->supported_return_value() !== '*') { + $tmp = $repository->supported_return_value(); + if ($tmp != $returnvalue) { + $is_supported = false; + } + } if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) { - $ret[] = $repository; + if ($is_supported) { + $ret[] = $repository; + } } } return $ret; @@ -1226,7 +1246,7 @@ abstract class repository { * @return array return '*' means this repository support any files, otherwise * return mimetypes of files, it can be an array */ - public function supported_mimetype() { + public function supported_filetypes() { // return array('text/plain', 'image/gif'); return '*'; } @@ -1238,7 +1258,7 @@ abstract class repository { public function supported_return_value() { // return 'link'; // return 'ref_id'; - return '*'; + return 'ref_id'; } /** diff --git a/repository/youtube/repository.class.php b/repository/youtube/repository.class.php index 40f2e4d9bd..efb0c8e338 100644 --- a/repository/youtube/repository.class.php +++ b/repository/youtube/repository.class.php @@ -83,4 +83,12 @@ class repository_youtube extends repository { return $ret; } } + public function supported_return_value() { + // return 'link'; + // return 'ref_id'; + return 'link'; + } + public function supported_filetypes() { + return array('web_video'); + } }