From 837ebb781dce2f2f2bf8432cc9574f59b0bf2ee1 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Fri, 8 Aug 2008 03:31:51 +0000 Subject: [PATCH] MDL-13766 1. Create "local" plugin 2. Improve document --- .../flickr/lang/en_utf8/repository_local.php | 6 + repository/flickr/repository.class.php | 190 ++---------------- repository/lib.php | 75 +++++-- 3 files changed, 81 insertions(+), 190 deletions(-) create mode 100644 repository/flickr/lang/en_utf8/repository_local.php diff --git a/repository/flickr/lang/en_utf8/repository_local.php b/repository/flickr/lang/en_utf8/repository_local.php new file mode 100644 index 0000000000..00434444b0 --- /dev/null +++ b/repository/flickr/lang/en_utf8/repository_local.php @@ -0,0 +1,6 @@ +dirroot.'/repository/flickr/'.'phpFlickr.php'); - -class repository_flickr extends repository{ - private $flickr; - public $photos; - public $type = 'flickr'; +class repository_local extends repository{ + public $type = 'local'; 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); - $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'; - } - } - } - } + // get the parameter from client side + // $this->context can be used here. } public function print_login($ajax = true){ global $SESSION; - 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(); - } + // 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 = '1', $search = ''){ + public function get_listing($path = '/', $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->url = $photos_url; + $ret->upload = array('name'=>'attachment', 'id'=>'', 'url'=>''); $ret->list = array(); - $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'); - } + // 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('nullphotolist', 'repository_flickr'); + throw new repository_exception('emptyfilelist', 'repository_local'); } else { return $ret; } } public function print_listing(){ - 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; + // will be used in non-javascript file picker } 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/lib.php b/repository/lib.php index 65f70f1a4a..8c745575a5 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -141,9 +141,9 @@ abstract class repository { * * @param string $list * $list = array( - * array('name'=>'moodle.txt', 'size'=>12, 'path'=>'', 'date'=>''), - * array('name'=>'repository.txt', 'size'=>32, 'path'=>'', 'date'=>''), - * array('name'=>'forum.txt', 'size'=>82, 'path'=>'', 'date'=>''), + * array('title'=>'moodle.txt', 'size'=>12, 'source'=>'url', 'date'=>''), + * array('title'=>'repository.txt', 'size'=>32, 'source'=>'', 'date'=>''), + * array('title'=>'forum.txt', 'size'=>82, 'source'=>'', 'date'=>''), * ); * * @param boolean $print if printing the listing directly @@ -180,18 +180,6 @@ abstract class repository { /** * Show the login screen, if required * This is an abstract function, it must be overriden. - * The specific plug-in need to specify authentication types in database - * options field - * Imagine following cases: - * 1. no need of authentication - * 2. Use username and password to authenticate - * 3. Redirect to authentication page, in this case, the repository - * will callback moodle with following common parameters: - * (1) boolean callback To tell moodle this is a callback - * (2) int id Specify repository ID - * The callback page need to use these parameters to init - * the repository plug-ins correctly. Also, auth_token or ticket may - * attach in the callback url, these must be taken into account too. * */ abstract public function print_login(); @@ -209,7 +197,7 @@ abstract class repository { * @param string $username * @param string $password * @param string $userid The id of specific user - * @return array the list of files, including meta infomation + * @return int Id of the record */ public function store_login($username = '', $password = '', $userid = 1) { global $DB; @@ -259,6 +247,14 @@ class repository_exception extends moodle_exception { abstract class repository_listing { } +/** + * Save settings for repository instance + * + * @param int repository Id + * @param int from 1 to 5 + * @param array settings + * @return int Id of the record + */ function repository_set_option($id, $position, $config = array()){ global $DB; $repository = new stdclass; @@ -275,6 +271,14 @@ function repository_set_option($id, $position, $config = array()){ } return false; } + +/** + * Get settings for repository instance + * + * @param int repository Id + * @param int from 1 to 5 + * @return array Settings + */ function repository_get_option($id, $position){ global $DB; $entry = $DB->get_record('repository', array('id'=>$id)); @@ -283,6 +287,12 @@ function repository_get_option($id, $position){ return $ret; } +/** + * Get user's repositories + * + * @param object context object + * @return array repository list + */ function repository_user_instances($context){ global $DB, $CFG, $USER; $params = array(); @@ -303,6 +313,12 @@ function repository_user_instances($context){ return $repos; } +/** + * Get single repository instance + * + * @param int repository id + * @return object repository instance + */ function repository_instance($id){ global $DB, $CFG; @@ -314,6 +330,12 @@ function repository_instance($id){ $classname = 'repository_' . $instance->repositorytype; return new $classname($instance->id, $instance->contextid); } + +/** + * Get list of repository plugin + * + * @return array repository plugin list + */ function repository_get_plugins(){ global $CFG; $repo = $CFG->dirroot.'/repository/'; @@ -333,6 +355,14 @@ function repository_get_plugins(){ return $ret; } +/** + * Move file from download folder to file pool using FILE API + * + * @param string file path in download folder + * @param string file name + * @param int itemid to identify a file in filepool + * @return array information of file in file pool + */ function move_to_filepool($path, $name, $itemid) { global $DB, $CFG, $USER; $context = get_context_instance(CONTEXT_USER, $USER->id); @@ -361,8 +391,12 @@ function move_to_filepool($path, $name, $itemid) { } } -// TODO -// Need to pass contextid and contextlevel here +/** + * Return javascript to create file picker to browse repositories + * + * @param object context + * @return array + */ function get_repository_client($context){ global $CFG, $USER; $suffix = uniqid(); @@ -603,6 +637,11 @@ function get_repository_client($context){ } } + // TODO + // If _client.datasource.upload == true + // then create a iframe to upload file + // We may need a new page named repository/upload.php to process this. + _client.viewthumb = function(){ var panel = new YAHOO.util.Element('panel-$suffix'); _client.viewbar.check(1); -- 2.39.5