thumbnails -> icons
[moodle.git] / repository / flickr / repository.class.php
CommitLineData
36ea9a6c 1<?php
2/**
6a391ebf 3 * repository_flickr class
36ea9a6c 4 * This is a subclass of repository class
5 *
6a391ebf 6 * @author Dongsheng Cai
7 * @version 0.1 dev
36ea9a6c 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 */
10
6a391ebf 11require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
12
13class repository_flickr extends repository{
14 private $flickr;
15 public $photos;
19add4c0 16
36ea9a6c 17 public function __construct($repositoryid, $context = SITEID, $options = array()){
d72ff7a2 18 global $SESSION, $action, $CFG;
6a391ebf 19 $options['page'] = optional_param('p', 1, PARAM_INT);
36ea9a6c 20 parent::__construct($repositoryid, $context, $options);
eb9aa683 21 $this->api_key = $this->get_option('api_key');
621dca51 22 if (empty($this->api_key)) {
621dca51 23 }
24 $this->flickr = new phpFlickr($this->api_key);
6a391ebf 25
26 $reset = optional_param('reset', 0, PARAM_INT);
eb9aa683 27 $sess_name = 'flickrmail'.$this->id;
6a391ebf 28 if(!empty($reset)) {
29 // logout from flickr
eb9aa683 30 unset($SESSION->$sess_name);
31 set_user_preference('flickrmail'.$this->id, '');
6a391ebf 32 }
33
eb9aa683 34 if(!empty($SESSION->$sess_name)) {
6a391ebf 35 if(empty($action)) {
36 $action = 'list';
37 }
38 } else {
39 // get flickr account
eb9aa683 40 $account = optional_param('flickrmail', '', PARAM_RAW);
41 if(!empty($account)) {
42 $people = $this->flickr->people_findByEmail($account);
6a391ebf 43 if(!empty($people)) {
44 $remember = optional_param('remember', '', PARAM_RAW);
45 if(!empty($remember)) {
eb9aa683 46 set_user_preference('flickrmail'.$this->id, $account);
6a391ebf 47 }
eb9aa683 48 $SESSION->$sess_name = $account;
5fd3e8f7 49 if (empty($account)) {
6a391ebf 50 $action = 'list';
5fd3e8f7 51 } else {
52 $action = 'login';
6a391ebf 53 }
54 } else {
55 throw new repository_exception('invalidemail', 'repository_flickr');
56 }
57 } else {
eb9aa683 58 if($account = get_user_preferences('flickrmail'.$this->id, '')){
59 $SESSION->$sess_name = $account;
6a391ebf 60 if(empty($action)) {
61 $action = 'list';
62 }
5fd3e8f7 63 } else {
64 $action = 'login';
6a391ebf 65 }
66 }
67 }
36ea9a6c 68 }
0eb58cf4 69 public function print_login($ajax = true){
36ea9a6c 70 global $SESSION;
eb9aa683 71 $sess_name = 'flickrmail'.$this->id;
72 if(empty($SESSION->$sess_name)) {
6a391ebf 73 $str =<<<EOD
74<form id="moodle-repo-login">
75<label for="account">Account (Email)</label><br/>
76<input type='text' name='flickrmail' id='account' />
4a65c39a 77<input type='hidden' name='id' value='$this->id' /><br/>
6a391ebf 78<input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
79<p><input type='button' onclick="repository_client.login()" value="Go" /></p>
80</form>
81EOD;
82 if($ajax){
83 $ret = array();
84 $e1->label = get_string('username', 'repository_flickr');
85 $e1->id = 'account';
86 $e1->type = 'text';
87 $e1->name = 'flickrmail';
88
89 $e2->id = 'keepid';
90 $e2->label = get_string('remember', 'repository_flickr');
91 $e2->type = 'checkbox';
92 $e2->name = 'remember';
93
94 $e3->type = 'hidden';
95 $e3->name = 'repo_id';
4a65c39a 96 $e3->value = $this->id;
d373f63f 97 $ret['login'] = array($e1, $e2, $e3);
6a391ebf 98 return $ret;
99 }else{
100 echo $str;
101 }
102 } else {
103 return $this->get_listing();
104 }
36ea9a6c 105 }
6a391ebf 106 public function get_listing($path = '1', $search = ''){
36ea9a6c 107 global $SESSION;
eb9aa683 108 $sess_name = 'flickrmail'.$this->id;
109 $people = $this->flickr->people_findByEmail($SESSION->$sess_name);
6a391ebf 110 $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
111
112 if(!empty($search)) {
113 // do searching, if $path is not empty, ignore it.
114 $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
115 } elseif(!empty($path) && empty($search)) {
733cfd80 116 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 36, $path);
6a391ebf 117 }
118
0eb58cf4 119 $ret = new stdclass;
ae8d6f23 120 $ret->manage = $photos_url;
0eb58cf4 121 $ret->list = array();
6a391ebf 122 $ret->pages = $photos['pages'];
123 if(is_int($path) && $path <= $ret->pages) {
124 $ret->page = $path;
125 } else {
126 $ret->page = 1;
127 }
128 foreach ($photos['photo'] as $p) {
129 if(empty($p['title'])) {
130 $p['title'] = get_string('notitle', 'repository_flickr');
131 }
733cfd80 132 if (isset($p['originalformat'])) {
133 $format = $p['originalformat'];
134 } else {
135 $format = 'jpg';
136 }
6a391ebf 137 $ret->list[] =
733cfd80 138 array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown');
6a391ebf 139 }
bf1fccf0 140 if(empty($ret)) {
6a391ebf 141 throw new repository_exception('nullphotolist', 'repository_flickr');
bf1fccf0 142 } else {
143 return $ret;
144 }
36ea9a6c 145 }
146 public function print_listing(){
6a391ebf 147 if(empty($this->photos)){
148 $this->get_listing();
149 }
150 $str = '';
151 $str .= '<h2>Account: <span>'.$this->photos['a'].'</span></h2>';
152 foreach ((array)$this->photos['photo'] as $photo) {
153 $str .= "<a href='".$this->photos['url'].$photo[id]."'>";
154 $str .= "<img border='0' alt='$photo[title]' ".
155 "src=" . $photo['thumbnail'] . ">";
156 $str .= "</a>";
157 $i++;
158
159 if ($i % 4 == 0) {
160 $str .= "<br/>";
161 }
162 }
163 $str .= <<<EOD
164<style type='text/css'>
165#paging{margin-top: 10px; clear:both}
166#paging a{padding: 4px; border: 1px solid gray}
167</style>
168EOD;
169 $str .= '<div id="paging">';
170 for($i=1; $i <= $this->photos['pages']; $i++) {
4a65c39a 171 $str .= '<a href="###" onclick="cr('.$this->id.', '.$i.', 0)">';
6a391ebf 172 $str .= $i;
173 $str .= '</a> ';
174 }
175 $str .= '</div>';
176 echo $str;
36ea9a6c 177 }
178 public function print_search(){
6a391ebf 179 echo '<input type="text" name="Search" value="search terms..." size="40" class="right"/>';
36ea9a6c 180 return true;
181 }
6a391ebf 182 public function get_file($photo_id, $file = ''){
183 global $CFG;
184 $result = $this->flickr->photos_getSizes($photo_id);
185 $url = '';
186 if(!empty($result[4])) {
187 $url = $result[4]['source'];
188 } elseif(!empty($result[3])) {
189 $url = $result[3]['source'];
190 } elseif(!empty($result[2])) {
191 $url = $result[2]['source'];
192 }
193 if (!file_exists($CFG->dataroot.'/repository/download')) {
194 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
195 }
196 if(is_dir($CFG->dataroot.'/repository/download')) {
197 $dir = $CFG->dataroot.'/repository/download/';
198 }
199
200 if(empty($file)) {
201 $file = $photo_id.'_'.time().'.jpg';
202 }
203 if(file_exists($dir.$file)) {
204 $file = uniqid('m').$file;
205 }
206 $fp = fopen($dir.$file, 'w');
207 $c = new curl;
208 $c->download(array(
209 array('url'=>$url, 'file'=>$fp)
210 ));
211 return $dir.$file;
212 }
4a65c39a 213 public static function has_admin_config() {
214 return true;
215 }
216 public function admin_config_form(&$mform) {
217 $strrequired = get_string('required');
218 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'));
219 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
220 }
221 public static function get_option_names(){
222 return array('api_key');
223 }
36ea9a6c 224}