Merge branch 'MDL-32164-master-2' of git://git.luns.net.uk/moodle
[moodle.git] / webservice / externallib.php
blob7b006d6e1b19bf53e2b2c64ef54518a2b6d29218
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.
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/>.
18 /**
19 * external API for mobile web services
21 * @package core_webservice
22 * @category external
23 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 /**
28 * Web service related functions
30 * @package core_webservice
31 * @category external
32 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 * @since Moodle 2.2
36 class core_webservice_external extends external_api {
38 /**
39 * Returns description of method parameters
41 * @return external_function_parameters
42 * @since Moodle 2.2
44 public static function get_site_info_parameters() {
45 return new external_function_parameters(
46 array('serviceshortnames' => new external_multiple_structure (
47 new external_value(
48 PARAM_ALPHANUMEXT,
49 'service shortname'),
50 'DEPRECATED PARAMETER - it was a design error in the original implementation. It is ignored now. (parameter kept for backward compatibility)',
51 VALUE_DEFAULT,
52 array()
58 /**
59 * Return user information including profile picture + basic site information
60 * Note:
61 * - no capability checking because we return only known information about logged user
63 * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored - it was an original design error, we keep for backward compatibility.
64 * @return array site info
65 * @since Moodle 2.2
67 public function get_site_info($serviceshortnames = array()) {
68 global $USER, $SITE, $CFG, $DB;
70 $params = self::validate_parameters(self::get_site_info_parameters(),
71 array('serviceshortnames'=>$serviceshortnames));
73 $profileimageurl = moodle_url::make_pluginfile_url(
74 get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'icon', NULL, '/', 'f1');
76 //site information
77 $siteinfo = array(
78 'sitename' => $SITE->fullname,
79 'siteurl' => $CFG->wwwroot,
80 'username' => $USER->username,
81 'firstname' => $USER->firstname,
82 'lastname' => $USER->lastname,
83 'fullname' => fullname($USER),
84 'userid' => $USER->id,
85 'userpictureurl' => $profileimageurl->out(false)
88 //Retrieve the service and functions from the web service linked to the token
89 //If you call this function directly from external (not a web service call),
90 //then it will still return site info without information about a service
91 //Note: wsusername/wspassword ws authentication is not supported.
92 $functions = array();
93 if ($CFG->enablewebservices) { //no need to check token if web service are disabled and not a ws call
94 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
96 if (!empty($token)) { //no need to run if not a ws call
97 //retrieve service shortname
98 $servicesql = 'SELECT s.*
99 FROM {external_services} s, {external_tokens} t
100 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
101 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
103 $siteinfo['downloadfiles'] = $service->downloadfiles;
105 if (!empty($service)) {
106 //retrieve the functions
107 $functionssql = "SELECT f.*
108 FROM {external_functions} f, {external_services_functions} sf
109 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
110 $functions = $DB->get_records_sql($functionssql, array($service->id));
111 } else {
112 throw new coding_exception('No service found in get_site_info: something is buggy, it should have fail at the ws server authentication layer.');
117 //built up the returned values of the list of functions
118 $componentversions = array();
119 $avalaiblefunctions = array();
120 foreach ($functions as $function) {
121 $functioninfo = array();
122 $functioninfo['name'] = $function->name;
123 if ($function->component == 'moodle') {
124 $version = $CFG->version; //moodle version
125 } else {
126 $versionpath = get_component_directory($function->component).'/version.php';
127 if (is_readable($versionpath)) {
128 //we store the component version once retrieved (so we don't load twice the version.php)
129 if (!isset($componentversions[$function->component])) {
130 include($versionpath);
131 $componentversions[$function->component] = $plugin->version;
132 $version = $plugin->version;
133 } else {
134 $version = $componentversions[$function->component];
136 } else {
137 //function component should always have a version.php,
138 //otherwise the function should have been described with component => 'moodle'
139 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
142 $functioninfo['version'] = $version;
143 $avalaiblefunctions[] = $functioninfo;
146 $siteinfo['functions'] = $avalaiblefunctions;
148 return $siteinfo;
152 * Returns description of method result value
154 * @return external_single_structure
155 * @since Moodle 2.2
157 public static function get_site_info_returns() {
158 return new external_single_structure(
159 array(
160 'sitename' => new external_value(PARAM_RAW, 'site name'),
161 'username' => new external_value(PARAM_RAW, 'username'),
162 'firstname' => new external_value(PARAM_TEXT, 'first name'),
163 'lastname' => new external_value(PARAM_TEXT, 'last name'),
164 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
165 'userid' => new external_value(PARAM_INT, 'user id'),
166 'siteurl' => new external_value(PARAM_RAW, 'site url'),
167 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
168 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
169 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
170 "webservice/pluginfile.php?token=WSTOKEN&file=". Of course the user can only see profile picture depending on his/her permissions.
171 Moreover it is recommended to use HTTPS too.'),
172 'functions' => new external_multiple_structure(
173 new external_single_structure(
174 array(
175 'name' => new external_value(PARAM_RAW, 'function name'),
176 'version' => new external_value(PARAM_FLOAT, 'The version number of moodle site/local plugin linked to the function')
177 ), 'functions that are available')
179 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not', VALUE_OPTIONAL),
186 * Deprecated web service related functions
188 * @package core_webservice
189 * @category external
190 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
191 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
192 * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
193 * @todo MDL-31194 This will be deleted in Moodle 2.5.
194 * @see core_webservice_external
195 * @since Moodle 2.1
197 class moodle_webservice_external extends external_api {
200 * Returns description of method parameters
202 * @return external_function_parameters
203 * @deprecated Moodle 2.2 - please do not use this function any more.
204 * @todo MDL-31194 This will be deleted in Moodle 2.5.
205 * @see core_webservice_external::get_site_info_parameters
206 * @since Moodle 2.1
208 public static function get_siteinfo_parameters() {
209 return core_webservice_external::get_site_info_parameters();
213 * Return user information including profile picture + basic site information
214 * Note:
215 * - no capability checking because we return just known information by logged user
217 * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
218 * @return array
219 * @deprecated Moodle 2.2 - please do not use this function any more.
220 * @todo MDL-31194 This will be deleted in Moodle 2.5.
221 * @see core_webservice_external::get_site_info
222 * @since Moodle 2.1
224 public function get_siteinfo($serviceshortnames = array()) {
225 return core_webservice_external::get_site_info($serviceshortnames);
229 * Returns description of method result value
231 * @return external_single_structure
232 * @deprecated Moodle 2.2 - please do not use this function any more.
233 * @todo MDL-31194 This will be deleted in Moodle 2.5.
234 * @see core_webservice_external::get_site_info_returns
235 * @since Moodle 2.1
237 public static function get_siteinfo_returns() {
238 return core_webservice_external::get_site_info_returns();