-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuizResultController.php
More file actions
289 lines (265 loc) · 7.55 KB
/
QuizResultController.php
File metadata and controls
289 lines (265 loc) · 7.55 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
<?php
/**
* QuizResult Controller
*
* @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
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppController', 'Controller');
/**
* QuizResultController
*
* @author Allcreator <info@allcreator.net>
* @package NetCommons\Quizzes\Controller
*/
class QuizResultController extends QuizzesAppController {
/**
* use model
*
* @var array
*/
public $uses = array(
'Quizzes.QuizAnswerSummary',
'Quizzes.QuizAnswer',
'Quizzes.QuizResult',
'Quizzes.QuizFrameSetting'
);
/**
* use components
*
* @var array
*/
public $components = array(
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'index' => 'block_editable',
),
),
'Quizzes.QuizzesOwnAnswerQuiz', // 回答済み小テスト管理
'Quizzes.QuizzesOwnAnswer', // 回答ID管理
'Quizzes.QuizzesPassQuiz', // 合格小テスト管理
'Paginator' => array('className' => 'NetCommons.AppPaginator'),
);
/**
* use helpers
*
*/
public $helpers = [
'NetCommons.Date',
'NetCommons.DisplayNumber',
'NetCommons.TitleIcon',
'NetCommons.TableList',
'Workflow.Workflow',
'Quizzes.QuizResult',
'Quizzes.QuizGradeLink',
];
/**
* target quiz data
*
*/
private $__quiz = null;
/**
* beforeFilter
* NetCommonsお約束:できることならControllerのbeforeFilterで実行可/不可の判定して流れを変える
*
* @return void
*/
public function beforeFilter() {
// 親クラスのbeforeFilterを済ませる
parent::beforeFilter();
// NetCommonsお約束:編集画面へのURLに編集対象のコンテンツキーが含まれている
// まずは、そのキーを取り出す
$quizKey = $this->_getQuizKeyFromPass();
// キーで指定されたデータを取り出しておく
$conditions = $this->Quiz->getResultViewCondition(
array('Quiz.key' => $quizKey)
);
$this->__quiz = $this->Quiz->find('first', array(
'conditions' => $conditions,
));
if (! $this->__quiz) {
$this->setAction('throwBadRequest');
}
// 表示方法設定値取得
list($displayType) =
$this->QuizFrameSetting->getQuizFrameSetting(Current::read('Frame.key'));
$this->set('displayType', $displayType);
}
/**
* index
* 編集権限を持つ人物だけがこの画面を見ることができる
*
* @return void
*/
public function index() {
$quiz = $this->__quiz;
// 集計処理モデル初期設定処理
$this->QuizResult->initResult($quiz);
// 総合情報取得
// 得点分布データ取得
$general = $this->QuizResult->getAllResult();
$options = $this->QuizResult->getPaginateOptions();
$this->Paginator->settings = array_merge(
$this->Paginator->settings,
array(
'page' => 1,
'limit' => 10,
'order' => array('User.handlename' => 'DESC'),
),
$options
);
$filter = $this->_getFilter();
$summaryList = $this->paginate(
'QuizResult',
$filter,
array(
'User.handlename',
'QuizAnswerSummary.id',
'QuizAnswerSummary.answer_number',
'QuizAnswerSummary.summary_score',
'Statistics.avg_elapsed_second',
'Statistics.max_score',
'Statistics.min_score',
)
);
$this->set('quiz', $quiz);
$this->set('general', $general);
$this->set('summaryList', $summaryList);
$this->set('passFilterStatus', $this->_getParam('passing_status'));
$this->set('winthinTimeFilterStatus', $this->_getParam('within_time_status'));
}
/**
* view method
* Display the question of the quiz , to accept the answer input
*
* @return void
*/
public function view() {
$quiz = $this->__quiz;
// 基本的には自分の履歴を見ようとしていることが前提
// 初期表示のテスト一覧から結果を見ようとしている=つまり編集権限は無し、自分のデータを見るパターン
$userId = Current::read('User.id');
$handleName = Current::read('User.handlename');
// サマリIDが指定されているときだけが、上記の前提から異なる可能性があるパターン
// 成績一覧画面(quiz_result/index)からこの画面へ来たときの遷移
// この時は対象人物が自分ではないので適宜設定しなおす
// サマリID
$summaryId = null;
if (isset($this->params['pass'][0])) {
$summaryId = $this->params['pass'][0];
$summary = $this->QuizAnswerSummary->findById($summaryId);
if (! $summary) {
$this->setAction('throwBadRequest');
}
$userId = $summary['QuizAnswerSummary']['user_id'];
$handleName = $summary['User']['handlename'];
}
if (! $handleName) {
$handleName = __d('quizzes', 'Guest');
}
// 採点する権限を持っていないなら 自分自身のデータであることが必要
$canGrade = $this->canGrade($quiz);
if (! $canGrade && $summaryId) {
// 自分の?
if (! $this->QuizzesOwnAnswer->checkOwnAnsweredSummaryId($summaryId)) {
$this->setAction('throwBadRequest');
}
}
// 初期設定
$this->QuizResult->initResult($quiz);
// 統合情報取得
$general = $this->QuizResult->getAllResult();
// 得点推移データ取得
// そのサマリIDに該当する人物のサマリ履歴を取得する
$scoreHistory = $this->_getScoreHistory($quiz, $userId, $summaryId);
$conditions = array(
'quiz_key' => $quiz['Quiz']['key'],
);
if ($userId) {
$conditions['user_id'] = $userId;
} else {
// 非会員
// サマリID指定
if ($summaryId) {
$conditions['id'] = $summaryId;
} else {
$conditions['id'] = $this->QuizzesOwnAnswer->getAnsweredSummaryIds();
}
}
$this->paginate = array(
'conditions' => $conditions,
'page' => 1,
'order' => array('QuizAnswerSummary.answer_number' => 'DESC'),
'limit' => 10,
'recursive' => -1,
);
$summaryList = $this->paginate('QuizAnswerSummary');
$this->set('quiz', $quiz);
$this->set('userId', $userId);
$this->set('handleName', $handleName);
$this->set('general', $general);
$this->set('summaryList', $summaryList);
$this->set('scoreHistory', $scoreHistory);
}
/**
* _getScoreHistory
*
* 得点履歴取得
*
* @param array $quiz 小テストデータ
* @param int $userId ユーザーID
* @param int $summaryId サマリID
* @return array
*/
protected function _getScoreHistory($quiz, $userId, $summaryId) {
if (! $userId) {
// 非会員データの場合は履歴を持たない(1回限り
return false;
}
$conditions = array(
'quiz_key' => $quiz['Quiz']['key'],
//'is_grade_finished' => true,
'user_id' => $userId,
);
$scoreHistory = $this->QuizAnswerSummary->find('all', array(
'fields' => array('answer_number', 'summary_score', 'is_grade_finished'),
'conditions' => $conditions,
'recursive' => -1,
'order' => array('answer_number' => 'ASC')
));
if ($scoreHistory) {
$scoreHistory = Hash::extract($scoreHistory, '{n}.QuizAnswerSummary');
}
return $scoreHistory;
}
/**
* _getParam method
* パラメータ取り出し
*
* @param string $name パラメータ名
* @return string
*/
protected function _getParam($name) {
if (isset($this->request->named[$name])) {
return $this->request->named[$name];
}
return '';
}
/**
* _getFilter method
* 絞込条件取り出し
*
* @return array
*/
protected function _getFilter() {
$filter = array();
$filter['QuizAnswerSummary.passing_status'] = $this->_getParam('passing_status');
$filter['QuizAnswerSummary.within_time_status'] = $this->_getParam('within_time_status');
$filter = Hash::filter($filter);
return $filter;
}
}