Skip to content

Commit 81baf0d

Browse files
committed
Merge branch 'MDL-34633-22' of git://github.com/FMCorz/moodle into MOODLE_22_STABLE
2 parents befce58 + 4516e72 commit 81baf0d

File tree

2 files changed

+86
-36
lines changed

2 files changed

+86
-36
lines changed

repository/flickr/lib.php

Lines changed: 38 additions & 10 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -36,6 +36,11 @@ class repository_flickr extends repository {
36
private $flickr;
36
private $flickr;
37
public $photos;
37
public $photos;
38

38

39+
/**
40+
* Stores sizes of images to prevent multiple API call
41+
*/
42+
static private $sizes = array();
43+
39
/**
44
/**
40
*
45
*
41
* @param int $repositoryid
46
* @param int $repositoryid
@@ -221,18 +226,41 @@ public function get_listing($path = '', $page = '') {
221
return $this->search('', $page);
226
return $this->search('', $page);
222
}
227
}
223

228

229+
/**
230+
* Return photo url by given photo id
231+
* @param string $photo_id
232+
* @return string
233+
*/
224
public function get_link($photo_id) {
234
public function get_link($photo_id) {
225-
global $CFG;
235+
$bestsize = $this->get_best_size($photo_id);
226-
$result = $this->flickr->photos_getSizes($photo_id);
236+
if (!isset($bestsize['source'])) {
227-
$url = '';
237+
throw new repository_exception('cannotdownload', 'repository');
228-
if(!empty($result[4])) {
238+
}
229-
$url = $result[4]['source'];
239+
return $bestsize['source'];
230-
} elseif(!empty($result[3])) {
240+
}
231-
$url = $result[3]['source'];
241+
232-
} elseif(!empty($result[2])) {
242+
/**
233-
$url = $result[2]['source'];
243+
* Returns the best size for a photo
244+
*
245+
* @param string $photoid the photo identifier
246+
* @return array of information provided by the API
247+
*/
248+
protected function get_best_size($photoid) {
249+
if (!isset(self::$sizes[$photoid])) {
250+
// Sizes are returned from smallest to greatest.
251+
self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid);
252+
}
253+
$sizes = self::$sizes[$photoid];
254+
$bestsize = array();
255+
if (is_array($sizes)) {
256+
while ($bestsize = array_pop($sizes)) {
257+
// Make sure the source is set. Exit the loop if found.
258+
if (isset($bestsize['source'])) {
259+
break;
260+
}
261+
}
234
}
262
}
235-
return $url;
263+
return $bestsize;
236
}
264
}
237

265

238
/**
266
/**

repository/flickr_public/lib.php

Lines changed: 48 additions & 26 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -35,6 +35,11 @@ class repository_flickr_public extends repository {
35
private $flickr;
35
private $flickr;
36
public $photos;
36
public $photos;
37

37

38+
/**
39+
* Stores sizes of images to prevent multiple API call
40+
*/
41+
static private $sizes = array();
42+
38
/**
43
/**
39
* constructor method
44
* constructor method
40
*
45
*
@@ -207,13 +212,14 @@ public function logout() {
207

212

208
public function license4moodle ($license_id) {
213
public function license4moodle ($license_id) {
209
$license = array(
214
$license = array(
215+
'0' => 'allrightsreserved',
210
'1' => 'cc-nc-sa',
216
'1' => 'cc-nc-sa',
211
'2' => 'cc-nc',
217
'2' => 'cc-nc',
212
'3' => 'cc-nc-nd',
218
'3' => 'cc-nc-nd',
213
'4' => 'cc',
219
'4' => 'cc',
214
'5' => 'cc-sa',
220
'5' => 'cc-sa',
215
'6' => 'cc-nd',
221
'6' => 'cc-nd',
216-
'7' => 'allrightsreserved'
222+
'7' => 'other'
217
);
223
);
218
return $license[$license_id];
224
return $license[$license_id];
219
}
225
}
@@ -390,18 +396,41 @@ public function print_search() {
390
return $str;
396
return $str;
391
}
397
}
392

398

399+
/**
400+
* Return photo url by given photo id
401+
* @param string $photo_id
402+
* @return string
403+
*/
393
public function get_link($photo_id) {
404
public function get_link($photo_id) {
394-
global $CFG;
405+
$bestsize = $this->get_best_size($photo_id);
395-
$result = $this->flickr->photos_getSizes($photo_id);
406+
if (!isset($bestsize['source'])) {
396-
$url = '';
407+
throw new repository_exception('cannotdownload', 'repository');
397-
if(!empty($result[4])) {
408+
}
398-
$url = $result[4]['source'];
409+
return $bestsize['source'];
399-
} elseif(!empty($result[3])) {
410+
}
400-
$url = $result[3]['source'];
411+
401-
} elseif(!empty($result[2])) {
412+
/**
402-
$url = $result[2]['source'];
413+
* Returns the best size for a photo
414+
*
415+
* @param string $photoid the photo identifier
416+
* @return array of information provided by the API
417+
*/
418+
protected function get_best_size($photoid) {
419+
if (!isset(self::$sizes[$photoid])) {
420+
// Sizes are returned from smallest to greatest.
421+
self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid);
422+
}
423+
$sizes = self::$sizes[$photoid];
424+
$bestsize = array();
425+
if (is_array($sizes)) {
426+
while ($bestsize = array_pop($sizes)) {
427+
// Make sure the source is set. Exit the loop if found.
428+
if (isset($bestsize['source'])) {
429+
break;
430+
}
431+
}
403
}
432
}
404-
return $url;
433+
return $bestsize;
405
}
434
}
406

435

407
/**
436
/**
@@ -420,21 +449,14 @@ public function get_file($photo_id, $file = '') {
420
$author = $info['owner']['username'];
449
$author = $info['owner']['username'];
421
}
450
}
422
$copyright = get_string('author', 'repository') . ': ' . $author;
451
$copyright = get_string('author', 'repository') . ': ' . $author;
423-
$result = $this->flickr->photos_getSizes($photo_id);
452+
424-
// download link
453+
// If we can read the original secret, it means that we have access to the original picture.
425-
$source = '';
454+
if (isset($info['originalsecret'])) {
426-
// flickr photo page
455+
$source = $this->flickr->buildPhotoURL($info, 'original');
427-
$url = '';
456+
} else {
428-
if (!empty($result[4])) {
457+
$source = $this->build_photo_url($photoid);
429-
$source = $result[4]['source'];
430-
$url = $result[4]['url'];
431-
} elseif(!empty($result[3])) {
432-
$source = $result[3]['source'];
433-
$url = $result[3]['url'];
434-
} elseif(!empty($result[2])) {
435-
$source = $result[2]['source'];
436-
$url = $result[2]['url'];
437
}
458
}
459+
438
$path = $this->prepare_file($file);
460
$path = $this->prepare_file($file);
439
$fp = fopen($path, 'w');
461
$fp = fopen($path, 'w');
440
$c = new curl;
462
$c = new curl;
@@ -446,7 +468,7 @@ public function get_file($photo_id, $file = '') {
446
$img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
468
$img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
447
}
469
}
448

470

449-
return array('path'=>$path, 'url'=>$url, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
471+
return array('path'=>$path, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
450
}
472
}
451

473

452
/**
474
/**

0 commit comments

Comments
 (0)