Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/webservice/externallib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00029 class core_webservice_external extends external_api {
00030 
00035     public static function get_site_info_parameters() {
00036         return new external_function_parameters(
00037             array('serviceshortnames' => new external_multiple_structure (
00038                 new external_value(
00039                     PARAM_ALPHANUMEXT,
00040                     'service shortname'),
00041                     'DEPRECATED PARAMETER - it was a design error in the original implementation. It is ignored now. (parameter kept for backward compatibility)',
00042                     VALUE_DEFAULT,
00043                     array()
00044                 ),
00045             )
00046         );
00047     }
00048 
00056     public function get_site_info($serviceshortnames = array()) {
00057         global $USER, $SITE, $CFG, $DB;
00058 
00059         $params = self::validate_parameters(self::get_site_info_parameters(),
00060                       array('serviceshortnames'=>$serviceshortnames));
00061 
00062         $profileimageurl = moodle_url::make_pluginfile_url(
00063                 get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'icon', NULL, '/', 'f1');
00064 
00065         //site information
00066         $siteinfo =  array(
00067             'sitename' => $SITE->fullname,
00068             'siteurl' => $CFG->wwwroot,
00069             'username' => $USER->username,
00070             'firstname' => $USER->firstname,
00071             'lastname' => $USER->lastname,
00072             'fullname' => fullname($USER),
00073             'userid' => $USER->id,
00074             'userpictureurl' => $profileimageurl->out(false)
00075         );
00076 
00077         //Retrieve the service and functions from the web service linked to the token
00078         //If you call this function directly from external (not a web service call),
00079         //then it will still return site info without information about a service
00080         //Note: wsusername/wspassword ws authentication is not supported.
00081         $functions = array();
00082         if ($CFG->enablewebservices) { //no need to check token if web service are disabled and not a ws call
00083             $token = optional_param('wstoken', '', PARAM_ALPHANUM);
00084 
00085             if (!empty($token)) { //no need to run if not a ws call
00086                 //retrieve service shortname
00087                 $servicesql = 'SELECT s.*
00088                                FROM {external_services} s, {external_tokens} t
00089                                WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
00090                 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
00091 
00092                 $siteinfo['downloadfiles'] = $service->downloadfiles;
00093 
00094                 if (!empty($service)) {
00095                     //retrieve the functions
00096                     $functionssql = "SELECT f.*
00097                             FROM {external_functions} f, {external_services_functions} sf
00098                             WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
00099                     $functions = $DB->get_records_sql($functionssql, array($service->id));
00100                 } else {
00101                     throw new coding_exception('No service found in get_site_info: something is buggy, it should have fail at the ws server authentication layer.');
00102                 }
00103             }
00104         }
00105 
00106         //built up the returned values of the list of functions
00107         $componentversions = array();
00108         $avalaiblefunctions = array();
00109         foreach ($functions as $function) {
00110             $functioninfo = array();
00111             $functioninfo['name'] = $function->name;
00112             if ($function->component == 'moodle') {
00113                 $version = $CFG->version; //moodle version
00114             } else {
00115                 $versionpath = get_component_directory($function->component).'/version.php';
00116                 if (is_readable($versionpath)) {
00117                     //we store the component version once retrieved (so we don't load twice the version.php)
00118                     if (!isset($componentversions[$function->component])) {
00119                         include($versionpath);
00120                         $componentversions[$function->component] = $plugin->version;
00121                         $version = $plugin->version;
00122                     } else {
00123                         $version = $componentversions[$function->component];
00124                     }
00125                 } else {
00126                     //function component should always have a version.php,
00127                     //otherwise the function should have been described with component => 'moodle'
00128                     throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
00129                 }
00130             }
00131             $functioninfo['version'] = $version;
00132             $avalaiblefunctions[] = $functioninfo;
00133         }
00134 
00135         $siteinfo['functions'] = $avalaiblefunctions;
00136 
00137         return $siteinfo;
00138     }
00139 
00144     public static function get_site_info_returns() {
00145         return new external_single_structure(
00146             array(
00147                 'sitename'       => new external_value(PARAM_RAW, 'site name'),
00148                 'username'       => new external_value(PARAM_RAW, 'username'),
00149                 'firstname'      => new external_value(PARAM_TEXT, 'first name'),
00150                 'lastname'       => new external_value(PARAM_TEXT, 'last name'),
00151                 'fullname'       => new external_value(PARAM_TEXT, 'user full name'),
00152                 'userid'         => new external_value(PARAM_INT, 'user id'),
00153                 'siteurl'        => new external_value(PARAM_RAW, 'site url'),
00154                 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture'),
00155                 'functions'      => new external_multiple_structure(
00156                     new external_single_structure(
00157                         array(
00158                             'name' => new external_value(PARAM_RAW, 'function name'),
00159                             'version' => new external_value(PARAM_FLOAT, 'The version number of moodle site/local plugin linked to the function')
00160                         ), 'functions that are available')
00161                     ),
00162                 'downloadfiles'  => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not', VALUE_OPTIONAL),
00163             )
00164         );
00165     }
00166 }
00167 
00172 class moodle_webservice_external extends external_api {
00173 
00179     public static function get_siteinfo_parameters() {
00180         return core_webservice_external::get_site_info_parameters();
00181     }
00182 
00191     public function get_siteinfo($serviceshortnames = array()) {
00192         return core_webservice_external::get_site_info($serviceshortnames);
00193     }
00194 
00200     public static function get_siteinfo_returns() {
00201         return core_webservice_external::get_site_info_returns();
00202     }
00203 }
 All Data Structures Namespaces Files Functions Variables Enumerations