Hi solipsist,

I'm working on some extension of your module ... hope to be released in few days ;-)

Now I'm doing batch user watcher assigning, but it doesn't work as expected, after half-hour debug I found that problem was in the function _watcher_user_settings_load. (I'm trying to use as much functions from your module as possible to reduce code redundance.) I was trying to use it to load settings for multiple accounts, but as you can see below it is designed to hold only first user account loaded setting and than it returns these same settings regardless to used account. I think it is little bit unexpected and tricky behaviour. Can you consider the possibility to load settings to 2D array using user UID as a first level key? It is two line change patch, check my suggestions:


/**
 * Load user settings for Watcher
 *
 * @param $setting
 *     The setting to be loaded
 * @param $acct_uid
 *     The UID of the user account the setting applies to.
 *
 * @return
 *     The setting's value as a string or null.
 */
function _watcher_user_settings_load($setting, $acct_uid = null) {
  // Include DB dependent functions
  module_load_include('inc', 'watcher', 'watcher.db');

  global $user;
  $uid = ( is_numeric($acct_uid) ? $acct_uid : $user->uid );

  // Anonymous users cannot have settings
  if (!$uid) {
    return false;
  }

  // Strip watcher_
  if (preg_match('/^watcher_(.*)/', $setting, $matches)) {
    $setting = $matches[1];
  }

  // Cache settings
  static $settings = array();

  // Load from db if setting isn't cached
  //if (preg_match('/^watcher_(.*)/', $setting, $matches)) {
  //  $setting = $matches[1];
  //}
  // SUGGESTION:
  if (!isset($settings[$uid])) {
    $settings[$uid] = _watcher_db_get_user_settings($uid);
  }

  // Return the requested setting
  // return $settings[$setting];
  // SUGGESTION:
  return $settings[$uid][$setting];
}

Comments

wojtha’s picture

Hmm tried and it doesn't help... because function _watcher_db_get_user_settings has the exactly same behavior ... :-(

/**
 * Load user settings from database
 *
 * @param $uid
 *     A user UID
 * @return
 *     An assoc array of user settings
 */
function _watcher_db_get_user_settings($uid) {
  // Cache settings
  static $settings;
  if (!isset($settings)) {
    $sql = 'SELECT * FROM {watcher_user_settings} WHERE uid = %d';
    $result = db_query($sql, $uid);
    while($row = db_fetch_array($result)) {
      unset($row['uid']);
      $settings = $row;
      return $settings;
    }
    $settings = false;
  }
  return $settings;
}
wojtha’s picture

Title: _watcher_user_settings_load function isn't able to handle multiple users » Function _watcher_user_settings_load isn't able to handle multiple users

better title