Skip to content

Commit

Permalink
MDL-63929 user: Support unsetting prefs in update_user_preferences WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Nov 9, 2018
1 parent 08c51ff commit 2979ca1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,8 @@
'description' => 'Set user preferences.',
'type' => 'write',
'capabilities' => 'moodle/site:config',
'ajax' => true
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_user_agree_site_policy' => array(
'classname' => 'core_user_external',
Expand Down
8 changes: 5 additions & 3 deletions user/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ public static function update_user_preferences_parameters() {
new external_single_structure(
array(
'type' => new external_value(PARAM_RAW, 'The name of the preference'),
'value' => new external_value(PARAM_RAW, 'The value of the preference')
'value' => new external_value(PARAM_RAW, 'The value of the preference, do not set this field if you
want to remove (unset) the current value.', VALUE_DEFAULT, null),
)
), 'User preferences', VALUE_DEFAULT, array()
)
Expand All @@ -383,7 +384,7 @@ public static function update_user_preferences_parameters() {
* @return null
* @since Moodle 3.2
*/
public static function update_user_preferences($userid, $emailstop = null, $preferences = array()) {
public static function update_user_preferences($userid = 0, $emailstop = null, $preferences = array()) {
global $USER, $CFG;

require_once($CFG->dirroot . '/user/lib.php');
Expand All @@ -401,7 +402,8 @@ public static function update_user_preferences($userid, $emailstop = null, $pref
'emailstop' => $emailstop,
'preferences' => $preferences
);
self::validate_parameters(self::update_user_preferences_parameters(), $params);
$params = self::validate_parameters(self::update_user_preferences_parameters(), $params);
$preferences = $params['preferences'];

// Preferences.
if (!empty($preferences)) {
Expand Down
34 changes: 34 additions & 0 deletions user/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,40 @@ public function test_set_user_preferences_capability() {
$this->assertEquals($user2->id, $result['warnings'][0]['itemid']);
}

/**
* Test update_user_preferences unsetting an existing preference.
*/
public function test_update_user_preferences_unset() {
global $DB;
$this->resetAfterTest(true);

$user = self::getDataGenerator()->create_user();

// Save users preferences.
$this->setAdminUser();
$preferences = array(
array(
'name' => 'htmleditor',
'value' => 'atto',
'userid' => $user->id,
)
);

$result = core_user_external::set_user_preferences($preferences);
$result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['saved']);

// Get preference from DB to avoid cache.
$this->assertEquals('atto', $DB->get_field('user_preferences', 'value',
array('userid' => $user->id, 'name' => 'htmleditor')));

// Now, unset.
$result = core_user_external::update_user_preferences($user->id, null, array(array('type' => 'htmleditor')));

$this->assertEquals(0, $DB->count_records('user_preferences', array('userid' => $user->id, 'name' => 'htmleditor')));
}

/**
* Test agree_site_policy
*/
Expand Down
2 changes: 2 additions & 0 deletions user/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ This files describes API changes for code that uses the user API.

* The following functions have been finally deprecated and can not be used anymore:
* useredit_update_picture()
* core_user_external::update_user_preferences() now allows to unset existing preferences values.
If the preference value field is not set, the preference will be unset.

0 comments on commit 2979ca1

Please sign in to comment.