| Conditions | 41 |
| Paths | > 20000 |
| Total Lines | 247 |
| Code Lines | 171 |
| 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 |
||
| 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 | } |
||
| 589 |