MDL-15488
[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(){
39 global $SESSION;
40 if (empty($this->flickr_account)) {
41 return false;
42 } else {
43 return true;
44 }
45 }
46
47 public function __construct($repositoryid, $context = SITEID, $options = array()){
48 global $SESSION, $action, $CFG;
49 $options['page'] = optional_param('p', 1, PARAM_INT);
50 parent::__construct($repositoryid, $context, $options);
51 $this->api_key = $this->get_option('api_key');
52 $this->flickr = new phpFlickr($this->api_key);
53
54 $this->flickr_account = $this->get_option('public_account');
55
56 if(!empty($this->flickr_account)) {
57 $action = 'list';
58 } else {
59 $account = optional_param('flickr_account', '', PARAM_RAW);
60 if(!empty($account)) {
61 $people = $this->flickr->people_findByEmail($account);
62 if(!empty($people)) {
63 $this->flickr_account = $account;
64 $action = 'list';
65 } else {
66 throw new repository_exception('invalidemail', 'repository_flickr_public');
67 }
68 } else {
69 $action = 'login';
70 }
71 }
72 }
73 public function print_login($ajax = true){
74 global $SESSION;
75 if(empty($this->flickr_account)) {
76 if($ajax){
77 $ret = array();
78 $e1->label = get_string('username', 'repository_flickr_public').': ';
79 $e1->id = 'account';
80 $e1->type = 'text';
81 $e1->name = 'flickr_account';
82
83 $e2->type = 'hidden';
84 $e2->name = 'repo_id';
85 $e2->value = $this->id;
86 $ret['login'] = array($e1, $e2);
87 return $ret;
88 }else{
89 echo $str;
90 }
91 } else {
92 return $this->get_listing();
93 }
94 }
95 public function get_listing($path = '1', $search = ''){
96 global $SESSION;
97 $people = $this->flickr->people_findByEmail($this->flickr_account);
98 $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
99
100 if(!empty($search)) {
101 // do searching, if $path is not empty, ignore it.
102 $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
103 } elseif(!empty($path) && empty($search)) {
104 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
105 }
106
107 $ret = array();
108 $ret['manage'] = $photos_url;
109 $ret['list'] = array();
110 $ret['nologin'] = true;
111 $ret['pages'] = $photos['pages'];
112 if(is_int($path) && $path <= $ret['pages']) {
113 $ret['page'] = $path;
114 } else {
115 $ret['page'] = 1;
116 }
117 foreach ($photos['photo'] as $p) {
118 if(empty($p['title'])) {
119 $p['title'] = get_string('notitle', 'repository_flickr_public');
120 }
121 if (isset($p['originalformat'])) {
122 $format = $p['originalformat'];
123 } else {
124 $format = 'jpg';
125 }
126 $ret['list'][] =
127 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']);
128 }
129 if(empty($ret)) {
130 throw new repository_exception('nullphotolist', 'repository_flickr_public');
131 } else {
132 return $ret;
133 }
134 }
135 public function print_listing(){
136 return false;
137 }
138 public function print_search(){
139 return false;
140 }
141 public function get_file($photo_id, $file = ''){
142 global $CFG;
143 $result = $this->flickr->photos_getSizes($photo_id);
144 $url = '';
145 if(!empty($result[4])) {
146 $url = $result[4]['source'];
147 } elseif(!empty($result[3])) {
148 $url = $result[3]['source'];
149 } elseif(!empty($result[2])) {
150 $url = $result[2]['source'];
151 }
152 if (!file_exists($CFG->dataroot.'/repository/download')) {
153 mkdir($CFG->dataroot.'/repository/download/', 0777, true);
154 }
155 if(is_dir($CFG->dataroot.'/repository/download')) {
156 $dir = $CFG->dataroot.'/repository/download/';
157 }
158
159 if(empty($file)) {
160 $file = $photo_id.'_'.time().'.jpg';
161 }
162 if(file_exists($dir.$file)) {
163 $file = uniqid('m').$file;
164 }
165 $fp = fopen($dir.$file, 'w');
166 $c = new curl;
167 $c->download(array(
168 array('url'=>$url, 'file'=>$fp)
169 ));
170 return $dir.$file;
171 }
172 public static function has_admin_config() {
173 return true;
174 }
175
176 public static function has_multiple_instances() {
177 return true;
178 }
179
180 public static function has_instance_config() {
181 return false;
182 }
183
184 public function admin_config_form(&$mform) {
185 $api_key = get_config('flickr_public', 'api_key');
186 if (empty($api_key)) {
187 $api_key = '';
188 }
189 $strrequired = get_string('required');
190 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
191 $mform->addElement('text', 'public_account', get_string('public_account', 'repository_flickr_public'), array('size' => '40'));
192 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
193 }
194 public static function get_admin_option_names(){
195 return array('api_key', 'public_account');
196 }
197
198}
199