Total Complexity | 46 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like UniqueAnswerNoOption often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UniqueAnswerNoOption, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class UniqueAnswerNoOption extends Question |
||
16 | { |
||
17 | public $typePicture = 'mcuao.png'; |
||
18 | public $explanationLangVar = 'Unique answer with unknown'; |
||
19 | |||
20 | public function __construct() |
||
25 | } |
||
26 | |||
27 | public function createAnswersForm($form) |
||
28 | { |
||
29 | // getting the exercise list |
||
30 | $obj_ex = Session::read('objExercise'); |
||
31 | |||
32 | $editor_config = [ |
||
33 | 'ToolbarSet' => 'TestProposedAnswer', |
||
34 | 'Width' => '100%', |
||
35 | 'Height' => '125', |
||
36 | ]; |
||
37 | // This line define how many question by default appear when creating a choice question |
||
38 | // The previous default value was 2. See task #1759. |
||
39 | $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 3; |
||
40 | $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
41 | |||
42 | /* |
||
43 | Types of Feedback |
||
44 | $feedback_option[0]=get_lang('Feedback'); |
||
45 | $feedback_option[1]=get_lang('Adaptative test with immediate feedback'); |
||
46 | $feedback_option[2]=get_lang('Exam (no feedback)'); |
||
47 | */ |
||
48 | |||
49 | $feedback_title = ''; |
||
50 | if (1 == $obj_ex->getFeedbackType()) { |
||
51 | $editor_config['Width'] = '250'; |
||
52 | $editor_config['Height'] = '110'; |
||
53 | $comment_title = '<th width="50%" >'.get_lang('Comment').'</th>'; |
||
54 | $feedback_title = '<th width="50%" >'.get_lang('Scenario').'</th>'; |
||
55 | } else { |
||
56 | $comment_title = '<th width="50%">'.get_lang('Comment').'</th>'; |
||
57 | } |
||
58 | |||
59 | $html = '<table class="table table-striped table-hover">'; |
||
60 | $html .= '<thead>'; |
||
61 | $html .= '<tr>'; |
||
62 | $html .= '<th>'.get_lang('N°').'</th>'; |
||
63 | $html .= '<th>'.get_lang('True').'</th>'; |
||
64 | $html .= '<th width="50%">'.get_lang('Answer').'</th>'; |
||
65 | $html .= $comment_title.$feedback_title; |
||
66 | $html .= '<th>'.get_lang('Score').'</th>'; |
||
67 | $html .= '</tr>'; |
||
68 | $html .= '</thead>'; |
||
69 | $html .= '<tbody>'; |
||
70 | |||
71 | $form->addHeader(get_lang('Answers')); |
||
72 | $form->addHtml($html); |
||
73 | |||
74 | $defaults = []; |
||
75 | $correct = 0; |
||
76 | $answer = false; |
||
77 | if (!empty($this->id)) { |
||
78 | $answer = new Answer($this->id); |
||
79 | $answer->read(); |
||
80 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
81 | $nb_answers = $answer->nbrAnswers; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $temp_scenario = []; |
||
86 | if ($nb_answers < 1) { |
||
87 | $nb_answers = 1; |
||
88 | echo Display::return_message(get_lang('You have to create at least one answer')); |
||
89 | } |
||
90 | $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : false; |
||
91 | if ($editQuestion) { |
||
92 | //fixing $nb_answers |
||
93 | $new_list = []; |
||
94 | $count = 1; |
||
95 | if (isset($_POST['lessAnswers'])) { |
||
96 | if (!isset($_SESSION['less_answer'])) { |
||
97 | $_SESSION['less_answer'] = $this->id; |
||
98 | $nb_answers--; |
||
99 | } |
||
100 | } |
||
101 | for ($k = 1; $k <= $nb_answers; $k++) { |
||
102 | if ('666' != $answer->position[$k]) { |
||
103 | $new_list[$count] = $count; |
||
104 | $count++; |
||
105 | } |
||
106 | } |
||
107 | } else { |
||
108 | for ($k = 1; $k <= $nb_answers; $k++) { |
||
109 | $new_list[$k] = $k; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | foreach ($new_list as $key) { |
||
|
|||
114 | $i = $key; |
||
115 | $form->addHtml('<tr>'); |
||
116 | if (is_object($answer)) { |
||
117 | if (666 == $answer->position[$i]) { |
||
118 | //we set nothing |
||
119 | } else { |
||
120 | if ($answer->correct[$i]) { |
||
121 | $correct = $i; |
||
122 | } |
||
123 | $answer_result = $answer->answer[$i]; |
||
124 | $weight_result = float_format($answer->weighting[$i], 1); |
||
125 | if ($nb_answers == $i) { |
||
126 | $weight_result = '0'; |
||
127 | } |
||
128 | |||
129 | $defaults['answer['.$i.']'] = $answer_result; |
||
130 | $defaults['comment['.$i.']'] = $answer->comment[$i]; |
||
131 | $defaults['weighting['.$i.']'] = $weight_result; |
||
132 | |||
133 | $item_list = explode('@@', $answer->destination[$i]); |
||
134 | |||
135 | $try = $item_list[0]; |
||
136 | $lp = $item_list[1]; |
||
137 | $list_dest = $item_list[2]; |
||
138 | $url = $item_list[3]; |
||
139 | |||
140 | if (0 == $try) { |
||
141 | $try_result = 0; |
||
142 | } else { |
||
143 | $try_result = 1; |
||
144 | } |
||
145 | |||
146 | if (0 == $url) { |
||
147 | $url_result = ''; |
||
148 | } else { |
||
149 | $url_result = $url; |
||
150 | } |
||
151 | |||
152 | $temp_scenario['url'.$i] = $url_result; |
||
153 | $temp_scenario['try'.$i] = $try_result; |
||
154 | $temp_scenario['lp'.$i] = $lp; |
||
155 | $temp_scenario['destination'.$i] = $list_dest; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $defaults['scenario'] = $temp_scenario; |
||
160 | $renderer = &$form->defaultRenderer(); |
||
161 | |||
162 | $renderer->setElementTemplate( |
||
163 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
164 | 'correct' |
||
165 | ); |
||
166 | $renderer->setElementTemplate( |
||
167 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
168 | 'counter['.$i.']' |
||
169 | ); |
||
170 | $renderer->setElementTemplate( |
||
171 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
172 | 'answer['.$i.']' |
||
173 | ); |
||
174 | $renderer->setElementTemplate( |
||
175 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
176 | 'comment['.$i.']' |
||
177 | ); |
||
178 | $renderer->setElementTemplate( |
||
179 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
180 | 'weighting['.$i.']' |
||
181 | ); |
||
182 | |||
183 | $answerNumber = $form->addText('counter['.$i.']', null, false, ['value' => $i]); |
||
184 | $answerNumber->freeze(); |
||
185 | |||
186 | $form->addElement( |
||
187 | 'radio', |
||
188 | 'correct', |
||
189 | null, |
||
190 | null, |
||
191 | $i, |
||
192 | ['class' => 'checkbox', 'style' => 'margin-left: 0em;'] |
||
193 | ); |
||
194 | $form->addHtmlEditor('answer['.$i.']', null, [], $editor_config); |
||
195 | $form->addHtmlEditor('comment['.$i.']', null, [], $editor_config); |
||
196 | $form->addText('weighting['.$i.']', null, false, ['style' => 'width: 60px;', 'value' => '0']); |
||
197 | $form->addHtml('</tr>'); |
||
198 | $i++; |
||
199 | } |
||
200 | |||
201 | if (empty($this->id)) { |
||
202 | $form->addHidden('new_question', 1); |
||
203 | } |
||
204 | |||
205 | //Adding the "I don't know" question answer |
||
206 | //if (empty($this -> id)) { |
||
207 | $i = 666; |
||
208 | $form->addHtml('<tr>'); |
||
209 | |||
210 | $defaults["counter[$i]"] = '-'; |
||
211 | $defaults['answer['.$i.']'] = get_lang('Don\'t know'); |
||
212 | $defaults['weighting['.$i.']'] = '0'; |
||
213 | $defaults['scenario'] = $temp_scenario; |
||
214 | $renderer = &$form->defaultRenderer(); |
||
215 | |||
216 | $renderer->setElementTemplate( |
||
217 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
218 | 'correct' |
||
219 | ); |
||
220 | $renderer->setElementTemplate( |
||
221 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
222 | 'counter['.$i.']' |
||
223 | ); |
||
224 | $renderer->setElementTemplate( |
||
225 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
226 | 'answer['.$i.']' |
||
227 | ); |
||
228 | $renderer->setElementTemplate( |
||
229 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
230 | 'comment['.$i.']' |
||
231 | ); |
||
232 | $renderer->setElementTemplate( |
||
233 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
234 | 'weighting['.$i.']' |
||
235 | ); |
||
236 | |||
237 | $form |
||
238 | ->addElement('text', 'counter['.$i.']', null) |
||
239 | ->freeze(); |
||
240 | |||
241 | $form->addHidden('position['.$i.']', '666'); |
||
242 | $form->addElement('radio', 'correct', null, null, $i, ['class' => 'checkbox', 'disabled' => true]); |
||
243 | $form->addHtmlEditor('answer['.$i.']', null, true, [], $editor_config); |
||
244 | |||
245 | $form->addRule('answer['.$i.']', get_lang('Required field'), 'required'); |
||
246 | $form->applyFilter("answer[$i]", 'attr_on_filter'); |
||
247 | $form->addHtmlEditor('comment['.$i.']', null, true, [], $editor_config); |
||
248 | $form->addElement('text', "weighting[$i]", null)->freeze(); |
||
249 | |||
250 | $form->addHTml('</tr>'); |
||
251 | $form->addHtml('</tbody></table>'); |
||
252 | |||
253 | $buttonGroup = []; |
||
254 | |||
255 | global $text; |
||
256 | //ie6 fix |
||
257 | if (true == $obj_ex->edit_exercise_in_lp || |
||
258 | (empty($this->exerciseList) && empty($obj_ex->id)) |
||
259 | ) { |
||
260 | //setting the save button here and not in the question class.php |
||
261 | $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true); |
||
262 | $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true); |
||
263 | $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true); |
||
264 | |||
265 | $form->addGroup($buttonGroup); |
||
266 | } |
||
267 | |||
268 | //We check the first radio button to be sure a radio button will be check |
||
269 | if (0 == $correct) { |
||
270 | $correct = 1; |
||
271 | } |
||
272 | $defaults['correct'] = $correct; |
||
273 | |||
274 | if (!empty($this->id)) { |
||
275 | $form->setDefaults($defaults); |
||
276 | } else { |
||
277 | $form->setDefaults($defaults); |
||
278 | } |
||
279 | |||
280 | $form->addElement('hidden', 'nb_answers'); |
||
281 | $form->setConstants(['nb_answers' => $nb_answers]); |
||
282 | } |
||
283 | |||
284 | public function processAnswersCreation($form, $exercise) |
||
390 | } |
||
391 | |||
392 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
418 |