Class constants have to be prefixed with self:: in PHP
[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 $query = 'select min(`id`) as `assurer` from `users` ' .
62 'where `email` like \'john.doe-___@example.com\' and ' .
63 '`id` not in (select `from` from `notary` where `to` = :user)';
64 $query_params['user'] = $user['id'];
65 $row = $this->db->query($query, $query_params)->fetch();
66 if ($row['assurer'] === NULL) {
67 throw new Exception(__METHOD__ . ': no more assurers that haven\'t '.
68 'already assured this account');
69 }
70 $assurer = $row['assurer'];
71
72
73 // Get current points of the user
74 $query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
75 $query_params['user'] = $user['id'];
76 $row = $this->db->query($query, $query_params)->fetch();
77 if ($row['total'] === NULL) $row['total'] = 0;
78 $user['points'] = $row['total'];
79
80
81 // Do the actual assurances
82 $assurance = array(); // Make sure the array is empty
83 $assurance['from'] = $assurer;
84 $assurance['to'] = $user['id'];
85 $assurance['location'] = $values['location'];
86 $assurance['date'] = $values['date'];
87 $assurance['when'] = new Zend_Db_Expr('now()');
88 $this->view->assurancesDone = array();
89
90 $points = $values['quantity'];
91 do {
92 // split up into multiple assurances
93 if ($points > self::MAX_POINTS_PER_ASSURANCE) {
94 $assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
95 $points -= self::MAX_POINTS_PER_ASSURANCE;
96 } else {
97 $assurance['awarded'] = $points;
98 $points = 0;
99 }
100
101 // only assign points whithin the limit
102 if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
103 $assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
104 } else {
105 $assurance['points'] = $assurance['awarded'];
106 }
107
108 $this->db->insert('notary', $assurance);
109
110 $user['points'] += $assurance['points'];
111 $this->view->assurancesDone[] = $assurance['points'];
112 } while ($points > 0);
113
114
115 // Fix the assurer flag
116 $where = array();
117 $query = '`users`.`id` = :user';
118 $query_params['user'] = $user['id'];
119 $where[] = $this->db->quoteInto($query, $query_params);
120 $query = 'exists(select * from `cats_passed` as `cp`, ' .
121 '`cats_variant` as `cv` where `cp`.`variant_id` = `cv`.`id` and ' .
122 '`cv`.`type_id` = 1 and `cp`.`user_id` = :user';
123 $where[] = $this->db->quoteInto($query, $query_params);
124 $query = '(select sum(`points`) from `notary` where `to`= :user and ' .
125 '`expire` > now()) >= 100';
126 $where[] = $this->db->quoteInto($query, $query_params);
127 $this->db->update('users', array('assurer' => 1), $where);
128
129 return;
130 }
131
132 protected function getAssuranceForm()
133 {
134 $form = new Zend_Form();
135 $form->setAction('/add-points/assurance')->setMethod('post');
136
137 $quantity = new Zend_Form_Element_Text('quantity');
138 $quantity->setRequired(true)
139 ->setLabel(I18n::_('Number of Points'))
140 ->addFilter(new Zend_Filter_Int())
141 ->addValidator(new Zend_Validate_Between(0, 100));
142 $form->addElement($quantity);
143
144 $location = new Zend_Form_Element_Text('location');
145 $location->setRequired(true)
146 ->setLabel(I18n::_('Location'))
147 ->setValue(I18n::_('CACert Test Manager'))
148 ->addValidator(new Zend_Validate_StringLength(1,255));
149 $form->addElement($location);
150
151 $date = new Zend_Form_Element_Text('date');
152 $date->setRequired(true)
153 ->setLabel(I18n::_('Date of Assurance'))
154 ->setValue(date('Y-m-d H:i:s'))
155 ->addValidator(new Zend_Validate_StringLength(1,255));
156 $form->addElement($date);
157
158 $submit = new Zend_Form_Element_Submit('submit');
159 $submit->setLabel(I18n::_('Assure Me'));
160 $form->addElement($submit);
161
162 return $form;
163 }
164 }