3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * repository_mahara class
20 * This plugin allowed to connect a retrieve a file from Mahara site
21 * This is a subclass of repository class
25 * @subpackage repository
26 * @copyright 2009 Jerome Mouneyrac <mouneyrac@moodle.org>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once($CFG->dirroot.'/repository/lib.php');
31 class repository_mahara extends repository {
36 * @param int $repositoryid
38 * @param array $options
40 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
42 parent::__construct($repositoryid, $context, $options);
43 $this->mnet = get_mnet_environment();
47 * Check if user logged in to mahara
48 * @global object $SESSION
51 public function check_login() {
54 return !empty($SESSION->loginmahara);
59 * Display the file listing - no login required
60 * @global object $SESSION
63 public function print_login() {
64 global $SESSION, $CFG, $DB;
65 //jump to the peer to create a session
67 $mnetauth = get_auth_plugin('mnet');
68 $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //need to retrieve the host url
69 $url = $mnetauth->start_jump_session($host->id, '/repository/repository_ajax.php?callback=yes&repo_id='.$this->id, true);
72 $SESSION->loginmahara = true;
75 $popup_btn = new stdclass;
76 $popup_btn->type = 'popup';
77 $popup_btn->url = $url;
78 $ret['login'] = array($popup_btn);
83 * Display the file listing for the search term
84 * @param string $search_text
87 public function search($search_text) {
88 return $this->get_listing('', '', $search_text);
92 * Retrieve the file listing - file picker function
95 * @global object $USER
98 * @param string $search
101 public function get_listing($path = null, $page = 1, $search = '') {
102 global $CFG, $DB, $USER, $OUTPUT;
104 ///check that Mahara has a good version
105 ///We also check that the "get file list" method has been activated (if it is not
106 ///the method will not be returned by the system method system/listMethods)
107 require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
109 ///check that the peer has been setup
110 if (!array_key_exists('peer',$this->options)) {
111 echo json_encode(array('e'=>get_string('error').' 9010: '.get_string('hostnotfound','repository_mahara')));
115 $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //need to retrieve the host url
117 ///check that the peer host exists into the database
119 echo json_encode(array('e'=>get_string('error').' 9011: '.get_string('hostnotfound','repository_mahara')));
123 $mnet_peer = new mnet_peer();
124 $mnet_peer->set_wwwroot($host->wwwroot);
125 $client = new mnet_xmlrpc_client();
126 $client->set_method('system/listMethods');
127 $client->send($mnet_peer);
128 $services = $client->response;
130 if (empty($search)) {
131 $methodname = 'get_folder_files';
133 $methodname = 'search_folders_and_files';
136 if (array_key_exists('repository/mahara/repository.class.php/'.$methodname, $services) === false) {
137 echo json_encode(array('e'=>get_string('connectionfailure','repository_mahara')));
141 ///connect to the remote moodle and retrieve the list of files
142 $client->set_method('repository/mahara/repository.class.php/'.$methodname);
143 $client->add_param($USER->username);
144 if (empty($search)) {
145 $client->add_param($path);
147 $client->add_param($search);
150 ///call the method and manage host error
151 if (!$client->send($mnet_peer)) {
153 foreach ($client->error as $errormessage) {
154 $message .= "ERROR: $errormessage . ";
156 echo json_encode(array('e'=>$message)); //display all error messages
160 $services = $client->response;
161 if (empty($search)) {
162 $newpath = $services[0];
163 $filesandfolders = $services[1];
166 $filesandfolders = $services;
169 ///display error message if we could retrieve the list or if nothing were returned
170 if (empty($filesandfolders)) {
171 echo json_encode(array('e'=>get_string('failtoretrievelist','repository_mahara')));
177 if (!empty($filesandfolders['folders'])) {
178 foreach ($filesandfolders['folders'] as $folder) {
179 $list[] = array('path'=>$folder['id'], 'title'=>$folder['title'], 'date'=>$folder['mtime'], 'size'=>'0', 'children'=>array(), 'thumbnail' => $OUTPUT->pix_url('f/folder'));
182 if (!empty($filesandfolders['files'])) {
183 foreach ($filesandfolders['files'] as $file) {
184 if ($file['artefacttype'] == 'image') {
185 $thumbnail = $host->wwwroot."/artefact/file/download.php?file=".$file['id']."&size=70x55";
187 $thumbnail = $OUTPUT->pix_url(file_extension_icon( $file['title'], 32));
189 $list[] = array( 'title'=>$file['title'], 'date'=>$file['mtime'], 'source'=>$file['id'], 'thumbnail' => $thumbnail);
194 $filepickerlisting = array(
199 'manage'=> $host->wwwroot.'/artefact/file/'
202 return $filepickerlisting;
209 * @global object $CFG
210 * @param string $url the url of file
211 * @param string $file save location
212 * @return string the location of the file
215 public function get_file($id, $file = '') {
216 global $CFG, $DB, $USER;
218 ///set mnet environment and set the mnet host
219 $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url
220 $mnet_peer = new mnet_peer();
221 $mnet_peer->set_wwwroot($host->wwwroot);
223 ///create the client and set the method to call
224 $client = new mnet_xmlrpc_client();
225 $client->set_method('repository/mahara/repository.class.php/get_file');
226 $client->add_param($USER->username);
227 $client->add_param($id);
229 ///call the method and manage host error
230 if (!$client->send($mnet_peer)) {
232 foreach ($client->error as $errormessage) {
233 $message .= "ERROR: $errormessage . ";
235 echo json_encode(array('e'=>$message));
239 $services = $client->response; //service contains the file content in the first case of the array,
240 //and the filename in the second
243 //the content has been encoded in base64, need to decode it
244 $content = base64_decode($services[0]);
245 $file = $services[1]; //filename
247 ///create a temporary folder with a file
248 $path = $this->prepare_file($file);
249 ///fill the file with the content
250 $fp = fopen($path, 'w');
251 fwrite($fp,$content);
254 return array('path'=>$path);
259 * Add Instance settings input to Moodle form
260 * @global object $CFG
262 * @param object $mform
264 public function instance_config_form($mform) {
267 //retrieve only Moodle peers
268 $hosts = $DB->get_records_sql(' SELECT
274 h.public_key_expires,
281 a.display_name as app_display_name,
284 JOIN {mnet_application} a ON h.applicationid=a.id
290 array($CFG->mnet_localhost_id, 'mahara', 'All Hosts'));
292 foreach($hosts as $host) {
293 $peers[$host->id] = $host->name;
297 $mform->addElement('select', 'peer', get_string('peer', 'repository_mahara'),$peers);
298 $mform->addRule('peer', get_string('required'), 'required', null, 'client');
301 $mform->addElement('static', null, '', get_string('nopeer','repository_mahara'));
306 * Names of the instance settings
309 public static function get_instance_option_names() {
310 ///the administrator just need to set a peer
311 return array('peer');
313 public function supported_returntypes() {
314 return FILE_INTERNAL;