MDL-13766

1. Clean up source code
2. use search api replace old get_listing api
This commit is contained in:
dongsheng 2008-09-16 03:11:17 +00:00
parent bddcd93318
commit 353d5cf39b
6 changed files with 130 additions and 456 deletions

View File

@ -10,45 +10,31 @@
require_once($CFG->libdir.'/boxlib.php'); require_once($CFG->libdir.'/boxlib.php');
/**
*
*/
class repository_boxnet extends repository { class repository_boxnet extends repository {
private $box; private $box;
/**
*
* @global <type> $SESSION
* @param <type> $repositoryid
* @param <type> $context
* @param <type> $options
*/
public function __construct($repositoryid, $context = SITEID, $options = array()) { public function __construct($repositoryid, $context = SITEID, $options = array()) {
global $SESSION; global $SESSION;
$options['username'] = optional_param('boxusername', '', PARAM_RAW); $options['username'] = optional_param('boxusername', '', PARAM_RAW);
$options['password'] = optional_param('boxpassword', '', PARAM_RAW); $options['password'] = optional_param('boxpassword', '', PARAM_RAW);
$options['ticket'] = optional_param('ticket', '', PARAM_RAW); $options['ticket'] = optional_param('ticket', '', PARAM_RAW);
$reset = optional_param('reset', 0, PARAM_INT);
parent::__construct($repositoryid, $context, $options); parent::__construct($repositoryid, $context, $options);
$this->api_key = $this->get_option('api_key'); $this->api_key = $this->get_option('api_key');
if (empty($this->api_key)) {
}
$sess_name = 'box_token'.$this->id; $sess_name = 'box_token'.$this->id;
$this->sess_name = 'box_token'.$this->id; $this->sess_name = 'box_token'.$this->id;
// do login // do login
if (!empty($options['username']) && !empty($options['password']) && !empty($options['ticket']) ) { if(!empty($options['username']) && !empty($options['password']) && !empty($options['ticket']) ) {
$this->box = new boxclient($this->api_key); $this->box = new boxclient($this->api_key);
try { try {
$SESSION->$sess_name = $this->box->getAuthToken($options['ticket'], $SESSION->$sess_name = $this->box->getAuthToken($options['ticket'],
$options['username'], $options['password']); $options['username'], $options['password']);
} catch (repository_exception $e) { } catch (repository_exception $e) {
throw $e; throw $e;
} }
} }
// already logged // already logged
if (!empty($SESSION->$sess_name)) { if(!empty($SESSION->$sess_name)) {
if (empty($this->box)) { if(empty($this->box)) {
$this->box = new boxclient($this->api_key, $SESSION->$sess_name); $this->box = new boxclient($this->api_key, $SESSION->$sess_name);
} }
$this->auth_token = $SESSION->$sess_name; $this->auth_token = $SESSION->$sess_name;
@ -57,34 +43,17 @@ class repository_boxnet extends repository {
} }
} }
/**
*
* @global <type> $SESSION
* @return <type>
*/
public function check_login() { public function check_login() {
global $SESSION; global $SESSION;
return !empty($SESSION->{$this->sess_name}); return !empty($SESSION->{$this->sess_name});
} }
/**
*
* @global <type> $SESSION
* @return <type>
*/
public function logout() { public function logout() {
global $SESSION; global $SESSION;
unset($SESSION->{$this->sess_name}); unset($SESSION->{$this->sess_name});
return $this->print_login(); return $this->print_login();
} }
/**
*
* @param <type> $options
* @return <type>
*/
public function set_option($options = array()) { public function set_option($options = array()) {
if (!empty($options['api_key'])) { if (!empty($options['api_key'])) {
set_config('api_key', trim($options['api_key']), 'boxnet'); set_config('api_key', trim($options['api_key']), 'boxnet');
@ -94,13 +63,8 @@ class repository_boxnet extends repository {
return $ret; return $ret;
} }
/**
*
* @param <type> $config
* @return <type>
*/
public function get_option($config = '') { public function get_option($config = '') {
if ($config==='api_key') { if($config==='api_key') {
return trim(get_config('boxnet', 'api_key')); return trim(get_config('boxnet', 'api_key'));
} else { } else {
$options['api_key'] = trim(get_config('boxnet', 'api_key')); $options['api_key'] = trim(get_config('boxnet', 'api_key'));
@ -109,29 +73,17 @@ class repository_boxnet extends repository {
return $options; return $options;
} }
/**
*
* @global <type> $SESSION
* @return <type>
*/
public function global_search() { public function global_search() {
global $SESSION; global $SESSION;
$sess_name = 'box_token'.$this->id; if (empty($SESSION->{$this->sess_name})) {
if (empty($SESSION->$sess_name)) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
/**
*
* @global <type> $DB
* @return <type>
*/
public function get_login() { public function get_login() {
global $DB; global $DB;
if ($entry = $DB->get_record('repository_instances', array('id'=>$this->id))) { if ($entry = $DB->get_record('repository_instances', array('id'=>$this->id))) {
$ret->username = $entry->username; $ret->username = $entry->username;
$ret->password = $entry->password; $ret->password = $entry->password;
@ -141,61 +93,46 @@ class repository_boxnet extends repository {
} }
return $ret; return $ret;
} }
public function search($search_text) {
/** global $CFG;
*
* @global <type> $CFG
* @global <type> $SESSION
* @param <type> $path
* @param <type> $search
* @return <type>
*/
public function get_listing($path = '/', $search = '') {
global $CFG, $SESSION;
$list = array(); $list = array();
$ret = array(); $ret = array();
if (!empty($search)) { $tree = $this->box->getAccountTree();
$tree = $this->box->getAccountTree(); if (!empty($tree)) {
if (!empty($tree)) { $filenames = $tree['file_name'];
$filenames = $tree['file_name']; $fileids = $tree['file_id'];
$fileids = $tree['file_id']; $filesizes = $tree['file_size'];
$filesizes = $tree['file_size']; $filedates = $tree['file_date'];
$filedates = $tree['file_date']; $fileicon = $tree['thumbnail'];
$fileicon = $tree['thumbnail']; foreach ($filenames as $n=>$v){
foreach ($filenames as $n=>$v) { if(strstr($v, $search_text) !== false) {
if (strstr($v, $search) !== false) { $list[] = array('title'=>$v,
$list[] = array('title'=>$v, 'size'=>$filesizes[$n],
'size'=>$filesizes[$n], 'date'=>$filedates[$n],
'date'=>$filedates[$n], 'source'=>'http://box.net/api/1.0/download/'
'source'=>'http://box.net/api/1.0/download/' .$this->options['auth_token'].'/'.$fileids[$n],
.$this->options['auth_token'].'/'.$fileids[$n], 'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
}
} }
} }
$ret['list'] = $list;
return $ret;
} }
$ret['list'] = $list;
return $ret;
}
public function get_listing($path = '/'){
global $CFG;
$list = array();
$ret = array();
$ret['list'] = array();
$tree = $this->box->getfiletree($path); $tree = $this->box->getfiletree($path);
if (!empty($tree)) { $ret['manage'] = 'http://www.box.net/files';
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
if(!empty($tree)) {
$ret['list'] = $tree; $ret['list'] = $tree;
$ret['manage'] = 'http://www.box.net/files';
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
$this->listing = $tree;
return $ret;
} else {
$sess_name = 'box_token'.$this->id;
unset($SESSION->$sess_name);
throw new repository_exception('nullfilelist', 'repository_boxnet');
} }
return $ret;
} }
/** public function print_login(){
*
* @return <type>
*/
public function print_login() {
$t = $this->box->getTicket(); $t = $this->box->getTicket();
$ret = $this->get_login(); $ret = $this->get_login();
if ($this->options['ajax']) { if ($this->options['ajax']) {
@ -220,58 +157,26 @@ class repository_boxnet extends repository {
} }
} }
/**
*
* @return <type>
*/
public function print_search() {
return false;
}
/**
*
* @return <type>
*/
public static function has_admin_config() { public static function has_admin_config() {
return true; return true;
} }
/**
*
* @return <type>
*/
public static function has_instance_config() { public static function has_instance_config() {
return false; return false;
} }
/**
*
* @return <type>
*/
public static function has_multiple_instances() { public static function has_multiple_instances() {
return false; return false;
} }
/**
*
* @return <type>
*/
public static function get_admin_option_names() { public static function get_admin_option_names() {
return array('api_key'); return array('api_key');
} }
/**
*
* @return <type>
*/
public static function get_instance_option_names() { public static function get_instance_option_names() {
return array('share_url'); return array('share_url');
} }
/**
*
* @param <type> $
*/
public function admin_config_form(&$mform) { public function admin_config_form(&$mform) {
$public_account = get_config('boxnet', 'public_account'); $public_account = get_config('boxnet', 'public_account');
$api_key = get_config('boxnet', 'api_key'); $api_key = get_config('boxnet', 'api_key');
@ -283,10 +188,6 @@ class repository_boxnet extends repository {
$mform->addRule('api_key', $strrequired, 'required', null, 'client'); $mform->addRule('api_key', $strrequired, 'required', null, 'client');
} }
/**
*
* @param <type> $
*/
public function instance_config_form(&$mform) { public function instance_config_form(&$mform) {
//$share_url = get_config('boxnet', 'share_url'); //$share_url = get_config('boxnet', 'share_url');
$mform->addElement('text', 'share_url', get_string('shareurl', 'repository_boxnet')); $mform->addElement('text', 'share_url', get_string('shareurl', 'repository_boxnet'));

View File

@ -10,24 +10,12 @@
require_once($CFG->libdir.'/flickrlib.php'); require_once($CFG->libdir.'/flickrlib.php');
/**
*
*/
class repository_flickr extends repository { class repository_flickr extends repository {
private $flickr; private $flickr;
public $photos; public $photos;
/**
*
* @global <type> $SESSION
* @global <type> $CFG
* @param <type> $repositoryid
* @param <type> $context
* @param <type> $options
*/
public function __construct($repositoryid, $context = SITEID, $options = array()) { public function __construct($repositoryid, $context = SITEID, $options = array()) {
global $SESSION, $CFG; global $SESSION, $CFG;
$options['page'] = optional_param('p', 1, PARAM_INT); $options['page'] = optional_param('p', 1, PARAM_INT);
parent::__construct($repositoryid, $context, $options); parent::__construct($repositoryid, $context, $options);
@ -41,32 +29,19 @@ class repository_flickr extends repository {
$this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token); $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
if (empty($this->token)) { $frob = optional_param('frob', '', PARAM_RAW);
$frob = optional_param('frob', '', PARAM_RAW); if (empty($this->token) && !empty($frob)) {
if (!empty($frob)) { $auth_info = $this->flickr->auth_getToken($frob);
$auth_info = $this->flickr->auth_getToken($frob); $this->token = $auth_info['token'];
$this->token = $auth_info['token']; $this->nsid = $auth_info['user']['nsid'];
$this->nsid = $auth_info['user']['nsid']; set_user_preference($this->setting, $auth_info['token']);
set_user_preference($this->setting, $auth_info['token']); set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
$this->perm = $auth_info['token'];
}
} }
} }
/**
*
* @return <type>
*/
public function check_login() { public function check_login() {
return !empty($this->token); return !empty($this->token);
} }
/**
*
* @return <type>
*/
public function logout() { public function logout() {
set_user_preference($this->setting, ''); set_user_preference($this->setting, '');
set_user_preference($this->setting.'_nsid', ''); set_user_preference($this->setting.'_nsid', '');
@ -74,12 +49,6 @@ class repository_flickr extends repository {
$this->nsid = ''; $this->nsid = '';
return $this->print_login(); return $this->print_login();
} }
/**
*
* @param <type> $options
* @return <type>
*/
public function set_option($options = array()) { public function set_option($options = array()) {
if (!empty($options['api_key'])) { if (!empty($options['api_key'])) {
set_config('api_key', trim($options['api_key']), 'flickr'); set_config('api_key', trim($options['api_key']), 'flickr');
@ -93,11 +62,6 @@ class repository_flickr extends repository {
return $ret; return $ret;
} }
/**
*
* @param <type> $config
* @return <type>
*/
public function get_option($config = '') { public function get_option($config = '') {
if ($config==='api_key') { if ($config==='api_key') {
return trim(get_config('flickr', 'api_key')); return trim(get_config('flickr', 'api_key'));
@ -111,31 +75,15 @@ class repository_flickr extends repository {
return $options; return $options;
} }
/**
*
* @global <type> $SESSION
* @return <type>
*/
public function global_search() { public function global_search() {
global $SESSION;
if (empty($this->token)) { if (empty($this->token)) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
/**
*
* @global <type> $SESSION
* @param <type> $ajax
* @return <type>
*/
public function print_login($ajax = true) { public function print_login($ajax = true) {
global $SESSION; if ($ajax) {
if($ajax){
$ret = array(); $ret = array();
$popup_btn = new stdclass; $popup_btn = new stdclass;
$popup_btn->type = 'popup'; $popup_btn->type = 'popup';
@ -144,48 +92,20 @@ class repository_flickr extends repository {
return $ret; return $ret;
} }
} }
private function build_list($photos, $path = 1) {
/** $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
*
* @global <type> $SESSION
* @param <type> $path
* @param <type> $search
* @return <type>
*/
public function get_listing($path = '1', $search = '') {
global $SESSION;
$nsid = get_user_preferences($this->setting.'_nsid');
$photos_url = $this->flickr->urls_getUserPhotos($nsid);
if (!empty($search)) {
$photos = $this->flickr->photos_search(array(
'user_id'=>$nsid,
'per_page'=>25,
'page'=>$path,
'extras'=>'original_format',
'text'=>$search
));
} elseif (!empty($path) && empty($search)) {
$photos = $this->flickr->photos_search(array(
'user_id'=>$nsid,
'per_page'=>25,
'page'=>$path,
'extras'=>'original_format'
));
}
$ret = array(); $ret = array();
$ret['manage'] = $photos_url; $ret['manage'] = $photos_url;
$ret['list'] = array(); $ret['list'] = array();
$ret['pages'] = $photos['pages']; $ret['pages'] = $photos['pages'];
if (is_int($path) && $path <= $ret['pages']) { if(is_int($path) && $path <= $ret['pages']) {
$ret['page'] = $path; $ret['page'] = $path;
} else { } else {
$ret['page'] = 1; $ret['page'] = 1;
} }
if (!empty($photos['photo'])) { if (!empty($photos['photo'])) {
foreach ($photos['photo'] as $p) { foreach ($photos['photo'] as $p) {
if (empty($p['title'])) { if(empty($p['title'])) {
$p['title'] = get_string('notitle', 'repository_flickr'); $p['title'] = get_string('notitle', 'repository_flickr');
} }
if (isset($p['originalformat'])) { if (isset($p['originalformat'])) {
@ -193,52 +113,45 @@ class repository_flickr extends repository {
} else { } else {
$format = 'jpg'; $format = 'jpg';
} }
$ret['list'][] = $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']); 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
} }
} }
if (empty($ret)) { return $ret;
throw new repository_exception('nullphotolist', 'repository_flickr');
} else {
return $ret;
}
} }
public function search($search_text) {
$photos = $this->flickr->photos_search(array(
'user_id'=>$this->nsid,
'per_page'=>25,
'extras'=>'original_format',
'text'=>$search_text
));
return $this->build_list($photos);
}
public function get_listing($path = '1') {
$photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
/** $photos = $this->flickr->photos_search(array(
* 'user_id'=>$this->nsid,
* @return <type> 'per_page'=>25,
*/ 'page'=>$path,
'extras'=>'original_format'
));
return $this->build_list($photos, $path);
}
public function print_listing() { public function print_listing() {
return false; return false;
} }
/**
*
* @return <type>
*/
public function print_search() {
echo '<input name="s" value="" />';
parent::print_search();
return true;
}
/**
*
* @global <type> $CFG
* @param <type> $photo_id
* @param <type> $file
* @return <type>
*/
public function get_file($photo_id, $file = '') { public function get_file($photo_id, $file = '') {
global $CFG; global $CFG;
$result = $this->flickr->photos_getSizes($photo_id); $result = $this->flickr->photos_getSizes($photo_id);
$url = ''; $url = '';
if (!empty($result[4])) { if(!empty($result[4])) {
$url = $result[4]['source']; $url = $result[4]['source'];
} elseif (!empty($result[3])) { } elseif(!empty($result[3])) {
$url = $result[3]['source']; $url = $result[3]['source'];
} elseif (!empty($result[2])) { } elseif(!empty($result[2])) {
$url = $result[2]['source']; $url = $result[2]['source'];
} }
if (!file_exists($CFG->dataroot.'/repository/download')) { if (!file_exists($CFG->dataroot.'/repository/download')) {
@ -248,10 +161,10 @@ class repository_flickr extends repository {
$dir = $CFG->dataroot.'/repository/download/'; $dir = $CFG->dataroot.'/repository/download/';
} }
if (empty($file)) { if(empty($file)) {
$file = $photo_id.'_'.time().'.jpg'; $file = $photo_id.'_'.time().'.jpg';
} }
if (file_exists($dir.$file)) { if(file_exists($dir.$file)) {
$file = uniqid('m').$file; $file = uniqid('m').$file;
} }
$fp = fopen($dir.$file, 'w'); $fp = fopen($dir.$file, 'w');
@ -261,56 +174,29 @@ class repository_flickr extends repository {
)); ));
return $dir.$file; return $dir.$file;
} }
/**
*
* @return <type>
*/
public static function has_admin_config() { public static function has_admin_config() {
return true; return true;
} }
/**
*
* @return <type>
*/
public static function has_multiple_instances() { public static function has_multiple_instances() {
return false; return false;
} }
/**
*
* @return <type>
*/
public static function has_instance_config() { public static function has_instance_config() {
return false; return false;
} }
/**
*
* @param <type> $
*/
public function instance_config_form(&$mform) { public function instance_config_form(&$mform) {
$mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr')); $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr'));
$mform->addRule('email_address', get_string('required'), 'required', null, 'client'); $mform->addRule('email_address', get_string('required'), 'required', null, 'client');
} }
/**
*
* @return <type>
*/
public static function get_instance_option_names() { public static function get_instance_option_names() {
return array('email_address'); return array('email_address');
} }
/**
*
* @global <type> $CFG
* @param <type> $
*/
public function admin_config_form(&$mform) { public function admin_config_form(&$mform) {
global $CFG; global $CFG;
$api_key = get_config('flickr', 'api_key'); $api_key = get_config('flickr', 'api_key');
$secret = get_config('flickr', 'secret'); $secret = get_config('flickr', 'secret');
@ -339,10 +225,6 @@ class repository_flickr extends repository {
$mform->addRule('secret', $strrequired, 'required', null, 'client'); $mform->addRule('secret', $strrequired, 'required', null, 'client');
} }
/**
*
* @return <type>
*/
public static function get_admin_option_names() { public static function get_admin_option_names() {
return array('api_key', 'secret'); return array('api_key', 'secret');
} }

View File

@ -12,18 +12,10 @@
require_once($CFG->libdir.'/flickrlib.php'); require_once($CFG->libdir.'/flickrlib.php');
/**
*
*/
class repository_flickr_public extends repository { class repository_flickr_public extends repository {
private $flickr; private $flickr;
public $photos; public $photos;
/**
*
* @param <type> $options
* @return <type>
*/
public function set_option($options = array()) { public function set_option($options = array()) {
if (!empty($options['api_key'])) { if (!empty($options['api_key'])) {
set_config('api_key', trim($options['api_key']), 'flickr_public'); set_config('api_key', trim($options['api_key']), 'flickr_public');
@ -33,11 +25,6 @@ class repository_flickr_public extends repository {
return $ret; return $ret;
} }
/**
*
* @param <type> $config
* @return <type>
*/
public function get_option($config = '') { public function get_option($config = '') {
if ($config==='api_key') { if ($config==='api_key') {
return trim(get_config('flickr_public', 'api_key')); return trim(get_config('flickr_public', 'api_key'));
@ -48,10 +35,6 @@ class repository_flickr_public extends repository {
return $options; return $options;
} }
/**
*
* @return <type>
*/
public function global_search() { public function global_search() {
if (empty($this->flickr_account)) { if (empty($this->flickr_account)) {
return false; return false;
@ -60,23 +43,16 @@ class repository_flickr_public extends repository {
} }
} }
/**
*
* @global <type> $CFG
* @param <type> $repositoryid
* @param <type> $context
* @param <type> $options
*/
public function __construct($repositoryid, $context = SITEID, $options = array()) { public function __construct($repositoryid, $context = SITEID, $options = array()) {
global $CFG; global $CFG;
$options['page'] = optional_param('p', 1, PARAM_INT);
$options['page'] = optional_param('p', 1, PARAM_INT);
parent::__construct($repositoryid, $context, $options); parent::__construct($repositoryid, $context, $options);
$this->api_key = $this->get_option('api_key'); $this->api_key = $this->get_option('api_key');
$this->flickr = new phpFlickr($this->api_key); $this->flickr = new phpFlickr($this->api_key);
$this->flickr_account = $this->get_option('email_address'); $this->flickr_account = $this->get_option('email_address');
// when flickr account hasn't been set by admin, user can
// submit a flickr account here.
$account = optional_param('flickr_account', '', PARAM_RAW); $account = optional_param('flickr_account', '', PARAM_RAW);
if (!empty($account)) { if (!empty($account)) {
$people = $this->flickr->people_findByEmail($account); $people = $this->flickr->people_findByEmail($account);
@ -87,20 +63,9 @@ class repository_flickr_public extends repository {
} }
} }
} }
/**
*
* @return <type>
*/
public function check_login() { public function check_login() {
return !empty($this->flickr_account); return !empty($this->flickr_account);
} }
/**
*
* @param <type> $ajax
* @return <type>
*/
public function print_login($ajax = true) { public function print_login($ajax = true) {
if ($ajax) { if ($ajax) {
$ret = array(); $ret = array();
@ -112,130 +77,82 @@ class repository_flickr_public extends repository {
return $ret; return $ret;
} }
} }
public function search($search_text) {
/**
*
* @return <type>
*/
public function search() {
$people = $this->flickr->people_findByEmail($this->flickr_account); $people = $this->flickr->people_findByEmail($this->flickr_account);
$tag = optional_param('tag', '', PARAM_CLEANHTML); $this->nsid = $people['nsid'];
$search = optional_param('s', '', PARAM_CLEANHTML); $tag = optional_param('tag', '', PARAM_CLEANHTML);
if (!empty($tag)) { if (!empty($tag)) {
$photos = $this->flickr->photos_search(array( $photos = $this->flickr->photos_search(array(
'tags'=>$tag 'tags'=>$tag
)); ));
} else { } else {
$photos = $this->flickr->photos_search(array( $photos = $this->flickr->photos_search(array(
'user_id'=>$people['nsid'], 'user_id'=>$this->nsid,
'text'=>$search)); 'text'=>$search_text));
}
$ret = array();
$ret['list'] = array();
$ret['nologin'] = true;
$ret['pages'] = $photos['pages'];
foreach ($photos['photo'] as $p) {
if (empty($p['title'])) {
$p['title'] = get_string('notitle', 'repository_flickr_public');
}
if (isset($p['originalformat'])) {
$format = $p['originalformat'];
} else {
$format = 'jpg';
}
$ret['list'][] =
array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
}
if (empty($ret)) {
throw new repository_exception('nullphotolist', 'repository_flickr_public');
} else {
return $ret;
} }
return $this->build_list($photos);
} }
public function get_listing($path = '1') {
/**
*
* @param <type> $path
* @param <type> $search
* @return <type>
*/
public function get_listing($path = '1', $search = '') {
$people = $this->flickr->people_findByEmail($this->flickr_account); $people = $this->flickr->people_findByEmail($this->flickr_account);
$photos_url = $this->flickr->urls_getUserPhotos($people['nsid']); $this->nsid = $people['nsid'];
$photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path); $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
return $this->build_list($photos, $path);
}
private function build_list($photos, $path = 1) {
$photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
$ret = array(); $ret = array();
$ret['manage'] = $photos_url; $ret['manage'] = $photos_url;
$ret['list'] = array(); $ret['list'] = array();
$ret['nologin'] = true;
$ret['pages'] = $photos['pages']; $ret['pages'] = $photos['pages'];
if (is_int($path) && $path <= $ret['pages']) { if (is_int($path) && $path <= $ret['pages']) {
$ret['page'] = $path; $ret['page'] = $path;
} else { } else {
$ret['page'] = 1; $ret['page'] = 1;
} }
foreach ($photos['photo'] as $p) { if (!empty($photos['photo'])) {
if (empty($p['title'])) { foreach ($photos['photo'] as $p) {
$p['title'] = get_string('notitle', 'repository_flickr_public'); if(empty($p['title'])) {
$p['title'] = get_string('notitle', 'repository_flickr');
}
if (isset($p['originalformat'])) {
$format = $p['originalformat'];
} else {
$format = 'jpg';
}
$ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
} }
if (isset($p['originalformat'])) {
$format = $p['originalformat'];
} else {
$format = 'jpg';
}
$ret['list'][] =
array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
}
if (empty($ret)) {
throw new repository_exception('nullphotolist', 'repository_flickr_public');
} else {
return $ret;
} }
return $ret;
} }
/**
*
* @return <type>
*/
public function print_listing() { public function print_listing() {
return false; return false;
} }
/**
*
* @return <type>
*/
public function print_search() { public function print_search() {
parent::print_search(); parent::print_search();
echo '<label>Keyword: </label><input type="text" name="s" /><br />';
echo '<label>Tag: </label><input type="text" name="tag" /><br />'; echo '<label>Tag: </label><input type="text" name="tag" /><br />';
return true; return true;
} }
/**
*
* @global <type> $CFG
* @param <type> $photo_id
* @param <type> $file
* @return <type>
*/
public function get_file($photo_id, $file = '') { public function get_file($photo_id, $file = '') {
global $CFG; global $CFG;
$result = $this->flickr->photos_getSizes($photo_id); $result = $this->flickr->photos_getSizes($photo_id);
$url = ''; $url = '';
if (!empty($result[4])) { if (!empty($result[4])) {
$url = $result[4]['source']; $url = $result[4]['source'];
} elseif (!empty($result[3])) { } elseif(!empty($result[3])) {
$url = $result[3]['source']; $url = $result[3]['source'];
} elseif (!empty($result[2])) { } elseif(!empty($result[2])) {
$url = $result[2]['source']; $url = $result[2]['source'];
} }
if (!file_exists($CFG->dataroot.'/repository/download')) { if (!file_exists($CFG->dataroot.'/repository/download')) {
mkdir($CFG->dataroot.'/repository/download/', 0777, true); mkdir($CFG->dataroot.'/repository/download/', 0777, true);
} }
if (is_dir($CFG->dataroot.'/repository/download')) { if(is_dir($CFG->dataroot.'/repository/download')) {
$dir = $CFG->dataroot.'/repository/download/'; $dir = $CFG->dataroot.'/repository/download/';
} }
@ -247,57 +164,31 @@ class repository_flickr_public extends repository {
} }
$fp = fopen($dir.$file, 'w'); $fp = fopen($dir.$file, 'w');
$c = new curl; $c = new curl;
$c->download(array( $c->download(array(array('url'=>$url, 'file'=>$fp)));
array('url'=>$url, 'file'=>$fp)
));
return $dir.$file; return $dir.$file;
} }
/**
*
* @return <type>
*/
public static function has_admin_config() { public static function has_admin_config() {
return true; return true;
} }
/**
*
* @return <type>
*/
public static function has_multiple_instances() { public static function has_multiple_instances() {
return true; return true;
} }
/**
*
* @return <type>
*/
public static function has_instance_config() { public static function has_instance_config() {
return true; return true;
} }
/**
*
* @param <type> $
*/
public function instance_config_form(&$mform) { public function instance_config_form(&$mform) {
$mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public')); $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
//$mform->addRule('email_address', get_string('required'), 'required', null, 'client'); //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
} }
/**
*
* @return <type>
*/
public static function get_instance_option_names() { public static function get_instance_option_names() {
return array('email_address'); return array('email_address');
} }
/**
*
* @param <type> $
*/
public function admin_config_form(&$mform) { public function admin_config_form(&$mform) {
$api_key = get_config('flickr_public', 'api_key'); $api_key = get_config('flickr_public', 'api_key');
if (empty($api_key)) { if (empty($api_key)) {
@ -308,17 +199,10 @@ class repository_flickr_public extends repository {
$mform->addRule('api_key', $strrequired, 'required', null, 'client'); $mform->addRule('api_key', $strrequired, 'required', null, 'client');
} }
/**
*
* @return <type>
*/
public static function get_admin_option_names() { public static function get_admin_option_names() {
return array('api_key'); return array('api_key');
} }
/**
*
*/
public static function type_init() { public static function type_init() {
//here we create a default instances for this type //here we create a default instances for this type
} }

View File

@ -796,6 +796,7 @@ abstract class repository {
* 'manage' => (string) link to file manager, * 'manage' => (string) link to file manager,
* 'nologin' => (bool) requires login, * 'nologin' => (bool) requires login,
* 'nosearch' => (bool) no search link, * 'nosearch' => (bool) no search link,
* 'search_result' => (bool) this list is a searching result,
* 'upload' => array( // upload manager * 'upload' => array( // upload manager
* 'name' => (string) label of the form element, * 'name' => (string) label of the form element,
* 'id' => (string) id of the form element * 'id' => (string) id of the form element
@ -827,15 +828,19 @@ abstract class repository {
* @param string $search The text will be searched. * @param string $search The text will be searched.
* @return array the list of files, including meta infomation * @return array the list of files, including meta infomation
*/ */
abstract public function get_listing($parent = '/', $search = ''); abstract public function get_listing($parent = '/');
/** /**
* Search * Search files in repository
* When doing global search, $search_text will be used as
* keyword.
*
* @return mixed, see get_listing() * @return mixed, see get_listing()
*/ */
public function search() { public function search($search_text) {
$search = optional_param('s', '', PARAM_CLEANHTML); $list = array();
return $this->get_listing(null, $search); $list['list'] = array();
return false;
} }
/** /**
@ -872,6 +877,7 @@ abstract class repository {
echo '<input type="hidden" name="repo_id" value="'.$this->id.'" />'; echo '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
echo '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />'; echo '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
echo '<input type="hidden" name="seekey" value="'.sesskey().'" />'; echo '<input type="hidden" name="seekey" value="'.sesskey().'" />';
echo '<input name="s" value="" />';
return true; return true;
} }
@ -2011,7 +2017,7 @@ success: function(o) {
postmethod: 'async', postmethod: 'async',
width : "30em", width : "30em",
fixedcenter : true, fixedcenter : true,
zindex: 666667, zindex: 766667,
visible : false, visible : false,
constraintoviewport : true, constraintoviewport : true,
buttons : [ { text:"Submit",handler: function() { buttons : [ { text:"Submit",handler: function() {

View File

@ -9,7 +9,7 @@
class repository_local extends repository { class repository_local extends repository {
public function __construct($repositoryid, $context = SITEID, $options = array()){ public function __construct($repositoryid, $context = SITEID, $options = array()) {
global $SESSION, $action, $CFG; global $SESSION, $action, $CFG;
parent::__construct($repositoryid, $context, $options); parent::__construct($repositoryid, $context, $options);
// TODO: // TODO:
@ -40,6 +40,10 @@ class repository_local extends repository {
return array('filearea' => $filearea, 'path' => $path); return array('filearea' => $filearea, 'path' => $path);
} }
public function search($search_text) {
return $this->get_listing('', $search_text);
}
public function get_listing($encodedpath = '', $search = '') { public function get_listing($encodedpath = '', $search = '') {
global $CFG; global $CFG;
$ret = array(); $ret = array();
@ -180,10 +184,6 @@ class repository_local extends repository {
// will be used in non-javascript file picker // will be used in non-javascript file picker
} }
public function print_search() {
return true;
}
public static function has_admin_config() { public static function has_admin_config() {
return false; return false;
} }

View File

@ -16,6 +16,7 @@
$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); // context ID $ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); // context ID
$repo_id = optional_param('repo_id', 1, PARAM_INT); // repository ID $repo_id = optional_param('repo_id', 1, PARAM_INT); // repository ID
$callback = optional_param('callback', '', PARAM_CLEANHTML); $callback = optional_param('callback', '', PARAM_CLEANHTML);
$search_text = optional_param('s', '', PARAM_CLEANHTML);
/// Headers to make it not cacheable /// Headers to make it not cacheable
header("Cache-Control: no-cache, must-revalidate"); header("Cache-Control: no-cache, must-revalidate");
@ -40,7 +41,7 @@
foreach($repos as $repo){ foreach($repos as $repo){
if ($repo->global_search()) { if ($repo->global_search()) {
try { try {
$ret = $repo->get_listing(null, $search); $ret = $repo->search($search_text);
array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below
$tmp = array_merge($list, $ret['list']); $tmp = array_merge($list, $ret['list']);
$list = $tmp; $list = $tmp;
@ -141,7 +142,7 @@ EOD;
break; break;
case 'search': case 'search':
try { try {
echo json_encode($repo->search()); echo json_encode($repo->search($search_text));
} catch (repository_exception $e) { } catch (repository_exception $e) {
$err = new stdclass; $err = new stdclass;
$err->e = $e->getMessage(); $err->e = $e->getMessage();