Updated the HEAD build version to 20080916
[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
15class repository_flickr_public extends repository{
16 private $flickr;
17 public $photos;
18
19 public function set_option($options = array()){
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
28 public function get_option($config = ''){
29 if($config==='api_key'){
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
38 public function global_search(){
539b3d13 39 if (empty($this->flickr_account)) {
40 return false;
41 } else {
42 return true;
43 }
44 }
45
46 public function __construct($repositoryid, $context = SITEID, $options = array()){
d68c527f 47 global $CFG;
539b3d13 48 $options['page'] = optional_param('p', 1, PARAM_INT);
49 parent::__construct($repositoryid, $context, $options);
50 $this->api_key = $this->get_option('api_key');
51 $this->flickr = new phpFlickr($this->api_key);
52
b8f46e17 53 $this->flickr_account = $this->get_option('email_address');
539b3d13 54
d68c527f 55 $account = optional_param('flickr_account', '', PARAM_RAW);
56 if(!empty($account)) {
57 $people = $this->flickr->people_findByEmail($account);
58 if(!empty($people)) {
59 $this->flickr_account = $account;
539b3d13 60 } else {
d68c527f 61 throw new repository_exception('invalidemail', 'repository_flickr_public');
539b3d13 62 }
63 }
64 }
d68c527f 65 public function check_login(){
66 return !empty($this->flickr_account);
67 }
539b3d13 68 public function print_login($ajax = true){
d68c527f 69 if($ajax){
70 $ret = array();
ca0174cc 71 $email_field->label = get_string('username', 'repository_flickr_public').': ';
72 $email_field->id = 'account';
73 $email_field->type = 'text';
74 $email_field->name = 'flickr_account';
75 $ret['login'] = array($email_field);
d68c527f 76 return $ret;
539b3d13 77 }
78 }
fbd508b4 79 public function search(){
80 $people = $this->flickr->people_findByEmail($this->flickr_account);
81 $tag = optional_param('tag', '', PARAM_CLEANHTML);
82 $search = optional_param('s', '', PARAM_CLEANHTML);
83 if(!empty($tag)){
84 $photos = $this->flickr->photos_search(array(
85 'tags'=>$tag
86 ));
87 } else {
88 $photos = $this->flickr->photos_search(array(
89 'user_id'=>$people['nsid'],
90 'text'=>$search));
91 }
92 $ret = array();
93 $ret['list'] = array();
94 $ret['nologin'] = true;
95 $ret['pages'] = $photos['pages'];
96 foreach ($photos['photo'] as $p) {
97 if(empty($p['title'])) {
98 $p['title'] = get_string('notitle', 'repository_flickr_public');
99 }
100 if (isset($p['originalformat'])) {
101 $format = $p['originalformat'];
102 } else {
103 $format = 'jpg';
104 }
105 $ret['list'][] =
106 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']);
107 }
108 if(empty($ret)) {
109 throw new repository_exception('nullphotolist', 'repository_flickr_public');
110 } else {
111 return $ret;
112 }
113 }
539b3d13 114 public function get_listing($path = '1', $search = ''){
539b3d13 115 $people = $this->flickr->people_findByEmail($this->flickr_account);
116 $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
117
fbd508b4 118 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
539b3d13 119
120 $ret = array();
121 $ret['manage'] = $photos_url;
122 $ret['list'] = array();
123 $ret['nologin'] = true;
124 $ret['pages'] = $photos['pages'];
125 if(is_int($path) && $path <= $ret['pages']) {
126 $ret['page'] = $path;
127 } else {
128 $ret['page'] = 1;
129 }
130 foreach ($photos['photo'] as $p) {
131 if(empty($p['title'])) {
132 $p['title'] = get_string('notitle', 'repository_flickr_public');
133 }
134 if (isset($p['originalformat'])) {
135 $format = $p['originalformat'];
136 } else {
137 $format = 'jpg';
138 }
139 $ret['list'][] =
140 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']);
141 }
142 if(empty($ret)) {
143 throw new repository_exception('nullphotolist', 'repository_flickr_public');
144 } else {
145 return $ret;
146 }
147 }
148 public function print_listing(){
149 return false;
150 }
151 public function print_search(){
2b9feb5f 152 parent::print_search();
fbd508b4 153 echo '<label>Keyword: </label><input type="text" name="s" /><br />';
154 echo '<label>Tag: </label><input type="text" name="tag" /><br />';
2b9feb5f 155 return true;
539b3d13 156 }
157 public function get_file($photo_id, $file = ''){
158 global $CFG;
159 $result = $this->flickr->photos_getSizes($photo_id);
160 $url = '';
161 if(!empty($result[4])) {
162 $url = $result[4]['source'];
163 } elseif(!empty($result[3])) {
164 $url = $result[3]['source'];
165 } elseif(!empty($result[2])) {
166 $url = $result[2]['source'];
167 }
168 if (!file_exists($CFG->dataroot.'/repository/download')) {
169 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
170 }
171 if(is_dir($CFG->dataroot.'/repository/download')) {
172 $dir = $CFG->dataroot.'/repository/download/';
173 }
174
175 if(empty($file)) {
176 $file = $photo_id.'_'.time().'.jpg';
177 }
178 if(file_exists($dir.$file)) {
179 $file = uniqid('m').$file;
180 }
181 $fp = fopen($dir.$file, 'w');
182 $c = new curl;
183 $c->download(array(
184 array('url'=>$url, 'file'=>$fp)
185 ));
186 return $dir.$file;
187 }
188 public static function has_admin_config() {
189 return true;
190 }
191
192 public static function has_multiple_instances() {
193 return true;
194 }
195
196 public static function has_instance_config() {
b8f46e17 197 return true;
198 }
199
200 public function instance_config_form(&$mform) {
201 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
202 //$mform->addRule('email_address', get_string('required'), 'required', null, 'client');
203 }
204
205 public static function get_instance_option_names() {
206 return array('email_address');
539b3d13 207 }
208
209 public function admin_config_form(&$mform) {
210 $api_key = get_config('flickr_public', 'api_key');
211 if (empty($api_key)) {
212 $api_key = '';
213 }
214 $strrequired = get_string('required');
215 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 216 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
217 }
b8f46e17 218
219 public static function get_admin_option_names() {
220 return array('api_key');
221 }
222
223 public static function type_init() {
224 //here we create a default instances for this type
539b3d13 225 }
226
227}
228