| Total Complexity | 58 |
| Total Lines | 502 |
| Duplicated Lines | 0 % |
| Changes | 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 = 'Multiple choice'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | */ |
||
| 26 | public function __construct() |
||
| 31 | } |
||
| 32 | |||
| 33 | public function createAnswersForm($form) |
||
| 34 | { |
||
| 35 | // Getting the exercise list |
||
| 36 | /** @var Exercise $obj_ex */ |
||
| 37 | $obj_ex = Session::read('objExercise'); |
||
| 38 | |||
| 39 | $editor_config = [ |
||
| 40 | 'ToolbarSet' => 'TestProposedAnswer', |
||
| 41 | 'Width' => '100%', |
||
| 42 | 'Height' => '125', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | //this line defines how many questions by default appear when creating a choice question |
||
| 46 | // The previous default value was 2. See task #1759. |
||
| 47 | $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; |
||
| 48 | $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
| 49 | |||
| 50 | $feedback_title = ''; |
||
| 51 | switch ($obj_ex->getFeedbackType()) { |
||
| 52 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 53 | // Scenario |
||
| 54 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 55 | $feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>'; |
||
| 56 | |||
| 57 | break; |
||
| 58 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 59 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 60 | |||
| 61 | break; |
||
| 62 | default: |
||
| 63 | $comment_title = '<th width="40%">'.get_lang('Comment').'</th>'; |
||
| 64 | |||
| 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('N°').'</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('Score').'</th> |
||
| 77 | </tr> |
||
| 78 | </thead> |
||
| 79 | <tbody>'; |
||
| 80 | |||
| 81 | $form->addHeader(get_lang('Answers')); |
||
| 82 | $form->addHtml($html); |
||
| 83 | |||
| 84 | $defaults = []; |
||
| 85 | $correct = 0; |
||
| 86 | if (!empty($this->id)) { |
||
| 87 | $answer = new Answer($this->id); |
||
| 88 | $answer->read(); |
||
| 89 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
| 90 | $nb_answers = $answer->nbrAnswers; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $form->addElement('hidden', 'nb_answers'); |
||
| 94 | |||
| 95 | $obj_ex->setQuestionList(true); |
||
| 96 | $question_list = $obj_ex->getQuestionList(); |
||
| 97 | $select_question = []; |
||
| 98 | $select_question[0] = get_lang('Select target question'); |
||
| 99 | if (is_array($question_list)) { |
||
| 100 | foreach ($question_list as $key => $questionid) { |
||
| 101 | //To avoid warning messages |
||
| 102 | if (!is_numeric($questionid)) { |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | $question = Question::read($questionid); |
||
| 106 | $questionTitle = strip_tags($question->selectTitle()); |
||
| 107 | $select_question[$questionid] = "Q$key: $questionTitle"; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $select_question[-1] = get_lang('Exit test'); |
||
| 111 | |||
| 112 | $list = new LearnpathList(api_get_user_id()); |
||
| 113 | $flat_list = $list->get_flat_list(); |
||
| 114 | $select_lp_id = []; |
||
| 115 | $select_lp_id[0] = get_lang('Select target course'); |
||
| 116 | |||
| 117 | foreach ($flat_list as $id => $details) { |
||
| 118 | $select_lp_id[$id] = cut($details['lp_name'], 20); |
||
| 119 | } |
||
| 120 | |||
| 121 | $temp_scenario = []; |
||
| 122 | if ($nb_answers < 1) { |
||
| 123 | $nb_answers = 1; |
||
| 124 | echo Display::return_message( |
||
| 125 | get_lang('You have to create at least one answer') |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
| 130 | $form->addHtml('<tr>'); |
||
| 131 | if (isset($answer) && is_object($answer)) { |
||
| 132 | if (isset($answer->correct[$i]) && $answer->correct[$i]) { |
||
| 133 | $correct = $i; |
||
| 134 | } |
||
| 135 | $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : ''; |
||
| 136 | $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : ''; |
||
| 137 | $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0; |
||
| 138 | $item_list = []; |
||
| 139 | if (isset($answer->destination[$i])) { |
||
| 140 | $item_list = explode('@@', $answer->destination[$i]); |
||
| 141 | } |
||
| 142 | $try = $item_list[0] ?? ''; |
||
| 143 | $lp = $item_list[1] ?? ''; |
||
| 144 | $list_dest = $item_list[2] ?? ''; |
||
| 145 | $url = $item_list[3] ?? ''; |
||
| 146 | |||
| 147 | if (0 == $try) { |
||
| 148 | $try_result = 0; |
||
| 149 | } else { |
||
| 150 | $try_result = 1; |
||
| 151 | } |
||
| 152 | if (0 == $url) { |
||
| 153 | $url_result = ''; |
||
| 154 | } else { |
||
| 155 | $url_result = $url; |
||
| 156 | } |
||
| 157 | |||
| 158 | $temp_scenario['url'.$i] = $url_result; |
||
| 159 | $temp_scenario['try'.$i] = $try_result; |
||
| 160 | $temp_scenario['lp'.$i] = $lp; |
||
| 161 | $temp_scenario['destination'.$i] = $list_dest; |
||
| 162 | } else { |
||
| 163 | $defaults['answer[1]'] = get_lang('A then B then C'); |
||
| 164 | $defaults['weighting[1]'] = 10; |
||
| 165 | $defaults['answer[2]'] = get_lang('A then C then B'); |
||
| 166 | $defaults['weighting[2]'] = 0; |
||
| 167 | $temp_scenario['destination'.$i] = ['0']; |
||
| 168 | $temp_scenario['lp'.$i] = ['0']; |
||
| 169 | } |
||
| 170 | $defaults['scenario'] = $temp_scenario; |
||
| 171 | |||
| 172 | $renderer = $form->defaultRenderer(); |
||
| 173 | |||
| 174 | $renderer->setElementTemplate( |
||
| 175 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 176 | 'correct' |
||
| 177 | ); |
||
| 178 | $renderer->setElementTemplate( |
||
| 179 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 180 | 'counter['.$i.']' |
||
| 181 | ); |
||
| 182 | $renderer->setElementTemplate( |
||
| 183 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 184 | 'answer['.$i.']' |
||
| 185 | ); |
||
| 186 | $renderer->setElementTemplate( |
||
| 187 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 188 | 'comment['.$i.']' |
||
| 189 | ); |
||
| 190 | $renderer->setElementTemplate( |
||
| 191 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 192 | 'weighting['.$i.']' |
||
| 193 | ); |
||
| 194 | |||
| 195 | $answer_number = $form->addElement( |
||
| 196 | 'text', |
||
| 197 | 'counter['.$i.']', |
||
| 198 | null, |
||
| 199 | ' value = "'.$i.'"' |
||
| 200 | ); |
||
| 201 | $answer_number->freeze(); |
||
| 202 | $form->addElement( |
||
| 203 | 'radio', |
||
| 204 | 'correct', |
||
| 205 | null, |
||
| 206 | null, |
||
| 207 | $i, |
||
| 208 | ['class' => 'checkbox'] |
||
| 209 | ); |
||
| 210 | |||
| 211 | $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config); |
||
| 212 | |||
| 213 | $form->addRule( |
||
| 214 | 'answer['.$i.']', |
||
| 215 | get_lang('Required field'), |
||
| 216 | 'required' |
||
| 217 | ); |
||
| 218 | |||
| 219 | switch ($obj_ex->getFeedbackType()) { |
||
| 220 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 221 | $this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question); |
||
| 222 | |||
| 223 | break; |
||
| 224 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 225 | default: |
||
| 226 | $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config); |
||
| 227 | |||
| 228 | break; |
||
| 229 | } |
||
| 230 | $form->addText('weighting['.$i.']', null, null, ['value' => '0']); |
||
| 231 | $form->addHtml('</tr>'); |
||
| 232 | } |
||
| 233 | |||
| 234 | $form->addHtml('</tbody>'); |
||
| 235 | $form->addHtml('</table>'); |
||
| 236 | |||
| 237 | global $text; |
||
| 238 | $buttonGroup = []; |
||
| 239 | |||
| 240 | if (true == $obj_ex->edit_exercise_in_lp || |
||
|
|
|||
| 241 | (empty($this->exerciseList) && empty($obj_ex->id)) |
||
| 242 | ) { |
||
| 243 | //setting the save button here and not in the question class.php |
||
| 244 | $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true); |
||
| 245 | $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true); |
||
| 246 | $buttonGroup[] = $form->addButton( |
||
| 247 | 'submitQuestion', |
||
| 248 | $text, |
||
| 249 | 'check', |
||
| 250 | 'primary', |
||
| 251 | 'default', |
||
| 252 | null, |
||
| 253 | ['id' => 'submit-question'], |
||
| 254 | true |
||
| 255 | ); |
||
| 256 | $form->addGroup($buttonGroup); |
||
| 257 | } |
||
| 258 | |||
| 259 | // We check the first radio button to be sure a radio button will be check |
||
| 260 | if (0 == $correct) { |
||
| 261 | $correct = 1; |
||
| 262 | } |
||
| 263 | |||
| 264 | if (isset($_POST) && isset($_POST['correct'])) { |
||
| 265 | $correct = (int) $_POST['correct']; |
||
| 266 | } |
||
| 267 | |||
| 268 | $defaults['correct'] = $correct; |
||
| 269 | |||
| 270 | if (!empty($this->id)) { |
||
| 271 | $form->setDefaults($defaults); |
||
| 272 | } else { |
||
| 273 | if (1 == $this->isContent) { |
||
| 274 | // Default sample content. |
||
| 275 | $form->setDefaults($defaults); |
||
| 276 | } else { |
||
| 277 | $correct = 1; |
||
| 278 | if (isset($_POST) && isset($_POST['correct'])) { |
||
| 279 | $correct = (int) $_POST['correct']; |
||
| 280 | } |
||
| 281 | |||
| 282 | $form->setDefaults(['correct' => $correct]); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | $form->setConstants(['nb_answers' => $nb_answers]); |
||
| 286 | } |
||
| 287 | |||
| 288 | public function setDirectOptions($i, FormValidator $form, $renderer, $select_lp_id, $select_question) |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | public function processAnswersCreation($form, $exercise) |
||
| 439 | } |
||
| 440 | |||
| 441 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Saves one answer to the database. |
||
| 465 | * |
||
| 466 | * @param int $id The ID of the answer (has to be calculated for this course) |
||
| 467 | * @param int $question_id The question ID (to which the answer is attached) |
||
| 468 | * @param string $title The text of the answer |
||
| 469 | * @param string $comment The feedback for the answer |
||
| 470 | * @param float $score The score you get when picking this answer |
||
| 471 | * @param int $correct Whether this answer is considered *the* correct one (this is the unique answer type) |
||
| 472 | */ |
||
| 473 | public function addAnswer( |
||
| 523 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.