summaryrefslogtreecommitdiff
path: root/manager
diff options
context:
space:
mode:
authorMichael Tänzer <neo@nhng.de>2010-04-29 15:27:33 +0200
committerMichael Tänzer <neo@nhng.de>2010-04-29 15:27:33 +0200
commit63afee5e1749aaf8b332362a5b96e898d335ec5f (patch)
tree7f17ba0f32a540345701a363d468f7441ab0eeb7 /manager
parentc0b92575118607007447ed064e1b81c9bc5e4311 (diff)
downloadcacert-mgr-63afee5e1749aaf8b332362a5b96e898d335ec5f.tar.gz
cacert-mgr-63afee5e1749aaf8b332362a5b96e898d335ec5f.tar.xz
cacert-mgr-63afee5e1749aaf8b332362a5b96e898d335ec5f.zip
Flesh out AddPointsController
- Implement procedure as in the live code (notification emails are not sent) - Add more elements to the form Signed-off-by: Michael Tänzer <neo@nhng.de>
Diffstat (limited to 'manager')
-rw-r--r--manager/application/controllers/AddPointsController.php117
1 files changed, 113 insertions, 4 deletions
diff --git a/manager/application/controllers/AddPointsController.php b/manager/application/controllers/AddPointsController.php
index ff66f0b..95b1d5f 100644
--- a/manager/application/controllers/AddPointsController.php
+++ b/manager/application/controllers/AddPointsController.php
@@ -5,9 +5,18 @@
class AddPointsController extends Zend_Controller_Action
{
+ const MAX_POINTS_PER_ASSURANCE = 35;
+ const MAX_ASSURANCE_POINTS = 100;
+
+ protected $db;
+
public function init()
{
- /* Initialize action controller here */
+ $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);
}
public function indexAction()
@@ -18,7 +27,7 @@ class AddPointsController extends Zend_Controller_Action
public function assuranceAction()
{
- /* Validate form */
+ // Validate form
if (!$this->getRequest()->isPost()) {
return $this->_forward('index');
}
@@ -29,9 +38,95 @@ class AddPointsController extends Zend_Controller_Action
return $this->render('index');
}
-
- /* Form is valid -> get values and process them */
+ // Form is valid -> get values for processing
$values = $form->getValues();
+
+
+ // 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'];
+
+
+ // Get the first assurer who didn't already assure the user
+ $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');
+ }
+ $assurer = $row['assurer'];
+
+
+ // 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();
+
+ $points = $values['quantity'];
+ do {
+ // split up into multiple assurances
+ if ($points > MAX_POINTS_PER_ASSURANCE) {
+ $assurance['awarded'] = MAX_POINTS_PER_ASSURANCE;
+ $points -= MAX_POINTS_PER_ASSURANCE;
+ } else {
+ $assurance['awarded'] = $points;
+ $points = 0;
+ }
+
+ // only assign points whithin the limit
+ if ($user['points'] + $assurance['awarded'] > MAX_ASSURANCE_POINTS){
+ $assurance['points'] = MAX_ASSURANCE_POINTS - $user['points'];
+ } else {
+ $assurance['points'] = $assurance['awarded'];
+ }
+
+ $this->db->insert('notary', $assurance);
+
+ $user['points'] += $assurance['points'];
+ $this->view->assurancesDone[] = $assurance['points'];
+ } while ($points > 0);
+
+
+ // 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;
}
protected function getAssuranceForm()
@@ -46,6 +141,20 @@ class AddPointsController extends Zend_Controller_Action
->addValidator(new Zend_Validate_Between(0, 100));
$form->addElement($quantity);
+ $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);
+
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel(I18n::_('Assure Me'));
$form->addElement($submit);