MDL-55266 user: New WS core_user_get_user_preferences
authorJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 21 Jul 2016 14:42:58 +0000 (15:42 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 28 Jul 2016 10:08:58 +0000 (12:08 +0200)
lib/db/services.php
user/externallib.php
user/tests/externallib_test.php
version.php

index 34744dc..8694520 100644 (file)
@@ -871,6 +871,14 @@ $functions = array(
         'capabilities' => 'moodle/user:viewdetails',
         'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
     ),
+    'core_user_get_user_preferences' => array(
+        'classname' => 'core_user_external',
+        'methodname' => 'get_user_preferences',
+        'classpath' => 'user/externallib.php',
+        'description' => 'Return user preferences.',
+        'type' => 'read',
+        'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
+    ),
 
     // Competencies functions.
     'core_competency_create_competency_framework' => array(
index 9a3828f..b8216ed 100644 (file)
@@ -1363,4 +1363,97 @@ class core_user_external extends external_api {
         );
     }
 
+    /**
+     * Returns description of method parameters
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.2
+     */
+    public static function get_user_preferences_parameters() {
+        return new external_function_parameters(
+            array(
+                'name' => new external_value(PARAM_RAW, 'preference name, empty for all', VALUE_DEFAULT, ''),
+                'userid' => new external_value(PARAM_INT, 'id of the user, default to current user', VALUE_DEFAULT, 0)
+            )
+        );
+    }
+
+    /**
+     * Return user preferences.
+     *
+     * @param string $name preference name, empty for all
+     * @param int $userid id of the user, 0 for current user
+     * @return array of warnings and preferences
+     * @since Moodle 3.2
+     * @throws moodle_exception
+     */
+    public static function get_user_preferences($name = '', $userid = 0) {
+        global $USER;
+
+        $params = self::validate_parameters(self::get_user_preferences_parameters(),
+                                            array(
+                                                'name' => $name,
+                                                'userid' => $userid
+                                            ));
+        $preferences = array();
+        $warnings = array();
+
+        $context = context_system::instance();
+        self::validate_context($context);
+
+        if (empty($params['name'])) {
+            $name = null;
+        }
+        if (empty($params['userid'])) {
+            $user = null;
+        } else {
+            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
+            core_user::require_active_user($user);
+            if ($user->id != $USER->id) {
+                // Only admins can retrieve other users preferences.
+                require_capability('moodle/site:config', $context);
+            }
+        }
+
+        $userpreferences = get_user_preferences($name, null, $user);
+        // Check if we received just one preference.
+        if (!is_array($userpreferences)) {
+            $userpreferences = array($name => $userpreferences);
+        }
+
+        foreach ($userpreferences as $name => $value) {
+            $preferences[] = array(
+                'name' => $name,
+                'value' => $value,
+            );
+        }
+
+        $result = array();
+        $result['preferences'] = $preferences;
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value
+     *
+     * @return external_description
+     * @since Moodle 3.2
+     */
+    public static function get_user_preferences_returns() {
+        return new external_single_structure(
+            array(
+                'preferences' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'name' => new external_value(PARAM_RAW, 'The name of the preference'),
+                            'value' => new external_value(PARAM_RAW, 'The value of the preference'),
+                        )
+                    ),
+                    'User custom fields (also known as user profile fields)'
+                ),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
 }
index b87d301..f31bf76 100644 (file)
@@ -791,4 +791,67 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
         $this->assertTrue($result['removed']);
     }
 
+    /**
+     * Test get_user_preferences
+     */
+    public function test_get_user_preferences() {
+        $this->resetAfterTest(true);
+
+        $user = self::getDataGenerator()->create_user();
+        set_user_preference('calendar_maxevents', 1, $user);
+        set_user_preference('some_random_text', 'text', $user);
+
+        $this->setUser($user);
+
+        $result = core_user_external::get_user_preferences();
+        $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
+        $this->assertCount(0, $result['warnings']);
+        // Expect 3, _lastloaded is always returned.
+        $this->assertCount(3, $result['preferences']);
+
+        foreach ($result['preferences'] as $pref) {
+            if ($pref['name'] === '_lastloaded') {
+                continue;
+            }
+            // Check we receive the expected preferences.
+            $this->assertEquals(get_user_preferences($pref['name']), $pref['value']);
+        }
+
+        // Retrieve just one preference.
+        $result = core_user_external::get_user_preferences('some_random_text');
+        $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
+        $this->assertCount(0, $result['warnings']);
+        $this->assertCount(1, $result['preferences']);
+        $this->assertEquals('text', $result['preferences'][0]['value']);
+
+        // Retrieve non-existent preference.
+        $result = core_user_external::get_user_preferences('non_existent');
+        $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
+        $this->assertCount(0, $result['warnings']);
+        $this->assertCount(1, $result['preferences']);
+        $this->assertEquals(null, $result['preferences'][0]['value']);
+
+        // Check that as admin we can retrieve all the preferences for any user.
+        $this->setAdminUser();
+        $result = core_user_external::get_user_preferences('', $user->id);
+        $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
+        $this->assertCount(0, $result['warnings']);
+        $this->assertCount(3, $result['preferences']);
+
+        foreach ($result['preferences'] as $pref) {
+            if ($pref['name'] === '_lastloaded') {
+                continue;
+            }
+            // Check we receive the expected preferences.
+            $this->assertEquals(get_user_preferences($pref['name'], null, $user), $pref['value']);
+        }
+
+        // Check that as a non admin user we cannot retrieve other users preferences.
+        $anotheruser = self::getDataGenerator()->create_user();
+        $this->setUser($anotheruser);
+
+        $this->setExpectedException('required_capability_exception');
+        $result = core_user_external::get_user_preferences('', $user->id);
+    }
+
 }
index 30ab917..f2a88b2 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2016072800.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2016072800.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.