summaryrefslogtreecommitdiff
path: root/manager/application/controllers/AddPointsController.php
diff options
context:
space:
mode:
Diffstat (limited to 'manager/application/controllers/AddPointsController.php')
-rw-r--r--manager/application/controllers/AddPointsController.php57
1 files changed, 34 insertions, 23 deletions
diff --git a/manager/application/controllers/AddPointsController.php b/manager/application/controllers/AddPointsController.php
index 95b1d5f..891ee5d 100644
--- a/manager/application/controllers/AddPointsController.php
+++ b/manager/application/controllers/AddPointsController.php
@@ -57,19 +57,6 @@ class AddPointsController extends Zend_Controller_Action
$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'];
@@ -80,27 +67,29 @@ class AddPointsController extends Zend_Controller_Action
// 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'];
+ $quantity = $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;
+ if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
+ $assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
+ $quantity -= self::MAX_POINTS_PER_ASSURANCE;
} 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'] > MAX_ASSURANCE_POINTS){
- $assurance['points'] = MAX_ASSURANCE_POINTS - $user['points'];
+ if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
+ $assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
} else {
$assurance['points'] = $assurance['awarded'];
}
@@ -109,7 +98,7 @@ class AddPointsController extends Zend_Controller_Action
$user['points'] += $assurance['points'];
$this->view->assurancesDone[] = $assurance['points'];
- } while ($points > 0);
+ } while ($quantity > 0);
// Fix the assurer flag
@@ -129,6 +118,28 @@ class AddPointsController extends Zend_Controller_Action
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();