| Conditions | 40 |
| Paths | > 20000 |
| Total Lines | 255 |
| Code Lines | 169 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public function createAnswersForm($form) |
||
| 30 | { |
||
| 31 | // Getting the exercise list |
||
| 32 | /** @var Exercise $obj_ex */ |
||
| 33 | $obj_ex = Session::read('objExercise'); |
||
| 34 | |||
| 35 | $editor_config = [ |
||
| 36 | 'ToolbarSet' => 'TestProposedAnswer', |
||
| 37 | 'Width' => '100%', |
||
| 38 | 'Height' => '125', |
||
| 39 | ]; |
||
| 40 | |||
| 41 | //this line defines how many questions by default appear when creating a choice question |
||
| 42 | // The previous default value was 2. See task #1759. |
||
| 43 | $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; |
||
| 44 | $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
| 45 | |||
| 46 | $feedback_title = ''; |
||
| 47 | switch ($obj_ex->getFeedbackType()) { |
||
| 48 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 49 | // Scenario |
||
| 50 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 51 | break; |
||
| 52 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 53 | $comment_title = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 54 | |||
| 55 | break; |
||
| 56 | default: |
||
| 57 | $comment_title = '<th width="40%">'.get_lang('Comment').'</th>'; |
||
| 58 | |||
| 59 | break; |
||
| 60 | } |
||
| 61 | |||
| 62 | $html = '<table class="table table-striped table-hover"> |
||
| 63 | <thead> |
||
| 64 | <tr style="text-align: center;"> |
||
| 65 | <th width="5%">'.get_lang('N°').'</th> |
||
| 66 | <th width="5%"> '.get_lang('True').'</th> |
||
| 67 | <th width="40%">'.get_lang('Answer').'</th> |
||
| 68 | '.$comment_title.' |
||
| 69 | <th width="10%">'.get_lang('Score').'</th> |
||
| 70 | </tr> |
||
| 71 | </thead> |
||
| 72 | <tbody>'; |
||
| 73 | |||
| 74 | $form->addHeader(get_lang('Answers')); |
||
| 75 | $form->addHtml($html); |
||
| 76 | |||
| 77 | $defaults = []; |
||
| 78 | $correct = 0; |
||
| 79 | if (!empty($this->id)) { |
||
| 80 | $answer = new Answer($this->id); |
||
| 81 | $answer->read(); |
||
| 82 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
| 83 | $nb_answers = $answer->nbrAnswers; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | $form->addHidden('nb_answers', $nb_answers); |
||
| 87 | |||
| 88 | $obj_ex->setQuestionList(true); |
||
| 89 | $question_list = $obj_ex->getQuestionList(); |
||
| 90 | $select_question = []; |
||
| 91 | $select_question[0] = get_lang('Select target question'); |
||
| 92 | if (is_array($question_list)) { |
||
| 93 | foreach ($question_list as $key => $questionid) { |
||
| 94 | //To avoid warning messages |
||
| 95 | if (!is_numeric($questionid)) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | $question = Question::read($questionid); |
||
| 99 | $questionTitle = strip_tags($question->selectTitle()); |
||
| 100 | $select_question[$questionid] = "Q$key: $questionTitle"; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | $select_question[-1] = get_lang('Exit test'); |
||
| 104 | |||
| 105 | $list = new LearnpathList(api_get_user_id()); |
||
| 106 | $flat_list = $list->get_flat_list(); |
||
| 107 | $select_lp_id = []; |
||
| 108 | $select_lp_id[0] = get_lang('Select target course'); |
||
| 109 | |||
| 110 | foreach ($flat_list as $id => $details) { |
||
| 111 | $select_lp_id[$id] = cut($details['lp_name'], 20); |
||
| 112 | } |
||
| 113 | |||
| 114 | $temp_scenario = []; |
||
| 115 | if ($nb_answers < 1) { |
||
| 116 | $nb_answers = 1; |
||
| 117 | echo Display::return_message(get_lang('You have to create at least one answer')); |
||
| 118 | } |
||
| 119 | |||
| 120 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
| 121 | $form->addHtml('<tr>'); |
||
| 122 | if (isset($answer) && is_object($answer)) { |
||
| 123 | if (isset($answer->correct[$i]) && $answer->correct[$i]) { |
||
| 124 | $correct = $i; |
||
| 125 | } |
||
| 126 | $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : ''; |
||
| 127 | $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : ''; |
||
| 128 | $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0; |
||
| 129 | } else { |
||
| 130 | $defaults['answer[1]'] = get_lang('A then B then C'); |
||
| 131 | $defaults['weighting[1]'] = 10; |
||
| 132 | $defaults['answer[2]'] = get_lang('A then C then B'); |
||
| 133 | $defaults['weighting[2]'] = 0; |
||
| 134 | $temp_scenario['destination'.$i] = ['0']; |
||
| 135 | $temp_scenario['lp'.$i] = ['0']; |
||
| 136 | } |
||
| 137 | $defaults['scenario'] = $temp_scenario; |
||
| 138 | |||
| 139 | $renderer = $form->defaultRenderer(); |
||
| 140 | |||
| 141 | $renderer->setElementTemplate( |
||
| 142 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 143 | 'correct' |
||
| 144 | ); |
||
| 145 | $renderer->setElementTemplate( |
||
| 146 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 147 | 'counter['.$i.']' |
||
| 148 | ); |
||
| 149 | $renderer->setElementTemplate( |
||
| 150 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 151 | 'answer['.$i.']' |
||
| 152 | ); |
||
| 153 | $renderer->setElementTemplate( |
||
| 154 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 155 | 'comment['.$i.']' |
||
| 156 | ); |
||
| 157 | $renderer->setElementTemplate( |
||
| 158 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 159 | 'weighting['.$i.']' |
||
| 160 | ); |
||
| 161 | |||
| 162 | $answerNumber = $form->addText( |
||
| 163 | 'counter['.$i.']', |
||
| 164 | null, |
||
| 165 | false, |
||
| 166 | ['value' => $i] |
||
| 167 | ); |
||
| 168 | $answerNumber->freeze(); |
||
| 169 | |||
| 170 | $form->addElement( |
||
| 171 | 'radio', |
||
| 172 | 'correct', |
||
| 173 | null, |
||
| 174 | null, |
||
| 175 | $i, |
||
| 176 | ['class' => 'checkbox'] |
||
| 177 | ); |
||
| 178 | |||
| 179 | $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config); |
||
| 180 | |||
| 181 | $form->addRule( |
||
| 182 | 'answer['.$i.']', |
||
| 183 | get_lang('Required field'), |
||
| 184 | 'required' |
||
| 185 | ); |
||
| 186 | |||
| 187 | switch ($obj_ex->getFeedbackType()) { |
||
| 188 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 189 | $this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question); |
||
| 190 | |||
| 191 | break; |
||
| 192 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 193 | default: |
||
| 194 | $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config); |
||
| 195 | |||
| 196 | break; |
||
| 197 | } |
||
| 198 | $form->addText('weighting['.$i.']', null, null, ['value' => '0']); |
||
| 199 | $form->addHtml('</tr>'); |
||
| 200 | } |
||
| 201 | |||
| 202 | $form->addHtml('</tbody>'); |
||
| 203 | $form->addHtml('</table>'); |
||
| 204 | |||
| 205 | global $text; |
||
| 206 | $buttonGroup = []; |
||
| 207 | |||
| 208 | if (true == $obj_ex->edit_exercise_in_lp || |
||
|
|
|||
| 209 | (empty($this->exerciseList) && empty($obj_ex->id)) |
||
| 210 | ) { |
||
| 211 | |||
| 212 | if (api_get_setting('enable_quiz_scenario') === 'true' && $obj_ex->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) { |
||
| 213 | $this->addAdaptiveScenarioFields($form, $question_list); |
||
| 214 | } |
||
| 215 | |||
| 216 | //setting the save button here and not in the question class.php |
||
| 217 | $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true); |
||
| 218 | $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true); |
||
| 219 | $buttonGroup[] = $form->addButton( |
||
| 220 | 'submitQuestion', |
||
| 221 | $text, |
||
| 222 | 'check', |
||
| 223 | 'primary', |
||
| 224 | 'default', |
||
| 225 | null, |
||
| 226 | ['id' => 'submit-question'], |
||
| 227 | true |
||
| 228 | ); |
||
| 229 | $form->addGroup($buttonGroup); |
||
| 230 | } |
||
| 231 | |||
| 232 | // We check the first radio button to be sure a radio button will be check |
||
| 233 | if (0 == $correct) { |
||
| 234 | $correct = 1; |
||
| 235 | } |
||
| 236 | |||
| 237 | if (isset($_POST) && isset($_POST['correct'])) { |
||
| 238 | $correct = (int) $_POST['correct']; |
||
| 239 | } |
||
| 240 | |||
| 241 | $defaults['correct'] = $correct; |
||
| 242 | |||
| 243 | if (!empty($this->id)) { |
||
| 244 | |||
| 245 | if (!empty($this->id)) { |
||
| 246 | $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 247 | $res = Database::select( |
||
| 248 | 'destination', |
||
| 249 | $table, |
||
| 250 | ['where' => ['question_id = ? AND quiz_id = ?' => [$this->id, $obj_ex->id]], 'limit' => 1], |
||
| 251 | 'first' |
||
| 252 | ); |
||
| 253 | |||
| 254 | if (!empty($res['destination'])) { |
||
| 255 | $json = json_decode($res['destination'], true); |
||
| 256 | $defaults['scenario_success_selector'] = $json['success'] ?? ''; |
||
| 257 | $defaults['scenario_failure_selector'] = $json['failure'] ?? ''; |
||
| 258 | |||
| 259 | if (str_starts_with($json['success'] ?? '', '/')) { |
||
| 260 | $defaults['scenario_success_selector'] = 'url'; |
||
| 261 | $defaults['scenario_success_url'] = $json['success']; |
||
| 262 | } |
||
| 263 | if (str_starts_with($json['failure'] ?? '', '/')) { |
||
| 264 | $defaults['scenario_failure_selector'] = 'url'; |
||
| 265 | $defaults['scenario_failure_url'] = $json['failure']; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | $form->setDefaults($defaults); |
||
| 270 | } else { |
||
| 271 | if (1 == $this->isContent) { |
||
| 272 | // Default sample content. |
||
| 273 | $form->setDefaults($defaults); |
||
| 274 | } else { |
||
| 275 | $correct = 1; |
||
| 276 | if (isset($_POST) && isset($_POST['correct'])) { |
||
| 277 | $correct = (int) $_POST['correct']; |
||
| 278 | } |
||
| 279 | |||
| 280 | $form->setDefaults(['correct' => $correct]); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | $form->setConstants(['nb_answers' => $nb_answers]); |
||
| 284 | } |
||
| 479 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.