Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/picasa/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00029 require_once($CFG->libdir.'/googleapi.php');
00030 
00031 class repository_picasa extends repository {
00032     private $subauthtoken = '';
00033 
00034     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
00035         global $USER;
00036         parent::__construct($repositoryid, $context, $options);
00037 
00038         // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
00039         $googletoken = optional_param('token', false, PARAM_RAW);
00040         if($googletoken){
00041             $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
00042             google_picasa::set_sesskey($gauth->get_sessiontoken(), $USER->id);
00043         }
00044         $this->check_login();
00045     }
00046 
00047     public function check_login() {
00048         global $USER;
00049 
00050         $sesskey = google_picasa::get_sesskey($USER->id);
00051 
00052         if($sesskey){
00053             try{
00054                 $gauth = new google_authsub($sesskey);
00055                 $this->subauthtoken = $sesskey;
00056                 return true;
00057             }catch(Exception $e){
00058                 // sesskey is not valid, delete store and re-auth
00059                 google_picasa::delete_sesskey($USER->id);
00060             }
00061         }
00062 
00063         return false;
00064     }
00065 
00066     public function print_login(){
00067         global $CFG;
00068         $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id;
00069         $authurl = google_authsub::login_url($returnurl, google_picasa::REALM);
00070         if($this->options['ajax']){
00071             $ret = array();
00072             $popup_btn = new stdClass();
00073             $popup_btn->type = 'popup';
00074             $popup_btn->url = $authurl;
00075             $ret['login'] = array($popup_btn);
00076             return $ret;
00077         } else {
00078             echo '<a target="_blank" href="'.$authurl.'">Login</a>';
00079         }
00080     }
00081 
00082     public function get_listing($path='', $page = '') {
00083         $picasa = new google_picasa(new google_authsub($this->subauthtoken));
00084 
00085         $ret = array();
00086         $ret['dynload'] = true;
00087         $ret['list'] = $picasa->get_file_list($path);
00088         return $ret;
00089     }
00090 
00091     public function search($query){
00092         $picasa = new google_picasa(new google_authsub($this->subauthtoken));
00093 
00094         $ret = array();
00095         $ret['list'] =  $picasa->do_photo_search($query);
00096         return $ret;
00097     }
00098 
00099     public function logout(){
00100         global $USER;
00101 
00102         $token = google_picasa::get_sesskey($USER->id);
00103 
00104         $gauth = new google_authsub($token);
00105         // revoke token from google
00106         $gauth->revoke_session_token();
00107 
00108         google_picasa::delete_sesskey($USER->id);
00109         $this->subauthtoken = '';
00110 
00111         return parent::logout();
00112     }
00113 
00114     public function get_name(){
00115         return get_string('pluginname', 'repository_picasa');
00116     }
00117     public function supported_filetypes() {
00118         return array('web_image');
00119     }
00120     public function supported_returntypes() {
00121         return (FILE_INTERNAL | FILE_EXTERNAL);
00122     }
00123 }
00124 
00125 // Icon for this plugin retrieved from http://www.iconspedia.com/icon/picasa-2711.html
00126 // Where the license is said documented to be Free
 All Data Structures Namespaces Files Functions Variables Enumerations