-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuizPage.php
More file actions
329 lines (302 loc) · 9.06 KB
/
QuizPage.php
File metadata and controls
329 lines (302 loc) · 9.06 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
<?php
/**
* QuizPage Model
*
* @property Language $Language
* @property Quiz $Quiz
* @property QuizQuestion $QuizQuestion
*
* @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 QuizPage Model
*/
class QuizPage extends QuizzesAppModel {
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
'Wysiwyg.Wysiwyg' => array(
'fields' => array('page_description'),
),
'M17n.M17n' => array(
'associations' => array(
'QuizQuestion' => array(
'class' => 'Quizzes.QuizQuestion',
'foreignKey' => 'quiz_page_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(
'Quiz' => array(
'className' => 'Quizzes.Quiz',
'foreignKey' => 'quiz_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'QuizQuestion' => array(
'className' => 'Quizzes.QuizQuestion',
'foreignKey' => 'quiz_page_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([
'QuizQuestion' => 'Quizzes.QuizQuestion',
]);
}
/**
* getDefaultPage
* get default data of quiz page
*
* @return array
*/
public function getDefaultPage() {
$page = array(
'page_title' => __d('quizzes', 'First Page'),
'page_sequence' => 0,
'key' => '',
'is_page_description' => QuizzesComponent::USES_NOT_USE,
'page_description' => '',
);
$page['QuizQuestion'][0] = $this->QuizQuestion->getDefaultQuestion();
return $page;
}
/**
* getPageForQuiz
* setup page data to quiz array
*
* @param array &$quiz quiz data
* @param array $pages all quiz-page data in this quiz
* @param array $questions all quiz-question data in this quiz
* @return void
*/
public function getPageForQuiz(&$quiz, $pages, $questions) {
$targetPages = Hash::extract(
$pages,
'{n}.QuizPage[quiz_id=' . $quiz['Quiz']['id'] . ']'
);
$targetPages = Hash::sort($targetPages, '{n}.page_sequence', 'asc');
foreach ($targetPages as &$page) {
if (isset($page['id'])) {
$this->QuizQuestion->getQuestionForPage($page, $questions);
}
}
$quiz['QuizPage'] = $targetPages;
$quiz['Quiz']['page_count'] = count($targetPages);
$quiz['Quiz']['question_count'] = array_sum(
Hash::extract($targetPages, '{n}.question_count')
);
}
/**
* 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 && isset($query['conditions']['quiz_id'])) {
$query['conditions']['quiz_id'] = $this->getQuizIdsForM17n($query['conditions']['quiz_id']);
$query['conditions']['OR'] = array(
'language_id' => Current::read('Language.id'),
'is_translation' => false,
);
return $query;
}
return parent::beforeFind($query);
}
/**
* 多言語データ取得のため、当言語のquiz_idから全言語のquiz_idを取得する
*
* @param id $quizId 当言語のquiz_id
* @return array
*/
public function getQuizIdsForM17n($quizId) {
$quiz = $this->Quiz->find('first', array(
'recursive' => -1,
'callbacks' => false,
'fields' => array('id', 'key', 'is_active', 'is_latest'),
'conditions' => array('id' => $quizId),
));
$conditions = array(
'key' => Hash::get($quiz, 'Quiz.key', '')
);
if (Hash::get($quiz, 'Quiz.is_latest')) {
$conditions['is_latest'] = true;
} else {
$conditions['is_active'] = true;
}
$quizIds = $this->Quiz->find('list', array(
'recursive' => -1,
'callbacks' => false,
'fields' => array('id', 'id'),
'conditions' => $conditions,
));
return array_values($quizIds);
}
/**
* 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
* @see Model::save()
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function beforeValidate($options = array()) {
$pageIndex = $options['pageIndex'];
// Pageモデルは繰り返し判定が行われる可能性高いのでvalidateルールは最初に初期化
// mergeはしません
$this->validate = array(
'page_sequence' => array(
'numeric' => array(
'rule' => array('naturalNumber', true),
//'allowEmpty' => false,
//'required' => true,
'message' => __d('quizzes', 'page sequence is illegal.')
),
'comparison' => array(
'rule' => array('comparison', '==', $pageIndex),
'message' => __d('quizzes', 'page sequence is illegal.')
),
),
);
// validates時にはまだquiz_idの設定ができないのでチェックしないことにする
// quiz_idの設定は上位のQuestionnaireクラスで責任を持って行われるものとする
parent::beforeValidate($options);
// 付属の質問以下のvalidate
if (! isset($this->data['QuizQuestion'][0])) {
$this->validationErrors['page_pickup_error'][] =
__d('quizzes', 'please set at least one question.');
} else {
$validationErrors = array();
foreach ($this->data['QuizQuestion'] as $qIndex => $question) {
// 質問データバリデータ
$this->QuizQuestion->create();
$this->QuizQuestion->set($question);
$options['questionIndex'] = $qIndex;
if (! $this->QuizQuestion->validates($options)) {
$validationErrors['QuizQuestion'][$qIndex] =
$this->QuizQuestion->validationErrors;
}
}
$this->validationErrors += $validationErrors;
}
return true;
}
/**
* saveQuizPage
* save QuizPage data
*
* @param array &$quizPages quiz pages
* @throws InternalErrorException
* @return bool
*/
public function saveQuizPage(&$quizPages) {
$this->loadModels([
'QuizQuestion' => 'Quizzes.QuizQuestion',
]);
// QuizPageが単独でSaveされることはない
// 必ず上位のQuizのSaveの折に呼び出される
// なので、$this->setDataSource('master');といった
// 決まり処理は上位で行われる
// ここでは行わない
foreach ($quizPages as &$page) {
// アンケートは履歴を取っていくタイプのコンテンツデータなのでSave前にはID項目はカット
// (そうしないと既存レコードのUPDATEになってしまうから)
$page = Hash::remove($page, 'QuizPage.id');
$tmpPage = array();
$tmpPage['QuizPage'] = $page;
$tmpPage['Block'] = Current::read('Block');
$this->create();
if (! $this->save($tmpPage, false)) { // validateは上位のquizで済んでいるはず
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$pageId = $this->id;
$page = Hash::insert($page, 'QuizQuestion.{n}.quiz_page_id', $pageId);
// もしもQuestionやChoiceのsaveがエラーになった場合は、
// QuestionやChoiceのほうでInternalExceptionErrorが発行されるのでここでは何も行わない
$this->QuizQuestion->saveQuizQuestion($page['QuizQuestion']);
}
return true;
}
/**
* deleteQuizPage
*
* 小テストページ情報削除、配下の質問情報に削除命令を実施
*
* @param int $quizId 小テストID
* @return bool
*/
public function deleteQuizPage($quizId) {
$quizPages = $this->find('all', array(
'conditions' => array(
'QuizPage.quiz_id' => $quizId
),
'recursive' => -1
));
foreach ($quizPages as $page) {
if (! $this->QuizQuestion->deleteQuizQuestion($page['QuizPage']['id'])) {
return false;
}
if (! $this->delete($page['QuizPage']['id'], false)) {
return false;
}
}
return true;
}
}