forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsRepository.php
More file actions
417 lines (379 loc) · 14 KB
/
Copy pathStatsRepository.php
File metadata and controls
417 lines (379 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* Ushahidi Form Contact Repository
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Repository\Form;
use Ohanzee\DB;
use Ohanzee\Database;
use Ushahidi\App\Repository\OhanzeeRepository;
use Ushahidi\Core\Entity;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\FormContactRepository;
use Ushahidi\Core\Usecase\SearchRepository;
use Ushahidi\Core\Traits\Event;
class StatsRepository extends OhanzeeRepository implements
Entity\FormStatsRepository,
SearchRepository
{
use Event;
protected $form_repo;
protected $targeted_survey_state_repo;
/**
* Construct
* @param Database $db
* @param FormRepository $form_repo
*/
public function __construct(
\Ushahidi\App\Multisite\OhanzeeResolver $resolver,
Entity\FormRepository $form_repo
) {
parent::__construct($resolver);
$this->form_repo = $form_repo;
}
// OhanzeeRepository
protected function getTable()
{
return 'contacts';
}
// CreateRepository
// ReadRepository
public function getEntity(array $data = null)
{
return new Entity\FormStats($data);
}
// SearchRepository
public function getSearchFields()
{
return ['form_id', 'contacts', 'created_after', 'created_before'];
}
/**
* @param $query
* @param $column
* @param null $before
* @param null $after
* @return mixed
*/
private function betweenDates($query, $column, $before_dt = null, $after_dt = null)
{
if ($before_dt && $after_dt) {
$query->where($column, 'BETWEEN', [strtotime($after_dt), strtotime($before_dt)]);
} elseif ($before_dt) {
$query->where($column, '<=', strtotime($before_dt));
} elseif ($after_dt) {
$query->where($column, '>=', strtotime($after_dt));
}
return $query;
}
// OhanzeeRepository
protected function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
if ($search->form_id) {
$query->where('form_id', '=', $search->form_id);
}
}
public function getResponses($form_id, $created_after, $created_before)
{
$where = [
'posts.form_id' => $form_id,
'messages.direction' => 'incoming',
'targeted_survey_state.survey_status' => [
Entity\TargetedSurveyState::RECEIVED_RESPONSE,
Entity\TargetedSurveyState::PENDING_RESPONSE,
Entity\TargetedSurveyState::SURVEY_FINISHED,
]
];
$query = $this->selectQuery($where);
$query = $this->betweenDates($query, 'posts.created', $created_before, $created_after);
$query
->resetSelect()
->select([DB::expr('COUNT(messages.id)'), 'total']);
$query
->join('targeted_survey_state', 'INNER')
->on('contacts.id', '=', 'targeted_survey_state.contact_id')
->join('posts', 'INNER')
->on('posts.id', '=', 'targeted_survey_state.post_id')
->join('messages')
->on('messages.post_id', '=', 'targeted_survey_state.post_id');
return $query
->execute($this->db())
->get('total');
}
/**
* @param $form_id
* @param $total_sent
* @return float|int
* Returns the number of TOTAL pending questions (for invalidated+valid contacts off a
* targeted_survey)
*/
public function countTotalPending($form_id, $total_sent)
{
$form_id = intval($form_id);
$total_contacts = $this->getTotalContacts($form_id);
$total_attributes =$this->getTotalAttributes($form_id);
$total_pending_for_inactive = DB::query(Database::SELECT, $this->getPendingCountQuery())
->bind(':form_id', $form_id)
->execute($this->db())->get('total');
return ($total_contacts * $total_attributes) - $total_sent - $total_pending_for_inactive;
}
/**
* @return string | sql query to get pending questions for all invalidated contacts
* on a targeted_survey_state group
* Does not receive form_id because it is bound in a later step
*/
private function getPendingCountQuery()
{
/**
* Selects attribute priority & id by contact,for contacts marked in targeted_survey_state as Inactive
* (noted by the ACTIVE CONTACT IN SURVEY # format of survey_status)
*/
$attributeListQuery =
"SELECT
form_attributes.priority,
targeted_survey_state.form_attribute_id,
targeted_survey_state.contact_id
FROM form_attributes
INNER JOIN targeted_survey_state
ON form_attributes.id =targeted_survey_state.form_attribute_id
WHERE targeted_survey_state.form_id=:form_id
and targeted_survey_state.survey_status LIKE 'ACTIVE CONTACT IN SURVEY%'";
/**
* counts attributes that have a priority higher than the one of the attributess referenced
* in targeted_survey_state for each invalidated contact
*/
$attributeCountQuery = "
SELECT count(form_attributes.form_stage_id) as counted from targeted_survey_state
INNER JOIN form_stages ON targeted_survey_state.form_id=form_stages.form_id
INNER JOIN form_attributes ON form_stages.id = form_attributes.form_stage_id
INNER JOIN ($attributeListQuery) as internal_query
ON internal_query.contact_id = targeted_survey_state.contact_id
WHERE form_attributes.priority > internal_query.priority
AND survey_status LIKE 'ACTIVE CONTACT IN SURVEY%' AND form_stages.form_id=:form_id
GROUP BY targeted_survey_state.contact_id, form_attributes.form_stage_id";
/**
* sums the result of the previous joined queries to gget how many attributes where not yet sent
* for invalidated contacts
*/
$sql = "SELECT SUM(results.counted) as total FROM ($attributeCountQuery) as results";
return $sql;
}
/**
* @param $form_id
* @return mixed
*/
private function getTotalAttributes($form_id)
{
return DB::query(
Database::SELECT,
"SELECT count(form_attributes.id) as total from form_attributes
INNER JOIN
form_stages ON form_attributes.form_stage_id = form_stages.id
WHERE form_stages.form_id=:form"
)
->bind(':form', $form_id)
->execute($this->db())->get('total');
}
/**
* @param $form_id
* @return mixed
*/
private function getTotalContacts($form_id)
{
return DB::query(
Database::SELECT,
"select count(contact_id) as total from targeted_survey_state where form_id=:form
and survey_status NOT IN ('SURVEY FINISHED')"
)
->bind(':form', $form_id)
->execute($this->db())->get('total');
}
/**
* @param $form_id
* @return array
*/
public function countOutgoingMessages($form_id, $created_after, $created_before)
{
$query = $this->selectQuery()
->reset()
->from('messages')
->select(DB::expr('count(messages.status) as total, messages.status'))
->where(
'post_id',
'IN',
DB::expr('(select post_id FROM targeted_survey_state WHERE form_id ='.$form_id.')')
)
->where('direction', '=', 'outgoing')
->group_by('status');
$query = $this->betweenDates($query, 'created', $created_before, $created_after);
$result = $query
->execute($this->db());
$ret = ['pending' => 0, 'sent' => 0];
foreach ($result->as_array() as $item) {
if ($item['status'] === 'pending') {
$ret['pending'] = $item['total'];
} elseif ($item['status'] === 'sent') {
$ret['sent'] = $item['total'];
}
}
return $ret;
}
/**
* @param $form_id
* @return mixed
*/
public function countPendingMessages($form_id)
{
$where = [
'posts.form_id' => $form_id,
'messages.direction' => 'outgoing',
'messages.status' => 'pending',
'targeted_survey_state.survey_status' => [
Entity\TargetedSurveyState::RECEIVED_RESPONSE,
Entity\TargetedSurveyState::PENDING_RESPONSE,
Entity\TargetedSurveyState::SURVEY_FINISHED,
]
];
$query = $this->selectQuery($where)
->resetSelect()
->select([DB::expr('COUNT(distinct message_id)'), 'total']);
$query = $this->targetedSurveyStateJoin($query)
->join('messages', 'INNER')->on('messages.id', '=', 'targeted_survey_state.message_id');
return $query
->execute($this->db())
->get('total');
}
/**
* @param int $contact_id
* @param int $form_id
* @return bool
*/
public function getRecipients($form_id, $created_after, $created_before)
{
$where = [
'posts.form_id' => $form_id,
'targeted_survey_state.survey_status' => [
Entity\TargetedSurveyState::RECEIVED_RESPONSE,
Entity\TargetedSurveyState::PENDING_RESPONSE,
Entity\TargetedSurveyState::SURVEY_FINISHED,
]
];
$query = $this->selectQuery($where)
->resetSelect()
->select([DB::expr('COUNT(contacts.id)'), 'total']);
$query = $this->targetedSurveyStateJoin($query);
if ($created_after || $created_before) {
$query
->join('messages', 'INNER')
->on('messages.contact_id', '=', 'contacts.id')
->where('messages.direction', '=', 'outgoing');
$query = $this->betweenDates($query, 'messages.created', $created_before, $created_after);
}
return $query
->execute($this->db())
->get('total');
}
/**
* @param $query
* @param int $form_id
* @param string $created_after
* @param string $created_before
* @param $result
* @return array
*/
public function getResponseRecipients($form_id, $created_after, $created_before)
{
$query = DB::select()
->from('posts')
->where('form_id', '=', $form_id);
$query = $this->betweenDates($query, 'created', $created_before, $created_after);
$query = DB::select([DB::expr('COUNT(contact_id)'), 'total'])
->distinct(true)
->from([$query,'targeted_posts'])
->join('messages', 'INNER')
->on('messages.post_id', '=', 'targeted_posts.id')
->where('messages.direction', '=', 'incoming');
return $query
->execute($this->db())
->get('total');
}
/**
* @param $query
* @return mixed
* A reusable join because we use it everywhere.
*/
private function targetedSurveyStateJoin($query)
{
return $query->join('targeted_survey_state', 'INNER')
->on('contacts.id', '=', 'targeted_survey_state.contact_id')
->join('posts', 'INNER')
->on('posts.id', '=', 'targeted_survey_state.post_id');
}
public function getSurveyType($form_id)
{
$query = DB::select('targeted_survey')
->from('forms')
->where('id', '=', $form_id);
$results = $query->execute($this->db());
return $results->as_array();
}
public function getPostCountByDataSource($form_id, $created_after, $created_before)
{
if ($created_after) {
$created_after = strtotime($created_after);
}
if ($created_before) {
$created_before = strtotime($created_before);
}
$dataSourceCounts = $this->queryByDataSource($form_id, $created_after, $created_before);
$result = [
'sms' => $dataSourceCounts['sms'],
'email' => $dataSourceCounts['email'],
'twitter' => $dataSourceCounts['twitter'],
'web' => $this->queryForWeb($form_id, $created_after, $created_before),
];
$result['all'] = $result['web'] + $result['email'] + $result['twitter'] + $result['sms'];
return $result;
}
private function queryByDataSource($form_id, $created_after, $created_before)
{
$query = DB::select('messages.type', [DB::expr('COUNT(messages.id)'), 'total'])
->from('posts')
->join('messages', 'INNER')
->on('messages.post_id', '=', 'posts.id')
->where('posts.form_id', '=', $form_id)
->group_by('messages.type');
$query = $this->betweenDates($query, 'messages.created', $created_before, $created_after);
$result = $query
->execute($this->db());
$ret = ['sms' => 0, 'email' => 0, 'twitter' => 0];
foreach ($result->as_array() as $item) {
if ($item['type'] === 'sms') {
$ret['sms'] = $item['total'];
} elseif ($item['type'] === 'email') {
$ret['email'] = $item['total'];
} elseif ($item['type'] === 'twitter') {
$ret['twitter'] = $item['total'];
}
}
return $ret;
}
private function queryForWeb($form_id, $created_after, $created_before)
{
$query = DB::select([DB::expr('COUNT(posts.id)'), 'total'])
->from('posts')
->join('messages', 'LEFT')
->on('messages.post_id', '=', 'posts.id')
->where('messages.post_id', 'is', null)
->where('posts.form_id', '=', $form_id);
$query = $this->betweenDates($query, 'posts.created', $created_before, $created_after);
$query->and_where('posts.type', '=', 'report');
return $query
->execute($this->db())
->get('total');
}
}