MDL-21694 Moving question types strings into plugin space
[moodle.git] / repository / mahara / repository.class.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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.
14 //
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/>.
18 /**
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
22  *
23  * @since 2.0
24  * @package moodlecore
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
28  */
29 require_once($CFG->dirroot.'/repository/lib.php');
31 class repository_mahara extends repository {
33     /**
34      * Constructor
35      * @global object $CFG
36      * @param int $repositoryid
37      * @param int $context
38      * @param array $options
39      */
40     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
41         global $CFG;
42         parent::__construct($repositoryid, $context, $options);
43         $this->mnet = get_mnet_environment();
44     }
46     /**
47      * Check if user logged in to mahara
48      * @global object $SESSION
49      * @return boolean
50      */
51     public function check_login() {
52         //check session
53         global $SESSION;
54         return !empty($SESSION->loginmahara);
55     }
58     /**
59      * Display the file listing - no login required
60      * @global object $SESSION
61      * @return array
62      */
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);
71         //set session
72         $SESSION->loginmahara = true;
74         $ret = array();
75         $popup_btn = new stdclass;
76         $popup_btn->type = 'popup';
77         $popup_btn->url = $url;
78         $ret['login'] = array($popup_btn);
79         return $ret;
80     }
82     /**
83      * Display the file listing for the search term
84      * @param string $search_text
85      * @return array
86      */
87     public function search($search_text) {
88         return $this->get_listing('', '', $search_text);
89     }
91     /**
92      * Retrieve the file listing - file picker function
93      * @global object $CFG
94      * @global object $DB
95      * @global object $USER
96      * @param string $path
97      * @param string $page
98      * @param string $search
99      * @return array
100      */
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')));
112             exit;
113         }
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
118         if (empty($host)) {
119             echo json_encode(array('e'=>get_string('error').' 9011: '.get_string('hostnotfound','repository_mahara')));
120             exit;
121         }
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';
132         } else {
133             $methodname = 'search_folders_and_files';
134         }
136         if (array_key_exists('repository/mahara/repository.class.php/'.$methodname, $services) === false) {
137             echo json_encode(array('e'=>get_string('connectionfailure','repository_mahara')));
138             exit;
139         }
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);
146         } else {
147              $client->add_param($search);
148         }
150         ///call the method and manage host error
151         if (!$client->send($mnet_peer)) {
152             $message =" ";
153             foreach ($client->error as $errormessage) {
154                 $message .= "ERROR: $errormessage . ";
155             }
156             echo json_encode(array('e'=>$message)); //display all error messages
157             exit;
158         }
160         $services = $client->response;
161         if (empty($search)) {
162              $newpath = $services[0];
163             $filesandfolders = $services[1];
164         } else {
165             $newpath = '';
166             $filesandfolders = $services;
167         }
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')));
172             exit;
173         }
176         $list = array();
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'));
180             }
181         }
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";
186                 } else {
187                     $thumbnail = $OUTPUT->pix_url(file_extension_icon( $file['title'], 32));
188                 }
189                 $list[] = array( 'title'=>$file['title'], 'date'=>$file['mtime'], 'source'=>$file['id'], 'thumbnail' => $thumbnail);
190             }
191         }
194         $filepickerlisting = array(
195             'path' => $newpath,
196             'dynload' => 1,
197             'nosearch' => 0,
198             'list'=> $list,
199             'manage'=> $host->wwwroot.'/artefact/file/'
200         );
202         return $filepickerlisting;
203     }
207     /**
208      * Download a file
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
213      * @see curl package
214      */
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)) {
231             $message =" ";
232             foreach ($client->error as $errormessage) {
233                 $message .= "ERROR: $errormessage . ";
234             }
235             echo json_encode(array('e'=>$message));
236             exit;
237         }
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);
252         fclose($fp);
254         return array('path'=>$path);
256     }
258     /**
259      * Add Instance settings input to Moodle form
260      * @global object $CFG
261      * @global object $DB
262      * @param object $mform
263      */
264     public function instance_config_form($mform) {
265         global $CFG, $DB;
267         //retrieve only Moodle peers
268         $hosts = $DB->get_records_sql('  SELECT
269                                     h.id,
270                                     h.wwwroot,
271                                     h.ip_address,
272                                     h.name,
273                                     h.public_key,
274                                     h.public_key_expires,
275                                     h.transport,
276                                     h.portno,
277                                     h.last_connect_time,
278                                     h.last_log_id,
279                                     h.applicationid,
280                                     a.name as app_name,
281                                     a.display_name as app_display_name,
282                                     a.xmlrpc_server_url
283                                 FROM {mnet_host} h
284                                     JOIN {mnet_application} a ON h.applicationid=a.id
285                                 WHERE
286                                     h.id <> ? AND
287                                     h.deleted = 0 AND
288                                     a.name = ? AND
289                                     h.name <> ?',
290             array($CFG->mnet_localhost_id, 'mahara', 'All Hosts'));
291         $peers = array();
292         foreach($hosts as $host) {
293             $peers[$host->id] = $host->name;
294         }
297         $mform->addElement('select', 'peer', get_string('peer', 'repository_mahara'),$peers);
298         $mform->addRule('peer', get_string('required'), 'required', null, 'client');
300         if (empty($peers)) {
301             $mform->addElement('static', null, '',  get_string('nopeer','repository_mahara'));
302         }
303     }
305     /**
306      * Names of the instance settings
307      * @return int
308      */
309     public static function get_instance_option_names() {
310         ///the administrator just need to set a peer
311         return array('peer');
312     }
313     public function supported_returntypes() {
314         return FILE_INTERNAL;
315     }