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