New admin increase feature
authorMichael Tänzer <neo@nhng.de>
Tue, 29 Jun 2010 18:00:03 +0000 (20:00 +0200)
committerMichael Tänzer <neo@nhng.de>
Tue, 29 Jun 2010 18:00:03 +0000 (20:00 +0200)
TODO: Add to the menu somewhere
TODO: Test ;-)

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

index 98147ba..6cb238a 100644 (file)
@@ -7,6 +7,11 @@ class ManageAccountController extends Zend_Controller_Action
 {
     const MAX_POINTS_PER_ASSURANCE = 35;
     const MAX_ASSURANCE_POINTS = 100;
+    const MAX_POINTS_TOTAL = 150;
+    const ADMIN_INCREASE_FRAGMENT_SIZE = 2;
+    
+    // Value used in the database to identify a admin increase
+    const ADMIN_INCREASE_METHOD = 'Administrative Increase';
     
     protected $db;
     
@@ -84,6 +89,70 @@ class ManageAccountController extends Zend_Controller_Action
         return;
     }
     
+    public function adminIncreaseAction()
+    {
+        // Validate form
+        $form = $this->getAdminIncreaseForm();
+        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
+            $this->view->admin_increase_form = $form;
+            return $this->render('admin-increase-form');
+        }
+        
+        // Form is valid -> get values for processing
+        $values = $form->getValues();
+        
+        // Get user data
+        $user['id'] = $this->getUserId();
+        $user['points'] = $this->getPoints($user['id']);
+        
+        
+        // Do the actual increase
+        $increase = array(); // Make sure the array is empty
+        $increase['from'] = $user['id'];
+        $increase['to'] = $user['id'];
+        $increase['location'] = $values['location'];
+        $increase['date'] = $values['date'];
+        $increase['method'] = self::ADMIN_INCREASE_METHOD;
+        $increase['when'] = new Zend_Db_Expr('now()');
+        $this->view->adminIncreasesDone = array();
+        
+        $quantity = $values['quantity'];
+        do {
+            // Split up into multiple increases if fragment flag is set
+            if ($values['fragment'] == '1' &&
+                    $quantity > self::ADMIN_INCREASE_FRAGMENT_SIZE) {
+                $increase['awarded'] = self::ADMIN_INCREASE_FRAGMENT_SIZE;
+                $quantity -= self::ADMIN_INCREASE_FRAGMENT_SIZE;
+            } else {
+                $increase['awarded'] = $quantity;
+                $quantity = 0;
+            }
+            
+            // Only assign points within the limit if unlimited flag is not set
+            if ($values['unlimited'] != '1') {
+                if ($user['points'] >= self::MAX_POINTS_TOTAL) {
+                    // No more administrative increases should be done
+                    break;
+                } elseif ($user['points'] + $increase['awarded'] > self::MAX_POINTS_TOTAL) {
+                    $increase['awarded'] = self::MAX_POINTS_TOTAL - $user['points'];
+                }
+            }
+            
+            // Admin increases always have `points` == `awarded`
+            $increase['points'] = $increase['awarded'];
+            
+            $this->db->insert('notary', $increase);
+            
+            $user['points'] += $increase['points'];
+            $this->view->adminIncreasesDone[] = $increase['points'];
+        } while ($quantity > 0);
+        
+        // Maybe user is now assurer
+        $this->fixAssurerFlag($user['id']);
+        
+        return;
+    }
+    
     /**
      * Get and check the user ID of the current user
      * 
diff --git a/manager/application/views/scripts/manage-account/admin-increase-form.phtml b/manager/application/views/scripts/manage-account/admin-increase-form.phtml
new file mode 100644 (file)
index 0000000..16d6f31
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+/**
+ * @author Michael Tänzer
+ * @todo Text
+ */
+?>
+
+<?php print $this->admin_increase_form ?>
diff --git a/manager/application/views/scripts/manage-account/admin-increase.phtml b/manager/application/views/scripts/manage-account/admin-increase.phtml
new file mode 100644 (file)
index 0000000..bf984d3
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+/**
+ * @author Michael Tänzer
+ * @todo Text
+ */
+?>
+
+<h1><?php print I18n::_('Points added successfully')?></h1>
+
+<table>
+    <thead>
+        <tr><th>#</th><th><?php print I18n::_('Number of points')?></th></tr>
+    </thead>
+    <tbody>
+        <?php foreach ($this->adminIncreasesDone as $i => $points) {
+            printf('<tr><td> %1$d </td><td> %2$d </td></tr>', $i, $points);
+        }?>
+    </tbody>
+</table>