/** * Create a UserVisit record linking a user to a record at a specific time. * * @param array $config Keys: record (@see AIR2_Record), user (@see User), ip (string|int). * @return void * @author sgilbertson **/ public static function create_visit($config) { $record = null; $user = null; $ip = null; extract($config); // Make sure all required data is available. if (!$record || !$user || !$ip) { throw new Exception('Invalid or missing record, user, or ip.'); } // Determine if this record type is 'visitable.' $code = null; $model_class = get_class($record); if (in_array($model_class, array_keys(UserVisit::$VISITABLE))) { $code = UserVisit::$VISITABLE[$model_class]; } // Not a visitable record type? Throw an exception. if (!$code) { throw new Exception("Record type '{$model_class}' not visitable."); } // We only support single-keyed records. $pkey = $record->identifier(); if (count($pkey) > 1) { throw new Exception("Can't visit records with multi-column primary keys."); } $pkey = array_values($pkey); $pkey = $pkey[0]; $visit = new UserVisit(); // Allow int and string IPs. if (is_string($ip)) { $ip = ip2long($ip); } // If invalid IP address, throw an exception. if (!$ip) { throw new Exception('Invalid IP address.'); } /** * Populate new UserVisit record. */ $visit->uv_ip = $ip; $visit->uv_xid = $pkey; $visit->uv_user_id = $user->user_id; // Type of record, as we determined above. $visit->uv_ref_type = $code; $visit->save(); }
/** * Set up relationship(s). * * @return void * @author sgilbertson **/ public function setUp() { parent::setUp(); $this->hasOne('SrcResponseSet', array('local' => 'uv_xid', 'foreign' => 'srs_id')); }
/** * Record a visit against this record by a given user, at a given IPv4 address. * * @return void * @author sgilbertson * @param array $config Keys: user (@see User); ip (string|int). * */ public function visit($config) { $user = null; $ip = null; extract($config); UserVisit::create_visit(array('record' => $this, 'user' => $user, 'ip' => $ip)); }