Merge branch 'bug-1391'

bug-1396
Benny Baumann 9 years ago
commit 733814fd08

@ -5,20 +5,20 @@
class Default_Model_User { class Default_Model_User {
protected $db; protected $db;
protected $id; protected $id;
protected $points = null; protected $points = null;
protected function __construct(Zend_Db_Adapter_Abstract $db, $id) { protected function __construct(Zend_Db_Adapter_Abstract $db, $id) {
// Not allowed to create new users from within the manager // Not allowed to create new users from within the manager
$this->db = $db; $this->db = $db;
$this->id = $id; $this->id = $id;
} }
/** /**
* Get an user object for the given ID * Get an user object for the given ID
* *
* @param $id int * @param $id int
* @return Default_Model_User * @return Default_Model_User
*/ */
@ -29,7 +29,7 @@ class Default_Model_User {
APPLICATION_ENV); APPLICATION_ENV);
$db = Zend_Db::factory($config->ca_mgr->db->auth->pdo, $db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
$config->ca_mgr->db->auth); $config->ca_mgr->db->auth);
// Check if the ID is present on the test server // Check if the ID is present on the test server
$query = 'select `id` from `users` where `id` = :user'; $query = 'select `id` from `users` where `id` = :user';
$query_params['user'] = $id; $query_params['user'] = $id;
@ -39,13 +39,13 @@ class Default_Model_User {
__METHOD__ . ': user ID not found in the data base'); __METHOD__ . ': user ID not found in the data base');
} }
$row = $result->fetch(); $row = $result->fetch();
return new Default_Model_User($db, $row['id']); return new Default_Model_User($db, $row['id']);
} }
/** /**
* Get an user object for the currently logged in user * Get an user object for the currently logged in user
* *
* @return Default_Model_User * @return Default_Model_User
*/ */
public static function findCurrentUser() { public static function findCurrentUser() {
@ -54,72 +54,72 @@ class Default_Model_User {
throw new Exception( throw new Exception(
__METHOD__ . ': you need to log in to use this feature'); __METHOD__ . ': you need to log in to use this feature');
} }
return self::findById($session->authdata['authed_id']); return self::findById($session->authdata['authed_id']);
} }
/** /**
* Get the first assurer who didn't already assure the user * Get the first assurer who didn't already assure the user
* *
* @return Default_Model_User * @return Default_Model_User
*/ */
public function findNewAssurer() public function findNewAssurer()
{ {
$query = 'select min(`id`) as `assurer` from `users` ' . $query = 'select min(`id`) as `assurer` from `users` ' .
'where `email` like \'john.doe-___@example.com\' and ' . 'where `email` like \'john.doe-___@example.com\' and ' .
'`id` not in (select `from` from `notary` where `to` = :user)'; '`id` not in (select `from` from `notary` where `to` = :user)';
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
$row = $this->db->query($query, $query_params)->fetch(); $row = $this->db->query($query, $query_params)->fetch();
if ($row['assurer'] === NULL) { if ($row['assurer'] === NULL) {
throw new Exception( throw new Exception(
__METHOD__ . ': no more assurers that haven\'t already '. __METHOD__ . ': no more assurers that haven\'t already '.
'assured this account'); 'assured this account');
} }
return new Default_Model_User($this->db, $row['assurer']); return new Default_Model_User($this->db, $row['assurer']);
} }
/** /**
* Get the first assuree who hasn't already been assured by this user * Get the first assuree who hasn't already been assured by this user
* *
* @return Default_Model_User * @return Default_Model_User
*/ */
public function findNewAssuree() { public function findNewAssuree() {
$query = 'select min(`id`) as `assuree` from `users` ' . $query = 'select min(`id`) as `assuree` from `users` ' .
'where `email` like \'john.doe-___@example.com\' and ' . 'where `email` like \'john.doe-___@example.com\' and ' .
'`id` not in (select `to` from `notary` where `from` = :user)'; '`id` not in (select `to` from `notary` where `from` = :user)';
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
$row = $this->db->query($query, $query_params)->fetch(); $row = $this->db->query($query, $query_params)->fetch();
if ($row['assuree'] === NULL) { if ($row['assuree'] === NULL) {
throw new Exception( throw new Exception(
__METHOD__ . ': no more assurees that haven\'t already '. __METHOD__ . ': no more assurees that haven\'t already '.
'been assured by this account'); 'been assured by this account');
} }
return new Default_Model_User($this->db, $row['assuree']); return new Default_Model_User($this->db, $row['assuree']);
} }
/** /**
* Refresh the current value of points from the test server * Refresh the current value of points from the test server
* *
* Needed if operations outside this class are made, that might affect the * Needed if operations outside this class are made, that might affect the
* user's points * user's points
*/ */
public function refreshPoints() { public function refreshPoints() {
$query = "select sum(`points`) as `total` from `notary` " . $query = "SELECT SUM(`points`) AS `total` FROM `notary` " .
"where `to` = :user and method != 'Administrative Increase' and from != to"; "WHERE `to` = :user AND `method` != 'Administrative Increase' AND `from` != `to`";
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
$row = $this->db->query($query, $query_params)->fetch(); $row = $this->db->query($query, $query_params)->fetch();
if ($row['total'] === null) $row['total'] = 0; if ($row['total'] === null) $row['total'] = 0;
$this->points = $row['total']; $this->points = $row['total'];
} }
/** /**
* Get points of the user * Get points of the user
* *
* @return int * @return int
* The amount of points the user has * The amount of points the user has
*/ */
@ -128,40 +128,40 @@ class Default_Model_User {
if ($this->points === null) { if ($this->points === null) {
$this->refreshPoints(); $this->refreshPoints();
} }
return $this->points; return $this->points;
} }
/** /**
* Fix the assurer flag for the user * Fix the assurer flag for the user
*/ */
public function fixAssurerFlag() public function fixAssurerFlag()
{ {
// TODO: unset flag if requirements are not met // TODO: unset flag if requirements are not met
$query = 'UPDATE `users` SET `assurer` = 1 WHERE `users`.`id` = :user AND '. $query = 'UPDATE `users` SET `assurer` = 1 WHERE `users`.`id` = :user AND '.
'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '. 'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '. 'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
'`cp`.`user_id` = :user) AND '. '`cp`.`user_id` = :user) AND '.
'(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '. '(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
'`expire` < now()) >= 100'; '`expire` < now()) >= 100';
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
$this->db->query($query, $query_params); $this->db->query($query, $query_params);
} }
/** /**
* @return boolean * @return boolean
*/ */
public function getAssurerStatus() { public function getAssurerStatus() {
$query = 'SELECT 1 FROM `users` WHERE `users`.`id` = :user AND '. $query = 'SELECT 1 FROM `users` WHERE `users`.`id` = :user AND '.
'`assurer_blocked` = 0 AND '. '`assurer_blocked` = 0 AND '.
'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '. 'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '. 'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
'`cp`.`user_id` = :user) AND '. '`cp`.`user_id` = :user) AND '.
'(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '. '(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
'`expire` < now()) >= 100'; '`expire` < now()) >= 100';
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
@ -169,10 +169,10 @@ class Default_Model_User {
if ($result->rowCount() === 1) { if ($result->rowCount() === 1) {
return true; return true;
} }
return false; return false;
} }
/** /**
* @return Zend_Date * @return Zend_Date
*/ */
@ -180,10 +180,10 @@ class Default_Model_User {
$query = 'select `dob` from `users` where `id` = :user'; $query = 'select `dob` from `users` where `id` = :user';
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
$row = $this->db->query($query, $query_params)->fetch(); $row = $this->db->query($query, $query_params)->fetch();
return new Zend_Date($row['dob'], Zend_Date::ISO_8601); return new Zend_Date($row['dob'], Zend_Date::ISO_8601);
} }
/** /**
* @return int * @return int
*/ */
@ -191,16 +191,16 @@ class Default_Model_User {
$now = new Zend_Date(); $now = new Zend_Date();
$dob = $this->getDob(); $dob = $this->getDob();
$age = $now->get(Zend_Date::YEAR) - $dob->get(Zend_Date::YEAR); $age = $now->get(Zend_Date::YEAR) - $dob->get(Zend_Date::YEAR);
// Did we have a happy birthday already this year? // Did we have a happy birthday already this year?
$dob->setYear($now); $dob->setYear($now);
if ($dob->compare($now) > 0) { if ($dob->compare($now) > 0) {
$age -= 1; $age -= 1;
} }
return $age; return $age;
} }
/** /**
* @return string * @return string
*/ */
@ -208,44 +208,43 @@ class Default_Model_User {
$query = 'select `email` from `users` where `id` = :user'; $query = 'select `email` from `users` where `id` = :user';
$query_params['user'] = $this->id; $query_params['user'] = $this->id;
$row = $this->db->query($query, $query_params)->fetch(); $row = $this->db->query($query, $query_params)->fetch();
return $row['email']; return $row['email'];
} }
/** /**
* Assure another user. Usual restrictions apply * Assure another user. Usual restrictions apply
* *
* @param $assuree Default_Model_User * @param $assuree Default_Model_User
* @param $points int * @param $points int
* @param $location string * @param $location string
* @param $date string * @param $date string
* @throws Exception * @throws Exception
* *
* @return int * @return int
* The amount of points that have been issued (might be less than * The amount of points that have been issued (might be less than
* $points) * $points)
*/ */
public function assure(Default_Model_User $assuree, $points, $location, public function assure(Default_Model_User $assuree, $points, $location, $date) {
$date) {
// Sanitize inputs // Sanitize inputs
$points = intval($points); $points = intval($points);
$location = stripslashes($location); $location = stripslashes($location);
$date = stripslashes($date); $date = stripslashes($date);
if (!$this->getAssurerStatus()) { if (!$this->getAssurerStatus()) {
throw new Exception( throw new Exception(
__METHOD__ . ': '.$this->id.' needs to be an assurer to do '. __METHOD__ . ': '.$this->id.' needs to be an assurer to do '.
'assurances'); 'assurances');
} }
if ($this->id === $assuree->id) { if ($this->id === $assuree->id) {
throw new Exception( throw new Exception(
__METHOD__ . ': '.$this->id.' is not allowed to assure '. __METHOD__ . ': '.$this->id.' is not allowed to assure '.
'himself'); 'himself');
} }
$query = 'select * from `notary` where `from`= :assurer and '. $query = 'select * from `notary` where `from`= :assurer and '.
'`to`= :assuree'; '`to`= :assuree';
$query_params['assurer'] = $this->id; $query_params['assurer'] = $this->id;
$query_params['assuree'] = $assuree->id; $query_params['assuree'] = $assuree->id;
$result = $this->db->query($query, $query_params); $result = $this->db->query($query, $query_params);
@ -254,11 +253,11 @@ class Default_Model_User {
__METHOD__ . ': '.$this->id.' is not allowed to assure '. __METHOD__ . ': '.$this->id.' is not allowed to assure '.
$assuree->id .' more than once'); $assuree->id .' more than once');
} }
// Respect the maximum points // Respect the maximum points
$max = $this->maxpoints(); $max = $this->maxpoints();
$points = min($points, $max); $points = min($points, $max);
$rounddown = $points; $rounddown = $points;
if ($max < 100) { if ($max < 100) {
if ($assuree->getPoints() + $points > 100) if ($assuree->getPoints() + $points > 100)
@ -268,10 +267,10 @@ class Default_Model_User {
$rounddown = $max - $assuree->getPoints(); $rounddown = $max - $assuree->getPoints();
} }
if ($rounddown < 0) $rounddown = 0; if ($rounddown < 0) $rounddown = 0;
$query = 'select * from `notary` where `from` = :assurer and '. $query = 'select * from `notary` where `from` = :assurer and '.
'`to` = :assuree and `awarded` = :points and '. '`to` = :assuree and `awarded` = :points and '.
'`location` = :location and `date` = :date'; '`location` = :location and `date` = :date';
$query_params['assurer'] = $this->id; $query_params['assurer'] = $this->id;
$query_params['assuree'] = $assuree->id; $query_params['assuree'] = $assuree->id;
$query_params['points'] = $points; $query_params['points'] = $points;
@ -283,7 +282,7 @@ class Default_Model_User {
__METHOD__ . ': '.$this->id.' is not allowed to do the same '. __METHOD__ . ': '.$this->id.' is not allowed to do the same '.
'assurance to '.$assuree->id.' more than once'); 'assurance to '.$assuree->id.' more than once');
} }
// Make sure it is empty // Make sure it is empty
$assurance = array(); $assurance = array();
$assurance['from'] = $this->id; $assurance['from'] = $this->id;
@ -293,16 +292,16 @@ class Default_Model_User {
$assurance['location'] = $location; $assurance['location'] = $location;
$assurance['date'] = $date; $assurance['date'] = $date;
$assurance['when'] = new Zend_Db_Expr('now()'); $assurance['when'] = new Zend_Db_Expr('now()');
$this->db->insert('notary', $assurance); $this->db->insert('notary', $assurance);
$assuree->points += $rounddown; $assuree->points += $rounddown;
$assuree->fixAssurerFlag(); $assuree->fixAssurerFlag();
return $rounddown; return $rounddown;
} }
/** /**
* Do an administrative increase * Do an administrative increase
* *
* @param $points int * @param $points int
* @param $location string * @param $location string
* @param $date string * @param $date string
@ -312,7 +311,7 @@ class Default_Model_User {
$points = intval($points); $points = intval($points);
$location = stripslashes($location); $location = stripslashes($location);
$date = stripslashes($date); $date = stripslashes($date);
$increase = array(); $increase = array();
$increase['from'] = $this->id; $increase['from'] = $this->id;
$increase['to'] = $this->id; $increase['to'] = $this->id;
@ -322,23 +321,23 @@ class Default_Model_User {
$increase['date'] = $date; $increase['date'] = $date;
$increase['method'] = 'Administrative Increase'; $increase['method'] = 'Administrative Increase';
$increase['when'] = new Zend_Db_Expr('now()'); $increase['when'] = new Zend_Db_Expr('now()');
$this->db->insert('notary', $increase); $this->db->insert('notary', $increase);
$this->points += $points; $this->points += $points;
$this->fixAssurerFlag(); $this->fixAssurerFlag();
} }
/** /**
* Maximum number of points the user may issue * Maximum number of points the user may issue
* *
* @return int * @return int
*/ */
public function maxpoints() { public function maxpoints() {
if (!$this->getAssurerStatus()) return 0; if (!$this->getAssurerStatus()) return 0;
if ($this->getAge() < 18) return 10; if ($this->getAge() < 18) return 10;
$points = $this->getPoints(); $points = $this->getPoints();
if ($points >= 300) return 200; if ($points >= 300) return 200;
if ($points >= 200) return 150; if ($points >= 200) return 150;
@ -348,18 +347,18 @@ class Default_Model_User {
if ($points >= 120) return 20; if ($points >= 120) return 20;
if ($points >= 110) return 15; if ($points >= 110) return 15;
if ($points >= 100) return 10; if ($points >= 100) return 10;
// Should not get here // Should not get here
throw new Exception( throw new Exception(
__METHOD__ . ': '.$this->id.' We have reached unreachable code'); __METHOD__ . ': '.$this->id.' We have reached unreachable code');
} }
/** /**
* Get the challenge types that are available in the database * Get the challenge types that are available in the database
* *
* @param $db Zend_Db_Adapter_Abstract * @param $db Zend_Db_Adapter_Abstract
* The database connection to use * The database connection to use
* *
* @return array(int => string) * @return array(int => string)
*/ */
public static function getAvailableChallengeTypes( public static function getAvailableChallengeTypes(
@ -367,16 +366,16 @@ class Default_Model_User {
$query = 'select `id`, `type_text` from `cats_type`'; $query = 'select `id`, `type_text` from `cats_type`';
return $db->fetchPairs($query); return $db->fetchPairs($query);
} }
/** /**
* Get the challenge variants for this type that are available in the * Get the challenge variants for this type that are available in the
* database * database
* *
* @param $db Zend_Db_Adapter_Abstract * @param $db Zend_Db_Adapter_Abstract
* The database connection to use * The database connection to use
* @param $type int * @param $type int
* The type of challenge you want to get the variants of * The type of challenge you want to get the variants of
* *
* @return array(int => string) * @return array(int => string)
*/ */
public static function getAvailableChallengeVariants( public static function getAvailableChallengeVariants(
@ -386,10 +385,10 @@ class Default_Model_User {
$query_params['type'] = $type; $query_params['type'] = $type;
return $db->fetchPairs($query, $query_params); return $db->fetchPairs($query, $query_params);
} }
/** /**
* Assign the challenge to the user * Assign the challenge to the user
* *
* @param $type int * @param $type int
* The type of the challenge, has to be one of the keys returned by * The type of the challenge, has to be one of the keys returned by
* getAvailableChallengeTypes() * getAvailableChallengeTypes()
@ -406,14 +405,14 @@ class Default_Model_User {
__METHOD__ . ': got wrong challenge type '.$type.' when '. __METHOD__ . ': got wrong challenge type '.$type.' when '.
'assigning challenge to user '.$this->id); 'assigning challenge to user '.$this->id);
} }
$variants = self::getAvailableChallengeVariants($this->db, $type); $variants = self::getAvailableChallengeVariants($this->db, $type);
if (!isset($variants[$variant])) { if (!isset($variants[$variant])) {
throw new Exception( throw new Exception(
__METHOD__ . ': got wrong challenge variant '.$variant.' when '. __METHOD__ . ': got wrong challenge variant '.$variant.' when '.
'assigning challenge to user '.$this->id); 'assigning challenge to user '.$this->id);
} }
$challenge = array(); $challenge = array();
$challenge['user_id'] = $this->id; $challenge['user_id'] = $this->id;
$challenge['variant_id'] = $variant; $challenge['variant_id'] = $variant;
@ -421,21 +420,21 @@ class Default_Model_User {
$challenge['pass_date'] = $date->toString('Y-m-d H:i:s'); $challenge['pass_date'] = $date->toString('Y-m-d H:i:s');
// otherwise default value of the database will be used // otherwise default value of the database will be used
} }
$this->db->insert('cats_passed', $challenge); $this->db->insert('cats_passed', $challenge);
$this->fixAssurerFlag(); $this->fixAssurerFlag();
} }
/** /**
* Get the flags that are set * Get the flags that are set
* *
* @return array (string => boolean) * @return array (string => boolean)
*/ */
public function getFlags() { public function getFlags() {
$flags = $this->db->select()->from('users', self::flags()) $flags = $this->db->select()->from('users', self::flags())
->where('`id` = ?', $this->id)->query()->fetch(); ->where('`id` = ?', $this->id)->query()->fetch();
foreach ($flags as $key => $value) { foreach ($flags as $key => $value) {
if ($value === '0') { if ($value === '0') {
$flags[$key] = false; $flags[$key] = false;
@ -443,20 +442,20 @@ class Default_Model_User {
$flags[$key] = true; $flags[$key] = true;
} }
} }
return $flags; return $flags;
} }
/** /**
* Set the flags - to know which flags exist you might want to call * Set the flags - to know which flags exist you might want to call
* getFlags() first * getFlags() first
* *
* @param $flags array (string => boolean) * @param $flags array (string => boolean)
* Currently unknown flags are silently ignored * Currently unknown flags are silently ignored
*/ */
public function setFlags(array $flags) { public function setFlags(array $flags) {
$newflags = array(); $newflags = array();
// filter values // filter values
foreach (self::flags() as $flag) { foreach (self::flags() as $flag) {
if (isset($flags[$flag])) { if (isset($flags[$flag])) {
@ -467,11 +466,11 @@ class Default_Model_User {
} }
} }
} }
$where = $this->db->quoteInto('`id` = ?', $this->id, Zend_Db::INT_TYPE); $where = $this->db->quoteInto('`id` = ?', $this->id, Zend_Db::INT_TYPE);
$this->db->update('users', $newflags, $where); $this->db->update('users', $newflags, $where);
} }
/** /**
* The flags from the `users` table that might be set * The flags from the `users` table that might be set
*/ */
@ -492,4 +491,4 @@ class Default_Model_User {
'assurer', 'assurer',
'assurer_blocked'); 'assurer_blocked');
} }
} }

Loading…
Cancel
Save