2008-09-12 03:32:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* repository_flickr_public class
|
|
|
|
* This one is used to create public repository
|
|
|
|
* You can set up a public account in admin page, so everyone can
|
|
|
|
* access photos in this public account
|
|
|
|
*
|
|
|
|
* @author Dongsheng Cai <dongsheng@moodle.com>
|
|
|
|
* @version $Id$
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once($CFG->libdir.'/flickrlib.php');
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
class repository_flickr_public extends repository {
|
2008-09-12 03:32:31 +00:00
|
|
|
private $flickr;
|
|
|
|
public $photos;
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-11-21 04:19:51 +00:00
|
|
|
* save api_key in config table
|
|
|
|
* @param array $options
|
|
|
|
* @return boolean
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function set_option($options = array()) {
|
2008-09-12 03:32:31 +00:00
|
|
|
if (!empty($options['api_key'])) {
|
|
|
|
set_config('api_key', trim($options['api_key']), 'flickr_public');
|
|
|
|
}
|
|
|
|
unset($options['api_key']);
|
|
|
|
$ret = parent::set_option($options);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-11-21 04:19:51 +00:00
|
|
|
* get api_key from config table
|
|
|
|
* @param string $config
|
|
|
|
* @return mixed
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function get_option($config = '') {
|
|
|
|
if ($config==='api_key') {
|
2008-09-12 03:32:31 +00:00
|
|
|
return trim(get_config('flickr_public', 'api_key'));
|
|
|
|
} else {
|
|
|
|
$options['api_key'] = trim(get_config('flickr_public', 'api_key'));
|
|
|
|
}
|
|
|
|
$options = parent::get_option($config);
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-11-21 04:19:51 +00:00
|
|
|
* is global_search available?
|
|
|
|
* @return boolean
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function global_search() {
|
2008-09-12 03:32:31 +00:00
|
|
|
if (empty($this->flickr_account)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2008-11-21 04:19:51 +00:00
|
|
|
* @global object $CFG
|
|
|
|
* @param int $repositoryid
|
|
|
|
* @param int $context
|
|
|
|
* @param array $options
|
|
|
|
* @param boolean $readonly
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 09:08:36 +00:00
|
|
|
public function __construct($repositoryid, $context = SITEID, $options = array(), $readonly=0) {
|
2008-11-21 04:19:51 +00:00
|
|
|
global $CFG, $SESSION;
|
2008-09-16 09:08:36 +00:00
|
|
|
parent::__construct($repositoryid, $context, $options,$readonly);
|
2008-09-12 03:32:31 +00:00
|
|
|
$this->api_key = $this->get_option('api_key');
|
2008-11-21 04:19:51 +00:00
|
|
|
$this->flickr = new phpFlickr($this->api_key);
|
2008-09-15 09:21:29 +00:00
|
|
|
$this->flickr_account = $this->get_option('email_address');
|
2008-09-12 03:32:31 +00:00
|
|
|
|
2008-11-21 04:19:51 +00:00
|
|
|
$account = optional_param('flickr_account', '', PARAM_RAW);
|
|
|
|
$fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
|
|
|
|
$tag = optional_param('flickr_tag', '', PARAM_RAW);
|
|
|
|
$this->sess_account = 'flickr_public_'.$this->id.'_account';
|
|
|
|
$this->sess_tag = 'flickr_public_'.$this->id.'_tag';
|
|
|
|
$this->sess_text = 'flickr_public_'.$this->id.'_text';
|
|
|
|
if (!empty($account) or !empty($fulltext) or !empty($tag)) {
|
|
|
|
$SESSION->{$this->sess_account} = $account;
|
|
|
|
$SESSION->{$this->sess_tag} = $tag;
|
|
|
|
$SESSION->{$this->sess_text} = $fulltext;
|
2008-11-21 07:07:04 +00:00
|
|
|
$response = $this->search($fulltext);
|
|
|
|
$response['search_result'] = true;
|
|
|
|
echo json_encode($response);
|
2008-11-21 04:19:51 +00:00
|
|
|
exit;
|
2008-09-22 01:30:14 +00:00
|
|
|
}
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
|
|
|
/**
|
2008-11-21 04:19:51 +00:00
|
|
|
* check flickr account
|
|
|
|
* @return boolean
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function check_login() {
|
2008-09-15 09:31:41 +00:00
|
|
|
return !empty($this->flickr_account);
|
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2008-11-21 04:19:51 +00:00
|
|
|
* @param boolean $ajax
|
|
|
|
* @return array
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function print_login($ajax = true) {
|
|
|
|
if ($ajax) {
|
2008-09-15 09:31:41 +00:00
|
|
|
$ret = array();
|
2008-11-21 04:19:51 +00:00
|
|
|
$fulltext = new stdclass;
|
|
|
|
$fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
|
|
|
|
$fulltext->id = 'el_fulltext';
|
|
|
|
$fulltext->type = 'text';
|
|
|
|
$fulltext->name = 'flickr_fulltext';
|
|
|
|
|
|
|
|
$tag = new stdclass;
|
|
|
|
$tag->label = get_string('tag', 'repository_flickr_public').': ';
|
|
|
|
$tag->id = 'el_tag';
|
|
|
|
$tag->type = 'text';
|
|
|
|
$tag->name = 'flickr_tag';
|
|
|
|
|
|
|
|
$email_field = new stdclass;
|
2008-09-15 09:42:38 +00:00
|
|
|
$email_field->label = get_string('username', 'repository_flickr_public').': ';
|
|
|
|
$email_field->id = 'account';
|
|
|
|
$email_field->type = 'text';
|
|
|
|
$email_field->name = 'flickr_account';
|
2008-11-21 04:19:51 +00:00
|
|
|
$ret['login'] = array($fulltext, $tag, $email_field);
|
2008-09-15 09:31:41 +00:00
|
|
|
return $ret;
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
2008-09-22 01:30:14 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return <type>
|
|
|
|
*/
|
|
|
|
public function logout() {
|
2008-11-21 04:19:51 +00:00
|
|
|
global $SESSION;
|
|
|
|
unset($SESSION->{$this->sess_tag});
|
|
|
|
unset($SESSION->{$this->sess_text});
|
|
|
|
unset($SESSION->{$this->sess_account});
|
2008-09-22 01:30:14 +00:00
|
|
|
return $this->print_login();
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param <type> $search_text
|
|
|
|
* @return <type>
|
|
|
|
*/
|
2008-09-16 03:11:17 +00:00
|
|
|
public function search($search_text) {
|
2008-09-19 04:44:37 +00:00
|
|
|
global $SESSION;
|
2008-11-21 04:19:51 +00:00
|
|
|
if (!empty($this->flickr_account)) {
|
|
|
|
$people = $this->flickr->people_findByEmail($this->flickr_account);
|
|
|
|
$this->nsid = $people['nsid'];
|
|
|
|
}
|
|
|
|
if (!empty($SESSION->{$this->sess_account})) {
|
|
|
|
$people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
|
|
|
|
$this->nsid = $people['nsid'];
|
|
|
|
}
|
|
|
|
if (empty($this->nsid)) {
|
|
|
|
$this->nsid = null;
|
|
|
|
}
|
2008-09-19 04:44:37 +00:00
|
|
|
$is_paging = optional_param('search_paging', '', PARAM_RAW);
|
|
|
|
if (!empty($is_paging)) {
|
|
|
|
$page = optional_param('p', '', PARAM_INT);
|
2008-09-12 08:16:09 +00:00
|
|
|
} else {
|
2008-11-21 04:19:51 +00:00
|
|
|
$page = 1;
|
|
|
|
}
|
|
|
|
if (!empty($SESSION->{$this->sess_tag}) or !empty($SESSION->{$this->sess_text})
|
|
|
|
or !empty($SESSION->{$this->sess_account})
|
|
|
|
or !empty($this->nsid)) {
|
2008-11-21 07:07:04 +00:00
|
|
|
|
2008-09-12 08:16:09 +00:00
|
|
|
$photos = $this->flickr->photos_search(array(
|
2008-11-21 04:19:51 +00:00
|
|
|
'tags'=>$SESSION->{$this->sess_tag},
|
|
|
|
'page'=>$page,
|
2008-11-24 05:20:29 +00:00
|
|
|
'per_page'=>24,
|
2008-09-16 03:11:17 +00:00
|
|
|
'user_id'=>$this->nsid,
|
2008-11-21 07:07:04 +00:00
|
|
|
'text'=>$SESSION->{$this->sess_text}));
|
2008-09-12 08:16:09 +00:00
|
|
|
}
|
2008-09-19 04:44:37 +00:00
|
|
|
$ret = array();
|
2008-09-24 06:25:22 +00:00
|
|
|
return $this->build_list($photos, $page, $ret);
|
2008-09-12 08:16:09 +00:00
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2009-02-18 06:52:54 +00:00
|
|
|
* @param string $path
|
|
|
|
* @param int $page
|
2008-09-18 05:33:44 +00:00
|
|
|
* @return <type>
|
|
|
|
*/
|
2009-02-18 06:52:54 +00:00
|
|
|
public function get_listing($path = '', $page = 1) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$people = $this->flickr->people_findByEmail($this->flickr_account);
|
2008-09-16 03:11:17 +00:00
|
|
|
$this->nsid = $people['nsid'];
|
2009-02-18 06:52:54 +00:00
|
|
|
$photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
|
2008-09-19 04:44:37 +00:00
|
|
|
$ret = array();
|
2008-09-12 03:32:31 +00:00
|
|
|
|
2009-02-18 06:52:54 +00:00
|
|
|
return $this->build_list($photos, $page, $ret);
|
2008-09-16 03:11:17 +00:00
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param <type> $photos
|
2009-02-18 06:52:54 +00:00
|
|
|
* @param <type> $page
|
2008-09-18 05:33:44 +00:00
|
|
|
* @return <type>
|
|
|
|
*/
|
2009-02-18 06:52:54 +00:00
|
|
|
private function build_list($photos, $page = 1, &$ret) {
|
2008-11-21 04:19:51 +00:00
|
|
|
if (!empty($this->nsid)) {
|
|
|
|
$photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
|
|
|
|
$ret['manage'] = $photos_url;
|
|
|
|
}
|
2008-09-12 03:32:31 +00:00
|
|
|
$ret['list'] = array();
|
|
|
|
$ret['pages'] = $photos['pages'];
|
2009-02-18 06:52:54 +00:00
|
|
|
if (is_int($page) && $page <= $ret['pages']) {
|
|
|
|
$ret['page'] = $page;
|
2008-09-12 03:32:31 +00:00
|
|
|
} else {
|
|
|
|
$ret['page'] = 1;
|
|
|
|
}
|
2008-09-16 03:11:17 +00:00
|
|
|
if (!empty($photos['photo'])) {
|
|
|
|
foreach ($photos['photo'] as $p) {
|
|
|
|
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'),
|
2008-09-22 06:32:52 +00:00
|
|
|
'date'=>'', 'size'=>'unknown', 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id']);
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-16 03:11:17 +00:00
|
|
|
return $ret;
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
2008-09-16 02:01:59 +00:00
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return <type>
|
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function print_listing() {
|
2008-09-12 03:32:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-16 02:01:59 +00:00
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return <type>
|
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function print_search() {
|
2008-11-21 04:19:51 +00:00
|
|
|
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="seekey" value="'.sesskey().'" />';
|
|
|
|
echo '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
|
|
|
|
echo '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="tag" /><br />';
|
2008-09-12 07:28:40 +00:00
|
|
|
return true;
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
2008-09-16 02:01:59 +00:00
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @global <type> $CFG
|
|
|
|
* @param <type> $photo_id
|
|
|
|
* @param <type> $file
|
|
|
|
* @return <type>
|
|
|
|
*/
|
2008-09-16 02:01:59 +00:00
|
|
|
public function get_file($photo_id, $file = '') {
|
2008-09-12 03:32:31 +00:00
|
|
|
global $CFG;
|
|
|
|
$result = $this->flickr->photos_getSizes($photo_id);
|
|
|
|
$url = '';
|
2008-09-16 02:01:59 +00:00
|
|
|
if (!empty($result[4])) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$url = $result[4]['source'];
|
2008-09-16 03:11:17 +00:00
|
|
|
} elseif(!empty($result[3])) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$url = $result[3]['source'];
|
2008-09-16 03:11:17 +00:00
|
|
|
} elseif(!empty($result[2])) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$url = $result[2]['source'];
|
|
|
|
}
|
|
|
|
if (!file_exists($CFG->dataroot.'/repository/download')) {
|
|
|
|
mkdir($CFG->dataroot.'/repository/download/', 0777, true);
|
|
|
|
}
|
2008-09-16 03:11:17 +00:00
|
|
|
if(is_dir($CFG->dataroot.'/repository/download')) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$dir = $CFG->dataroot.'/repository/download/';
|
|
|
|
}
|
|
|
|
|
2008-09-16 02:01:59 +00:00
|
|
|
if (empty($file)) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$file = $photo_id.'_'.time().'.jpg';
|
|
|
|
}
|
2008-09-16 02:01:59 +00:00
|
|
|
if (file_exists($dir.$file)) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$file = uniqid('m').$file;
|
|
|
|
}
|
|
|
|
$fp = fopen($dir.$file, 'w');
|
|
|
|
$c = new curl;
|
2008-09-16 03:11:17 +00:00
|
|
|
$c->download(array(array('url'=>$url, 'file'=>$fp)));
|
|
|
|
|
2008-09-12 03:32:31 +00:00
|
|
|
return $dir.$file;
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Add Instance settings input to Moodle form
|
2008-09-18 05:33:44 +00:00
|
|
|
* @param <type> $
|
|
|
|
*/
|
2008-09-15 09:21:29 +00:00
|
|
|
public function instance_config_form(&$mform) {
|
|
|
|
$mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
|
2008-11-20 06:24:21 +00:00
|
|
|
//$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
|
2008-09-15 09:21:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Names of the instance settings
|
2008-09-18 05:33:44 +00:00
|
|
|
* @return <type>
|
|
|
|
*/
|
2008-09-15 09:21:29 +00:00
|
|
|
public static function get_instance_option_names() {
|
|
|
|
return array('email_address');
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Add Plugin settings input to Moodle form
|
2008-09-18 05:33:44 +00:00
|
|
|
* @param <type> $
|
|
|
|
*/
|
2008-09-18 03:06:10 +00:00
|
|
|
public function type_config_form(&$mform) {
|
2008-09-12 03:32:31 +00:00
|
|
|
$api_key = get_config('flickr_public', 'api_key');
|
|
|
|
if (empty($api_key)) {
|
|
|
|
$api_key = '';
|
|
|
|
}
|
|
|
|
$strrequired = get_string('required');
|
|
|
|
$mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
|
|
|
|
$mform->addRule('api_key', $strrequired, 'required', null, 'client');
|
2008-09-17 06:09:10 +00:00
|
|
|
$mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|
2008-09-15 09:21:29 +00:00
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Names of the plugin settings
|
2008-09-18 05:33:44 +00:00
|
|
|
* @return <type>
|
|
|
|
*/
|
2008-09-18 03:19:52 +00:00
|
|
|
public static function get_type_option_names() {
|
2008-09-15 09:21:29 +00:00
|
|
|
return array('api_key');
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:54:23 +00:00
|
|
|
* is run when moodle administrator add the plugin
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 09:08:36 +00:00
|
|
|
public static function plugin_init() {
|
2008-09-17 06:09:10 +00:00
|
|
|
//here we create a default instance for this type
|
2008-09-12 03:32:31 +00:00
|
|
|
|
2008-11-26 07:03:10 +00:00
|
|
|
repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null),1);
|
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
public function supported_filetypes() {
|
|
|
|
return array('web_image');
|
|
|
|
}
|
2008-09-12 03:32:31 +00:00
|
|
|
}
|