-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegistrationAnswerSummary.php
More file actions
276 lines (257 loc) · 8.51 KB
/
RegistrationAnswerSummary.php
File metadata and controls
276 lines (257 loc) · 8.51 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
<?php
/**
* RegistrationAnswerSummary Model
*
* @property Registration $Registration
* @property User $User
* @property RegistrationAnswer $RegistrationAnswer
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author AllCreator <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('RegistrationsAppModel', 'Registrations.Model');
App::uses('RegistrationsComponent', 'Registrations.Controller/Component');
/**
* Summary for RegistrationAnswerSummary Model
*/
class RegistrationAnswerSummary extends RegistrationsAppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'registration_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Registration' => array(
'className' => 'Registrations.Registration',
'foreignKey' => 'registration_key',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'Users.User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'RegistrationAnswer' => array(
'className' => 'Registrations.RegistrationAnswer',
'foreignKey' => 'registration_answer_summary_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* getNowSummaryOfThisUser 指定された登録フォームIDと指定ユーザーに合致する登録フォーム登録を取得する
*
* @param int $registrationKey 登録フォームKey
* @param int $userId ユーザID (指定しない場合は null)
* @param string $sessionId セッションID
* @return array
*/
public function getNowSummaryOfThisUser($registrationKey, $userId, $sessionId) {
if ($userId) {
$conditions = array(
'answer_status' => RegistrationsComponent::ACTION_ACT,
'registration_key' => $registrationKey,
'user_id' => $userId
);
} else {
$conditions = array(
'answer_status' => RegistrationsComponent::ACTION_ACT,
'registration_key' => $registrationKey,
'session_value' => $sessionId
);
}
$summary = $this->find('all', array(
'conditions' => $conditions
));
return $summary;
}
/**
* forceGetProgressiveAnswerSummary
* get answer summary record if there is no summary , then create
*
* @param array $registration registration
* @param int $userId user id
* @param string $sessionId session id
* @throws $ex
* @return array summary
*/
public function forceGetProgressiveAnswerSummary($registration, $userId, $sessionId) {
$this->begin();
try {
$this->create();
if (! $this->save(array(
'answer_status' => RegistrationsComponent::ACTION_NOT_ACT,
'test_status' => ($registration['Registration']['status'] != WorkflowComponent::STATUS_PUBLISHED) ? RegistrationsComponent::TEST_ANSWER_STATUS_TEST : RegistrationsComponent::TEST_ANSWER_STATUS_PEFORM,
'answer_number' => 1,
'registration_key' => $registration['Registration']['key'],
'session_value' => $sessionId,
'user_id' => $userId,
))) {
$this->rollback();
return false;
}
//$summary = array();
//$summary['RegistrationAnswerSummary']['id'] = $this->id;
//return $summary;
$this->commit();
} catch (Exception $ex) {
$this->rollback();
CakeLog::error($ex);
throw $ex;
}
$summary = $this->findById($this->id);
return $summary;
}
/**
* getResultCondition
*
* @param int $registration Registration
* @return array
*/
public function getResultCondition($registration) {
// 指定された登録フォームを集計するときのサマリ側の条件を返す
$baseConditions = array(
'RegistrationAnswerSummary.answer_status' => RegistrationsComponent::ACTION_ACT,
'RegistrationAnswerSummary.registration_key' => $registration['Registration']['key']
);
//公開時は本番時登録のみ、テスト時(=非公開時)は本番登録+テスト登録を対象とする。
if ($registration['Registration']['status'] == WorkflowComponent::STATUS_PUBLISHED) {
$baseConditions['RegistrationAnswerSummary.test_status'] = RegistrationsComponent::TEST_ANSWER_STATUS_PEFORM;
}
return $baseConditions;
}
/**
* getAggrigates
* 集計処理の実施
*
* @param array $registration 登録フォーム情報
* @return void
*/
public function getAggregate($registration) {
$this->RegistrationAnswer = ClassRegistry::init('Registrations.RegistrationAnswer', true);
// 質問データのとりまとめ
//$questionsは、registration_question_keyをキーとし、registration_question配下が代入されている。
$questions = Hash::combine($registration,
'RegistrationPage.{n}.RegistrationQuestion.{n}.key',
'RegistrationPage.{n}.RegistrationQuestion.{n}');
// 集計データを集める際の基本条件
$baseConditions = $this->getResultCondition($registration);
//質問毎に集計
foreach ($questions as &$question) {
if ($question['is_result_display'] != RegistrationsComponent::EXPRESSION_SHOW) {
//集計表示をしない、なので飛ばす
continue;
}
// 戻り値の、この質問の合計登録数を記録しておく。
// skip ロジックがあるため、単純にsummaryのcountじゃない..
$questionConditions = $baseConditions + array(
'RegistrationAnswer.registration_question_key' => $question['key'],
);
$question['answer_total_cnt'] = $this->RegistrationAnswer->getAnswerCount($questionConditions);
if (RegistrationsComponent::isMatrixInputType($question['question_type'])) {
$this->__aggregateAnswerForMatrix($question, $questionConditions);
} else {
$this->__aggregateAnswerForNotMatrix($question, $questionConditions);
}
}
return $questions;
}
/**
* __aggregateAnswerForMatrix
* matrix aggregate
*
* @param array &$question 登録フォーム質問(集計結果を配列追加して返します)
* @param array $questionConditions get aggregate base condition
* @return void
*/
private function __aggregateAnswerForMatrix(&$question, $questionConditions) {
$rowCnt = 0;
$cols = Hash::extract($question['RegistrationChoice'], '{n}[matrix_type=' . RegistrationsComponent::MATRIX_TYPE_COLUMN . ']');
foreach ($question['RegistrationChoice'] as &$c) {
if ($c['matrix_type'] == RegistrationsComponent::MATRIX_TYPE_ROW_OR_NO_MATRIX) {
foreach ($cols as $col) {
$conditions = $questionConditions + array(
'RegistrationAnswer.matrix_choice_key' => $c['key'],
'RegistrationAnswer.answer_value LIKE ' => '%' . RegistrationsComponent::ANSWER_DELIMITER . $col['key'] . RegistrationsComponent::ANSWER_VALUE_DELIMITER . '%',
);
$cnt = $this->RegistrationAnswer->getAnswerCount($conditions);
$c['aggregate_total'][$col['key']] = $cnt;
}
$rowCnt++;
}
}
$question['answer_total_cnt'] /= $rowCnt;
}
/**
* __aggregateAnswerForNotMatrix
* not matrix aggregate
*
* @param array &$question 登録フォーム質問(集計結果を配列追加して返します)
* @param array $questionConditions get aggregate base condition
* @return void
*/
private function __aggregateAnswerForNotMatrix(&$question, $questionConditions) {
foreach ($question['RegistrationChoice'] as &$c) {
$conditions = $questionConditions + array(
'RegistrationAnswer.answer_value LIKE ' => '%' . RegistrationsComponent::ANSWER_DELIMITER . $c['key'] . RegistrationsComponent::ANSWER_VALUE_DELIMITER . '%',
);
$cnt = $this->RegistrationAnswer->getAnswerCount($conditions);
$c['aggregate_total']['aggregate_not_matrix'] = $cnt;
}
}
/**
* deleteTestAnswerSummary
* when registration is published, delete test answer summary
*
* @param int $key registration key
* @param int $status publish status
* @return bool
*/
public function deleteTestAnswerSummary($key, $status) {
if ($status != WorkflowComponent::STATUS_PUBLISHED) {
return true;
}
$this->deleteAll(array(
'registration_key' => $key,
'test_status' => RegistrationsComponent::TEST_ANSWER_STATUS_TEST), true);
return true;
}
}