Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
 
18
/**
19
 * Moodle REST library
20
 *
21
 * @package    webservice_rest
22
 * @copyright  2009 Jerome Mouneyrac
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
 
27
/**
28
 * Moodle REST client
29
 *
30
 * It has been implemented for unit testing purpose (all protocols have similar client)
31
 *
32
 * @package    webservice_rest
33
 * @copyright  2010 Jerome Mouneyrac
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class webservice_rest_client {
37
 
38
    /** @var moodle_url the REST server url */
39
    private $serverurl;
40
 
41
    /** @var string token */
42
    private $token;
43
 
44
    /** @var string returned value format: xml or json */
45
    private $format;
46
 
47
    /**
48
     * Constructor
49
     *
50
     * @param string $serverurl a Moodle URL
51
     * @param string $token the token used to do the web service call
52
     * @param string $format returned value format: xml or json
53
     */
54
    public function __construct($serverurl, $token, $format = 'xml') {
55
        $this->serverurl = new moodle_url($serverurl);
56
        $this->token = $token;
57
        $this->format = $format;
58
    }
59
 
60
    /**
61
     * Set the token used to do the REST call
62
     *
63
     * @param string $token the token used to do the web service call
64
     */
65
    public function set_token($token) {
66
        $this->token = $token;
67
    }
68
 
69
    /**
70
     * Execute client WS request with token authentication
71
     *
72
     * @param string $functionname the function name
73
     * @param array $params the parameters of the function
74
     * @return mixed
75
     */
76
    public function call($functionname, $params) {
77
        global $DB, $CFG;
78
 
79
         if ($this->format == 'json') {
80
             $formatparam = '&moodlewsrestformat=json';
81
             $this->serverurl->param('moodlewsrestformat','json');
82
         } else {
83
             $formatparam = ''; //to keep retro compability with old server that only support xml (they don't expect this param)
84
         }
85
 
86
        $this->serverurl->param('wstoken',$this->token);
87
        $this->serverurl->param('wsfunction',$functionname); //you could also use params().
88
 
89
        $result = download_file_content($this->serverurl->out(false), null, $params);
90
 
91
        //TODO MDL-22965 transform the XML result into PHP values
92
        if ($this->format == 'json') {
93
            $result = json_decode($result);
94
        }
95
 
96
        return $result;
97
    }
98
 
99
}