-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuizQuestion.php
More file actions
392 lines (366 loc) · 11.1 KB
/
QuizQuestion.php
File metadata and controls
392 lines (366 loc) · 11.1 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
<?php
/**
* QuizQuestion Model
*
* @property Language $Language
* @property QuizPage $QuizPage
* @property QuizChoice $QuizChoice
*
* @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('QuizzesAppModel', 'Quizzes.Model');
/**
* Summary for QuizQuestion Model
*/
class QuizQuestion extends QuizzesAppModel {
/**
* 配点デフォルト値
*/
const QUIZ_QUESTION_DEFAULT_ALLOTMENT = 10;
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
'Quizzes.QuizQuestionValidate',
'Wysiwyg.Wysiwyg' => array(
'fields' => array('question_value', 'commentary'),
),
//多言語
'M17n.M17n' => array(
'commonFields' => array(
'question_sequence',
'question_type',
'is_choice_random',
'is_choice_horizon',
'is_order_fixed',
'allotment',
),
'associations' => array(
'QuizChoice' => array(
'class' => 'Quizzes.QuizChoice',
'foreignKey' => 'quiz_question_id',
),
'QuizCorrect' => array(
'class' => 'Quizzes.QuizCorrect',
'foreignKey' => 'quiz_question_id',
),
),
'afterCallback' => false,
),
);
/**
* Validation rules
*
* @var array
*/
public $validate = array(
);
//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(
'QuizPage' => array(
'className' => 'Quizzes.QuizPage',
'foreignKey' => 'quiz_page_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'QuizChoice' => array(
'className' => 'Quizzes.QuizChoice',
'foreignKey' => 'quiz_question_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'QuizCorrect' => array(
'className' => 'Quizzes.QuizCorrect',
'foreignKey' => 'quiz_question_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* Constructor. Binds the model's database table to the object.
*
* @param bool|int|string|array $id Set this ID for this model on startup,
* can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
* @see Model::__construct()
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->loadModels([
'QuizChoice' => 'Quizzes.QuizChoice',
'QuizCorrect' => 'Quizzes.QuizCorrect',
]);
}
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$qIndex = $options['questionIndex'];
// Questionモデルは繰り返し判定が行われる可能性高いのでvalidateルールは最初に初期化
// mergeはしません
$this->validate = array(
'question_sequence' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
),
'comparison' => array(
'rule' => array('comparison', '==', $qIndex),
'message' => __d('quizzes', 'question sequence is illegal.')
),
),
'question_type' => array(
'inList' => array(
'rule' => array('inList', QuizzesComponent::$typesList),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'question_value' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('quizzes', 'Please input question text.'),
),
),
'is_choice_random' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'is_choice_horizon' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'is_order_fixed' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'allotment' => array(
'numeric' => array(
'rule' => array('naturalNumber'),
'required' => true,
'allowEmpty' => false,
'message' => __d('quizzes', 'Please enter a number greater than 0 .'),
),
),
);
// validates時にはまだquiz_page_idの設定ができないのでチェックしないことにする
// quiz_page_idの設定は上位のQuizPageクラスで責任を持って行われるものとする
parent::beforeValidate($options);
return true;
}
/**
* getDefaultQuestion
* get default data of quiz question
*
* @return array
*/
public function getDefaultQuestion() {
$question = array(
'question_sequence' => 0,
'question_value' => __d('quizzes', 'New Question') . '1',
'question_type' => QuizzesComponent::TYPE_SELECTION,
'is_choice_random' => QuizzesComponent::USES_NOT_USE,
'is_choice_horizon' => QuizzesComponent::USES_NOT_USE,
'is_order_fixed' => QuizzesComponent::USES_NOT_USE,
'allotment' => self::QUIZ_QUESTION_DEFAULT_ALLOTMENT,
'commentary' => '',
);
$question['QuizChoice'] = $this->QuizChoice->getDefaultChoice();
$question['QuizCorrect'] = $this->QuizCorrect->getDefaultCorrect();
return $question;
}
/**
* getQuestionForPage
* setup page data to quiz array
*
* @param array &$page quiz-page data
* @param array $questions all question data in this quiz
* @return void
*/
public function getQuestionForPage(&$page, $questions) {
$targetQuestions = Hash::extract(
$questions,
'{n}.QuizQuestion[quiz_page_id=' . $page['id'] . ']'
);
$targetQuestions = Hash::sort($targetQuestions, '{n}.question_sequence', 'asc');
foreach ($targetQuestions as &$question) {
$targetChoices = Hash::extract(
$questions,
'{n}.QuizChoice.{n}[quiz_question_id=' . $question['id'] . ']'
);
$targetCorrects = Hash::extract(
$questions,
'{n}.QuizCorrect.{n}[quiz_question_id=' . $question['id'] . ']'
);
$question['QuizChoice'] = $targetChoices;
$question['QuizCorrect'] = $targetCorrects;
// 万が一ShufflePageComponentを通らなかったときのための保険
$question['serial_number'] = $question['question_sequence'];
$page['QuizQuestion'][] = $question;
}
$page['question_count'] = count($targetQuestions);
}
/**
* Called before each find operation. Return false if you want to halt the find
* call, otherwise return the (modified) query data.
*
* @param array $query Data used to execute this query, i.e. conditions, order, etc.
* @return mixed true if the operation should continue, false if it should abort; or, modified
* $query to continue with new $query
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforefind
*/
public function beforeFind($query) {
//hasManyで実行されたとき、多言語の条件追加
if (! $this->id && ! empty($query['conditions']['quiz_page_id'])) {
$quizPageId = $query['conditions']['quiz_page_id'];
$query['conditions']['quiz_page_id'] = $this->getQuizPageIdsForM17n($quizPageId);
$query['conditions']['OR'] = array(
'QuizQuestion.language_id' => Current::read('Language.id'),
'QuizQuestion.is_translation' => false,
);
return $query;
}
return parent::beforeFind($query);
}
/**
* 多言語データ取得のため、当言語のquiz_page_idから全言語のquiz_page_idを取得する
*
* @param id $quizPageId 当言語のquiz_page_id
* @return array
*/
public function getQuizPageIdsForM17n($quizPageId) {
$quizPage = $this->QuizPage->find('first', array(
'recursive' => -1,
'callbacks' => false,
'fields' => array('id', 'key', 'quiz_id'),
'conditions' => array('id' => $quizPageId),
));
$quizPageIds = $this->QuizPage->find('list', array(
'recursive' => -1,
'callbacks' => false,
'fields' => array('id', 'id'),
'conditions' => array(
'quiz_id' => $this->QuizPage->getQuizIdsForM17n($quizPage['QuizPage']['quiz_id']),
'key' => $quizPage['QuizPage']['key']
),
));
return array_values($quizPageIds);
}
/**
* saveQuizQuestion
* save QuizQuestion data
*
* @param array &$questions quiz questions
* @throws InternalErrorException
* @return bool
*/
public function saveQuizQuestion(&$questions) {
// QuizQuestionが単独でSaveされることはない
// 必ず上位のQuizのSaveの折に呼び出される
// なので、$this->setDataSource('master');といった
// 決まり処理は上位で行われる
// ここでは行わない
foreach ($questions as &$question) {
// 小テストは履歴を取っていくタイプのコンテンツデータなのでSave前にはID項目はカット
// (そうしないと既存レコードのUPDATEになってしまうから)
$question = Hash::remove($question, 'QuizQuestion.id');
$tmpQ = array();
$tmpQ['QuizQuestion'] = $question;
$tmpQ['Block'] = Current::read('Block');
$this->create();
if (! $this->save($tmpQ, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$questionId = $this->id;
if (isset($question['QuizChoice'])) {
$question = Hash::insert($question, 'QuizChoice.{n}.quiz_question_id', $questionId);
// もしもChoiceのsaveがエラーになった場合は、
// ChoiceのほうでInternalExceptionErrorが発行されるのでここでは何も行わない
$this->QuizChoice->saveQuizChoice($question['QuizChoice']);
}
if (isset($question['QuizCorrect'])) {
$question = Hash::insert($question, 'QuizCorrect.{n}.quiz_question_id', $questionId);
$this->QuizCorrect->saveQuizCorrect($question['QuizCorrect']);
}
}
return true;
}
/**
* deleteQuizQuestion
*
* 小テスト問題情報削除、配下の選択肢、正解情報も削除
*
* @param int $quizPageId 小テストページID
* @return bool
*/
public function deleteQuizQuestion($quizPageId) {
$quizQuestions = $this->find('all', array(
'conditions' => array(
'QuizQuestion.quiz_page_id' => $quizPageId
),
'recursive' => -1
));
foreach ($quizQuestions as $question) {
if (! $this->QuizChoice->deleteAll(array(
'QuizChoice.quiz_question_id' => $question['QuizQuestion']['id']))) {
return false;
}
if (! $this->QuizCorrect->deleteAll(array(
'QuizCorrect.quiz_question_id' => $question['QuizQuestion']['id']))) {
return false;
}
if (! $this->delete($question['QuizQuestion']['id'], false)) {
return false;
}
}
return true;
}
}