Skip to content

Commit

Permalink
MDL-24693 Flickr.com portfolio plugin fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrd8mz committed Nov 11, 2010
1 parent bf0fc3c commit 76116d2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 42 deletions.
57 changes: 36 additions & 21 deletions lib/flickrlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
* 3. Remove all cache code, it will implement in curl class.
* 4. Clean up session code
*
* Modified by David Mudrak <[email protected]>
* ChangeLog:
* 1. upload() method uses Moodle stored_file
* 2. upload() method supports all params provided by http://www.flickr.com/services/api/upload.api.html
*
* @package moodlecore
* @subpackage 3rd-party
*/
Expand Down Expand Up @@ -1084,37 +1089,49 @@ function urls_lookupUser($url)
$this->request("flickr.urls.lookupUser", array("url"=>$url));
return $this->parsed_response ? $this->parsed_response['user'] : false;
}

/**
* upload a photo to flickr
* @param string $photo must be the path of the file
* @param string $title
* @param string $description
* @param string $tags
* @param string $is_public
* @param string $is_friend
* @param string $is_family
* Upload a photo from Moodle file pool to Flickr
*
* Optional meta information are title, description, tags, is_public, is_friend, is_family, safety_level,
* content_type and hidden {@see http://www.flickr.com/services/api/upload.api.html}
*
* @param stored_file $photo stored in Moodle file pool
* @param array $meta optional meta information
* @return boolean
*/
function upload(stored_file $photo, array $meta = array()) {

$args = array();

$args['title'] = isset($meta['title']) ? $meta['title'] : null;
$args['description'] = isset($meta['description']) ? $meta['description'] : null;
$args['tags'] = isset($meta['tags']) ? $meta['tags'] : null;
$args['is_public'] = isset($meta['is_public']) ? $meta['is_public'] : 0;
$args['is_friend'] = isset($meta['is_friend']) ? $meta['is_friend'] : 0;
$args['is_family'] = isset($meta['is_family']) ? $meta['is_family'] : 0;
$args['safety_level'] = isset($meta['safety_level']) ? $meta['safety_level'] : 1; // safe by default
$args['content_type'] = isset($meta['content_type']) ? $meta['content_type'] : 1; // photo by default
$args['hidden'] = isset($meta['hidden']) ? $meta['hidden'] : 2; // hide from public searches by default

$args['async'] = 1;
$args['api_key'] = $this->api_key;

function upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
global $SESSION;
$args = array("async" => 1, "api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->email)) {
$args = array_merge($args, array("email" => $this->email));
$args['email'] = $this->email;
}
if (!empty($this->password)) {
$args = array_merge($args, array("password" => $this->password));
$args['password'] = $this->password;
}
// TODO:
// should we request a token if it is not valid?
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
$args['auth_token'] = $this->token;
}

// sign the arguments using the shared secret
ksort($args);
$auth_sig = "";
$auth_sig = '';
foreach ($args as $key => $data) {
if ($data !== null) {
if (!is_null($data)) {
$auth_sig .= $key . $data;
} else {
unset($args[$key]);
Expand All @@ -1125,8 +1142,7 @@ function upload ($photo, $title = null, $description = null, $tags = null, $is_p
$args['api_sig'] = $api_sig;
}

$photo = realpath($photo);
$args['photo'] = '@'.$photo;
$args['photo'] = $photo; // $this->curl will process it correctly

if ($response = $this->curl->post($this->Upload, $args)) {
return true;
Expand All @@ -1135,4 +1151,3 @@ function upload ($photo, $title = null, $description = null, $tags = null, $is_p
}
}
}
?>
8 changes: 5 additions & 3 deletions portfolio/flickr/lang/en/portfolio_flickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['apikey'] = 'API key (you need to get this from http://flickr.com/services/api/keys/apply)';
$string['apikey'] = 'API key';
$string['contenttype'] = 'Content types';
$string['err_noapikey'] = 'No API Key';
$string['err_noapikey_help'] = 'There is no API Key configured for this plugin. You can get one of these from http://flickr.com/services/api/keys/apply';
Expand All @@ -35,12 +35,14 @@
$string['noauthtoken'] = 'Could not retrieve an authentication token for use in this session';
$string['other'] = 'Art, illustration, CGI, or other non-photographic images';
$string['photo'] = 'Photos';
$string['pluginname'] = 'Flickr online photo management system';
$string['pluginname'] = 'Flickr.com';
$string['restricted'] = 'Restricted';
$string['safe'] = 'Safe';
$string['safetylevel'] = 'Safety level';
$string['screenshot'] = 'Screenshots';
$string['set'] = 'Set';
$string['sharedsecret'] = 'Your shared secret string (will be provided with your generated API key)';
$string['setupinfo'] = 'Setup instructions';
$string['setupinfodetails'] = 'To obtain API key and the secret string, log in to Flickr and <a href="{$a->applyurl}">apply for a new key</a>. Once new key and secret are generated for you, follow the \'Edit auth flow for this app\' link at the page. Select \'App Type\' to \'Web Application\'. Into the \'Callback URL\' field, put the value: <br /><code>{$a->callbackurl}</code><br />Optionally, you can also provide your Moodle site description and logo. These values can be set later at <a href="{$a->keysurl}">the page</a> listing your Flickr applications.';
$string['sharedsecret'] = 'Secret string';
$string['title'] = 'Title';
$string['uploadfailed'] = 'Failed to upload image(s) to flickr.com: {$a}';
74 changes: 56 additions & 18 deletions portfolio/flickr/lib.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package portfolio
* @subpackage flickr
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir.'/portfolio/plugin.php');
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/flickrlib.php');
Expand Down Expand Up @@ -27,26 +52,25 @@ public function send_package() {
$filesize = $file->get_filesize();

if ($file->is_valid_image()) {
/**
$return = $this->flickr->upload (array('photo' => $file,
'title' => $this->get_export_config('title'),
'description' => $this->get_export_config('description'),
'tags' => $this->get_export_config('tags'),
'is_public' => $this->get_export_config('is_public'),
'is_friend' => $this->get_export_config('is_friend'),
'is_family' => $this->get_export_config('is_family'),
'safety_level' => $this->get_export_config('safety_level'),
'content_type' => $this->get_export_config('content_type'),
'hidden' => $this->get_export_config('hidden')));
*/
$return = $this->flickr->upload($file, $this->get_export_config('title'), $this->get_export_config('description'), $this->get_export_config('tags'), $this->get_export_config('is_public'), $this->get_export_config('is_friend'), $this->get_export_config('is_family'));
$return = $this->flickr->upload($file, array(
'title' => $this->get_export_config('title'),
'description' => $this->get_export_config('description'),
'tags' => $this->get_export_config('tags'),
'is_public' => $this->get_export_config('is_public'),
'is_friend' => $this->get_export_config('is_friend'),
'is_family' => $this->get_export_config('is_family'),
'safety_level' => $this->get_export_config('safety_level'),
'content_type' => $this->get_export_config('content_type'),
'hidden' => $this->get_export_config('hidden')));
if ($return) {
// Attach photo to a set if requested
if ($this->get_export_config('set')) {
$this->flickr->photosets_addPhoto($this->get_export_config('set'), $this->flickr->parsed_response['photoid']);
$this->flickr->photosets_addPhoto($this->get_export_config('set'),
$this->flickr->parsed_response['photoid']);
}
} else {
throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', $this->flickr->error_code . ': ' . $this->flickr->error_msg);
throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr',
$this->flickr->error_code . ': ' . $this->flickr->error_msg);
}
}
}
Expand All @@ -57,8 +81,7 @@ public static function allows_multiple_instances() {
}

public function get_interactive_continue_url() {
// return $this->flickr->urls_getUserPhotos();
return "http://www.flickr.com/tools/uploader_edit.gne?ids="; // Add ids of uploaded files
return $this->flickr->urls_getUserPhotos();
}

public function expected_time($callertime) {
Expand All @@ -74,11 +97,19 @@ public static function has_admin_config() {
}

public function admin_config_form(&$mform) {
global $CFG;

$strrequired = get_string('required');
$mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'));
$mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'), array('size' => 30));
$mform->addRule('apikey', $strrequired, 'required', null, 'client');
$mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
$mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
$a = new stdClass();
$a->applyurl = 'http://www.flickr.com/services/api/keys/apply/';
$a->keysurl = 'http://www.flickr.com/services/api/keys/';
$a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=boxnet';
$mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_flickr'),
get_string('setupinfodetails', 'portfolio_flickr', $a));
}

public function has_export_config() {
Expand Down Expand Up @@ -208,4 +239,11 @@ private function get_export_value_name($param, $value) {
return '-';
}
}

/**
* For now, flickr doesn't support this because we can't dynamically construct callbackurl
*/
public static function allows_multiple_exports() {
return false;
}
}

0 comments on commit 76116d2

Please sign in to comment.