-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuestionEditHelper.php
More file actions
222 lines (208 loc) · 6.98 KB
/
QuestionEditHelper.php
File metadata and controls
222 lines (208 loc) · 6.98 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
<?php
/**
* Question Edit Helper
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Allcreator Co., Ltd. <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('AppHelper', 'View/Helper');
/**
* Question edit Helper
*
* @author Allcreator Co., Ltd. <info@allcreator.net>
* @package NetCommons\Quizzes\View\Helper
*/
class QuestionEditHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'NetCommons.Token',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
'Form'
);
/**
* 質問属性設定作成
*
* @param string $fieldName フィールド名
* @param string $title 見出しラベル
* @param array $options INPUT要素に与えるオプション属性
* @param string $label checkboxの時のラベル
* @return string HTML
*/
public function questionInput($fieldName, $title, $options, $label = '') {
if (isset($options['ng-model'])) {
$errorMsgModelName = $this->quizGetNgErrorModelName($options['ng-model']);
$ret = '<div class="row form-group" ng-class="{\'has-error\':' . $errorMsgModelName . '}">';
} else {
$ret = '<div class="row form-group">';
}
$ret .= '<label class="col-xs-2 control-label">' . $title;
if (isset($options['required']) && $options['required'] == true) {
$ret .= $this->_View->element('NetCommons.required');
}
$ret .= '</label><div class="col-xs-10">';
$type = $options['type'];
if ($this->_View->viewVars['isPublished']) {
$options = Hash::merge($options, array('disabled' => true));
}
$options = Hash::merge(array('div' => false, 'label' => false), $options);
if ($type == 'wysiwyg') {
if ($this->_View->viewVars['isPublished']) {
$ret .= '<div class="well well-sm quiz-edit-disabled-well" ng-bind-html="';
$ret .= $options['ng-model'] . ' | ncHtmlContent"></div>';
} else {
$ret .= $this->NetCommonsForm->wysiwyg($fieldName, $options);
}
} elseif ($type == 'checkbox') {
$options = Hash::merge($options, array('label' => $label));
$ret .= $this->NetCommonsForm->checkbox($fieldName, $options);
} else {
$ret .= $this->NetCommonsForm->input($fieldName, $options);
}
if (isset($options['ng-model'])) {
$ret .= $this->quizNgError($options['ng-model']);
}
$ret .= '</div></div>';
return $ret;
}
/**
* Angularモデルに対するエラーメッセージモデル名取得
*
* @param string $ngModelName Angularモデル名
* @return string エラーメッセージ保持するモデル名
*/
public function quizGetNgErrorModelName($ngModelName) {
$modelNames = explode('.', $ngModelName);
$errorMsgModelName = $modelNames[0] . '.errorMessages.' . $modelNames[1];
return $errorMsgModelName;
}
/**
* Angularモデルに対するエラーメッセージ表示
*
* @param string $ngModelName Angularモデル名
* @return string エラーメッセージ表示HTML
*/
public function quizNgError($ngModelName) {
$errorMsgModelName = $this->quizGetNgErrorModelName($ngModelName);
$ret = '<div class="has-error" ng-if="' . $errorMsgModelName . '">';
$ret .= '<div class="help-block" ng-repeat="errorMessage in ' . $errorMsgModelName . '">';
$ret .= '{{errorMessage}}</div></div>';
return $ret;
}
/**
* 小テスト属性設定作成
*
* @param string $fieldName フィールド名
* @param string $label checkboxの時のラベル
* @param array $options INPUT要素に与えるオプション属性
* @param string $help 追加説明文
* @return string HTML
*/
public function quizAttributeCheckbox($fieldName, $label, $options = array(), $help = '') {
$ngModel = 'quiz.quiz.' . Inflector::variable($fieldName);
$ret = '<div class=" checkbox"><label>';
$options = Hash::merge(array(
'type' => 'checkbox',
'div' => false,
'label' => false,
'class' => '',
'error' => false,
'ng-model' => $ngModel,
// この記述でないと チェックON,OFFが正常に動作しない。
'ng-false-value' => '"0"',
'ng-true-value' => '"1"'
),
$options
);
$ret .= $this->NetCommonsForm->input($fieldName, $options);
$ret .= $label;
if (!empty($help)) {
$ret .= '<span class="help-block">' . $help . '</span>';
}
$ret .= '</label>';
$ret .= $this->NetCommonsForm->error($fieldName, null, array('class' => 'help-block'));
$ret .= '</div>';
return $ret;
}
/**
* 小テスト期間設定作成
*
* @param string $fieldName フィールド名
* @param array $options オプション
* @param string $help 追加説明文
* @return string HTML
*/
public function quizAttributeDatetime($fieldName, $options, $help = '') {
//$ngModel = 'quiz.quiz.' . Inflector::variable($fieldName);
$defaultOptions = array(
'type' => 'datetime',
'id' => $fieldName,
//'ng-model' => $ngModel,
);
$options = Hash::merge($defaultOptions, $options);
if (isset($options['min']) && isset($options['max'])) {
$min = 'NetCommonsFormDatetimePickerModel_Quiz_' . $options['min'];
$max = 'NetCommonsFormDatetimePickerModel_Quiz_' . $options['max'];
$options = Hash::merge($options, array(
'ng-focus' => 'setMinMaxDate($event, \'' . $min . '\', \'' . $max . '\')',
));
}
$ret = $this->NetCommonsForm->input($fieldName, $options);
if (!empty($help)) {
$ret .= '<span class="help-block">' . $help . '</span>';
}
return $ret;
}
/**
* quizGetFinallySubmit
*
* 小テストは質問編集画面では分割送信をする
* 分割送信後、最終POSTをする時に使用するFormを作成する
*
* @param array $postUrl POST先URL情報
* @return string HTML
*/
public function quizGetFinallySubmit($postUrl) {
$html = '';
$html .= $this->NetCommonsForm->create('QuizQuestion',
Hash::merge(array('id' => 'finallySubmitForm'), $postUrl)
);
$html .= $this->NetCommonsForm->hidden('Frame.id');
$html .= $this->NetCommonsForm->hidden('Block.id');
$html .= $this->NetCommonsForm->hidden('Quiz.key');
$this->NetCommonsForm->unlockField('QuizPage');
$html .= $this->NetCommonsForm->end();
return $html;
}
/**
* getJsPostData
*
* 小テストは分割送信をAjaxで行う
* AjaxでPostを行うときにtoken含みの配列を取得する
*
* @param string $quizKey 小テストキー(Postデータの一つとして必要)
* @param string $ajaxPostUrl Post先URL(セッション保存キーが含まれるためコントローラーからもらわないとわからない)
* @return array
*/
public function getJsPostData($quizKey, $ajaxPostUrl) {
$postData = array(
'Frame' => array('id' => Current::read('Frame.id')),
'Block' => array('id' => Current::read('Block.id')),
'Quiz' => array('key' => $quizKey),
);
$tokenFields = Hash::flatten($postData);
$hiddenFields = $tokenFields;
$hiddenFields = array_keys($hiddenFields);
$this->Token->unlockField('QuizPage');
$tokens = $this->Token->getToken('Quiz', $ajaxPostUrl, $tokenFields, $hiddenFields);
$postData += $tokens;
return $postData;
}
}