From 6a391ebfb47d3b6af8f647cbc16763031c4559fc Mon Sep 17 00:00:00 2001 From: dongsheng Date: Fri, 8 Aug 2008 03:47:14 +0000 Subject: [PATCH] MDL-13766, revert flickr/repository.class.php, commit local plugin. --- repository/flickr/repository.class.php | 191 ++++++++++++++++-- .../local/lang/en_utf8/repository_local.php | 6 + repository/local/repository.class.php | 48 +++++ 3 files changed, 226 insertions(+), 19 deletions(-) create mode 100644 repository/local/lang/en_utf8/repository_local.php create mode 100755 repository/local/repository.class.php diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index 33a8c04be16..c0d53c7ccc4 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -1,48 +1,201 @@ dirroot.'/repository/flickr/'.'phpFlickr.php'); + +class repository_flickr extends repository{ + private $flickr; + public $photos; + public $type = 'flickr'; public function __construct($repositoryid, $context = SITEID, $options = array()){ global $SESSION, $action, $CFG; + $options['page'] = optional_param('p', 1, PARAM_INT); + $options['api_key'] = 'bf85ae2b5b105a2c645f32a32cd6ad59'; + $options['secret'] = '7cb2f9d7cf70aebe'; parent::__construct($repositoryid, $context, $options); - // get the parameter from client side - // $this->context can be used here. + $this->flickr = new phpFlickr($this->options['api_key'], $this->options['secret']); + + $reset = optional_param('reset', 0, PARAM_INT); + if(!empty($reset)) { + // logout from flickr + unset($SESSION->flickrmail); + set_user_preference('flickrmail', ''); + } + + if(!empty($SESSION->flickrmail)) { + if(empty($action)) { + $action = 'list'; + } + } else { + // get flickr account + $options['flickrmail'] = optional_param('flickrmail', '', PARAM_RAW); + if(!empty($options['flickrmail'])) { + $people = $this->flickr->people_findByEmail($options['flickrmail']); + if(!empty($people)) { + $remember = optional_param('remember', '', PARAM_RAW); + if(!empty($remember)) { + set_user_preference('flickrmail', $options['flickrmail']); + } + $SESSION->flickrmail = $options['flickrmail']; + if(empty($action)) { + $action = 'list'; + } + } else { + throw new repository_exception('invalidemail', 'repository_flickr'); + } + } else { + if($account = get_user_preferences('flickrmail', '')){ + $SESSION->flickrmail = $account; + if(empty($action)) { + $action = 'list'; + } + } + } + } } public function print_login($ajax = true){ global $SESSION; - // TODO - // Return file list in moodle - // Also, this plugin should have ability to - // upload files in user's computer, a iframe - // need to be created. - return $this->get_listing(); + if(empty($SESSION->flickrmail)) { + $str =<< +
+ +
+ +

+ +EOD; + if($ajax){ + $ret = array(); + $e1->label = get_string('username', 'repository_flickr'); + $e1->id = 'account'; + $e1->type = 'text'; + $e1->name = 'flickrmail'; + + $e2->id = 'keepid'; + $e2->label = get_string('remember', 'repository_flickr'); + $e2->type = 'checkbox'; + $e2->name = 'remember'; + + $e3->type = 'hidden'; + $e3->name = 'repo_id'; + $e3->value = $this->repositoryid; + $ret['l'] = array($e1, $e2, $e3); + return $ret; + }else{ + echo $str; + } + } else { + return $this->get_listing(); + } } - public function get_listing($path = '/', $search = ''){ + public function get_listing($path = '1', $search = ''){ global $SESSION; + $people = $this->flickr->people_findByEmail($SESSION->flickrmail); + $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']); + + if(!empty($search)) { + // do searching, if $path is not empty, ignore it. + $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search)); + } elseif(!empty($path) && empty($search)) { + $photos = $this->flickr->people_getPublicPhotos($people['nsid'], null, 36, $path); + } + $ret = new stdclass; - $ret->upload = array('name'=>'attachment', 'id'=>'', 'url'=>''); + $ret->url = $photos_url; $ret->list = array(); - // call file api get the list of the file - $ret->list[] = array('title'=>'title','source'=>'download url', 'thumbnail'=>'url of thumbnail', 'date'=>'', 'size'=>'unknown'); + $ret->pages = $photos['pages']; + if(is_int($path) && $path <= $ret->pages) { + $ret->page = $path; + } else { + $ret->page = 1; + } + foreach ($photos['photo'] as $p) { + if(empty($p['title'])) { + $p['title'] = get_string('notitle', 'repository_flickr'); + } + $ret->list[] = + array('title'=>$p['title'],'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown'); + } if(empty($ret)) { - throw new repository_exception('emptyfilelist', 'repository_local'); + throw new repository_exception('nullphotolist', 'repository_flickr'); } else { return $ret; } } public function print_listing(){ - // will be used in non-javascript file picker + if(empty($this->photos)){ + $this->get_listing(); + } + $str = ''; + $str .= '

Account: '.$this->photos['a'].'

'; + foreach ((array)$this->photos['photo'] as $photo) { + $str .= ""; + $str .= "$photo[title]"; + $str .= ""; + $i++; + + if ($i % 4 == 0) { + $str .= "
"; + } + } + $str .= << +#paging{margin-top: 10px; clear:both} +#paging a{padding: 4px; border: 1px solid gray} + +EOD; + $str .= '
'; + for($i=1; $i <= $this->photos['pages']; $i++) { + $str .= ''; + $str .= $i; + $str .= ' '; + } + $str .= '
'; + echo $str; } public function print_search(){ + echo ''; return true; } + public function get_file($photo_id, $file = ''){ + 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']; + } + if (!file_exists($CFG->dataroot.'/repository/download')) { + mkdir($CFG->dataroot.'/repository/download/', 0777, true); + } + if(is_dir($CFG->dataroot.'/repository/download')) { + $dir = $CFG->dataroot.'/repository/download/'; + } + + if(empty($file)) { + $file = $photo_id.'_'.time().'.jpg'; + } + if(file_exists($dir.$file)) { + $file = uniqid('m').$file; + } + $fp = fopen($dir.$file, 'w'); + $c = new curl; + $c->download(array( + array('url'=>$url, 'file'=>$fp) + )); + return $dir.$file; + } } -?> diff --git a/repository/local/lang/en_utf8/repository_local.php b/repository/local/lang/en_utf8/repository_local.php new file mode 100644 index 00000000000..00434444b00 --- /dev/null +++ b/repository/local/lang/en_utf8/repository_local.php @@ -0,0 +1,6 @@ +context can be used here. + } + public function print_login($ajax = true){ + global $SESSION; + // TODO + // Return file list in moodle + // Also, this plugin should have ability to + // upload files in user's computer, a iframe + // need to be created. + return $this->get_listing(); + } + public function get_listing($path = '/', $search = ''){ + global $SESSION; + $ret = new stdclass; + $ret->upload = array('name'=>'attachment', 'id'=>'', 'url'=>''); + $ret->list = array(); + // call file api get the list of the file + $ret->list[] = array('title'=>'title','source'=>'download url', 'thumbnail'=>'url of thumbnail', 'date'=>'', 'size'=>'unknown'); + if(empty($ret)) { + throw new repository_exception('emptyfilelist', 'repository_local'); + } else { + return $ret; + } + } + public function print_listing(){ + // will be used in non-javascript file picker + } + public function print_search(){ + return true; + } +} +?>