MDL-15402: remove has_multiple_instance(), API will know there is multiple instance...
[moodle.git] / repository / flickr_public / repository.class.php
CommitLineData
539b3d13 1<?php
2/**
3 * repository_flickr_public class
4 * This one is used to create public repository
5 * You can set up a public account in admin page, so everyone can
6 * access photos in this public account
7 *
8 * @author Dongsheng Cai <dongsheng@moodle.com>
9 * @version $Id$
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 */
12
13require_once($CFG->libdir.'/flickrlib.php');
14
dc0976b8 15class repository_flickr_public extends repository {
539b3d13 16 private $flickr;
17 public $photos;
18
dc0976b8 19 public function set_option($options = array()) {
539b3d13 20 if (!empty($options['api_key'])) {
21 set_config('api_key', trim($options['api_key']), 'flickr_public');
22 }
23 unset($options['api_key']);
24 $ret = parent::set_option($options);
25 return $ret;
26 }
27
dc0976b8 28 public function get_option($config = '') {
29 if ($config==='api_key') {
539b3d13 30 return trim(get_config('flickr_public', 'api_key'));
31 } else {
32 $options['api_key'] = trim(get_config('flickr_public', 'api_key'));
33 }
34 $options = parent::get_option($config);
35 return $options;
36 }
37
dc0976b8 38 public function global_search() {
539b3d13 39 if (empty($this->flickr_account)) {
40 return false;
41 } else {
42 return true;
43 }
44 }
45
948c2860 46 public function __construct($repositoryid, $context = SITEID, $options = array(), $readonly=0) {
d68c527f 47 global $CFG;
353d5cf3 48 $options['page'] = optional_param('p', 1, PARAM_INT);
948c2860 49 parent::__construct($repositoryid, $context, $options,$readonly);
539b3d13 50 $this->api_key = $this->get_option('api_key');
51 $this->flickr = new phpFlickr($this->api_key);
b8f46e17 52 $this->flickr_account = $this->get_option('email_address');
539b3d13 53
353d5cf3 54 // when flickr account hasn't been set by admin, user can
55 // submit a flickr account here.
d68c527f 56 $account = optional_param('flickr_account', '', PARAM_RAW);
dc0976b8 57 if (!empty($account)) {
d68c527f 58 $people = $this->flickr->people_findByEmail($account);
dc0976b8 59 if (!empty($people)) {
d68c527f 60 $this->flickr_account = $account;
539b3d13 61 } else {
d68c527f 62 throw new repository_exception('invalidemail', 'repository_flickr_public');
539b3d13 63 }
64 }
65 }
dc0976b8 66 public function check_login() {
d68c527f 67 return !empty($this->flickr_account);
68 }
dc0976b8 69 public function print_login($ajax = true) {
70 if ($ajax) {
d68c527f 71 $ret = array();
ca0174cc 72 $email_field->label = get_string('username', 'repository_flickr_public').': ';
73 $email_field->id = 'account';
74 $email_field->type = 'text';
75 $email_field->name = 'flickr_account';
76 $ret['login'] = array($email_field);
d68c527f 77 return $ret;
539b3d13 78 }
79 }
353d5cf3 80 public function search($search_text) {
fbd508b4 81 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 82 $this->nsid = $people['nsid'];
83 $tag = optional_param('tag', '', PARAM_CLEANHTML);
dc0976b8 84 if (!empty($tag)) {
fbd508b4 85 $photos = $this->flickr->photos_search(array(
86 'tags'=>$tag
87 ));
88 } else {
89 $photos = $this->flickr->photos_search(array(
353d5cf3 90 'user_id'=>$this->nsid,
91 'text'=>$search_text));
fbd508b4 92 }
353d5cf3 93 return $this->build_list($photos);
fbd508b4 94 }
353d5cf3 95 public function get_listing($path = '1') {
539b3d13 96 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 97 $this->nsid = $people['nsid'];
fbd508b4 98 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
539b3d13 99
353d5cf3 100 return $this->build_list($photos, $path);
101 }
102 private function build_list($photos, $path = 1) {
103 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
539b3d13 104 $ret = array();
105 $ret['manage'] = $photos_url;
106 $ret['list'] = array();
539b3d13 107 $ret['pages'] = $photos['pages'];
dc0976b8 108 if (is_int($path) && $path <= $ret['pages']) {
539b3d13 109 $ret['page'] = $path;
110 } else {
111 $ret['page'] = 1;
112 }
353d5cf3 113 if (!empty($photos['photo'])) {
114 foreach ($photos['photo'] as $p) {
115 if(empty($p['title'])) {
116 $p['title'] = get_string('notitle', 'repository_flickr');
117 }
118 if (isset($p['originalformat'])) {
119 $format = $p['originalformat'];
120 } else {
121 $format = 'jpg';
122 }
123 $ret['list'][] = array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],
124 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
125 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
539b3d13 126 }
539b3d13 127 }
353d5cf3 128 return $ret;
539b3d13 129 }
dc0976b8 130
dc0976b8 131 public function print_listing() {
539b3d13 132 return false;
133 }
dc0976b8 134
dc0976b8 135 public function print_search() {
2b9feb5f 136 parent::print_search();
8d419e59 137 echo '<label>Tag: </label><br /><input type="text" name="tag" /><br />';
2b9feb5f 138 return true;
539b3d13 139 }
dc0976b8 140
dc0976b8 141 public function get_file($photo_id, $file = '') {
539b3d13 142 global $CFG;
143 $result = $this->flickr->photos_getSizes($photo_id);
144 $url = '';
dc0976b8 145 if (!empty($result[4])) {
539b3d13 146 $url = $result[4]['source'];
353d5cf3 147 } elseif(!empty($result[3])) {
539b3d13 148 $url = $result[3]['source'];
353d5cf3 149 } elseif(!empty($result[2])) {
539b3d13 150 $url = $result[2]['source'];
151 }
152 if (!file_exists($CFG->dataroot.'/repository/download')) {
153 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
154 }
353d5cf3 155 if(is_dir($CFG->dataroot.'/repository/download')) {
539b3d13 156 $dir = $CFG->dataroot.'/repository/download/';
157 }
158
dc0976b8 159 if (empty($file)) {
539b3d13 160 $file = $photo_id.'_'.time().'.jpg';
161 }
dc0976b8 162 if (file_exists($dir.$file)) {
539b3d13 163 $file = uniqid('m').$file;
164 }
165 $fp = fopen($dir.$file, 'w');
166 $c = new curl;
353d5cf3 167 $c->download(array(array('url'=>$url, 'file'=>$fp)));
168
539b3d13 169 return $dir.$file;
170 }
539b3d13 171
b8f46e17 172 public function instance_config_form(&$mform) {
173 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
948c2860 174 $mform->addRule('email_address', get_string('required'), 'required', null, 'client');
b8f46e17 175 }
176
177 public static function get_instance_option_names() {
178 return array('email_address');
539b3d13 179 }
180
daff8f50 181 public function type_config_form(&$mform) {
539b3d13 182 $api_key = get_config('flickr_public', 'api_key');
183 if (empty($api_key)) {
184 $api_key = '';
185 }
186 $strrequired = get_string('required');
187 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 188 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 189 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 190 }
b8f46e17 191
1b79955a 192 public static function get_type_option_names() {
b8f46e17 193 return array('api_key');
194 }
195
948c2860 196
197 public static function plugin_init() {
e49eaacb 198 //here we create a default instance for this type
948c2860 199 repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => 'default instance','email_address' => null),1);
539b3d13 200 }
201
202}
203