Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/boxnet/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 
00018 require_once($CFG->libdir.'/boxlib.php');
00019 
00031 class repository_boxnet extends repository {
00032     private $boxclient;
00033 
00040     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
00041         parent::__construct($repositoryid, $context, $options);
00042         $this->api_key = $this->get_option('api_key');
00043         $this->setting_prefix = 'boxnet_';
00044 
00045         $this->auth_token = get_user_preferences($this->setting_prefix.'_auth_token', '');
00046         $this->logged = false;
00047         if (!empty($this->auth_token)) {
00048             $this->logged = true;
00049         }
00050         // already logged
00051         if(!empty($this->logged)) {
00052             if(empty($this->boxclient)) {
00053                 $this->boxclient = new boxclient($this->api_key, $this->auth_token);
00054             }
00055         } else {
00056             $this->boxclient = new boxclient($this->api_key);
00057         }
00058     }
00059 
00064     public function check_login() {
00065         return $this->logged;
00066     }
00067 
00073     public function logout() {
00074         // reset auth token
00075         set_user_preference($this->setting_prefix . '_auth_token', '');
00076         return $this->print_login();
00077     }
00078 
00085     public function set_option($options = array()) {
00086         if (!empty($options['api_key'])) {
00087             set_config('api_key', trim($options['api_key']), 'boxnet');
00088         }
00089         unset($options['api_key']);
00090         $ret = parent::set_option($options);
00091         return $ret;
00092     }
00093 
00100     public function get_option($config = '') {
00101         if($config==='api_key') {
00102             return trim(get_config('boxnet', 'api_key'));
00103         } else {
00104             $options['api_key'] = trim(get_config('boxnet', 'api_key'));
00105         }
00106         $options = parent::get_option($config);
00107         return $options;
00108     }
00109 
00117     public function search($search_text) {
00118         global $OUTPUT;
00119         $list = array();
00120         $ret  = array();
00121         $tree = $this->boxclient->getAccountTree();
00122         if (!empty($tree)) {
00123             $filenames = $tree['file_name'];
00124             $fileids   = $tree['file_id'];
00125             $filesizes = $tree['file_size'];
00126             $filedates = $tree['file_date'];
00127             $fileicon  = $tree['thumbnail'];
00128             foreach ($filenames as $n=>$v){
00129                 if(strstr(strtolower($v), strtolower($search_text)) !== false) {
00130                     $list[] = array('title'=>$v,
00131                             'size'=>$filesizes[$n],
00132                             'date'=>$filedates[$n],
00133                             'source'=>'http://box.net/api/1.0/download/'
00134                                 .$this->auth_token.'/'.$fileids[$n],
00135                             'thumbnail' => $OUTPUT->pix_url(file_extension_icon($v, 32))->out(false));
00136                 }
00137             }
00138         }
00139         $ret['list'] = array_filter($list, array($this, 'filter'));
00140         return $ret;
00141     }
00142 
00149     public function get_listing($path = '/', $page = ''){
00150         $list = array();
00151         $ret  = array();
00152         $ret['list'] = array();
00153         $tree = $this->boxclient->getfiletree($path);
00154         $ret['manage'] = 'http://www.box.net/files';
00155         $ret['path'] = array(array('name'=>'Root', 'path'=>0));
00156         if(!empty($tree)) {
00157             $ret['list'] = array_filter($tree, array($this, 'filter'));
00158         }
00159         return $ret;
00160     }
00161 
00167     public function print_login(){
00168         $t = $this->boxclient->getTicket();
00169         if ($this->options['ajax']) {
00170             $popup_btn = new stdClass();
00171             $popup_btn->type = 'popup';
00172             $popup_btn->url = ' https://www.box.net/api/1.0/auth/' . $t['ticket'];
00173 
00174             $ret = array();
00175             $ret['login'] = array($popup_btn);
00176             return $ret;
00177         } else {
00178             echo '<table>';
00179             echo '<tr><td><label>'.get_string('username', 'repository_boxnet').'</label></td>';
00180             echo '<td><input type="text" name="boxusername" /></td></tr>';
00181             echo '<tr><td><label>'.get_string('password', 'repository_boxnet').'</label></td>';
00182             echo '<td><input type="password" name="boxpassword" /></td></tr>';
00183             echo '<input type="hidden" name="ticket" value="'.$t['ticket'].'" />';
00184             echo '</table>';
00185             echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
00186         }
00187     }
00188 
00193     public static function get_type_option_names() {
00194         return array('api_key', 'pluginname');
00195     }
00196 
00200     public function callback() {
00201         $this->auth_token  = optional_param('auth_token', '', PARAM_TEXT);
00202         set_user_preference($this->setting_prefix . '_auth_token',    $this->auth_token);
00203     }
00204 
00209     public function type_config_form($mform) {
00210         global $CFG;
00211         parent::type_config_form($mform);
00212         $public_account = get_config('boxnet', 'public_account');
00213         $api_key = get_config('boxnet', 'api_key');
00214         if (empty($api_key)) {
00215             $api_key = '';
00216         }
00217         $strrequired = get_string('required');
00218         $mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'), array('value'=>$api_key,'size' => '40'));
00219         $mform->addRule('api_key', $strrequired, 'required', null, 'client');
00220         $mform->addElement('static', null, '',  get_string('information','repository_boxnet'));
00221 
00222         //retrieve the flickr instances
00223         $params = array();
00224         $params['context'] = array();
00225         //$params['currentcontext'] = $this->context;
00226         $params['onlyvisible'] = false;
00227         $params['type'] = 'boxnet';
00228         $instances = repository::get_instances($params);
00229         if (empty($instances)) {
00230             $callbackurl = get_string('callbackwarning', 'repository_boxnet');
00231             $mform->addElement('static', null, '',  $callbackurl);
00232         } else {
00233             $instance = array_shift($instances);
00234             $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
00235             $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_boxnet', $callbackurl));
00236         }
00237     }
00242     public function supported_returntypes() {
00243         return FILE_INTERNAL | FILE_EXTERNAL;
00244     }
00245 }
00246 
 All Data Structures Namespaces Files Functions Variables Enumerations