/
SurveysController.php
345 lines (317 loc) · 12.8 KB
/
SurveysController.php
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
<?php
declare(strict_types=1);
namespace App\Controller;
use App\SurveyMonkey\SurveyMonkey;
use Cake\Event\Event;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\Validation\Validation;
/**
* Surveys Controller
*
* @property \App\Model\Table\SurveysTable $Surveys
*/
class SurveysController extends AppController
{
/**
* initialize method
*
* @return void
*/
public function initialize()
{
parent::initialize();
$this->Auth->allow(['cronImport', 'import']);
}
/**
* Method for /surveys/import
*
* @param null|int $surveyId Survey ID
* @return \Cake\Http\Response
*/
public function import($surveyId = null)
{
$this->viewBuilder()->setLayout('blank');
$importedCount = 0;
// Disallow import if survey is inactive
$survey = $this->Surveys->get($surveyId);
if (! $survey->active) {
$this->response->withStatus(403);
$this->set('message', 'This questionnaire is currently inactive. New responses cannot be imported.');
return $this->render('import');
}
// Collect responses
$responsesTable = TableRegistry::getTableLocator()->get('Responses');
$SurveyMonkey = new SurveyMonkey();
[$success, $responses] = $SurveyMonkey->getNewResponses($surveyId);
if (! $success) {
return $this->renderImportError($responses);
}
// Loop through each response and add to / update records
$areasTable = TableRegistry::getTableLocator()->get('Areas');
$respondentsTable = TableRegistry::getTableLocator()->get('Respondents');
$errorMsgs = [];
if (is_array($responses)) {
foreach ($responses as $smRespondentId => $response) {
$respondent = $SurveyMonkey->extractRespondentInfo($response);
$name = $respondent['name'] ?: '(no name)';
$respondentRecord = $respondentsTable->getMatching($surveyId, $respondent, $smRespondentId);
$serializedResponse = base64_encode(serialize($response));
// Ignore response if it doesn't include all PWRRR ranks
$responseRanks = $responsesTable->getResponseRanks($serializedResponse, $survey);
if (! $responseRanks) {
$errorMsgs[] = "Response from $name did not contain all PWR<sup>3</sup> rankings.";
continue;
}
// Ignore responses if email address is missing or invalid
if (empty($respondent['email'])) {
$errorMsgs[] = "Response from $name is missing an email address.";
continue;
}
if (! Validation::email($respondent['email'])) {
$errorMsgs[] = "Response from $name has an invalid email address: {$respondent['email']}.";
continue;
}
// Add new respondent
if (empty($respondentRecord)) {
$approved = $respondentsTable->isAutoApproved($survey, $respondent['email']);
$newRespondent = $respondentsTable->newEntity([
'email' => $respondent['email'],
'name' => $name,
'survey_id' => $surveyId,
'sm_respondent_id' => $smRespondentId,
'invited' => false,
'approved' => $approved ? 1 : 0,
]);
$errors = $newRespondent->getErrors();
if (empty($errors)) {
$respondentsTable->save($newRespondent);
$respondentId = $newRespondent->id;
} else {
/* Don't record anything for this response
* (this condition should theoretically never happen,
* since any validation errors should have been caught above) */
continue;
}
// Update existing respondent
} else {
$newData = [];
if (empty($respondentRecord->smRespondentId)) {
$newData['sm_respondent_id'] = $smRespondentId;
}
if (empty($respondentRecord->name)) {
$newData['name'] = $respondent['name'];
}
if (! empty($newData)) {
$respondentRecord = $respondentsTable->patchEntity($respondentRecord, $newData);
$errors = $respondentRecord->getErrors();
if (empty($errors)) {
$respondentsTable->save($respondentRecord);
} else {
/* Don't record anything for this response
* (this condition should theoretically never happen,
* since any validation errors should have been caught above) */
continue;
}
}
$respondentId = $respondentRecord->id;
}
// Skip recording response if it's already recorded
if ($responsesTable->isRecorded($respondentId, $survey, $serializedResponse)) {
continue;
}
// Calculate alignment
$communitiesTable = TableRegistry::getTableLocator()->get('Communities');
$community = $communitiesTable->get($survey->community_id);
$actualRanksLocal = $areasTable->getPwrrrRanks($community->local_area_id);
$actualRanksParent = $areasTable->getPwrrrRanks($community->parent_area_id);
$alignmentVsLocal = $responsesTable->calculateAlignment($actualRanksLocal, $responseRanks);
$alignmentVsParent = $responsesTable->calculateAlignment($actualRanksParent, $responseRanks);
// Save response
$responseFields = [
'respondent_id' => $respondentId,
'survey_id' => $surveyId,
'response' => $serializedResponse,
'alignment_vs_local' => $alignmentVsLocal,
'alignment_vs_parent' => $alignmentVsParent,
'response_date' => new Time($response['date_modified']),
'aware_of_plan' => $responsesTable->getAwareOfPlan($serializedResponse, $survey),
];
foreach ($responseRanks as $sector => $rank) {
$responseFields["{$sector}_rank"] = $rank;
}
$newResponse = $responsesTable->newEntity($responseFields);
$errors = $newResponse->getErrors();
if (empty($errors)) {
$responsesTable->save($newResponse);
$importedCount++;
} else {
$errorMsgs[] = "Response from $name is missing required data.";
continue;
}
}
/* Set new last_modified_date if there are no errors
* (if this set of responses contains errors, advancing the last_modified_date
* would prevent those responses from being imported after those errors are corrected) */
if (empty($errorMsgs)) {
$mostRecentDate = $SurveyMonkey->getMostRecentModifiedDate($responses);
$survey->respondents_last_modified_date = new Time($mostRecentDate);
}
$survey->import_errors = $errorMsgs ? serialize($errorMsgs) : null;
$this->Surveys->save($survey);
}
// Finalize
$this->Surveys->setChecked($surveyId);
if (empty($errorMsgs)) {
if ($importedCount) {
$message = $importedCount . __n(' response', ' responses', $importedCount) . ' imported';
} else {
$message = 'No new responses to import';
}
} else {
if ($importedCount) {
$message = $importedCount . __n(' response', ' responses', $importedCount) . ' imported<br />';
} else {
$message = '';
}
$message .=
'Errors prevented the following ' .
__n('response', 'responses', count($errorMsgs)) .
' from being imported:' .
'<ul>';
foreach ($errorMsgs as $errorMsg) {
$message .= '<li>' . $errorMsg . '</li>';
}
$message .= '</ul>';
$this->response->withStatus(500);
}
if ($importedCount) {
$event = new Event('Model.Response.afterImport', $this, ['meta' => [
'communityId' => $survey->community_id,
'surveyId' => $survey->id,
'surveyType' => $survey->type,
'responseCount' => $importedCount,
]]);
$this->getEventManager()->dispatch($event);
}
$this->set(compact('message'));
}
/**
* Sets a 500 status code and passes $message to the view
*
* @param string $message Error message
* @return \Cake\Http\Response
*/
private function renderImportError($message)
{
$this->response->withStatus(500);
$this->set(compact('message'));
return $this->render('import');
}
/**
* Method for /surveys/get-survey-list
*
* @return void
*/
public function getSurveyList()
{
$SurveyMonkey = new SurveyMonkey();
$result = $SurveyMonkey->getSurveyList($this->request->getQueryParams());
$this->set([
'result' => json_encode($result),
]);
$this->viewBuilder()->setLayout('json');
$this->render('api');
}
/**
* Method for /surveys/get-survey-url
*
* @param string|null $smId SurveyMonkey survey ID
* @return void
*/
public function getSurveyUrl($smId = null)
{
$SurveyMonkey = new SurveyMonkey();
$this->set([
'result' => $SurveyMonkey->getSurveyUrl($smId),
]);
$this->viewBuilder()->setLayout('json');
$this->render('api');
}
/**
* Used by a JS call to find out what community, if any, a survey has already been assigned to
*
* @param string|null $smSurveyId SurveyMonkey survey ID
* @return void
*/
public function checkSurveyAssignment($smSurveyId = null)
{
$survey = $this->Surveys->find('all')
->select(['community_id', 'type'])
->where(['sm_id' => $smSurveyId])
->limit(1);
if ($survey->isEmpty()) {
$community = null;
} else {
$survey = $survey->first();
$communitiesTable = TableRegistry::getTableLocator()->get('Communities');
$community = [
'id' => $survey->community_id,
'name' => $communitiesTable->get($survey->community_id)->name,
'type' => $survey->type,
];
}
$this->viewBuilder()->setLayout('json');
$this->set('community', $community);
}
/**
* Method for /surveys/get-qna-ids
*
* @param string|null $smId SurveyMonkey survey ID
* @return void
*/
public function getQnaIds($smId)
{
$SurveyMonkey = new SurveyMonkey();
$result = $SurveyMonkey->getQuestionAndAnswerIds($smId);
$this->set('result', json_encode($result));
$this->viewBuilder()->setLayout('json');
$this->render('api');
}
/**
* Method for /surveys/cron-import, the target of a cron job
*
* @return void
*/
public function cronImport()
{
$surveyId = $this->Surveys->getNextAutoImportCandidate();
if ($surveyId) {
$this->import($surveyId);
$this->Surveys->setChecked($surveyId);
} else {
$this->set('message', 'No questionnaires are currently eligible for automatic imports');
$this->viewBuilder()->setLayout('blank');
}
$this->render('import');
}
/**
* Method for clearing this user's save invitation data for the specified survey
*
* @param int $surveyId Survey ID
* @return \Cake\Http\Response
*/
public function clearSavedInvitationData($surveyId)
{
$this->loadComponent('SurveyProcessing');
$userId = $this->Auth->user('id');
$success = $this->SurveyProcessing->clearSavedInvitations($surveyId, $userId);
if (! $success) {
$this->response->withStatus(500);
}
$this->set('result', $success);
$this->set('_serialize', ['success']);
$this->viewBuilder()->setLayout('json');
return $this->render('api');
}
}