From: jerome Date: Wed, 22 Apr 2009 09:04:38 +0000 (+0000) Subject: repository MDL-16909 working beta mahara plugin, miss search function X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=842feb0612b228645438a445f2d22352a82e6177;p=moodle.git repository MDL-16909 working beta mahara plugin, miss search function --- diff --git a/repository/mahara/icon.png b/repository/mahara/icon.png new file mode 100644 index 0000000000..53763ff131 Binary files /dev/null and b/repository/mahara/icon.png differ diff --git a/repository/mahara/repository.class.php b/repository/mahara/repository.class.php new file mode 100644 index 0000000000..acaf301dca --- /dev/null +++ b/repository/mahara/repository.class.php @@ -0,0 +1,303 @@ +dirroot.'/repository/lib.php'); + +/** + * repository_mahara class + * This plugin allowed to connect a retrieve a file from Mahara site + * This is a subclass of repository class + */ +class repository_mahara extends repository { + + /** + * Constructor + * @global $SESSION + * @global $action + * @global $CFG + * @param $repositoryid + * @param $context + * @param $options + */ + public function __construct($repositoryid, $context = SITEID, $options = array()) { + global $SESSION, $action, $CFG; + parent::__construct($repositoryid, $context, $options); + } + + /** + * Declaration of the methods avalaible from mnet + * @return + */ + public static function mnet_publishes() { + $pf= array(); + $pf['name'] = 'remoterep'; // Name & Description go in lang file + $pf['apiversion'] = 1; + $pf['methods'] = array('get_folder_files_for_moodle', 'get_file'); + + return array($pf); + } + + /** + * Display the file listing - no login required + * @global $SESSION + * @param $ajax + * @return + */ + public function print_login($ajax = true) { + global $SESSION; + return $this->get_listing(); + } + + /** + * Display the file listing for the search term + * @param $search_text + * @return + */ + public function search($search_text) { + return $this->get_listing('', '', $search_text); + } + + /** + * Set the MNET environment + * @global $MNET + */ + private function ensure_environment() { + global $MNET; + if (empty($MNET)) { + $MNET = new mnet_environment(); + $MNET->init(); + } + } + + /** + * Retrieve the file listing - file picker function + * @global $CFG + * @global $DB + * @global $USER + * @param $encodedpath + * @param $search + * @return + */ + public function get_listing($path = null, $page = '', $search = '') { + global $CFG, $DB, $USER; + varlog($path); + ///check that Mahara has a good version + ///We also check that the "get file list" method has been activated (if it is not + ///the method will not be returned by the system method system/listMethods) + require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); + $this->ensure_environment(); + + ///check that the peer has been setup + if (!array_key_exists('peer',$this->options)) { + echo json_encode(array('e'=>get_string('error').' 9010: '.get_string('hostnotfound','repository_mahara'))); + exit; + } + + $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //need to retrieve the host url + + ///check that the peer host exists into the database + if (empty($host)) { + echo json_encode(array('e'=>get_string('error').' 9011: '.get_string('hostnotfound','repository_mahara'))); + exit; + } + + $mnet_peer = new mnet_peer(); + $mnet_peer->set_wwwroot($host->wwwroot); + $client = new mnet_xmlrpc_client(); + $client->set_method('system/listMethods'); + $client->send($mnet_peer); + $services = $client->response; + + if (array_key_exists('repository/mahara/repository.class.php/get_folder_files_for_moodle', $services) === false) { + varlog($services); + echo json_encode(array('e'=>get_string('connectionfailure','repository_mahara'))); + exit; + } + + + ///connect to the remote moodle and retrieve the list of files + $client->set_method('repository/mahara/repository.class.php/get_folder_files_for_moodle'); + $client->add_param($USER->username); + $client->add_param($path); + $client->add_param($search); + + ///call the method and manage host error + if (!$client->send($mnet_peer)) { + $message =" "; + foreach ($client->error as $errormessage) { + $message .= "ERROR: $errormessage . "; + } + echo json_encode(array('e'=>$message)); //display all error messages + exit; + } + + $services = $client->response; + $newpath = $services[0]; + $filesandfolders = $services[1]; + varlog("Here is the return value:"); + varlog($filesandfolders); + ///display error message if we could retrieve the list or if nothing were returned + if (empty($filesandfolders)) { + echo json_encode(array('e'=>get_string('failtoretrievelist','repository_mahara'))); + exit; + } + + + $list = array(); + if (!empty($filesandfolders['files'])) { + foreach ($filesandfolders['files'] as $file) { + $list[] = array( 'title'=>$file['title'], 'date'=>$file['mtime'], 'size'=>'10MB', 'source'=>$file['id'], 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $file['title'])); + } + } + if (!empty($filesandfolders['folders'])) { + foreach ($filesandfolders['folders'] as $folder) { + $list[] = array('path'=>$folder['id'], 'title'=>$folder['title'], 'date'=>$folder['mtime'], 'size'=>'0', 'children'=>array(), 'thumbnail' => $CFG->pixpath .'/f/folder.gif'); + } + } + + $filepickerlisting = array( + 'path' => $newpath, + 'dynload' => 1, + 'nosearch' => 1, + 'list'=> $list, + ); + + varlog($filepickerlisting); + + return $filepickerlisting; + } + + + + /** + * Download a file + * @global object $CFG + * @param string $url the url of file + * @param string $file save location + * @return string the location of the file + * @see curl package + */ + public function get_file($id, $file = '') { + global $CFG, $DB, $USER; + + ///set mnet environment and set the mnet host + require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); + $this->ensure_environment(); + $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url + $mnet_peer = new mnet_peer(); + $mnet_peer->set_wwwroot($host->wwwroot); + + ///create the client and set the method to call + $client = new mnet_xmlrpc_client(); + $client->set_method('repository/mahara/repository.class.php/get_file'); + $client->add_param($USER->username); + $client->add_param($id); + + ///call the method and manage host error + if (!$client->send($mnet_peer)) { + $message =" "; + foreach ($client->error as $errormessage) { + $message .= "ERROR: $errormessage . "; + } + echo json_encode(array('e'=>$message)); + exit; + } + + $services = $client->response; //service contains the file content in the first case of the array, + //and the filename in the second + + + //the content has been encoded in base64, need to decode it + $content = base64_decode($services[0]); + $file = $services[1]; //filename + + ///create a temporary folder with a file + $path = $this->prepare_file($file); + ///fill the file with the content + $fp = fopen($path, 'w'); + fwrite($fp,$content); + fclose($fp); + + return $path; + + } + + /** + * Add Instance settings input to Moodle form + * @global $CFG + * @global $DB + * @param $ + */ + public function instance_config_form(&$mform) { + global $CFG, $DB; + + //retrieve only Moodle peers + $hosts = $DB->get_records_sql(' SELECT + h.id, + h.wwwroot, + h.ip_address, + h.name, + h.public_key, + h.public_key_expires, + h.transport, + h.portno, + h.last_connect_time, + h.last_log_id, + h.applicationid, + a.name as app_name, + a.display_name as app_display_name, + a.xmlrpc_server_url + FROM {mnet_host} h + JOIN {mnet_application} a ON h.applicationid=a.id + WHERE + h.id <> ? AND + h.deleted = 0 AND + a.name = ? AND + h.name <> ?', + array($CFG->mnet_localhost_id, 'mahara', 'All Hosts')); + $peers = array(); + foreach($hosts as $host) { + $peers[$host->id] = $host->name; + } + + + $mform->addElement('select', 'peer', get_string('peer', 'repository_mahara'),$peers); + $mform->addRule('peer', get_string('required'), 'required', null, 'client'); + + if (empty($peers)) { + $mform->addElement('static', null, '', get_string('nopeer','repository_mahara')); + } + } + + /** + * Names of the instance settings + * @return + */ + public static function get_instance_option_names() { + ///the administrator just need to set a peer + return array('peer'); + } +} +?>