e9d214c83028b7fdf9595d3b125c486121649f88
3 * @author Michael Tänzer
6 class ManageAccountController
extends Zend_Controller_Action
8 const MAX_POINTS_PER_ASSURANCE
= 35;
9 const MAX_ASSURANCE_POINTS
= 100;
13 public function init()
15 $config = new Zend_Config_Ini(APPLICATION_PATH
. '/configs/application.ini',
18 $this->db
= Zend_Db
::factory($config->ca_mgr
->db
->auth
->pdo
,
19 $config->ca_mgr
->db
->auth
);
22 public function indexAction()
24 // Just render the view
28 public function assuranceAction()
31 $form = $this->getAssuranceForm();
32 if (!$this->getRequest()->isPost() ||
!$form->isValid($_POST)) {
33 $this->view
->assurance_form
= $form;
34 return $this->render('assuranceform');
37 // Form is valid -> get values for processing
38 $values = $form->getValues();
41 // Check identity of the user
42 $session = Zend_Registry
::get('session');
43 if ($session->authdata
['authed'] !== true
) {
44 throw new Exception(__METHOD__
. ': you need to log in to use this feature');
46 $query = 'select `id` from `users` where `id` = :user';
47 $query_params['user'] = $session->authdata
['authed_id'];
48 $result = $this->db
->query($query, $query_params);
49 if ($result->rowCount() !== 1) {
50 throw new Exception(__METHOD__
. ': user ID not found in the data base');
52 $row = $result->fetch();
53 $user['id'] = $row['id'];
56 // Get current points of the user
57 $query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
58 $query_params['user'] = $user['id'];
59 $row = $this->db
->query($query, $query_params)->fetch();
60 if ($row['total'] === NULL
) $row['total'] = 0;
61 $user['points'] = $row['total'];
64 // Do the actual assurances
65 $assurance = array(); // Make sure the array is empty
66 $assurance['to'] = $user['id'];
67 $assurance['location'] = $values['location'];
68 $assurance['date'] = $values['date'];
69 $assurance['when'] = new Zend_Db_Expr('now()');
70 $this->view
->assurancesDone
= array();
72 $quantity = $values['quantity'];
74 // split up into multiple assurances
75 if ($quantity > self
::MAX_POINTS_PER_ASSURANCE
) {
76 $assurance['awarded'] = self
::MAX_POINTS_PER_ASSURANCE
;
77 $quantity -= self
::MAX_POINTS_PER_ASSURANCE
;
79 $assurance['awarded'] = $quantity;
83 // Get the assurer for this assurance
84 $assurance['from'] = $this->getNewAssurer($user['id']);
86 // only assign points whithin the limit
87 if ($user['points'] +
$assurance['awarded'] > self
::MAX_ASSURANCE_POINTS
){
88 $assurance['points'] = self
::MAX_ASSURANCE_POINTS
- $user['points'];
90 $assurance['points'] = $assurance['awarded'];
93 $this->db
->insert('notary', $assurance);
95 $user['points'] +
= $assurance['points'];
96 $this->view
->assurancesDone
[] = $assurance['points'];
97 } while ($quantity > 0);
100 // Fix the assurer flag
101 $query = 'UPDATE `users` SET `assurer` = 1 WHERE `users`.`id` = :user AND '.
103 'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
104 'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
105 '`cp`.`user_id` = :user) AND '.
107 '(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
108 '`expire` < now()) >= 100';
109 $query_params['user'] = $user['id'];
110 $this->db
->query($query, $query_params);
116 * Get the first assurer who didn't already assure the user
118 * @param int $user_id The ID of the user who should get assured
119 * @return int The ID of the selected assurer
121 protected function getNewAssurer($user_id)
123 $query = 'select min(`id`) as `assurer` from `users` ' .
124 'where `email` like \'john.doe-___@example.com\' and ' .
125 '`id` not in (select `from` from `notary` where `to` = :user)';
126 $query_params['user'] = $user_id;
127 $row = $this->db
->query($query, $query_params)->fetch();
129 if ($row['assurer'] === NULL
) {
130 throw new Exception(__METHOD__
. ': no more assurers that haven\'t '.
131 'already assured this account');
134 return $row['assurer'];
137 protected function getAssuranceForm()
139 $form = new Zend_Form();
140 $form->setAction('/manage-account/assurance')->setMethod('post');
142 $quantity = new Zend_Form_Element_Text('quantity');
143 $quantity->setRequired(true
)
144 ->setLabel(I18n
::_('Number of Points'))
145 ->addFilter(new Zend_Filter_Int())
146 ->addValidator(new Zend_Validate_Between(0, 100));
147 $form->addElement($quantity);
149 $location = new Zend_Form_Element_Text('location');
150 $location->setRequired(true
)
151 ->setLabel(I18n
::_('Location'))
152 ->setValue(I18n
::_('CAcert Test Manager'))
153 ->addValidator(new Zend_Validate_StringLength(1,255));
154 $form->addElement($location);
156 $date = new Zend_Form_Element_Text('date');
157 $date->setRequired(true
)
158 ->setLabel(I18n
::_('Date of Assurance'))
159 ->setValue(date('Y-m-d H:i:s'))
160 ->addValidator(new Zend_Validate_StringLength(1,255));
161 $form->addElement($date);
163 $submit = new Zend_Form_Element_Submit('submit');
164 $submit->setLabel(I18n
::_('Assure Me'));
165 $form->addElement($submit);
170 protected function getAdminIncreaseForm()
172 $form = new Zend_Form();
173 $form->setAction('/manage-account/admin-increase')->setMethod('post');
175 $quantity = new Zend_Form_Element_Text('quantity');
176 $quantity->setRequired(true
)
177 ->setLabel(I18n
::_('Number of Points'))
178 ->addFilter(new Zend_Filter_Int())
179 ->addValidator(new Zend_Validate_GreaterThan(0));
180 $form->addElement($quantity);
182 $fragment = new Zend_Form_Element_Checkbox('fragment');
183 $fragment->setLabel(I18n
::_('Split into 2-Point Fragments'))
185 $form->addElement($fragment);
187 $unlimited = new Zend_Form_Element_Checkbox('unlimited');
188 $unlimited->setLabel(I18n
::_('Assign Points even if the Limit of 150 '.
191 $form->addElement($unlimited);
193 $location = new Zend_Form_Element_Text('location');
194 $location->setRequired(true
)
195 ->setLabel(I18n
::_('Location'))
196 ->setValue(I18n
::_('CAcert Test Manager'))
197 ->addValidator(new Zend_Validate_StringLength(1,255));
198 $form->addElement($location);
200 $date = new Zend_Form_Element_Text('date');
201 $date->setRequired(true
)
202 ->setLabel(I18n
::_('Date of Increase'))
203 ->setValue(date('Y-m-d H:i:s'))
204 ->addValidator(new Zend_Validate_StringLength(1,255));
205 $form->addElement($date);
207 $submit = new Zend_Form_Element_Submit('submit');
208 $submit->setLabel(I18n
::_('Give Me Points'));
209 $form->addElement($submit);