A new Assurer has to be selected for every single assurance
[cacert-mgr.git] / manager / application / controllers / AddPointsController.php
index 1b3380b..891ee5d 100644 (file)
@@ -57,19 +57,6 @@ class AddPointsController extends Zend_Controller_Action
         $user['id'] = $row['id'];
         
         
         $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'];
         // Get current points of the user
         $query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
         $query_params['user'] = $user['id'];
@@ -80,24 +67,26 @@ class AddPointsController extends Zend_Controller_Action
         
         // Do the actual assurances
         $assurance = array(); // Make sure the array is empty
         
         // 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();
         
         $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'];
+        $quantity = $values['quantity'];
         do {
             // split up into multiple assurances
         do {
             // split up into multiple assurances
-            if ($points > self::MAX_POINTS_PER_ASSURANCE) {
+            if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
                 $assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
                 $assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
-                $points -= self::MAX_POINTS_PER_ASSURANCE;
+                $quantity -= self::MAX_POINTS_PER_ASSURANCE;
             } else {
             } else {
-                $assurance['awarded'] = $points;
-                $points = 0;
+                $assurance['awarded'] = $quantity;
+                $quantity = 0;
             }
             
             }
             
+            // Get the assurer for this assurance
+            $assurance['from'] = $this->getNewAssurer($user['id']);
+            
             // only assign points whithin the limit
             if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
                 $assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
             // only assign points whithin the limit
             if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
                 $assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
@@ -109,7 +98,7 @@ class AddPointsController extends Zend_Controller_Action
             
             $user['points'] += $assurance['points'];
             $this->view->assurancesDone[] = $assurance['points'];
             
             $user['points'] += $assurance['points'];
             $this->view->assurancesDone[] = $assurance['points'];
-        } while ($points > 0);
+        } while ($quantity > 0);
         
         
         // Fix the assurer flag
         
         
         // Fix the assurer flag
@@ -129,6 +118,28 @@ class AddPointsController extends Zend_Controller_Action
         return;
     }
     
         return;
     }
     
+    /**
+     * 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'];
+    }
+    
     protected function getAssuranceForm()
     {
         $form = new Zend_Form();
     protected function getAssuranceForm()
     {
         $form = new Zend_Form();