Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/googledocs/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 
00028 require_once($CFG->libdir.'/googleapi.php');
00029 
00030 class repository_googledocs extends repository {
00031     private $subauthtoken = '';
00032 
00033     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
00034         global $USER;
00035         parent::__construct($repositoryid, $context, $options);
00036 
00037         // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
00038         $googletoken = optional_param('token', false, PARAM_RAW);
00039         if($googletoken){
00040             $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
00041             google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id);
00042         }
00043         $this->check_login();
00044     }
00045 
00046     public function check_login() {
00047         global $USER;
00048 
00049         $sesskey = google_docs::get_sesskey($USER->id);
00050 
00051         if($sesskey){
00052             try{
00053                 $gauth = new google_authsub($sesskey);
00054                 $this->subauthtoken = $sesskey;
00055                 return true;
00056             }catch(Exception $e){
00057                 // sesskey is not valid, delete store and re-auth
00058                 google_docs::delete_sesskey($USER->id);
00059             }
00060         }
00061 
00062         return false;
00063     }
00064 
00065     public function print_login($ajax = true){
00066         global $CFG;
00067         if($ajax){
00068             $ret = array();
00069             $popup_btn = new stdClass();
00070             $popup_btn->type = 'popup';
00071             $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id;
00072             $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM);
00073             $ret['login'] = array($popup_btn);
00074             return $ret;
00075         }
00076     }
00077 
00078     public function get_listing($path='', $page = '') {
00079         $gdocs = new google_docs(new google_authsub($this->subauthtoken));
00080 
00081         $ret = array();
00082         $ret['dynload'] = true;
00083         $ret['list'] = $gdocs->get_file_list();
00084         return $ret;
00085     }
00086 
00087     public function search($query){
00088         $gdocs = new google_docs(new google_authsub($this->subauthtoken));
00089 
00090         $ret = array();
00091         $ret['dynload'] = true;
00092         $ret['list'] = $gdocs->get_file_list($query);
00093         return $ret;
00094     }
00095 
00096     public function logout(){
00097         global $USER;
00098 
00099         $token = google_docs::get_sesskey($USER->id);
00100 
00101         $gauth = new google_authsub($token);
00102         // revoke token from google
00103         $gauth->revoke_session_token();
00104 
00105         google_docs::delete_sesskey($USER->id);
00106         $this->subauthtoken = '';
00107 
00108         return parent::logout();
00109     }
00110 
00111     public function get_file($url, $file) {
00112         global $CFG;
00113         $path = $this->prepare_file($file);
00114 
00115         $fp = fopen($path, 'w');
00116         $gdocs = new google_docs(new google_authsub($this->subauthtoken));
00117         $gdocs->download_file($url, $fp);
00118 
00119         return array('path'=>$path, 'url'=>$url);
00120     }
00121 
00122     public function supported_filetypes() {
00123        return array('document');
00124     }
00125     public function supported_returntypes() {
00126         return FILE_INTERNAL;
00127     }
00128 }
00129 //Icon from: http://www.iconspedia.com/icon/google-2706.html
 All Data Structures Namespaces Files Functions Variables Enumerations