';
return $str;
}
/**
* Return photo url by given photo id
* @param string $photoid
* @return string
*/
private function build_photo_url($photoid) {
$bestsize = $this->get_best_size($photoid);
if (!isset($bestsize['source'])) {
throw new repository_exception('cannotdownload', 'repository');
}
return $bestsize['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 $bestsize;
}
public function get_link($photoid) {
return $this->build_photo_url($photoid);
}
/**
*
* @global object $CFG
* @param string $photoid
* @param string $file
* @return string
*/
public function get_file($photoid, $file = '') {
global $CFG;
$info = $this->flickr->photos_getInfo($photoid);
if ($info['owner']['realname']) {
$author = $info['owner']['realname'];
} else {
$author = $info['owner']['username'];
}
$copyright = get_string('author', 'repository') . ': ' . $author;
// If we can read the original secret, it means that we have access to the original picture.
if (isset($info['originalsecret'])) {
$source = $this->flickr->buildPhotoURL($info, 'original');
} else {
$source = $this->build_photo_url($photoid);
}
$result = parent::get_file($source, $file);
$path = $result['path'];
if (!empty($this->usewatermarks)) {
$img = new moodle_image($path);
$img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
}
return array('path'=>$path, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
}
/**
* Add Instance settings input to Moodle form
* @param object $mform
*/
public static function instance_config_form($mform) {
$mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
$mform->setType('email_address', PARAM_RAW_TRIMMED); // This is for sending to flickr. Not our job to validate it.
$mform->addElement('checkbox', 'usewatermarks', get_string('watermark', 'repository_flickr_public'));
$mform->setDefault('usewatermarks', 0);
}
/**
* Names of the instance settings
* @return array
*/
public static function get_instance_option_names() {
return array('email_address', 'usewatermarks');
}
/**
* Add Plugin settings input to Moodle form
* @param object $mform
*/
public static function type_config_form($mform, $classname = 'repository') {
$api_key = get_config('flickr_public', 'api_key');
if (empty($api_key)) {
$api_key = '';
}
$strrequired = get_string('required');
$mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
$mform->setType('api_key', PARAM_RAW_TRIMMED);
$mform->addRule('api_key', $strrequired, 'required', null, 'client');
$mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
}
/**
* Names of the plugin settings
* @return array
*/
public static function get_type_option_names() {
return array('api_key', 'pluginname');
}
/**
* is run when moodle administrator add the plugin
*/
public static function plugin_init() {
//here we create a default instance for this type
$id = repository::static_function('flickr_public','create', 'flickr_public', 0, context_system::instance(), array('name'=>'', 'email_address' => null, 'usewatermarks' => false), 0);
if (empty($id)) {
return false;
} else {
return true;
}
}
public function supported_filetypes() {
return array('web_image');
}
public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL);
}
/**
* Return the source information
*
* @param string $photoid photo id
* @return string|null
*/
public function get_file_source_info($photoid) {
return $this->build_photo_url($photoid);
}
/**
* Is this repository accessing private data?
*
* @return bool
*/
public function contains_private_data() {
return false;
}
}