Refactor
[cacert-mgr.git] / manager / application / controllers / AddPointsController.php
1 <?php
2 /**
3 * @author Michael Tänzer
4 */
5
6 class AddPointsController extends Zend_Controller_Action
7 {
8 const MAX_POINTS_PER_ASSURANCE = 35;
9 const MAX_ASSURANCE_POINTS = 100;
10
11 protected $db;
12
13 public function init()
14 {
15 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
16 APPLICATION_ENV);
17
18 $this->db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
19 $config->ca_mgr->db->auth);
20 }
21
22 public function indexAction()
23 {
24 $this->view->assurance_form = $this->getAssuranceForm();
25 $this->render('index');
26 }
27
28 public function assuranceAction()
29 {
30 // Validate form
31 if (!$this->getRequest()->isPost()) {
32 return $this->_forward('index');
33 }
34
35 $form = $this->getAssuranceForm();
36 if (!$form->isValid($_POST)) {
37 $this->view->assurance_form = $form;
38 return $this->render('index');
39 }
40
41 // Form is valid -> get values for processing
42 $values = $form->getValues();
43
44
45 // Check identity of the user
46 $session = Zend_Registry::get('session');
47 if ($session->authdata['authed'] !== true) {
48 throw new Exception(__METHOD__ . ': you need to log in to use this feature');
49 }
50 $query = 'select `id` from `users` where `id` = :user';
51 $query_params['user'] = $session->authdata['authed_id'];
52 $result = $this->db->query($query, $query_params);
53 if ($result->rowCount() !== 1) {
54 throw new Exception(__METHOD__ . ': user ID not found in the data base');
55 }
56 $row = $result->fetch();
57 $user['id'] = $row['id'];
58
59
60 // Get the first assurer who didn't already assure the user
61 $assurer = $this->getNewAssurer($user['id']);
62
63
64 // Get current points of the user
65 $query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
66 $query_params['user'] = $user['id'];
67 $row = $this->db->query($query, $query_params)->fetch();
68 if ($row['total'] === NULL) $row['total'] = 0;
69 $user['points'] = $row['total'];
70
71
72 // Do the actual assurances
73 $assurance = array(); // Make sure the array is empty
74 $assurance['from'] = $assurer;
75 $assurance['to'] = $user['id'];
76 $assurance['location'] = $values['location'];
77 $assurance['date'] = $values['date'];
78 $assurance['when'] = new Zend_Db_Expr('now()');
79 $this->view->assurancesDone = array();
80
81 $quantity = $values['quantity'];
82 do {
83 // split up into multiple assurances
84 if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
85 $assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
86 $quantity -= self::MAX_POINTS_PER_ASSURANCE;
87 } else {
88 $assurance['awarded'] = $quantity;
89 $quantity = 0;
90 }
91
92 // only assign points whithin the limit
93 if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
94 $assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
95 } else {
96 $assurance['points'] = $assurance['awarded'];
97 }
98
99 $this->db->insert('notary', $assurance);
100
101 $user['points'] += $assurance['points'];
102 $this->view->assurancesDone[] = $assurance['points'];
103 } while ($quantity > 0);
104
105
106 // Fix the assurer flag
107 $where = array();
108 $query = '`users`.`id` = :user';
109 $query_params['user'] = $user['id'];
110 $where[] = $this->db->quoteInto($query, $query_params);
111 $query = 'exists(select * from `cats_passed` as `cp`, ' .
112 '`cats_variant` as `cv` where `cp`.`variant_id` = `cv`.`id` and ' .
113 '`cv`.`type_id` = 1 and `cp`.`user_id` = :user';
114 $where[] = $this->db->quoteInto($query, $query_params);
115 $query = '(select sum(`points`) from `notary` where `to`= :user and ' .
116 '`expire` > now()) >= 100';
117 $where[] = $this->db->quoteInto($query, $query_params);
118 $this->db->update('users', array('assurer' => 1), $where);
119
120 return;
121 }
122
123 /**
124 * Get the first assurer who didn't already assure the user
125 *
126 * @param int $user_id The ID of the user who should get assured
127 * @return int The ID of the selected assurer
128 */
129 protected function getNewAssurer($user_id)
130 {
131 $query = 'select min(`id`) as `assurer` from `users` ' .
132 'where `email` like \'john.doe-___@example.com\' and ' .
133 '`id` not in (select `from` from `notary` where `to` = :user)';
134 $query_params['user'] = $user_id;
135 $row = $this->db->query($query, $query_params)->fetch();
136
137 if ($row['assurer'] === NULL) {
138 throw new Exception(__METHOD__ . ': no more assurers that haven\'t '.
139 'already assured this account');
140 }
141
142 return $row['assurer'];
143 }
144
145 protected function getAssuranceForm()
146 {
147 $form = new Zend_Form();
148 $form->setAction('/add-points/assurance')->setMethod('post');
149
150 $quantity = new Zend_Form_Element_Text('quantity');
151 $quantity->setRequired(true)
152 ->setLabel(I18n::_('Number of Points'))
153 ->addFilter(new Zend_Filter_Int())
154 ->addValidator(new Zend_Validate_Between(0, 100));
155 $form->addElement($quantity);
156
157 $location = new Zend_Form_Element_Text('location');
158 $location->setRequired(true)
159 ->setLabel(I18n::_('Location'))
160 ->setValue(I18n::_('CACert Test Manager'))
161 ->addValidator(new Zend_Validate_StringLength(1,255));
162 $form->addElement($location);
163
164 $date = new Zend_Form_Element_Text('date');
165 $date->setRequired(true)
166 ->setLabel(I18n::_('Date of Assurance'))
167 ->setValue(date('Y-m-d H:i:s'))
168 ->addValidator(new Zend_Validate_StringLength(1,255));
169 $form->addElement($date);
170
171 $submit = new Zend_Form_Element_Submit('submit');
172 $submit->setLabel(I18n::_('Assure Me'));
173 $form->addElement($submit);
174
175 return $form;
176 }
177 }