diff --git a/manager/application/controllers/ManageAccountController.php b/manager/application/controllers/ManageAccountController.php index be185c4..e012318 100644 --- a/manager/application/controllers/ManageAccountController.php +++ b/manager/application/controllers/ManageAccountController.php @@ -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(); diff --git a/manager/application/models/User.php b/manager/application/models/User.php index b8285a4..1e785f1 100644 --- a/manager/application/models/User.php +++ b/manager/application/models/User.php @@ -80,6 +80,27 @@ class Default_Model_User { return new Default_Model_User($this->db, $row['assurer']); } + /** + * 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 * @@ -180,6 +201,17 @@ class Default_Model_User { return $age; } + /** + * @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 * @@ -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 index 0000000..3a68691 --- /dev/null +++ b/manager/application/views/scripts/manage-account/batch-assurance-form.phtml @@ -0,0 +1,28 @@ + + +

+ +

+ +

+
+
+
+
+
+
+ +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 index 0000000..4e0ae82 --- /dev/null +++ b/manager/application/views/scripts/manage-account/batch-assurance.phtml @@ -0,0 +1,28 @@ + + */ +?> + +

+ +

+ + + + + + + + + + + assurancesDone as $i => $assurance) {?> + + + + + + + +
#
\ No newline at end of file