]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16341 Finished flickr plugin
authornicolasconnault <nicolasconnault>
Fri, 31 Oct 2008 09:14:06 +0000 (09:14 +0000)
committernicolasconnault <nicolasconnault>
Fri, 31 Oct 2008 09:14:06 +0000 (09:14 +0000)
lang/en_utf8/portfolio_flickr.php
lib/flickrlib.php
portfolio/type/flickr/lib.php

index a7e7b8568a7c342ef516ebe014b863b269870ca7..3bf5817219b17741b20232efd341d02ecedca114 100644 (file)
@@ -19,4 +19,5 @@ $string['screenshot'] = 'Screenshots';
 $string['other'] = 'Art, illustration, CGI, or other non-photographic images';
 $string['hidefrompublicsearches'] = 'Hide these images from public searches?';
 $string['set'] = 'Set';
+$string['uploadfailed'] = 'Failed to upload image(s) to flickr.com: $a';
 ?>
index f5bcc0729cbfc11096bed44d80660abf1affd316..14473e356975b97baf5dd952e7014f1eb7065c65 100755 (executable)
@@ -70,18 +70,39 @@ class phpFlickr {
 
     function request ($command, $args = array())
     {
-        if ($command == 'upload') {
-            $filecontent = $args['photo'];
-            unset($args['photo']);
-        }
-
         //Sends a request to Flickr's REST endpoint via POST.
         if (substr($command,0,7) != "flickr.") {
             $command = "flickr." . $command;
         }
 
         //Process arguments, including method and login data.
-        $args = array_merge(array("method" => $command, "format" => "php_serial", "api_key" => $this->api_key), $args);
+        if ($command == 'flickr.upload') {
+            $photo = $args['photo'];
+            if (empty($args['is_public'])) {
+                $args['is_public'] = 0;
+            }
+            if (empty($args['is_friend'])) {
+                $args['is_friend'] = 0;
+            }
+            if (empty($args['is_family'])) {
+                $args['is_family'] = 0;
+            }
+            if (empty($args['hidden'])) {
+                $args['hidden'] = 1;
+            }
+            $args = array("api_key" => $this->api_key,
+                          "title" => $args['title'],
+                          "description" => $args['description'],
+                          "tags" => $args['tags'],
+                          "is_public" => $args['is_public'],
+                          "is_friend" => $args['is_friend'],
+                          "is_family" => $args['is_family'],
+                          "safety_level" => $args['safety_level'],
+                          "content_type" => $args['content_type'],
+                          "hidden" => $args['hidden']);
+        } else {
+            $args = array_merge(array("method" => $command, "format" => "php_serial", "api_key" => $this->api_key), $args);
+        }
 
         if (!empty($this->token)) {
             $args = array_merge($args, array("auth_token" => $this->token));
@@ -90,9 +111,7 @@ class phpFlickr {
         ksort($args);
         $auth_sig = "";
         foreach ($args as $key => $data) {
-            if ($key != 'photo') {
-                $auth_sig .= $key . $data;
-            }
+            $auth_sig .= $key . $data;
         }
 
         if (!empty($this->secret)) {
@@ -103,12 +122,19 @@ class phpFlickr {
         //$this->req->addHeader("Connection", "Keep-Alive");
         if ($command != 'flickr.upload') {
             $ret = $this->curl->post($this->REST, $args);
+            $this->parsed_response = $this->clean_text_nodes(unserialize($ret));
         } else {
-            $ret = $this->curl->post($this->Upload, $args);
-            print_object($ret);die();
+            $args['photo'] = $photo;
+            $xml = simplexml_load_string($this->curl->post($this->Upload, $args));
+
+            if ($xml['stat'] == 'fail') {
+                $this->parsed_response = array('stat' => (string) $xml['stat'], 'code' => (int) $xml->err['code'], 'message' => (string) $xml->err['msg']);
+            } elseif ($xml['stat'] == 'ok') {
+                $this->parsed_response = array('stat' => (string) $xml['stat'], 'photoid' => (int) $xml->photoid);
+                $this->response = true;
+            }
         }
 
-        $this->parsed_response = $this->clean_text_nodes(unserialize($ret));
         if ($this->parsed_response['stat'] == 'fail') {
             if ($this->die_on_error) die("The Flickr API returned the following error: #{$this->parsed_response['code']} - {$this->parsed_response['message']}");
             else {
@@ -201,7 +227,7 @@ class phpFlickr {
         return unserialize(file_get_contents('http://phpflickr.com/geodata/?format=php&lat=' . $lat . '&lon=' . $lon));
     }
 
-    function auth ($perms = "read", $remember_uri = true)
+    function auth ($perms = "write", $remember_uri = true)
     {
         // Redirects to Flickr's authentication piece if there is no valid token.
         // If remember_uri is set to false, the callback script (included) will
index 4e8e6e6aa63ce0829f9f8dc4ff03a5349f2ad086..dd64cae0f70f73d45952e06e1b2219de2281cff5 100755 (executable)
@@ -26,20 +26,24 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base {
             $filesize = $file->get_filesize();
 
             if ($file->is_valid_image()) {
-                $return = $this->flickr->request ('upload', array('photo' => $file->get_content(),
-                                                                  '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')));
-
-                // TODO if a set was requested, attach the photo to that set
-                if ($return && $this->get_export_config('set')) {
-                    //
+                $return = $this->flickr->request ('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')));
+                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']);
+                    }
+                } else {
+                    throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', $this->flickr->error_code . ': ' . $this->flickr->error_msg);
                 }
-                // DEBUG!!!
-                var_dump($return);die();
             }
         }
     }