Ejemplo n.º 1
0
 public function test_mark_user_preferences_changed()
 {
     $this->resetAfterTest();
     $otheruser = $this->getDataGenerator()->create_user();
     $otheruserid = $otheruser->id;
     set_cache_flag('userpreferenceschanged', $otheruserid, null);
     mark_user_preferences_changed($otheruserid);
     $this->assertEquals(get_cache_flag('userpreferenceschanged', $otheruserid, time() - 10), 1);
     set_cache_flag('userpreferenceschanged', $otheruserid, null);
 }
Ejemplo n.º 2
0
 public function test_mark_user_preferences_changed()
 {
     if (!($otheruserid = $this->get_fake_preference_test_userid())) {
         $this->fail('Can not find unused user id for the preferences test');
         return;
     }
     set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
     mark_user_preferences_changed($otheruserid);
     $this->assertEqual(get_cache_flag('userpreferenceschanged', $otheruserid, time() - 10), 1);
     set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
 }
Ejemplo n.º 3
0
/**
 * Unsets a preference completely by deleting it from the database
 *
 * If a $user object is submitted it's 'preference' property is used for the preferences cache.
 *
 * @package  core
 * @category preference
 * @access   public
 * @param    string            $name The key to unset as preference for the specified user
 * @param    stdClass|int|null $user A moodle user object or id, null means current user
 * @throws   coding_exception
 * @return   bool                    Always true or exception
 */
function unset_user_preference($name, $user = null)
{
    global $USER, $DB;
    if (empty($name) or is_numeric($name) or $name === '_lastloaded') {
        throw new coding_exception('Invalid preference name in unset_user_preference() call');
    }
    if (is_null($user)) {
        $user = $USER;
    } else {
        if (isset($user->id)) {
            // It is a valid object.
        } else {
            if (is_numeric($user)) {
                $user = (object) array('id' => (int) $user);
            } else {
                throw new coding_exception('Invalid $user parameter in unset_user_preference() call');
            }
        }
    }
    check_user_preferences_loaded($user);
    if (empty($user->id) or isguestuser($user->id)) {
        // No permanent storage for not-logged-in user and guest.
        unset($user->preference[$name]);
        return true;
    }
    // Delete from DB.
    $DB->delete_records('user_preferences', array('userid' => $user->id, 'name' => $name));
    // Delete the preference from cache.
    unset($user->preference[$name]);
    // Set reload flag for other sessions.
    mark_user_preferences_changed($user->id);
    return true;
}
Ejemplo n.º 4
0
/**
 * Unsets a preference completely by deleting it from the database
 *
 * Optionally, can set a preference for a different user id
 *
 * @global object
 * @param string  $name The key to unset as preference for the specified user
 * @param int $otheruserid A moodle user ID
 */
function unset_user_preference($name, $otheruserid = NULL)
{
    global $USER, $DB;
    if (empty($otheruserid)) {
        $userid = $USER->id;
        check_user_preferences_loaded();
    } else {
        $userid = $otheruserid;
    }
    //Then from DB
    $DB->delete_records('user_preferences', array('userid' => $userid, 'name' => $name));
    mark_user_preferences_changed($userid);
    //Delete the preference from $USER if needed
    if ($userid == $USER->id) {
        unset($USER->preference[$name]);
        $USER->preference['_lastloaded'] = time();
    }
    return true;
}