Total Complexity | 74 |
Total Lines | 568 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like UniqueAnswer 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 UniqueAnswer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class UniqueAnswer extends Question |
||
19 | { |
||
20 | public $typePicture = 'mcua.png'; |
||
21 | public $explanationLangVar = 'UniqueSelect'; |
||
22 | |||
23 | /** |
||
24 | * Constructor. |
||
25 | */ |
||
26 | public function __construct() |
||
27 | { |
||
28 | parent::__construct(); |
||
29 | $this->type = UNIQUE_ANSWER; |
||
30 | $this->isContent = $this->getIsContent(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | public function createAnswersForm($form) |
||
37 | { |
||
38 | // Getting the exercise list |
||
39 | /** @var Exercise $obj_ex */ |
||
40 | $obj_ex = Session::read('objExercise'); |
||
41 | |||
42 | $editor_config = [ |
||
43 | 'ToolbarSet' => 'TestProposedAnswer', |
||
44 | 'Width' => '100%', |
||
45 | 'Height' => '125', |
||
46 | ]; |
||
47 | |||
48 | //this line defines how many questions by default appear when creating a choice question |
||
49 | // The previous default value was 2. See task #1759. |
||
50 | $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; |
||
51 | $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
52 | |||
53 | $feedback_title = ''; |
||
54 | switch ($obj_ex->getFeedbackType()) { |
||
55 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
56 | // Scenario |
||
57 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
58 | $feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>'; |
||
59 | break; |
||
60 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
61 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
62 | break; |
||
63 | default: |
||
64 | $comment_title = '<th width="40%">'.get_lang('Comment').'</th>'; |
||
65 | break; |
||
66 | } |
||
67 | |||
68 | $html = '<table class="table table-striped table-hover"> |
||
69 | <thead> |
||
70 | <tr style="text-align: center;"> |
||
71 | <th width="5%">'.get_lang('Number').'</th> |
||
72 | <th width="5%"> '.get_lang('True').'</th> |
||
73 | <th width="40%">'.get_lang('Answer').'</th> |
||
74 | '.$comment_title.' |
||
75 | '.$feedback_title.' |
||
76 | <th width="10%">'.get_lang('Weighting').'</th> |
||
77 | </tr> |
||
78 | </thead> |
||
79 | <tbody>'; |
||
80 | |||
81 | $form->addHeader(get_lang('Answers')); |
||
82 | $form->addHtml($html); |
||
83 | |||
84 | $defaults = []; |
||
85 | |||
86 | $correct = 0; |
||
87 | if (!empty($this->iid)) { |
||
88 | $answer = new Answer($this->iid); |
||
89 | $answer->read(); |
||
90 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
91 | $nb_answers = $answer->nbrAnswers; |
||
92 | } |
||
93 | } |
||
94 | $form->addElement('hidden', 'nb_answers'); |
||
95 | |||
96 | $obj_ex->setQuestionList(true); |
||
97 | $question_list = $obj_ex->getQuestionList(); |
||
98 | $select_question = []; |
||
99 | $select_question[0] = get_lang('SelectTargetQuestion'); |
||
100 | if (is_array($question_list)) { |
||
101 | foreach ($question_list as $key => $questionid) { |
||
102 | //To avoid warning messages |
||
103 | if (!is_numeric($questionid)) { |
||
104 | continue; |
||
105 | } |
||
106 | $question = Question::read($questionid); |
||
107 | $questionTitle = strip_tags($question->selectTitle()); |
||
108 | $select_question[$questionid] = "Q$key: $questionTitle"; |
||
109 | } |
||
110 | } |
||
111 | $select_question[-1] = get_lang('ExitTest'); |
||
112 | |||
113 | $list = new LearnpathList(api_get_user_id()); |
||
114 | $flat_list = $list->get_flat_list(); |
||
115 | $select_lp_id = []; |
||
116 | $select_lp_id[0] = get_lang('SelectTargetLP'); |
||
117 | |||
118 | foreach ($flat_list as $id => $details) { |
||
119 | $select_lp_id[$id] = cut($details['lp_name'], 20); |
||
120 | } |
||
121 | |||
122 | $temp_scenario = []; |
||
123 | if ($nb_answers < 1) { |
||
124 | $nb_answers = 1; |
||
125 | echo Display::return_message( |
||
126 | get_lang('YouHaveToCreateAtLeastOneAnswer') |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
131 | $form->addHtml('<tr>'); |
||
132 | if (isset($answer) && is_object($answer)) { |
||
133 | if (isset($answer->correct[$i]) && $answer->correct[$i]) { |
||
134 | $correct = $i; |
||
135 | } |
||
136 | $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : ''; |
||
137 | $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : ''; |
||
138 | $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0; |
||
139 | $item_list = []; |
||
140 | if (isset($answer->destination[$i])) { |
||
141 | $item_list = explode('@@', $answer->destination[$i]); |
||
142 | } |
||
143 | $try = isset($item_list[0]) ? $item_list[0] : ''; |
||
144 | $lp = isset($item_list[1]) ? $item_list[1] : ''; |
||
145 | $list_dest = isset($item_list[2]) ? $item_list[2] : ''; |
||
146 | $url = isset($item_list[3]) ? $item_list[3] : ''; |
||
147 | |||
148 | if ($try == 0) { |
||
149 | $try_result = 0; |
||
150 | } else { |
||
151 | $try_result = 1; |
||
152 | } |
||
153 | if ($url == 0) { |
||
154 | $url_result = ''; |
||
155 | } else { |
||
156 | $url_result = $url; |
||
157 | } |
||
158 | |||
159 | $temp_scenario['url'.$i] = $url_result; |
||
160 | $temp_scenario['try'.$i] = $try_result; |
||
161 | $temp_scenario['lp'.$i] = $lp; |
||
162 | $temp_scenario['destination'.$i] = $list_dest; |
||
163 | } else { |
||
164 | $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1'); |
||
165 | $defaults['weighting[1]'] = 10; |
||
166 | $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2'); |
||
167 | $defaults['weighting[2]'] = 0; |
||
168 | $temp_scenario['destination'.$i] = ['0']; |
||
169 | $temp_scenario['lp'.$i] = ['0']; |
||
170 | } |
||
171 | $defaults['scenario'] = $temp_scenario; |
||
172 | |||
173 | $renderer = $form->defaultRenderer(); |
||
174 | |||
175 | $renderer->setElementTemplate( |
||
176 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
177 | 'correct' |
||
178 | ); |
||
179 | $renderer->setElementTemplate( |
||
180 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
181 | 'counter['.$i.']' |
||
182 | ); |
||
183 | $renderer->setElementTemplate( |
||
184 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
185 | 'answer['.$i.']' |
||
186 | ); |
||
187 | $renderer->setElementTemplate( |
||
188 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
189 | 'comment['.$i.']' |
||
190 | ); |
||
191 | $renderer->setElementTemplate( |
||
192 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
193 | 'weighting['.$i.']' |
||
194 | ); |
||
195 | |||
196 | $answer_number = $form->addElement( |
||
197 | 'text', |
||
198 | 'counter['.$i.']', |
||
199 | null, |
||
200 | ' value = "'.$i.'"' |
||
201 | ); |
||
202 | $answer_number->freeze(); |
||
203 | $form->addElement( |
||
204 | 'radio', |
||
205 | 'correct', |
||
206 | null, |
||
207 | null, |
||
208 | $i, |
||
209 | 'class="checkbox"' |
||
210 | ); |
||
211 | |||
212 | $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config); |
||
213 | |||
214 | $form->addRule( |
||
215 | 'answer['.$i.']', |
||
216 | get_lang('ThisFieldIsRequired'), |
||
217 | 'required' |
||
218 | ); |
||
219 | |||
220 | switch ($obj_ex->getFeedbackType()) { |
||
221 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
222 | $this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question); |
||
223 | break; |
||
224 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
225 | default: |
||
226 | $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config); |
||
227 | break; |
||
228 | } |
||
229 | $form->addText('weighting['.$i.']', null, null, ['value' => '0']); |
||
230 | $form->addHtml('</tr>'); |
||
231 | } |
||
232 | |||
233 | $form->addHtml('</tbody>'); |
||
234 | $form->addHtml('</table>'); |
||
235 | |||
236 | global $text; |
||
237 | $buttonGroup = []; |
||
238 | |||
239 | if (true === $obj_ex->edit_exercise_in_lp || (empty($this->exerciseList) && empty($obj_ex->iid))) { |
||
240 | //setting the save button here and not in the question class.php |
||
241 | $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true); |
||
242 | $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true); |
||
243 | $buttonGroup[] = $form->addButton( |
||
244 | 'submitQuestion', |
||
245 | $text, |
||
246 | 'check', |
||
247 | 'primary', |
||
248 | 'default', |
||
249 | null, |
||
250 | ['id' => 'submit-question'], |
||
251 | true |
||
252 | ); |
||
253 | $form->addGroup($buttonGroup); |
||
254 | } |
||
255 | |||
256 | // We check the first radio button to be sure a radio button will be check |
||
257 | if ($correct == 0) { |
||
258 | $correct = 1; |
||
259 | } |
||
260 | |||
261 | if (isset($_POST) && isset($_POST['correct'])) { |
||
262 | $correct = (int) $_POST['correct']; |
||
263 | } |
||
264 | |||
265 | $defaults['correct'] = $correct; |
||
266 | |||
267 | if (!empty($this->iid)) { |
||
268 | $form->setDefaults($defaults); |
||
269 | } else { |
||
270 | if ($this->isContent == 1) { |
||
271 | // Default sample content. |
||
272 | $form->setDefaults($defaults); |
||
273 | } else { |
||
274 | $correct = 1; |
||
275 | if (isset($_POST) && isset($_POST['correct'])) { |
||
276 | $correct = (int) $_POST['correct']; |
||
277 | } |
||
278 | |||
279 | $form->setDefaults(['correct' => $correct]); |
||
280 | } |
||
281 | } |
||
282 | $form->setConstants(['nb_answers' => $nb_answers]); |
||
283 | } |
||
284 | |||
285 | public function setDirectOptions($i, FormValidator $form, $renderer, $select_lp_id, $select_question) |
||
335 | ); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Validate question answers before saving. |
||
340 | * @return bool|string True if valid, error message if invalid. |
||
341 | */ |
||
342 | public function validateAnswers($form) |
||
343 | { |
||
344 | $correct = $form->getSubmitValue('correct'); |
||
345 | $nb_answers = $form->getSubmitValue('nb_answers'); |
||
346 | $hasCorrectAnswer = false; |
||
347 | $hasPositiveScore = false; |
||
348 | $errors = []; |
||
349 | $error_fields = []; |
||
350 | |||
351 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
352 | $answer = trim($form->getSubmitValue("answer[$i]") ?? ''); |
||
353 | $weighting = trim($form->getSubmitValue("weighting[$i]") ?? ''); |
||
354 | $isCorrect = ($correct == $i); |
||
355 | if (empty($answer)) { |
||
356 | $errors[] = sprintf(get_lang('NoAnswerCanBeEmpty'), $i); |
||
357 | $error_fields[] = "answer[$i]"; |
||
358 | } |
||
359 | |||
360 | if ($weighting === '' || !is_numeric($weighting)) { |
||
361 | $errors[] = sprintf(get_lang('ScoreMustBeNumeric'), $i); |
||
362 | $error_fields[] = "weighting[$i]"; |
||
363 | } |
||
364 | |||
365 | if ($isCorrect) { |
||
366 | $hasCorrectAnswer = true; |
||
367 | if (floatval($weighting) > 0) { |
||
368 | $hasPositiveScore = true; |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | |||
373 | if (!$hasCorrectAnswer) { |
||
374 | $errors[] = get_lang('ACorrectAnswerIsRequired'); |
||
375 | $error_fields[] = "correct"; |
||
376 | } |
||
377 | |||
378 | if (!$hasPositiveScore) { |
||
379 | $errors[] = get_lang('TheCorrectAnswerMustHaveAPositiveScore'); |
||
380 | } |
||
381 | |||
382 | return empty($errors) ? true : ['errors' => $errors, 'error_fields' => $error_fields]; |
||
|
|||
383 | } |
||
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | public function processAnswersCreation($form, $exercise) |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * {@inheritdoc} |
||
496 | */ |
||
497 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Saves one answer to the database. |
||
521 | * |
||
522 | * @param int $id The ID of the answer (has to be calculated for this course) |
||
523 | * @param int $question_id The question ID (to which the answer is attached) |
||
524 | * @param string $title The text of the answer |
||
525 | * @param string $comment The feedback for the answer |
||
526 | * @param float $score The score you get when picking this answer |
||
527 | * @param int $correct Whether this answer is considered *the* correct one (this is the unique answer type) |
||
528 | */ |
||
529 | public function addAnswer( |
||
589 |