Implement batch assurance.
authorMichael Tänzer <neo@nhng.de>
Tue, 5 Jul 2011 16:54:19 +0000 (18:54 +0200)
committerMichael Tänzer <neo@nhng.de>
Tue, 5 Jul 2011 16:54:19 +0000 (18:54 +0200)
Fixes bug 944

Signed-off-by: Michael Tänzer <neo@nhng.de>
manager/application/controllers/ManageAccountController.php
manager/application/models/User.php
manager/application/views/scripts/manage-account/batch-assurance-form.phtml [new file with mode: 0644]
manager/application/views/scripts/manage-account/batch-assurance.phtml [new file with mode: 0644]

index be185c4..e012318 100644 (file)
@@ -25,6 +25,7 @@ class ManageAccountController extends Zend_Controller_Action
         // Build the left navigation
         $actions = array();
         $actions['assurance'] = I18n::_('Automated Assurance');
+        $actions['batch-assurance'] = I18n::_('Batch Assurance');
         $actions['admin-increase'] = I18n::_('Administrative Increase');
         $actions['assurer-challenge'] = I18n::_('Assurer Challenge');
         $actions['flags'] = I18n::_('Set Flags');
@@ -81,6 +82,45 @@ class ManageAccountController extends Zend_Controller_Action
         return;
     }
     
+    public function batchAssuranceAction() {
+       // Validate form
+        $form = $this->getBatchAssuranceForm();
+        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
+            $this->view->batch_assurance_form = $form;
+            return $this->render('batch-assurance-form');
+        }
+        
+        // Form is valid -> get values for processing
+        $values = $form->getValues();
+        
+        $user = Default_Model_User::findCurrentUser();
+        
+        $location = $values['location'];
+        $date = $values['date'];
+        
+        $this->view->assurances = array();
+        
+        for ($i = 0; $i < intval($values['quantity']); $i++) {
+            $assuree = $user->findNewAssuree();
+            
+            if ($values['percentage'] === 'percentage') {
+                $points = ($user->maxpoints() * intval($values['points']) /100);
+            }elseif ($values['percentage'] === 'absolute') {
+                $points = intval($values['points']);
+            }
+            
+            $user->assure($assuree, $points, $location, $date);
+            
+            $this->view->assurances[] = array(
+                    'assuree'=>$assuree->getPrimEmail(),
+                    'points'=>$points,
+                    'location'=>$location,
+                    'date'=>$date);
+        }
+        
+        return;
+    }
+    
     public function adminIncreaseAction()
     {
         // Validate form
@@ -190,6 +230,57 @@ class ManageAccountController extends Zend_Controller_Action
         return $form;
     }
     
+    protected function getBatchAssuranceForm() {
+       $form = new Zend_Form();
+        $form->setAction('/manage-account/batch-assurance')->setMethod('post');
+        
+        $quantity = new Zend_Form_Element_Text('quantity');
+        $quantity->setRequired(true)
+                ->setLabel(I18n::_('Number of Assurances'))
+                ->setValue('25')
+                ->addFilter(new Zend_Filter_Int())
+                ->addValidator(new Zend_Validate_Between(0, 100));
+        $form->addElement($quantity);
+        
+        $percentage = new Zend_Form_Element_Select('percentage');
+        $percentage->setRequired(true)
+                ->setLabel(I18n::_('Are the points specified absolute?'))
+                ->setValue('percentage')
+                ->setMultiOptions(array(
+                           'percentage' => I18n::_('Percentage'),
+                        'absolute' => I18n::_('Absolute'),
+                    ));
+        $form->addElement($percentage);
+        
+        $points = new Zend_Form_Element_Text('points');
+        $points->setRequired(true)
+            ->setLabel(I18n::_('Points per Assurance'))
+            ->setValue('100')
+            ->addFilter(new Zend_Filter_Int())
+            ->addValidator(new Zend_Validate_Between(0, 100));
+        $form->addElement($points);
+        
+        $location = new Zend_Form_Element_Text('location');
+        $location->setRequired(true)
+                ->setLabel(I18n::_('Location'))
+                ->setValue(I18n::_('CAcert Test Manager Batch Assurance'))
+                ->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::_('Make Batch Assurance'));
+        $form->addElement($submit);
+        
+        return $form;
+    }
+    
     protected function getAdminIncreaseForm()
     {
         $form = new Zend_Form();
index b8285a4..1e785f1 100644 (file)
@@ -81,6 +81,27 @@ class Default_Model_User {
     }
     
     /**
+     * Get the first assuree who hasn't already been assured by this user
+     * 
+     * @return Default_Model_User
+     */
+    public function findNewAssuree() {
+        $query = 'select min(`id`) as `assuree` from `users` ' .
+               'where `email` like \'john.doe-___@example.com\' and ' .
+            '`id` not in (select `to` from `notary` where `from` = :user)';
+        $query_params['user'] = $this->id;
+        $row = $this->db->query($query, $query_params)->fetch();
+        
+        if ($row['assuree'] === NULL) {
+            throw new Exception(
+                __METHOD__ . ': no more assurees that haven\'t already '.
+                'been assured by this account');
+        }
+        
+        return new Default_Model_User($this->db, $row['assuree']);
+    }
+    
+    /**
      * Refresh the current value of points from the test server
      * 
      * Needed if operations outside this class are made, that might affect the
@@ -181,6 +202,17 @@ class Default_Model_User {
     }
     
     /**
+     * @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();
+        
+        return $row['email'];
+    }
+    
+    /**
      * Assure another user. Usual restrictions apply
      * 
      * @param $assuree Default_Model_User
@@ -314,7 +346,7 @@ class Default_Model_User {
      * 
      * @return int
      */
-    private function maxpoints() {
+    public function maxpoints() {
         if (!$this->getAssurerStatus()) return 0;
         
         if ($this->getAge() < 18) return 10;
diff --git a/manager/application/views/scripts/manage-account/batch-assurance-form.phtml b/manager/application/views/scripts/manage-account/batch-assurance-form.phtml
new file mode 100644 (file)
index 0000000..3a68691
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @author Michael Tänzer
+ */
+?>
+
+<h1><?php print I18n::_('Do Multiple Assurances at Once') ?></h1>
+
+<p><?php print I18n::_('Assure multiple dummy accounts in one step. '.
+       'The assurances look just like normal assurances and can be used to gain '.
+    'experience points without suffering from RSI (also known as mouse arm).')
+?></p>
+
+<p><?php print I18n::_('The amount of points given per Assurance can be '.
+       'specified in two ways:') ?></p>
+<dl>
+       <dt><?php print I18n::_('Percentage') ?></dt>
+       <dd><?php print I18n::_('n percent of the maximal possible points are '.
+               'given per Assurance. If it\'s not an integer it is rounded down to '.
+               'the next integer') ?></dd>
+       <dt><?php print I18n::_('Absolute') ?></dt>
+       <dd><?php print I18n::_('Exactly n points are given. If n exceeds the '.
+               'current maximum of points that may be given, it is set to the '.
+               'maximum until due to the gained experience points more points may be '.
+               'given') ?></dd>
+</dl>
+
+<?php print $this->batch_assurance_form ?>
diff --git a/manager/application/views/scripts/manage-account/batch-assurance.phtml b/manager/application/views/scripts/manage-account/batch-assurance.phtml
new file mode 100644 (file)
index 0000000..4e0ae82
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @author Michael Tänzer <neo@nhng.de>
+ */
+?>
+
+<h1><?php print I18n::_('Assurances executed successfully')?></h1>
+
+<p><?php print I18n::_('The following assurances were executed:')?></p>
+
+<table>
+       <thead>
+               <tr>
+                       <th>#</th>
+                       <th><?php print I18n::_('Assuree')?></th>
+                       <th><?php print I18n::_('Number of points')?></th>
+               </tr>
+       </thead>
+       <tbody>
+               <?php foreach ($this->assurancesDone as $i => $assurance) {?>
+                   <tr>
+                       <td><?php print $i ?></td>
+                       <td><?php print $assurance['assuree'] ?></td>
+                       <td><?php print $assurance['points'] ?></td>
+                   </tr>
+               <?php }?>
+       </tbody>
+</table>
\ No newline at end of file