2011-06-20 11:41:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Michael Tänzer
|
|
|
|
*/
|
|
|
|
|
2011-06-20 21:08:08 +00:00
|
|
|
class Default_Model_User {
|
2011-06-20 11:41:52 +00:00
|
|
|
protected $db;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
protected $id;
|
|
|
|
protected $points = null;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
protected function __construct(Zend_Db_Adapter_Abstract $db, $id) {
|
|
|
|
// Not allowed to create new users from within the manager
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$this->db = $db;
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Get an user object for the given ID
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 11:41:52 +00:00
|
|
|
* @param $id int
|
2011-06-20 21:08:08 +00:00
|
|
|
* @return Default_Model_User
|
2011-06-20 11:41:52 +00:00
|
|
|
*/
|
|
|
|
public static function findById($id) {
|
|
|
|
// Get database connection
|
|
|
|
$config = new Zend_Config_Ini(
|
|
|
|
APPLICATION_PATH . '/configs/application.ini',
|
|
|
|
APPLICATION_ENV);
|
|
|
|
$db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
|
|
|
|
$config->ca_mgr->db->auth);
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
// Check if the ID is present on the test server
|
|
|
|
$query = 'select `id` from `users` where `id` = :user';
|
|
|
|
$query_params['user'] = $id;
|
|
|
|
$result = $db->query($query, $query_params);
|
|
|
|
if ($result->rowCount() !== 1) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': user ID not found in the data base');
|
|
|
|
}
|
|
|
|
$row = $result->fetch();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 21:08:08 +00:00
|
|
|
return new Default_Model_User($db, $row['id']);
|
2011-06-20 11:41:52 +00:00
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Get an user object for the currently logged in user
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 21:08:08 +00:00
|
|
|
* @return Default_Model_User
|
2011-06-20 11:41:52 +00:00
|
|
|
*/
|
|
|
|
public static function findCurrentUser() {
|
|
|
|
$session = Zend_Registry::get('session');
|
|
|
|
if ($session->authdata['authed'] !== true) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': you need to log in to use this feature');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
return self::findById($session->authdata['authed_id']);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Get the first assurer who didn't already assure the user
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 21:08:08 +00:00
|
|
|
* @return Default_Model_User
|
2011-06-20 11:41:52 +00:00
|
|
|
*/
|
|
|
|
public function findNewAssurer()
|
|
|
|
{
|
|
|
|
$query = 'select min(`id`) as `assurer` from `users` ' .
|
2015-07-31 05:59:57 +00:00
|
|
|
'where `email` like \'john.doe-___@example.com\' and ' .
|
2011-06-20 11:41:52 +00:00
|
|
|
'`id` not in (select `from` from `notary` where `to` = :user)';
|
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
if ($row['assurer'] === NULL) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': no more assurers that haven\'t already '.
|
|
|
|
'assured this account');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 21:08:08 +00:00
|
|
|
return new Default_Model_User($this->db, $row['assurer']);
|
2011-06-20 11:41:52 +00:00
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-07-05 16:54:19 +00:00
|
|
|
/**
|
|
|
|
* Get the first assuree who hasn't already been assured by this user
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-07-05 16:54:19 +00:00
|
|
|
* @return Default_Model_User
|
|
|
|
*/
|
|
|
|
public function findNewAssuree() {
|
|
|
|
$query = 'select min(`id`) as `assuree` from `users` ' .
|
2015-07-31 05:59:57 +00:00
|
|
|
'where `email` like \'john.doe-___@example.com\' and ' .
|
2011-07-05 16:54:19 +00:00
|
|
|
'`id` not in (select `to` from `notary` where `from` = :user)';
|
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-07-05 16:54:19 +00:00
|
|
|
if ($row['assuree'] === NULL) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': no more assurees that haven\'t already '.
|
|
|
|
'been assured by this account');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-07-05 16:54:19 +00:00
|
|
|
return new Default_Model_User($this->db, $row['assuree']);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Refresh the current value of points from the test server
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 11:41:52 +00:00
|
|
|
* Needed if operations outside this class are made, that might affect the
|
|
|
|
* user's points
|
|
|
|
*/
|
|
|
|
public function refreshPoints() {
|
2015-07-22 19:40:31 +00:00
|
|
|
$query = "select sum(`points`) as `total` from `notary` " .
|
|
|
|
"where `to` = :user and method != 'Administrative Increase' and from != to";
|
2011-06-20 11:41:52 +00:00
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
|
|
|
if ($row['total'] === null) $row['total'] = 0;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$this->points = $row['total'];
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
|
|
|
/**
|
2011-06-20 11:41:52 +00:00
|
|
|
* Get points of the user
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 11:41:52 +00:00
|
|
|
* @return int
|
|
|
|
* The amount of points the user has
|
|
|
|
*/
|
|
|
|
public function getPoints()
|
|
|
|
{
|
|
|
|
if ($this->points === null) {
|
|
|
|
$this->refreshPoints();
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
return $this->points;
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Fix the assurer flag for the user
|
|
|
|
*/
|
|
|
|
public function fixAssurerFlag()
|
|
|
|
{
|
2015-07-31 05:59:57 +00:00
|
|
|
// TODO: unset flag if requirements are not met
|
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$query = 'UPDATE `users` SET `assurer` = 1 WHERE `users`.`id` = :user AND '.
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
|
|
|
|
'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
|
|
|
|
'`cp`.`user_id` = :user) AND '.
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
'(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
|
|
|
|
'`expire` < now()) >= 100';
|
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$this->db->query($query, $query_params);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getAssurerStatus() {
|
|
|
|
$query = 'SELECT 1 FROM `users` WHERE `users`.`id` = :user AND '.
|
2015-07-31 05:59:57 +00:00
|
|
|
'`assurer_blocked` = 0 AND '.
|
|
|
|
|
|
|
|
'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
|
|
|
|
'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
|
|
|
|
'`cp`.`user_id` = :user) AND '.
|
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
'(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
|
|
|
|
'`expire` < now()) >= 100';
|
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$result = $this->db->query($query, $query_params);
|
|
|
|
if ($result->rowCount() === 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* @return Zend_Date
|
|
|
|
*/
|
|
|
|
public function getDob() {
|
|
|
|
$query = 'select `dob` from `users` where `id` = :user';
|
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
return new Zend_Date($row['dob'], Zend_Date::ISO_8601);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getAge() {
|
|
|
|
$now = new Zend_Date();
|
2011-06-20 23:12:54 +00:00
|
|
|
$dob = $this->getDob();
|
|
|
|
$age = $now->get(Zend_Date::YEAR) - $dob->get(Zend_Date::YEAR);
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 23:12:54 +00:00
|
|
|
// Did we have a happy birthday already this year?
|
|
|
|
$dob->setYear($now);
|
|
|
|
if ($dob->compare($now) > 0) {
|
2015-07-31 05:59:57 +00:00
|
|
|
$age -= 1;
|
2011-06-20 23:12:54 +00:00
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 23:12:54 +00:00
|
|
|
return $age;
|
2011-06-20 11:41:52 +00:00
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-07-05 16:54:19 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPrimEmail() {
|
|
|
|
$query = 'select `email` from `users` where `id` = :user';
|
|
|
|
$query_params['user'] = $this->id;
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-07-05 16:54:19 +00:00
|
|
|
return $row['email'];
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Assure another user. Usual restrictions apply
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 21:08:08 +00:00
|
|
|
* @param $assuree Default_Model_User
|
2011-06-20 11:41:52 +00:00
|
|
|
* @param $points int
|
|
|
|
* @param $location string
|
|
|
|
* @param $date string
|
|
|
|
* @throws Exception
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 11:41:52 +00:00
|
|
|
* @return int
|
|
|
|
* The amount of points that have been issued (might be less than
|
|
|
|
* $points)
|
|
|
|
*/
|
2015-07-31 05:59:57 +00:00
|
|
|
public function assure(Default_Model_User $assuree, $points, $location, $date) {
|
2011-06-20 11:41:52 +00:00
|
|
|
// Sanitize inputs
|
|
|
|
$points = intval($points);
|
|
|
|
$location = stripslashes($location);
|
|
|
|
$date = stripslashes($date);
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
if (!$this->getAssurerStatus()) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': '.$this->id.' needs to be an assurer to do '.
|
|
|
|
'assurances');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
if ($this->id === $assuree->id) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': '.$this->id.' is not allowed to assure '.
|
|
|
|
'himself');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$query = 'select * from `notary` where `from`= :assurer and '.
|
2015-07-31 05:59:57 +00:00
|
|
|
'`to`= :assuree';
|
2011-06-20 11:41:52 +00:00
|
|
|
$query_params['assurer'] = $this->id;
|
|
|
|
$query_params['assuree'] = $assuree->id;
|
|
|
|
$result = $this->db->query($query, $query_params);
|
|
|
|
if ($result->rowCount() > 0 && $this->getPoints() < 200) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': '.$this->id.' is not allowed to assure '.
|
|
|
|
$assuree->id .' more than once');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
// Respect the maximum points
|
|
|
|
$max = $this->maxpoints();
|
|
|
|
$points = min($points, $max);
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$rounddown = $points;
|
|
|
|
if ($max < 100) {
|
|
|
|
if ($assuree->getPoints() + $points > 100)
|
|
|
|
$rounddown = 100 - $assuree->getPoints();
|
|
|
|
} else {
|
|
|
|
if ($assuree->getPoints() + $points > $max)
|
|
|
|
$rounddown = $max - $assuree->getPoints();
|
|
|
|
}
|
|
|
|
if ($rounddown < 0) $rounddown = 0;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$query = 'select * from `notary` where `from` = :assurer and '.
|
2015-07-31 05:59:57 +00:00
|
|
|
'`to` = :assuree and `awarded` = :points and '.
|
|
|
|
'`location` = :location and `date` = :date';
|
2011-06-20 11:41:52 +00:00
|
|
|
$query_params['assurer'] = $this->id;
|
|
|
|
$query_params['assuree'] = $assuree->id;
|
|
|
|
$query_params['points'] = $points;
|
|
|
|
$query_params['location'] = $location;
|
|
|
|
$query_params['date'] = $date;
|
|
|
|
$result = $this->db->query($query, $query_params);
|
|
|
|
if ($result->rowCount() > 0) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': '.$this->id.' is not allowed to do the same '.
|
|
|
|
'assurance to '.$assuree->id.' more than once');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
// Make sure it is empty
|
|
|
|
$assurance = array();
|
|
|
|
$assurance['from'] = $this->id;
|
|
|
|
$assurance['to'] = $assuree->id;
|
|
|
|
$assurance['points'] = $rounddown;
|
|
|
|
$assurance['awarded'] = $points;
|
|
|
|
$assurance['location'] = $location;
|
|
|
|
$assurance['date'] = $date;
|
|
|
|
$assurance['when'] = new Zend_Db_Expr('now()');
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$this->db->insert('notary', $assurance);
|
|
|
|
$assuree->points += $rounddown;
|
|
|
|
$assuree->fixAssurerFlag();
|
|
|
|
return $rounddown;
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-27 23:20:56 +00:00
|
|
|
/**
|
|
|
|
* Do an administrative increase
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-27 23:20:56 +00:00
|
|
|
* @param $points int
|
|
|
|
* @param $location string
|
|
|
|
* @param $date string
|
|
|
|
*/
|
|
|
|
public function adminIncrease($points, $location, $date) {
|
|
|
|
//Sanitize inputs
|
|
|
|
$points = intval($points);
|
|
|
|
$location = stripslashes($location);
|
|
|
|
$date = stripslashes($date);
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-27 23:20:56 +00:00
|
|
|
$increase = array();
|
|
|
|
$increase['from'] = $this->id;
|
|
|
|
$increase['to'] = $this->id;
|
|
|
|
$increase['points'] = $points;
|
|
|
|
$increase['awarded'] = $points;
|
|
|
|
$increase['location'] = $location;
|
|
|
|
$increase['date'] = $date;
|
|
|
|
$increase['method'] = 'Administrative Increase';
|
|
|
|
$increase['when'] = new Zend_Db_Expr('now()');
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-27 23:20:56 +00:00
|
|
|
$this->db->insert('notary', $increase);
|
|
|
|
$this->points += $points;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-27 23:20:56 +00:00
|
|
|
$this->fixAssurerFlag();
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
/**
|
|
|
|
* Maximum number of points the user may issue
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-20 11:41:52 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2011-07-05 16:54:19 +00:00
|
|
|
public function maxpoints() {
|
2011-06-20 11:41:52 +00:00
|
|
|
if (!$this->getAssurerStatus()) return 0;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
if ($this->getAge() < 18) return 10;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
$points = $this->getPoints();
|
|
|
|
if ($points >= 300) return 200;
|
|
|
|
if ($points >= 200) return 150;
|
|
|
|
if ($points >= 150) return 35;
|
|
|
|
if ($points >= 140) return 30;
|
|
|
|
if ($points >= 130) return 25;
|
|
|
|
if ($points >= 120) return 20;
|
|
|
|
if ($points >= 110) return 15;
|
|
|
|
if ($points >= 100) return 10;
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-20 11:41:52 +00:00
|
|
|
// Should not get here
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': '.$this->id.' We have reached unreachable code');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
/**
|
|
|
|
* Get the challenge types that are available in the database
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-28 18:11:03 +00:00
|
|
|
* @param $db Zend_Db_Adapter_Abstract
|
|
|
|
* The database connection to use
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-28 18:11:03 +00:00
|
|
|
* @return array(int => string)
|
|
|
|
*/
|
|
|
|
public static function getAvailableChallengeTypes(
|
|
|
|
Zend_Db_Adapter_Abstract $db) {
|
|
|
|
$query = 'select `id`, `type_text` from `cats_type`';
|
|
|
|
return $db->fetchPairs($query);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
/**
|
|
|
|
* Get the challenge variants for this type that are available in the
|
|
|
|
* database
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-28 18:11:03 +00:00
|
|
|
* @param $db Zend_Db_Adapter_Abstract
|
|
|
|
* The database connection to use
|
|
|
|
* @param $type int
|
|
|
|
* The type of challenge you want to get the variants of
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-28 18:11:03 +00:00
|
|
|
* @return array(int => string)
|
|
|
|
*/
|
|
|
|
public static function getAvailableChallengeVariants(
|
|
|
|
Zend_Db_Adapter_Abstract $db, $type) {
|
|
|
|
$query = 'select `id`, `test_text` from `cats_variant`
|
|
|
|
where `type_id` = :type';
|
|
|
|
$query_params['type'] = $type;
|
|
|
|
return $db->fetchPairs($query, $query_params);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
/**
|
|
|
|
* Assign the challenge to the user
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-28 18:11:03 +00:00
|
|
|
* @param $type int
|
|
|
|
* The type of the challenge, has to be one of the keys returned by
|
|
|
|
* getAvailableChallengeTypes()
|
|
|
|
* @param $variant int
|
|
|
|
* The variant of the challenge, has to be one of the keys returned by
|
|
|
|
* getAvailableChallengeVariants()
|
|
|
|
* @param $date Zend_Date
|
|
|
|
* The date the challenge was passed, defaults to current time
|
|
|
|
*/
|
|
|
|
public function assignChallenge($type, $variant, Zend_Date $date = null) {
|
|
|
|
$types = self::getAvailableChallengeTypes($this->db);
|
|
|
|
if (!isset($types[$type])) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': got wrong challenge type '.$type.' when '.
|
|
|
|
'assigning challenge to user '.$this->id);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
$variants = self::getAvailableChallengeVariants($this->db, $type);
|
|
|
|
if (!isset($variants[$variant])) {
|
|
|
|
throw new Exception(
|
|
|
|
__METHOD__ . ': got wrong challenge variant '.$variant.' when '.
|
|
|
|
'assigning challenge to user '.$this->id);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
$challenge = array();
|
|
|
|
$challenge['user_id'] = $this->id;
|
|
|
|
$challenge['variant_id'] = $variant;
|
2011-06-30 11:41:26 +00:00
|
|
|
if ($date !== null) {
|
|
|
|
$challenge['pass_date'] = $date->toString('Y-m-d H:i:s');
|
|
|
|
// otherwise default value of the database will be used
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
$this->db->insert('cats_passed', $challenge);
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-28 18:11:03 +00:00
|
|
|
$this->fixAssurerFlag();
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
/**
|
|
|
|
* Get the flags that are set
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-30 19:48:35 +00:00
|
|
|
* @return array (string => boolean)
|
|
|
|
*/
|
|
|
|
public function getFlags() {
|
|
|
|
$flags = $this->db->select()->from('users', self::flags())
|
|
|
|
->where('`id` = ?', $this->id)->query()->fetch();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
foreach ($flags as $key => $value) {
|
|
|
|
if ($value === '0') {
|
|
|
|
$flags[$key] = false;
|
|
|
|
} else {
|
|
|
|
$flags[$key] = true;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
return $flags;
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
/**
|
|
|
|
* Set the flags - to know which flags exist you might want to call
|
|
|
|
* getFlags() first
|
2015-07-31 05:59:57 +00:00
|
|
|
*
|
2011-06-30 19:48:35 +00:00
|
|
|
* @param $flags array (string => boolean)
|
|
|
|
* Currently unknown flags are silently ignored
|
|
|
|
*/
|
|
|
|
public function setFlags(array $flags) {
|
|
|
|
$newflags = array();
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
// filter values
|
|
|
|
foreach (self::flags() as $flag) {
|
|
|
|
if (isset($flags[$flag])) {
|
|
|
|
if ($flags[$flag]) {
|
|
|
|
$newflags[$flag] = 1;
|
|
|
|
} else {
|
|
|
|
$newflags[$flag] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
$where = $this->db->quoteInto('`id` = ?', $this->id, Zend_Db::INT_TYPE);
|
|
|
|
$this->db->update('users', $newflags, $where);
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
|
2011-06-30 19:48:35 +00:00
|
|
|
/**
|
|
|
|
* The flags from the `users` table that might be set
|
|
|
|
*/
|
|
|
|
private static function flags() {
|
|
|
|
return array(
|
|
|
|
'verified',
|
|
|
|
'listme',
|
|
|
|
'codesign',
|
|
|
|
'1024bit',
|
|
|
|
'admin',
|
|
|
|
'orgadmin',
|
|
|
|
'ttpadmin',
|
|
|
|
'adadmin',
|
|
|
|
'board',
|
|
|
|
'tverify',
|
|
|
|
'locadmin',
|
|
|
|
'locked',
|
|
|
|
'assurer',
|
|
|
|
'assurer_blocked');
|
|
|
|
}
|
2015-07-31 05:59:57 +00:00
|
|
|
}
|