Skip to content

Commit

Permalink
Merge branch 'MDL-34633-22' of git://github.com/FMCorz/moodle into MO…
Browse files Browse the repository at this point in the history
…ODLE_22_STABLE
  • Loading branch information
stronk7 committed Sep 3, 2012
2 parents befce58 + 4516e72 commit 81baf0d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 36 deletions.
48 changes: 38 additions & 10 deletions repository/flickr/lib.php
Expand Up @@ -36,6 +36,11 @@ class repository_flickr extends repository {
private $flickr; private $flickr;
public $photos; public $photos;


/**
* Stores sizes of images to prevent multiple API call
*/
static private $sizes = array();

/** /**
* *
* @param int $repositoryid * @param int $repositoryid
Expand Down Expand Up @@ -221,18 +226,41 @@ public function get_listing($path = '', $page = '') {
return $this->search('', $page); return $this->search('', $page);
} }


/**
* Return photo url by given photo id
* @param string $photo_id
* @return string
*/
public function get_link($photo_id) { public function get_link($photo_id) {
global $CFG; $bestsize = $this->get_best_size($photo_id);
$result = $this->flickr->photos_getSizes($photo_id); if (!isset($bestsize['source'])) {
$url = ''; throw new repository_exception('cannotdownload', 'repository');
if(!empty($result[4])) { }
$url = $result[4]['source']; return $bestsize['source'];
} elseif(!empty($result[3])) { }
$url = $result[3]['source'];
} elseif(!empty($result[2])) { /**
$url = $result[2]['source']; * Returns the best size for a photo
*
* @param string $photoid the photo identifier
* @return array of information provided by the API
*/
protected function get_best_size($photoid) {
if (!isset(self::$sizes[$photoid])) {
// Sizes are returned from smallest to greatest.
self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid);
}
$sizes = self::$sizes[$photoid];
$bestsize = array();
if (is_array($sizes)) {
while ($bestsize = array_pop($sizes)) {
// Make sure the source is set. Exit the loop if found.
if (isset($bestsize['source'])) {
break;
}
}
} }
return $url; return $bestsize;
} }


/** /**
Expand Down
74 changes: 48 additions & 26 deletions repository/flickr_public/lib.php
Expand Up @@ -35,6 +35,11 @@ class repository_flickr_public extends repository {
private $flickr; private $flickr;
public $photos; public $photos;


/**
* Stores sizes of images to prevent multiple API call
*/
static private $sizes = array();

/** /**
* constructor method * constructor method
* *
Expand Down Expand Up @@ -207,13 +212,14 @@ public function logout() {


public function license4moodle ($license_id) { public function license4moodle ($license_id) {
$license = array( $license = array(
'0' => 'allrightsreserved',
'1' => 'cc-nc-sa', '1' => 'cc-nc-sa',
'2' => 'cc-nc', '2' => 'cc-nc',
'3' => 'cc-nc-nd', '3' => 'cc-nc-nd',
'4' => 'cc', '4' => 'cc',
'5' => 'cc-sa', '5' => 'cc-sa',
'6' => 'cc-nd', '6' => 'cc-nd',
'7' => 'allrightsreserved' '7' => 'other'
); );
return $license[$license_id]; return $license[$license_id];
} }
Expand Down Expand Up @@ -390,18 +396,41 @@ public function print_search() {
return $str; return $str;
} }


/**
* Return photo url by given photo id
* @param string $photo_id
* @return string
*/
public function get_link($photo_id) { public function get_link($photo_id) {
global $CFG; $bestsize = $this->get_best_size($photo_id);
$result = $this->flickr->photos_getSizes($photo_id); if (!isset($bestsize['source'])) {
$url = ''; throw new repository_exception('cannotdownload', 'repository');
if(!empty($result[4])) { }
$url = $result[4]['source']; return $bestsize['source'];
} elseif(!empty($result[3])) { }
$url = $result[3]['source'];
} elseif(!empty($result[2])) { /**
$url = $result[2]['source']; * Returns the best size for a photo
*
* @param string $photoid the photo identifier
* @return array of information provided by the API
*/
protected function get_best_size($photoid) {
if (!isset(self::$sizes[$photoid])) {
// Sizes are returned from smallest to greatest.
self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid);
}
$sizes = self::$sizes[$photoid];
$bestsize = array();
if (is_array($sizes)) {
while ($bestsize = array_pop($sizes)) {
// Make sure the source is set. Exit the loop if found.
if (isset($bestsize['source'])) {
break;
}
}
} }
return $url; return $bestsize;
} }


/** /**
Expand All @@ -420,21 +449,14 @@ public function get_file($photo_id, $file = '') {
$author = $info['owner']['username']; $author = $info['owner']['username'];
} }
$copyright = get_string('author', 'repository') . ': ' . $author; $copyright = get_string('author', 'repository') . ': ' . $author;
$result = $this->flickr->photos_getSizes($photo_id);
// download link // If we can read the original secret, it means that we have access to the original picture.
$source = ''; if (isset($info['originalsecret'])) {
// flickr photo page $source = $this->flickr->buildPhotoURL($info, 'original');
$url = ''; } else {
if (!empty($result[4])) { $source = $this->build_photo_url($photoid);
$source = $result[4]['source'];
$url = $result[4]['url'];
} elseif(!empty($result[3])) {
$source = $result[3]['source'];
$url = $result[3]['url'];
} elseif(!empty($result[2])) {
$source = $result[2]['source'];
$url = $result[2]['url'];
} }

$path = $this->prepare_file($file); $path = $this->prepare_file($file);
$fp = fopen($path, 'w'); $fp = fopen($path, 'w');
$c = new curl; $c = new curl;
Expand All @@ -446,7 +468,7 @@ public function get_file($photo_id, $file = '') {
$img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path); $img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
} }


return array('path'=>$path, 'url'=>$url, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license'])); return array('path'=>$path, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
} }


/** /**
Expand Down

0 comments on commit 81baf0d

Please sign in to comment.