Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/portfolio/boxnet/lib.php
Go to the documentation of this file.
00001 <?php
00002 require_once($CFG->libdir.'/portfolio/plugin.php');
00003 require_once($CFG->libdir.'/filelib.php');
00004 require_once($CFG->libdir.'/boxlib.php');
00005 
00006 class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
00007 
00008     public $boxclient;
00009     private $ticket;
00010     private $authtoken;
00011     private $folders;
00012     private $accounttree;
00013 
00014     public static function get_name() {
00015         return get_string('pluginname', 'portfolio_boxnet');
00016     }
00017 
00018     public function prepare_package() {
00019         // don't do anything for this plugin, we want to send all files as they are.
00020     }
00021 
00022     public function send_package() {
00023         // if we need to create the folder, do it now
00024         if ($newfolder = $this->get_export_config('newfolder')) {
00025             if (!$created = $this->boxclient->createFolder($newfolder, array('share' => (int)$this->get_export_config('sharefolder')))) {
00026                 throw new portfolio_plugin_exception('foldercreatefailed', 'portfolio_boxnet');
00027             }
00028             $this->folders[$created['folder_id']] = $created['folder_name'];
00029             $this->set_export_config(array('folder' => $created['folder_id']));
00030         }
00031         foreach ($this->exporter->get_tempfiles() as $file) {
00032             $return = $this->boxclient->uploadFile(
00033                 array(
00034                     'file'      => $file,
00035                     'folder_id' => $this->get_export_config('folder'),
00036                     'share'     => $this->get_export_config('sharefile'),
00037                 )
00038             );
00039             if (array_key_exists('status', $return) && $return['status'] == 'upload_ok'
00040                 && array_key_exists('id', $return) && count($return['id']) == 1) {
00041                 $this->rename_file($return['id'][array_pop(array_keys($return['id']))], $file->get_filename());
00042                 // if this fails, the file was sent but not renamed - this triggers a warning but is not fatal.
00043             }
00044         }
00045         if ($this->boxclient->isError()) {
00046             throw new portfolio_plugin_exception('sendfailed', 'portfolio_boxnet', $this->boxclient->getErrorMsg());
00047         }
00048     }
00049 
00050     public function get_export_summary() {
00051         $allfolders = $this->get_folder_list();
00052         if ($newfolder = $this->get_export_config('newfolder')) {
00053             $foldername = $newfolder . ' (' . get_string('tobecreated', 'portfolio_boxnet') . ')';
00054         } elseif ($this->get_export_config('folder')) {
00055             $foldername = $allfolders[$this->get_export_config('folder')];
00056         } else {
00057             $foldername = '';
00058         }
00059         return array(
00060             get_string('targetfolder', 'portfolio_boxnet') => $foldername
00061         );
00062     }
00063 
00064     public function get_interactive_continue_url() {
00065         return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
00066     }
00067 
00068     public function expected_time($callertime) {
00069         return $callertime;
00070     }
00071 
00072     public static function has_admin_config() {
00073         return true;
00074     }
00075 
00076     public static function get_allowed_config() {
00077         return array('apikey');
00078     }
00079 
00080     public function has_export_config() {
00081         return true;
00082     }
00083 
00084     public function get_allowed_user_config() {
00085         return array('authtoken', 'authtokenctime');
00086     }
00087 
00088     public function get_allowed_export_config() {
00089         return array('folder', 'newfolder', 'sharefile', 'sharefolder');
00090     }
00091 
00092     public function export_config_form(&$mform) {
00093         $folders = $this->get_folder_list();
00094         $mform->addElement('checkbox', 'plugin_sharefile', get_string('sharefile', 'portfolio_boxnet'));
00095         $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
00096         $mform->addElement('checkbox', 'plugin_sharefolder', get_string('sharefolder', 'portfolio_boxnet'));
00097         $folders[0] = '----';
00098         ksort($folders);
00099         $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
00100     }
00101 
00102     public function export_config_validation($data) {
00103         $allfolders = $this->get_folder_list();
00104         if (in_array($data['plugin_newfolder'], $allfolders)) {
00105             return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
00106         }
00107     }
00108 
00109     public function admin_config_form(&$mform) {
00110         global $CFG;
00111 
00112         $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
00113         $mform->addRule('apikey', get_string('required'), 'required', null, 'client');
00114         $a = new stdClass();
00115         $a->servicesurl = 'http://www.box.net/developers/services';
00116         $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=boxnet';
00117         $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_boxnet'),
00118             get_string('setupinfodetails', 'portfolio_boxnet', $a));
00119     }
00120 
00121     public function steal_control($stage) {
00122         if ($stage != PORTFOLIO_STAGE_CONFIG) {
00123             return false;
00124         }
00125         if ($this->authtoken) {
00126             return false;
00127         }
00128         if (!$this->ensure_ticket()) {
00129             throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
00130         }
00131         $token = $this->get_user_config('authtoken', $this->get('user')->id);
00132         $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
00133         if (!empty($token) && (($ctime + 60*60*20) > time())) {
00134             $this->authtoken = $token;
00135             $this->boxclient->auth_token = $token;
00136             return false;
00137         }
00138         return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
00139     }
00140 
00141     public function post_control($stage, $params) {
00142         if ($stage != PORTFOLIO_STAGE_CONFIG) {
00143             return;
00144         }
00145         if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
00146             throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
00147         }
00148         $this->authtoken = $params['auth_token'];
00149         $this->boxclient->auth_token = $this->authtoken;
00150         $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
00151     }
00152 
00153     private function ensure_ticket() {
00154         if (!empty($this->boxclient)) {
00155             return true;
00156         }
00157         $this->boxclient = new boxclient($this->get_config('apikey'), '');
00158         $ticket_return = $this->boxclient->getTicket();
00159         if ($this->boxclient->isError() || empty($ticket_return)) {
00160             throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
00161         }
00162         $this->ticket = $ticket_return['ticket'];
00163         return $this->ticket;
00164     }
00165 
00166     private function ensure_account_tree() {
00167         if (!empty($this->accounttree)) {
00168             return;
00169         }
00170         if (empty($this->ticket)
00171             || empty($this->authtoken)
00172             || empty($this->boxclient)) {
00173             // if we don't have these we're pretty much screwed
00174             throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
00175             return false;
00176         }
00177         $this->accounttree = $this->boxclient->getAccountTree();
00178         if ($this->boxclient->isError()) {
00179             throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
00180         }
00181         if (!is_array($this->accounttree)) {
00182             return false;
00183         }
00184     }
00185 
00186     private function get_folder_list() {
00187         if (!empty($this->folders)) {
00188             return $this->folders;
00189         }
00190         $this->ensure_account_tree();
00191         $folders = array();
00192         foreach ($this->accounttree['folder_id'] as $key => $id) {
00193             if (empty($id)) {
00194                 continue;
00195             }
00196             $name = $this->accounttree['folder_name'][$key];
00197             if (!empty($this->accounttree['shared'][$key])) {
00198                 $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
00199             }
00200             $folders[$id] = $name;
00201         }
00202         $this->folders = $folders;
00203         return $folders;
00204     }
00205 
00206     private function rename_file($fileid, $newname) {
00207         // look at moving this to the boxnet client class
00208         $this->ensure_account_tree();
00209         $count = 1;
00210         $bits = explode('.', $newname);
00211         $suffix = '';
00212         if (count($bits) == 1) {
00213             $prefix = $newname;
00214         } else {
00215             $suffix = '.' . array_pop($bits);
00216             $prefix = implode('.', $bits);
00217         }
00218         while (true) {
00219             if (!array_key_exists('file_name', $this->accounttree) || !in_array($newname, $this->accounttree['file_name'])) {
00220                 for ($i = 0; $i < 2; $i++) {
00221                     if ($this->boxclient->renameFile($fileid, $newname)) {
00222                         return true;
00223                     }
00224                 }
00225                 debugging("tried three times to rename file and failed");
00226                 return false;
00227             }
00228             $newname = $prefix . '(' . $count . ')' . $suffix;
00229             $count++;
00230         }
00231         return false;
00232     }
00233 
00234     public function instance_sanity_check() {
00235         if (!$this->get_config('apikey')) {
00236             return 'err_noapikey';
00237         }
00238     //@TODO see if we can verify the api key without actually getting an authentication token
00239     }
00240 
00241     public static function allows_multiple_instances() {
00242         return false;
00243     }
00244 
00245     public function supported_formats() {
00246         return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
00247     }
00248 
00249     /*
00250      * for now , boxnet doesn't support this,
00251      * because we can't dynamically construct return urls.
00252      */
00253     public static function allows_multiple_exports() {
00254         return false;
00255     }
00256 }
 All Data Structures Namespaces Files Functions Variables Enumerations