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);
 }
예제 #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);
 }
예제 #3
0
/**
 * Refresh user preference cache. This is used most often for $USER
 * object that is stored in session, but it also helps with performance in cron script.
 *
 * Preferences for each user are loaded on first use on every page, then again after the timeout expires.
 *
 * @package  core
 * @category preference
 * @access   public
 * @param    stdClass         $user          User object. Preferences are preloaded into 'preference' property
 * @param    int              $cachelifetime Cache life time on the current page (in seconds)
 * @throws   coding_exception
 * @return   null
 */
function check_user_preferences_loaded(stdClass $user, $cachelifetime = 120)
{
    global $DB;
    // Static cache, we need to check on each page load, not only every 2 minutes.
    static $loadedusers = array();
    if (!isset($user->id)) {
        throw new coding_exception('Invalid $user parameter in check_user_preferences_loaded() call, missing id field');
    }
    if (empty($user->id) or isguestuser($user->id)) {
        // No permanent storage for not-logged-in users and guest.
        if (!isset($user->preference)) {
            $user->preference = array();
        }
        return;
    }
    $timenow = time();
    if (isset($loadedusers[$user->id]) and isset($user->preference) and isset($user->preference['_lastloaded'])) {
        // Already loaded at least once on this page. Are we up to date?
        if ($user->preference['_lastloaded'] + $cachelifetime > $timenow) {
            // No need to reload - we are on the same page and we loaded prefs just a moment ago.
            return;
        } else {
            if (!get_cache_flag('userpreferenceschanged', $user->id, $user->preference['_lastloaded'])) {
                // No change since the lastcheck on this page.
                $user->preference['_lastloaded'] = $timenow;
                return;
            }
        }
    }
    // OK, so we have to reload all preferences.
    $loadedusers[$user->id] = true;
    $user->preference = $DB->get_records_menu('user_preferences', array('userid' => $user->id), '', 'name,value');
    // All values.
    $user->preference['_lastloaded'] = $timenow;
}
예제 #4
0
/**
 * Refresh current $USER session global variable with all their current preferences.
 *
 * @global object
 * @param mixed $time default null
 * @return void
 */
function check_user_preferences_loaded($time = null)
{
    global $USER, $DB;
    static $timenow = null;
    // Static cache, so we only check up-to-dateness once per request.
    if (!empty($USER->preference) && isset($USER->preference['_lastloaded'])) {
        // Already loaded. Are we up to date?
        if (is_null($timenow) || !is_null($time) && $time != $timenow) {
            $timenow = time();
            if (!get_cache_flag('userpreferenceschanged', $USER->id, $USER->preference['_lastloaded'])) {
                // We are up-to-date.
                return;
            }
        } else {
            // Already checked for up-to-date-ness.
            return;
        }
    }
    // OK, so we have to reload. Reset preference
    $USER->preference = array();
    if (!isloggedin() or isguestuser()) {
        // No permanent storage for not-logged-in user and guest
    } else {
        if ($preferences = $DB->get_records('user_preferences', array('userid' => $USER->id))) {
            foreach ($preferences as $preference) {
                $USER->preference[$preference->name] = $preference->value;
            }
        }
    }
    $USER->preference['_lastloaded'] = $timenow;
}