From 6b4926281b4e09a4069135187fa519a7655bce0f Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 1 Sep 2016 17:09:32 +0200 Subject: [PATCH] MDL-54866 tool_mobile: New Web Service tool_mobile_get_config --- admin/tool/mobile/classes/api.php | 45 +++++++++++++++ admin/tool/mobile/classes/external.php | 60 ++++++++++++++++++++ admin/tool/mobile/db/services.php | 8 +++ admin/tool/mobile/tests/externallib_test.php | 42 ++++++++++++++ admin/tool/mobile/version.php | 2 +- 5 files changed, 156 insertions(+), 1 deletion(-) diff --git a/admin/tool/mobile/classes/api.php b/admin/tool/mobile/classes/api.php index 9653d45fa90..5bc279b2ff8 100644 --- a/admin/tool/mobile/classes/api.php +++ b/admin/tool/mobile/classes/api.php @@ -135,4 +135,49 @@ class api { return $settings; } + /** + * Returns a list of site configurations, filtering by section. + * + * @param string $section section name + * @return stdClass object containing the settings + */ + public static function get_config($section) { + global $CFG, $SITE; + + $settings = new \stdClass; + $context = context_system::instance(); + $isadmin = has_capability('moodle/site:config', $context); + + if (empty($section) or $section == 'frontpagesettings') { + require_once($CFG->dirroot . '/course/format/lib.php'); + // First settings that anyone can deduce. + $settings->fullname = $SITE->fullname; + $settings->shortname = $SITE->shortname; + $settings->summary = $SITE->summary; + $settings->frontpage = $CFG->frontpage; + $settings->frontpageloggedin = $CFG->frontpageloggedin; + $settings->maxcategorydepth = $CFG->maxcategorydepth; + $settings->frontpagecourselimit = $CFG->frontpagecourselimit; + $settings->numsections = course_get_format($SITE)->get_course()->numsections; + $settings->newsitems = $SITE->newsitems; + $settings->commentsperpage = $CFG->commentsperpage; + + // Now, admin settings. + if ($isadmin) { + $settings->defaultfrontpageroleid = $CFG->defaultfrontpageroleid; + } + } + + if (empty($section) or $section == 'sitepolicies') { + $settings->disableuserimages = $CFG->disableuserimages; + } + + if (empty($section) or $section == 'gradessettings') { + require_once($CFG->dirroot . '/user/lib.php'); + $settings->mygradesurl = user_mygrades_url()->out(false); + } + + return $settings; + } + } diff --git a/admin/tool/mobile/classes/external.php b/admin/tool/mobile/classes/external.php index 4ab5e930139..293b48067fe 100644 --- a/admin/tool/mobile/classes/external.php +++ b/admin/tool/mobile/classes/external.php @@ -147,4 +147,64 @@ class external extends external_api { ); } + /** + * Returns description of get_config() parameters. + * + * @return external_function_parameters + * @since Moodle 3.2 + */ + public static function get_config_parameters() { + return new external_function_parameters( + array( + 'section' => new external_value(PARAM_ALPHANUMEXT, 'Settings section name.', VALUE_DEFAULT, ''), + ) + ); + } + + /** + * Returns a list of site settings, filtering by section. + * + * @param string $section settings section name + * @return array with the settings and warnings + * @since Moodle 3.2 + */ + public static function get_config($section = '') { + + $params = self::validate_parameters(self::get_config_parameters(), array('section' => $section)); + + $settings = api::get_config($params['section']); + $result['settings'] = array(); + foreach ($settings as $name => $value) { + $result['settings'][] = array( + 'name' => $name, + 'value' => $value, + ); + } + + $result['warnings'] = array(); + return $result; + } + + /** + * Returns description of get_config() result value. + * + * @return external_description + * @since Moodle 3.2 + */ + public static function get_config_returns() { + return new external_single_structure( + array( + 'settings' => new external_multiple_structure( + new external_single_structure( + array( + 'name' => new external_value(PARAM_RAW, 'The name of the setting'), + 'value' => new external_value(PARAM_RAW, 'The value of the setting'), + ) + ), + 'Settings' + ), + 'warnings' => new external_warnings(), + ) + ); + } } diff --git a/admin/tool/mobile/db/services.php b/admin/tool/mobile/db/services.php index 20c6c84b20b..6aa1890583d 100644 --- a/admin/tool/mobile/db/services.php +++ b/admin/tool/mobile/db/services.php @@ -41,6 +41,14 @@ $functions = array( 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), 'ajax' => true, 'loginrequired' => false, + ), + + 'tool_mobile_get_config' => array( + 'classname' => 'tool_mobile\external', + 'methodname' => 'get_config', + 'description' => 'Returns a list of the site configurations, filtering by section.', + 'type' => 'read', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ) ); diff --git a/admin/tool/mobile/tests/externallib_test.php b/admin/tool/mobile/tests/externallib_test.php index 3d2c0edc517..3d230a4489d 100644 --- a/admin/tool/mobile/tests/externallib_test.php +++ b/admin/tool/mobile/tests/externallib_test.php @@ -100,4 +100,46 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase { $this->assertEquals($expected, $result); } + /** + * Test get_config + */ + public function test_get_config() { + global $CFG, $SITE; + require_once($CFG->dirroot . '/course/format/lib.php'); + + $this->resetAfterTest(true); + $result = external::get_config(); + $result = external_api::clean_returnvalue(external::get_config_returns(), $result); + + // Test default values. + $context = context_system::instance(); + $expected = array( + array('name' => 'fullname', 'value' => $SITE->fullname), + array('name' => 'shortname', 'value' => $SITE->shortname), + array('name' => 'summary', 'value' => $SITE->summary), + array('name' => 'frontpage', 'value' => $CFG->frontpage), + array('name' => 'frontpageloggedin', 'value' => $CFG->frontpageloggedin), + array('name' => 'maxcategorydepth', 'value' => $CFG->maxcategorydepth), + array('name' => 'frontpagecourselimit', 'value' => $CFG->frontpagecourselimit), + array('name' => 'numsections', 'value' => course_get_format($SITE)->get_course()->numsections), + array('name' => 'newsitems', 'value' => $SITE->newsitems), + array('name' => 'commentsperpage', 'value' => $CFG->commentsperpage), + array('name' => 'disableuserimages', 'value' => $CFG->disableuserimages), + array('name' => 'mygradesurl', 'value' => user_mygrades_url()->out(false)), + ); + $this->assertCount(0, $result['warnings']); + $this->assertEquals($expected, $result['settings']); + + // Change a value and retrieve filtering by section. + set_config('commentsperpage', 1); + $expected[9]['value'] = 1; + unset($expected[10]); + unset($expected[11]); + + $result = external::get_config('frontpagesettings'); + $result = external_api::clean_returnvalue(external::get_config_returns(), $result); + $this->assertCount(0, $result['warnings']); + $this->assertEquals($expected, $result['settings']); + } + } diff --git a/admin/tool/mobile/version.php b/admin/tool/mobile/version.php index 912b90174f4..e0d5dbeb05f 100644 --- a/admin/tool/mobile/version.php +++ b/admin/tool/mobile/version.php @@ -23,6 +23,6 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052303; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2016052304; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2016051900; // Requires this Moodle version. $plugin->component = 'tool_mobile'; // Full name of the plugin (used for diagnostics).