"MDL-13766, repository settings page"
[moodle.git] / repository / flickr / repository.class.php
CommitLineData
36ea9a6c 1<?php
2/**
3 * repository_flickr class
4 * This is a subclass of repository class
5 *
6 * @author Dongsheng Cai
7 * @version 0.1 dev
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 */
10
36ea9a6c 11require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
12
13class repository_flickr extends repository{
19add4c0 14 private $flickr;
15 public $photos;
16 public $type = 'flickr';
17
36ea9a6c 18 public function __construct($repositoryid, $context = SITEID, $options = array()){
d72ff7a2 19 global $SESSION, $action, $CFG;
36ea9a6c 20 $options['page'] = optional_param('p', 1, PARAM_INT);
21 $options['api_key'] = 'bf85ae2b5b105a2c645f32a32cd6ad59';
22 $options['secret'] = '7cb2f9d7cf70aebe';
23 parent::__construct($repositoryid, $context, $options);
24 $this->flickr = new phpFlickr($this->options['api_key'], $this->options['secret']);
25
26 $reset = optional_param('reset', 0, PARAM_INT);
27 if(!empty($reset)) {
bb2c046d 28 // logout from flickr
36ea9a6c 29 unset($SESSION->flickrmail);
30 set_user_preference('flickrmail', '');
31 }
32
a9fd1e48 33 if(!empty($SESSION->flickrmail)) {
1e28c767 34 if(empty($action)) {
35 $action = 'list';
36 }
36ea9a6c 37 } else {
bb2c046d 38 // get flickr account
36ea9a6c 39 $options['flickrmail'] = optional_param('flickrmail', '', PARAM_RAW);
40 if(!empty($options['flickrmail'])) {
41 $people = $this->flickr->people_findByEmail($options['flickrmail']);
42 if(!empty($people)) {
43 $remember = optional_param('remember', '', PARAM_RAW);
44 if(!empty($remember)) {
45 set_user_preference('flickrmail', $options['flickrmail']);
46 }
47 $SESSION->flickrmail = $options['flickrmail'];
1e28c767 48 if(empty($action)) {
49 $action = 'list';
50 }
bf1fccf0 51 } else {
52 throw new repository_exception('invalidemail', 'repository');
36ea9a6c 53 }
54 } else {
55 if($account = get_user_preferences('flickrmail', '')){
56 $SESSION->flickrmail = $account;
1e28c767 57 if(empty($action)) {
58 $action = 'list';
59 }
36ea9a6c 60 }
61 }
62 }
63 }
0eb58cf4 64 public function print_login($ajax = true){
36ea9a6c 65 global $SESSION;
66 if(empty($SESSION->flickrmail)) {
0eb58cf4 67 $str =<<<EOD
bb2c046d 68<form id="moodle-repo-login">
69<label for="account">Account (Email)</label><br/>
70<input type='text' name='flickrmail' id='account' />
71<input type='hidden' name='id' value='$this->repositoryid' /><br/>
72<input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
99d99cff 73<p><input type='button' onclick="repository_client.login()" value="Go" /></p>
bb2c046d 74</form>
36ea9a6c 75EOD;
0eb58cf4 76 if($ajax){
77 $ret = array();
78 $ret['l'] = $str;
79 return $ret;
80 }else{
81 echo $str;
82 }
36ea9a6c 83 } else {
0eb58cf4 84 return $this->get_listing();
36ea9a6c 85 }
36ea9a6c 86 }
a9fd1e48 87 public function get_listing($path = '1', $search = ''){
36ea9a6c 88 global $SESSION;
89 $people = $this->flickr->people_findByEmail($SESSION->flickrmail);
90 $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
75e7a38c 91
92 if(!empty($search)) {
93 // do searching, if $path is not empty, ignore it.
94 $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
95 } elseif(!empty($path) && empty($search)) {
96 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], null, 36, $path);
97 }
98
0eb58cf4 99 $ret = new stdclass;
100 $ret->url = $photos_url;
101 $ret->list = array();
0eb58cf4 102 $ret->pages = $photos['pages'];
75e7a38c 103 if(is_int($path) && $path <= $ret->pages) {
104 $ret->page = $path;
105 } else {
106 $ret->page = 1;
107 }
0eb58cf4 108 foreach ($photos['photo'] as $p) {
a9fd1e48 109 if(empty($p['title'])) {
110 $p['title'] = get_string('notitle', 'repository_flickr');
111 }
112 $ret->list[] =
2040478f 113 array('title'=>$p['title'],'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown');
0eb58cf4 114 }
bf1fccf0 115 if(empty($ret)) {
116 throw new repository_exception('nullphotolist', 'repository');
117 } else {
118 return $ret;
119 }
36ea9a6c 120 }
121 public function print_listing(){
122 if(empty($this->photos)){
123 $this->get_listing();
124 }
0eb58cf4 125 $str = '';
126 $str .= '<h2>Account: <span>'.$this->photos['a'].'</span></h2>';
0eb58cf4 127 foreach ((array)$this->photos['photo'] as $photo) {
128 $str .= "<a href='".$this->photos['url'].$photo[id]."'>";
129 $str .= "<img border='0' alt='$photo[title]' ".
130 "src=" . $photo['thumbnail'] . ">";
131 $str .= "</a>";
36ea9a6c 132 $i++;
75e7a38c 133
0eb58cf4 134 if ($i % 4 == 0) {
135 $str .= "<br/>";
36ea9a6c 136 }
137 }
0eb58cf4 138 $str .= <<<EOD
36ea9a6c 139<style type='text/css'>
140#paging{margin-top: 10px; clear:both}
141#paging a{padding: 4px; border: 1px solid gray}
142</style>
143EOD;
0eb58cf4 144 $str .= '<div id="paging">';
145 for($i=1; $i <= $this->photos['pages']; $i++) {
146 $str .= '<a href="###" onclick="cr('.$this->repositoryid.', '.$i.', 0)">';
147 $str .= $i;
148 $str .= '</a> ';
36ea9a6c 149 }
0eb58cf4 150 $str .= '</div>';
151 echo $str;
36ea9a6c 152 }
153 public function print_search(){
154 echo '<input type="text" name="Search" value="search terms..." size="40" class="right"/>';
155 return true;
156 }
bb2c046d 157 public function get_file($photo_id, $file = ''){
1e28c767 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 }
bb2c046d 174
175 if(empty($file)) {
1e28c767 176 $file = $photo_id.'_'.time().'.jpg';
1e28c767 177 }
bb2c046d 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;
1e28c767 187 }
36ea9a6c 188}
189?>