From 0bf486a619a1084a6ea6e5b7be8a9990a3cf062f Mon Sep 17 00:00:00 2001 From: Jerome Mouneyrac Date: Tue, 7 Jun 2011 15:19:53 +0800 Subject: [PATCH] MDL-26777 moodle_webservice_mobile_get_siteinfo TODO: add this service name into the service of MDL-27551 --- lang/en/webservice.php | 1 + lib/db/services.php | 10 ++ version.php | 2 +- webservice/externallib.php | 134 +++++++++++++++++++++++ webservice/lib.php | 25 +++++ webservice/simpletest/testwebservice.php | 15 ++- 6 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 webservice/externallib.php diff --git a/lang/en/webservice.php b/lang/en/webservice.php index f184bc6b046..eaab7b63815 100644 --- a/lang/en/webservice.php +++ b/lang/en/webservice.php @@ -115,6 +115,7 @@ $string['missingcaps'] = 'Missing capabilities'; $string['missingcaps_help'] = 'List of required capabilities for the service which the selected user does not have. Missing capabilities must be added to the user\'s role in order to use the service.'; $string['missingpassword'] = 'Missing password'; $string['missingusername'] = 'Missing username'; +$string['missingversionfile'] = 'Coding error: version.php file is missing for the component {$a}'; $string['nofunctions'] = 'This service has no functions.'; $string['norequiredcapability'] = 'No required capability'; $string['notoken'] = 'The token list is empty.'; diff --git a/lib/db/services.php b/lib/db/services.php index 51cf05bae67..035895696b1 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -206,4 +206,14 @@ $functions = array( 'capabilities'=> 'moodle/course:create,moodle/course:visibility', ), + // === webservice related functions === + + 'moodle_webservice_get_siteinfo' => array( + 'classname' => 'moodle_webservice_external', + 'methodname' => 'get_siteinfo', + 'classpath' => 'webservice/externallib.php', + 'description' => 'Return some site info / user info / list web service functions', + 'type' => 'read', + ), + ); diff --git a/version.php b/version.php index caaa6ff2258..0027a199225 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2011060200.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2011060200.01; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes diff --git a/webservice/externallib.php b/webservice/externallib.php new file mode 100644 index 00000000000..5081c04b119 --- /dev/null +++ b/webservice/externallib.php @@ -0,0 +1,134 @@ +. + +/** + * external API for mobile web services + * + * @package core + * @subpackage webservice + * @copyright 2011 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +class moodle_webservice_external extends external_api { + + public static function get_siteinfo_parameters() { + return new external_function_parameters( + array('serviceshortnames' => new external_multiple_structure ( + new external_value(PARAM_ALPHANUMEXT, 'service shortname'), + 'service shortnames - by default, if the list is empty and mobile web services are enabled, + we return the mobile service functions', VALUE_DEFAULT, array()), + ) + ); + } + + /** + * Return user information including profil picture + basic site information + * Note: + * - no capability checking because we return just known information by logged user + * $param $serviceshortnames array of service shortnames - the functions of these services will be returned + * @global type $USER + * @global type $SITE + * @global type $CFG + * @return type + */ + function get_siteinfo($serviceshortnames = array()) { + global $USER, $SITE, $CFG; + + $params = self::validate_parameters(self::get_siteinfo_parameters(), + array('serviceshortnames'=>$serviceshortnames)); + + $profileimageurl = moodle_url::make_pluginfile_url( + get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'icon', NULL, '/', 'f1'); + + require_once($CFG->dirroot . "/webservice/lib.php"); + $webservice = new webservice(); + + //If no service listed always return the mobile one by default + if (empty($params['serviceshortnames']) and $CFG->enablewebservices) { + $mobileservice = $webservice->get_external_service_by_shortname(MOODLE_OFFICIAL_MOBILE_SERVICE); + if ($mobileservice->enabled) { + $params['serviceshortnames'] = array(MOODLE_OFFICIAL_MOBILE_SERVICE); //return mobile service by default + } + } + + //retrieve the functions related to the services + $functions = $webservice->get_external_functions_by_enabled_services($params['serviceshortnames']); + + //built up the returned values of the list of functions + $componentversions = array(); + $avalaiblefunctions = array(); + foreach ($functions as $function) { + $functioninfo = array(); + $functioninfo['name'] = $function->name; + if ($function->component == 'moodle') { + $version = $CFG->version; //moodle version + } else { + $versionpath = get_component_directory($function->component).'/version.php'; + if (is_readable($versionpath)) { + //we store the component version once retrieved (so we don't load twice the version.php) + if (!isset($componentversions[$function->component])) { + include($versionpath); + $componentversions[$function->component] = $plugin->version; + $version = $plugin->version; + } else { + $version = $componentversions[$function->component]; + } + } else { + //function component should always have a version.php, + //otherwise the function should have been described with component => 'moodle' + throw new moodle_exception('missingversionfile', 'webservice', '', $function->component); + } + } + $functioninfo['version'] = $version; + $avalaiblefunctions[] = $functioninfo; + } + + return array( + 'sitename' => $SITE->fullname, + 'siteurl' => $CFG->wwwroot, + 'username' => $USER->username, + 'firstname' => $USER->firstname, + 'lastname' => $USER->lastname, + 'fullname' => fullname($USER), + 'userid' => $USER->id, + 'userpictureurl' => $profileimageurl->out(false), + 'functions' => $avalaiblefunctions + ); + } + + public static function get_siteinfo_returns() { + return new external_single_structure( + array( + 'sitename' => new external_value(PARAM_RAW, 'site name'), + 'username' => new external_value(PARAM_RAW, 'username'), + 'firstname' => new external_value(PARAM_TEXT, 'first name'), + 'lastname' => new external_value(PARAM_TEXT, 'last name'), + 'fullname' => new external_value(PARAM_TEXT, 'user full name'), + 'userid' => new external_value(PARAM_INT, 'user id'), + 'siteurl' => new external_value(PARAM_RAW, 'site url'), + 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture'), + 'functions' => new external_multiple_structure( + new external_single_structure( + array( + 'name' => new external_value(PARAM_RAW, 'function name'), + 'version' => new external_value(PARAM_FLOAT, 'The version number of moodle site/local plugin linked to the function') + ), 'functions that are available') + ), + ) + ); + } +} \ No newline at end of file diff --git a/webservice/lib.php b/webservice/lib.php index ede94bcfe83..f4c41e8046a 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -271,6 +271,31 @@ class webservice { return $functions; } + /** + * Get the list of all functions for given service shortnames + * @param array $serviceshortnames + * @param $enabledonly if true then only return function for the service that has been enabled + * @return array functions + */ + public function get_external_functions_by_enabled_services($serviceshortnames, $enabledonly = true) { + global $DB; + if (!empty($serviceshortnames)) { + $enabledonlysql = $enabledonly?' AND s.enabled = 1 ':''; + list($serviceshortnames, $params) = $DB->get_in_or_equal($serviceshortnames); + $sql = "SELECT f.* + FROM {external_functions} f + WHERE f.name IN (SELECT sf.functionname + FROM {external_services_functions} sf, {external_services} s + WHERE s.shortname $serviceshortnames + AND sf.externalserviceid = s.id + " . $enabledonlysql . ")"; + $functions = $DB->get_records_sql($sql, $params); + } else { + $functions = array(); + } + return $functions; + } + /** * Get the list of all functions not in the given service id * @param int $serviceid diff --git a/webservice/simpletest/testwebservice.php b/webservice/simpletest/testwebservice.php index cbc299cd2c3..a2e6e577161 100644 --- a/webservice/simpletest/testwebservice.php +++ b/webservice/simpletest/testwebservice.php @@ -73,7 +73,8 @@ class webservice_test extends UnitTestCase { 'moodle_user_get_users_by_id' => false, 'moodle_enrol_get_enrolled_users' => false, 'moodle_group_get_course_groups' => false, - 'moodle_group_get_groupmembers' => false + 'moodle_group_get_groupmembers' => false, + 'moodle_webservice_get_siteinfo' => false ); ////// WRITE DB tests //// @@ -217,6 +218,18 @@ class webservice_test extends UnitTestCase { $this->assertEqual(count($groups), count($groupids)); } + function moodle_webservice_get_siteinfo($client) { + global $SITE, $CFG; + + $function = 'moodle_webservice_get_siteinfo'; + + $params = array(); + $info = $client->call($function, $params); + + $this->assertEqual($info['sitename'], $SITE->fullname); + $this->assertEqual($info['siteurl'], $CFG->wwwroot); + } + function moodle_user_get_users_by_id($client) { global $DB; $dbusers = $DB->get_records('user', array('deleted' => 0));