2010-04-22 16:26:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Michael Tänzer
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AddPointsController extends Zend_Controller_Action
|
|
|
|
{
|
2010-04-29 13:27:33 +00:00
|
|
|
const MAX_POINTS_PER_ASSURANCE = 35;
|
|
|
|
const MAX_ASSURANCE_POINTS = 100;
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
2010-04-22 16:26:41 +00:00
|
|
|
public function init()
|
|
|
|
{
|
2010-04-29 13:27:33 +00:00
|
|
|
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
|
|
|
|
APPLICATION_ENV);
|
|
|
|
|
|
|
|
$this->db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
|
|
|
|
$config->ca_mgr->db->auth);
|
2010-04-22 16:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2010-04-26 18:57:27 +00:00
|
|
|
$this->view->assurance_form = $this->getAssuranceForm();
|
|
|
|
$this->render('index');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function assuranceAction()
|
|
|
|
{
|
2010-04-29 13:27:33 +00:00
|
|
|
// Validate form
|
2010-04-26 18:57:27 +00:00
|
|
|
if (!$this->getRequest()->isPost()) {
|
|
|
|
return $this->_forward('index');
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = $this->getAssuranceForm();
|
|
|
|
if (!$form->isValid($_POST)) {
|
|
|
|
$this->view->assurance_form = $form;
|
|
|
|
return $this->render('index');
|
|
|
|
}
|
|
|
|
|
2010-04-29 13:27:33 +00:00
|
|
|
// Form is valid -> get values for processing
|
2010-04-26 18:57:27 +00:00
|
|
|
$values = $form->getValues();
|
2010-04-29 13:27:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Check identity of the user
|
|
|
|
$session = Zend_Registry::get('session');
|
|
|
|
if ($session->authdata['authed'] !== true) {
|
|
|
|
throw new Exception(__METHOD__ . ': you need to log in to use this feature');
|
|
|
|
}
|
|
|
|
$query = 'select `id` from `users` where `id` = :user';
|
|
|
|
$query_params['user'] = $session->authdata['authed_id'];
|
|
|
|
$result = $this->db->query($query, $query_params);
|
|
|
|
if ($result->rowCount() !== 1) {
|
|
|
|
throw new Exception(__METHOD__ . ': user ID not found in the data base');
|
|
|
|
}
|
|
|
|
$row = $result->fetch();
|
|
|
|
$user['id'] = $row['id'];
|
|
|
|
|
|
|
|
|
2010-04-29 19:18:14 +00:00
|
|
|
// Get the first assurer who didn't already assure the user
|
|
|
|
$assurer = $this->getNewAssurer($user['id']);
|
2010-04-29 13:27:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Get current points of the user
|
|
|
|
$query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
|
|
|
|
$query_params['user'] = $user['id'];
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
|
|
|
if ($row['total'] === NULL) $row['total'] = 0;
|
|
|
|
$user['points'] = $row['total'];
|
|
|
|
|
|
|
|
|
|
|
|
// Do the actual assurances
|
|
|
|
$assurance = array(); // Make sure the array is empty
|
|
|
|
$assurance['from'] = $assurer;
|
|
|
|
$assurance['to'] = $user['id'];
|
|
|
|
$assurance['location'] = $values['location'];
|
|
|
|
$assurance['date'] = $values['date'];
|
|
|
|
$assurance['when'] = new Zend_Db_Expr('now()');
|
|
|
|
$this->view->assurancesDone = array();
|
|
|
|
|
2010-04-29 19:18:14 +00:00
|
|
|
$quantity = $values['quantity'];
|
2010-04-29 13:27:33 +00:00
|
|
|
do {
|
|
|
|
// split up into multiple assurances
|
2010-04-29 19:18:14 +00:00
|
|
|
if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
|
2010-04-29 18:49:27 +00:00
|
|
|
$assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
|
2010-04-29 19:18:14 +00:00
|
|
|
$quantity -= self::MAX_POINTS_PER_ASSURANCE;
|
2010-04-29 13:27:33 +00:00
|
|
|
} else {
|
2010-04-29 19:18:14 +00:00
|
|
|
$assurance['awarded'] = $quantity;
|
|
|
|
$quantity = 0;
|
2010-04-29 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only assign points whithin the limit
|
2010-04-29 18:49:27 +00:00
|
|
|
if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
|
|
|
|
$assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
|
2010-04-29 13:27:33 +00:00
|
|
|
} else {
|
|
|
|
$assurance['points'] = $assurance['awarded'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->insert('notary', $assurance);
|
|
|
|
|
|
|
|
$user['points'] += $assurance['points'];
|
|
|
|
$this->view->assurancesDone[] = $assurance['points'];
|
2010-04-29 19:18:14 +00:00
|
|
|
} while ($quantity > 0);
|
2010-04-29 13:27:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Fix the assurer flag
|
|
|
|
$where = array();
|
|
|
|
$query = '`users`.`id` = :user';
|
|
|
|
$query_params['user'] = $user['id'];
|
|
|
|
$where[] = $this->db->quoteInto($query, $query_params);
|
|
|
|
$query = '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';
|
|
|
|
$where[] = $this->db->quoteInto($query, $query_params);
|
|
|
|
$query = '(select sum(`points`) from `notary` where `to`= :user and ' .
|
|
|
|
'`expire` > now()) >= 100';
|
|
|
|
$where[] = $this->db->quoteInto($query, $query_params);
|
|
|
|
$this->db->update('users', array('assurer' => 1), $where);
|
|
|
|
|
|
|
|
return;
|
2010-04-26 18:57:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 19:18:14 +00:00
|
|
|
/**
|
|
|
|
* Get the first assurer who didn't already assure the user
|
|
|
|
*
|
|
|
|
* @param int $user_id The ID of the user who should get assured
|
|
|
|
* @return int The ID of the selected assurer
|
|
|
|
*/
|
|
|
|
protected function getNewAssurer($user_id)
|
|
|
|
{
|
|
|
|
$query = 'select min(`id`) as `assurer` from `users` ' .
|
|
|
|
'where `email` like \'john.doe-___@example.com\' and ' .
|
|
|
|
'`id` not in (select `from` from `notary` where `to` = :user)';
|
|
|
|
$query_params['user'] = $user_id;
|
|
|
|
$row = $this->db->query($query, $query_params)->fetch();
|
|
|
|
|
|
|
|
if ($row['assurer'] === NULL) {
|
|
|
|
throw new Exception(__METHOD__ . ': no more assurers that haven\'t '.
|
|
|
|
'already assured this account');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $row['assurer'];
|
|
|
|
}
|
|
|
|
|
2010-04-26 18:57:27 +00:00
|
|
|
protected function getAssuranceForm()
|
|
|
|
{
|
|
|
|
$form = new Zend_Form();
|
|
|
|
$form->setAction('/add-points/assurance')->setMethod('post');
|
|
|
|
|
|
|
|
$quantity = new Zend_Form_Element_Text('quantity');
|
|
|
|
$quantity->setRequired(true)
|
|
|
|
->setLabel(I18n::_('Number of Points'))
|
|
|
|
->addFilter(new Zend_Filter_Int())
|
|
|
|
->addValidator(new Zend_Validate_Between(0, 100));
|
|
|
|
$form->addElement($quantity);
|
|
|
|
|
2010-04-29 13:27:33 +00:00
|
|
|
$location = new Zend_Form_Element_Text('location');
|
|
|
|
$location->setRequired(true)
|
|
|
|
->setLabel(I18n::_('Location'))
|
|
|
|
->setValue(I18n::_('CACert Test Manager'))
|
|
|
|
->addValidator(new Zend_Validate_StringLength(1,255));
|
|
|
|
$form->addElement($location);
|
|
|
|
|
|
|
|
$date = new Zend_Form_Element_Text('date');
|
|
|
|
$date->setRequired(true)
|
|
|
|
->setLabel(I18n::_('Date of Assurance'))
|
|
|
|
->setValue(date('Y-m-d H:i:s'))
|
|
|
|
->addValidator(new Zend_Validate_StringLength(1,255));
|
|
|
|
$form->addElement($date);
|
|
|
|
|
2010-04-26 18:57:27 +00:00
|
|
|
$submit = new Zend_Form_Element_Submit('submit');
|
|
|
|
$submit->setLabel(I18n::_('Assure Me'));
|
|
|
|
$form->addElement($submit);
|
|
|
|
|
|
|
|
return $form;
|
2010-04-22 16:26:41 +00:00
|
|
|
}
|
|
|
|
}
|