Piwik\Plugins\UsersManager\API::initUserPreferenceWithDefault PHP Method

initUserPreferenceWithDefault() public method

Sets a user preference in the DB using the preference's default value.
public initUserPreferenceWithDefault ( string $userLogin, string $preferenceName )
$userLogin string
$preferenceName string
    public function initUserPreferenceWithDefault($userLogin, $preferenceName)
    {
        Piwik::checkUserHasSuperUserAccessOrIsTheUser($userLogin);
        $optionValue = $this->getPreferenceValue($userLogin, $preferenceName);
        if ($optionValue === false) {
            $defaultValue = $this->getDefaultUserPreference($preferenceName, $userLogin);
            if ($defaultValue !== false) {
                $this->setUserPreference($userLogin, $preferenceName, $defaultValue);
            }
        }
    }

Usage Example

Exemplo n.º 1
0
 public function test_initUserPreferenceWithDefault_ShouldNotSaveTheDefaultPreference_IfPreferenceIsAlreadySet()
 {
     // set value so there will already be a default
     Option::set($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT), '999');
     $siteId = Option::get($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT));
     $this->assertEquals('999', $siteId);
     $this->api->initUserPreferenceWithDefault($this->login, API::PREFERENCE_DEFAULT_REPORT);
     // make sure it did not save the preference
     $siteId = Option::get($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT));
     $this->assertEquals('999', $siteId);
 }