-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuizzesController.php
More file actions
328 lines (303 loc) · 9.36 KB
/
QuizzesController.php
File metadata and controls
328 lines (303 loc) · 9.36 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
<?php
/**
* Quizzes Controller
*
* @property PaginatorComponent $Paginator
*
* @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('QuizzesAppController', 'Quizzes.Controller');
/**
* Quizzes 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
*/
class QuizzesController extends QuizzesAppController {
/**
* quiz view filter
*
* @var string
*/
const QUIZ_ANSWER_VIEW_ALL = 'viewall';
const QUIZ_ANSWER_UNANSWERED = 'unanswered';
const QUIZ_ANSWER_ANSWERED = 'answered';
const QUIZ_ANSWER_TEST = 'test';
/**
* use model
*
* @var array
*/
public $uses = array(
'Quizzes.QuizAnswerSummary',
'Quizzes.QuizAnswer',
'Quizzes.QuizFrameSetting',
'Quizzes.QuizFrameDisplayQuiz',
'Files.FileModel', // FileUpload
'PluginManager.Plugin',
);
/**
* use components
*
* @var array
*/
public $components = array(
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'add' => 'content_creatable',
),
),
'Quizzes.Quizzes',
'Quizzes.QuizzesOwnAnswerQuiz', // 回答済み小テスト管理
'Quizzes.QuizzesOwnAnswer', // 回答ID管理
'Quizzes.QuizzesPassQuiz', // 合格小テスト管理
'Quizzes.QuizzesAnswerStart',
'Quizzes.QuizzesShuffle',
'Paginator',
);
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'Workflow.Workflow',
'NetCommons.Date',
'NetCommons.DisplayNumber',
'NetCommons.TitleIcon',
'NetCommons.Button',
'Quizzes.QuizStatusLabel',
'Quizzes.QuizGradeLink',
'Quizzes.QuizAnswerButton',
'Quizzes.QuizResultButton',
);
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
// ここへは設定画面の一覧から来たのか、一般画面の一覧から来たのか
$this->_decideSettingLayout();
}
/**
* index method
*
* @return void
*/
public function index() {
// この閲覧者が「回答スタートした」記録をセッションから消しておく
$this->QuizzesAnswerStart->deleteStartQuizOfThisUser();
// テストページ記憶も消しておく
$this->QuizzesShuffle->clear();
// 表示方法設定値取得
list(, $displayNum, $sort, $dir) =
$this->QuizFrameSetting->getQuizFrameSetting(Current::read('Frame.key'));
// 条件設定値取得
$conditions = $this->Quiz->getCondition();
// データ取得の条件設定
$this->Paginator->settings = array_merge(
$this->Paginator->settings,
array(
'conditions' => $conditions,
'page' => 1,
'order' => array($sort => $dir),
'limit' => $displayNum,
'recursive' => 0,
)
);
// 絞込の指定がない場合は、デフォルトは全て表示です
if (! isset($this->params['named']['answer_status'])) {
$this->request->params['named']['answer_status'] = self::QUIZ_ANSWER_VIEW_ALL;
}
// データ取得
$quizzes = $this->paginate('Quiz', $this->_getPaginateFilter());
$this->set('quizzes', $quizzes);
$this->set('currentStatus', $this->request->params['named']['answer_status']);
$this->set('filterList', $this->_getFilterSelectList());
// 回答済み、合格済、未採点ありなどの小テストキー配列を確保する
$this->__setOwnAnsweredQuizKeys();
$this->__setPassQuizKeys();
$this->__setNotScoringQuizKeys();
$this->__setAnswerSummaryIdWithQuizKey();
if (count($quizzes) == 0) {
$this->view = 'Quizzes/no_quiz';
}
}
/**
* add method
*
* @return void
*/
public function add() {
// POSTされたデータを読み取り
if ($this->request->is('post')) {
// Postデータをもとにした新アンケートデータの取得をModelに依頼する
$actionModel = ClassRegistry::init('Quizzes.ActionQuizAdd', 'true');
if ($quiz = $actionModel->createQuiz($this->request->data)) {
$tm = $this->_getQuizEditSessionIndex();
// 作成中アンケートデータをセッションキャッシュに書く
$this->Session->write('Quizzes.quizEdit.' . $tm, $quiz);
// 次の画面へリダイレクト
$urlArray = array(
'controller' => 'quiz_edit',
'action' => 'edit_question',
Current::read('Block.id'),
'frame_id' => Current::read('Frame.id'),
's_id' => $tm,
);
if ($this->layout == 'NetCommons.setting') {
$urlArray['q_mode'] = 'setting';
}
$this->redirect(NetCommonsUrl::actionUrl($urlArray));
return;
} else {
// データに不備があった場合
$this->NetCommons->handleValidationError($actionModel->validationErrors);
}
} else {
// 初期表示の場合は、create_optionは初期値として「NEW」を設定する
$this->request->data['ActionQuizAdd']['create_option'] = QuizzesComponent::QUIZ_CREATE_OPT_NEW;
}
// 過去データ 取り出し
$pastQuizzes = $this->Quiz->find('all',
array(
'fields' => array(
'id', 'title', 'status', 'answer_timing', 'answer_start_period', 'answer_end_period',
),
'conditions' => $this->Quiz->getCondition(),
'offset' => 0,
'limit' => 1000,
'recursive' => -1,
'order' => array('Quiz.modified DESC'),
));
$this->set('pastQuizzes', $pastQuizzes);
if ($this->layout == 'NetCommons.setting') {
$this->set('cancelUrl', NetCommonsUrl::backToIndexUrl('default_setting_action'));
} else {
$this->set('cancelUrl', NetCommonsUrl::backToPageUrl());
}
// NetCommonsお約束:投稿のデータはrequest dataに設定する
$this->request->data['Frame'] = Current::read('Frame');
$this->request->data['Block'] = Current::read('Block');
}
/**
* _getFilterSelectList method
*
* @return array
*/
protected function _getFilterSelectList() {
$list = array(
self::QUIZ_ANSWER_VIEW_ALL => __d('quizzes', 'View All'),
self::QUIZ_ANSWER_UNANSWERED => __d('quizzes', 'Unanswered'),
self::QUIZ_ANSWER_ANSWERED => __d('quizzes', 'Answered'),
);
if (Current::permission('content_creatable')) {
$list[self::QUIZ_ANSWER_TEST] = __d('quizzes', 'Test');
}
return $list;
}
/**
* _getPaginateFilter method
*
* @return array
*/
protected function _getPaginateFilter() {
$filter = array();
if ($this->request->params['named']['answer_status'] == self::QUIZ_ANSWER_TEST) {
$filter = array(
'Quiz.status !=' => WorkflowComponent::STATUS_PUBLISHED
);
return $filter;
}
$filterCondition = array('Quiz.key' => $this->QuizzesOwnAnswerQuiz->getOwnAnsweredKeys());
if ($this->request->params['named']['answer_status'] == self::QUIZ_ANSWER_UNANSWERED) {
$filter = array(
'Quiz.status' => WorkflowComponent::STATUS_PUBLISHED,
'NOT' => $filterCondition
);
} elseif ($this->request->params['named']['answer_status'] == self::QUIZ_ANSWER_ANSWERED) {
$filter = array(
'Quiz.status' => WorkflowComponent::STATUS_PUBLISHED,
$filterCondition
);
}
return $filter;
}
/**
* Set view value of answered quiz keys
* 閲覧者がすでに回答し終えている小テストのキーの配列を確保する
*
* @return void
*/
private function __setOwnAnsweredQuizKeys() {
if ($this->request->params['named']['answer_status'] == self::QUIZ_ANSWER_UNANSWERED) {
$this->set('ownAnsweredKeys', array());
return;
}
$this->set('ownAnsweredKeys', $this->QuizzesOwnAnswerQuiz->getOwnAnsweredKeys());
$this->set('ownAnsweredCounts', $this->QuizzesOwnAnswerQuiz->getOwnAnsweredCounts());
}
/**
* Set view value of passed quiz keys
* 閲覧者がすでに合格している小テストのキーの配列を確保する
*
* @return void
*/
private function __setPassQuizKeys() {
$this->set('passQuizKeys', $this->QuizzesPassQuiz->getPassQuizKeys());
}
/**
* Set view value of not scoring quiz keys
* 閲覧者が解答し終えていて、かつ、未採点である小テストのキーの配列を確保する
*
* @return void
*/
private function __setNotScoringQuizKeys() {
// 自分が編集権限を持つ小テストの回答サマリか
// もしくは自分が解答したサマリ
$ownAnswerSummaryIds = $this->QuizzesOwnAnswer->getAnsweredSummaryIds();
$canGradingSummaryIds = $this->QuizAnswerSummary->getCanGradingSummary();
$summaryIds = array_flip($ownAnswerSummaryIds) + array_flip($canGradingSummaryIds);
$summaryIds = array_flip($summaryIds);
$notScoringQuiz = $this->QuizAnswer->getNotScoringQuizKey(
$summaryIds
);
$notScoringQuiz = Hash::combine(
$notScoringQuiz,
'{n}.QuizAnswerSummary.quiz_key',
'{n}.QuizAnswerSummary.quiz_key'
);
$this->set('notScoringQuizKeys', $notScoringQuiz);
}
/**
* 自分の回答サマリIDと小テストキーの対応付けデータ
*
* @return void
*/
private function __setAnswerSummaryIdWithQuizKey() {
$ownAnswerSummaryIds = $this->QuizzesOwnAnswer->getAnsweredSummaryIds();
$answeredSummaryMap = $this->QuizAnswerSummary->find('all', array(
'fields' => array('id', 'quiz_key'),
'conditions' => array(
'id' => $ownAnswerSummaryIds,
),
'recursive' => -1,
));
$answeredSummaryMap = Hash::combine(
$answeredSummaryMap,
'{n}.QuizAnswerSummary.quiz_key',
'{n}.QuizAnswerSummary.id'
);
$this->set('ownAnswerdSummaryMap', $answeredSummaryMap);
}
}