Merge branch 'MDL-31983-master' of git://github.com/FMCorz/moodle
[moodle.git] / webservice / externallib.php
bloba51b2136f60b129a4b601fa95591370d6535b0af
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 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
31 /**
32 * Web service related functions
34 * @package core_webservice
35 * @category external
36 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.2
40 class core_webservice_external extends external_api {
42 /**
43 * Returns description of method parameters
45 * @return external_function_parameters
46 * @since Moodle 2.2
48 public static function get_site_info_parameters() {
49 return new external_function_parameters(
50 array('serviceshortnames' => new external_multiple_structure (
51 new external_value(
52 PARAM_ALPHANUMEXT,
53 'service shortname'),
54 'DEPRECATED PARAMETER - it was a design error in the original implementation. \
55 It is ignored now. (parameter kept for backward compatibility)',
56 VALUE_DEFAULT,
57 array()
63 /**
64 * Return user information including profile picture + basic site information
65 * Note:
66 * - no capability checking because we return only known information about logged user
68 * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored -
69 * it was an original design error, we keep for backward compatibility.
70 * @return array site info
71 * @since Moodle 2.2
73 public static function get_site_info($serviceshortnames = array()) {
74 global $USER, $SITE, $CFG, $DB;
76 $params = self::validate_parameters(self::get_site_info_parameters(),
77 array('serviceshortnames'=>$serviceshortnames));
79 $profileimageurl = moodle_url::make_pluginfile_url(
80 context_user::instance($USER->id)->id, 'user', 'icon', null, '/', 'f1');
82 // Site information.
83 $siteinfo = array(
84 'sitename' => $SITE->fullname,
85 'siteurl' => $CFG->wwwroot,
86 'username' => $USER->username,
87 'firstname' => $USER->firstname,
88 'lastname' => $USER->lastname,
89 'fullname' => fullname($USER),
90 'lang' => current_language(),
91 'userid' => $USER->id,
92 'userpictureurl' => $profileimageurl->out(false)
95 // Retrieve the service and functions from the web service linked to the token
96 // If you call this function directly from external (not a web service call),
97 // then it will still return site info without information about a service
98 // Note: wsusername/wspassword ws authentication is not supported.
99 $functions = array();
100 if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
101 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
103 if (!empty($token)) { // No need to run if not a ws call.
104 // Retrieve service shortname.
105 $servicesql = 'SELECT s.*
106 FROM {external_services} s, {external_tokens} t
107 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
108 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
110 $siteinfo['downloadfiles'] = $service->downloadfiles;
112 if (!empty($service)) {
113 // Return the release and version number for web service users only.
114 $siteinfo['release'] = $CFG->release;
115 $siteinfo['version'] = $CFG->version;
116 // Retrieve the functions.
117 $functionssql = "SELECT f.*
118 FROM {external_functions} f, {external_services_functions} sf
119 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
120 $functions = $DB->get_records_sql($functionssql, array($service->id));
121 } else {
122 throw new coding_exception('No service found in get_site_info: something is buggy, \
123 it should have fail at the ws server authentication layer.');
128 // Build up the returned values of the list of functions.
129 $componentversions = array();
130 $availablefunctions = array();
131 foreach ($functions as $function) {
132 $functioninfo = array();
133 $functioninfo['name'] = $function->name;
134 if ($function->component == 'moodle' || $function->component == 'core') {
135 $version = $CFG->version; // Moodle version.
136 } else {
137 $versionpath = get_component_directory($function->component).'/version.php';
138 if (is_readable($versionpath)) {
139 // We store the component version once retrieved (so we don't load twice the version.php).
140 if (!isset($componentversions[$function->component])) {
141 include($versionpath);
142 $componentversions[$function->component] = $plugin->version;
143 $version = $plugin->version;
144 } else {
145 $version = $componentversions[$function->component];
147 } else {
148 // Function component should always have a version.php,
149 // otherwise the function should have been described with component => 'moodle'.
150 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
153 $functioninfo['version'] = $version;
154 $availablefunctions[] = $functioninfo;
157 $siteinfo['functions'] = $availablefunctions;
159 // Mobile CSS theme and alternative login url
160 $siteinfo['mobilecssurl'] = get_config('admin', 'mobilecssurl');
162 return $siteinfo;
166 * Returns description of method result value
168 * @return external_single_structure
169 * @since Moodle 2.2
171 public static function get_site_info_returns() {
172 return new external_single_structure(
173 array(
174 'sitename' => new external_value(PARAM_RAW, 'site name'),
175 'username' => new external_value(PARAM_RAW, 'username'),
176 'firstname' => new external_value(PARAM_TEXT, 'first name'),
177 'lastname' => new external_value(PARAM_TEXT, 'last name'),
178 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
179 'lang' => new external_value(PARAM_LANG, 'user language'),
180 'userid' => new external_value(PARAM_INT, 'user id'),
181 'siteurl' => new external_value(PARAM_RAW, 'site url'),
182 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
183 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
184 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
185 "webservice/pluginfile.php?token=WSTOKEN&file="
186 Of course the user can only see profile picture depending
187 on his/her permissions. Moreover it is recommended to use HTTPS too.'),
188 'functions' => new external_multiple_structure(
189 new external_single_structure(
190 array(
191 'name' => new external_value(PARAM_RAW, 'function name'),
192 'version' => new external_value(PARAM_TEXT,
193 'The version number of the component to which the function belongs')
194 ), 'functions that are available')
196 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
197 VALUE_OPTIONAL),
198 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
199 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
200 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL)
207 * Deprecated web service related functions
209 * @package core_webservice
210 * @category external
211 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
212 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
213 * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
214 * @todo MDL-31194 This will be deleted in Moodle 2.5.
215 * @see core_webservice_external
216 * @since Moodle 2.1
218 class moodle_webservice_external extends external_api {
221 * Returns description of method parameters
223 * @return external_function_parameters
224 * @deprecated Moodle 2.2 - please do not use this function any more.
225 * @todo MDL-31194 This will be deleted in Moodle 2.5.
226 * @see core_webservice_external::get_site_info_parameters
227 * @since Moodle 2.1
229 public static function get_siteinfo_parameters() {
230 return core_webservice_external::get_site_info_parameters();
234 * Return user information including profile picture + basic site information
235 * Note:
236 * - no capability checking because we return just known information by logged user
238 * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
239 * @return array
240 * @deprecated Moodle 2.2 - please do not use this function any more.
241 * @todo MDL-31194 This will be deleted in Moodle 2.5.
242 * @see core_webservice_external::get_site_info
243 * @since Moodle 2.1
245 public function get_siteinfo($serviceshortnames = array()) {
246 return core_webservice_external::get_site_info($serviceshortnames);
250 * Returns description of method result value
252 * @return external_single_structure
253 * @deprecated Moodle 2.2 - please do not use this function any more.
254 * @todo MDL-31194 This will be deleted in Moodle 2.5.
255 * @see core_webservice_external::get_site_info_returns
256 * @since Moodle 2.1
258 public static function get_siteinfo_returns() {
259 return core_webservice_external::get_site_info_returns();