| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 210 |
| Code Lines | 139 |
| 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 |
||
| 31 | public function createAnswersForm($form) |
||
| 32 | { |
||
| 33 | $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4; |
||
| 34 | // The previous default value was 2. See task #1759. |
||
| 35 | $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
| 36 | |||
| 37 | $course_id = api_get_course_int_id(); |
||
| 38 | $obj_ex = Session::read('objExercise'); |
||
| 39 | $renderer = &$form->defaultRenderer(); |
||
| 40 | $defaults = []; |
||
| 41 | |||
| 42 | $html = '<table class="table table-striped table-hover">'; |
||
| 43 | $html .= '<thead>'; |
||
| 44 | $html .= '<tr>'; |
||
| 45 | $html .= '<th>'.get_lang('N°').'</th>'; |
||
| 46 | $html .= '<th>'.get_lang('True').'</th>'; |
||
| 47 | $html .= '<th>'.get_lang('False').'</th>'; |
||
| 48 | $html .= '<th>'.get_lang('Answer').'</th>'; |
||
| 49 | |||
| 50 | // show column comment when feedback is enable |
||
| 51 | if (EXERCISE_FEEDBACK_TYPE_EXAM != $obj_ex->getFeedbackType()) { |
||
| 52 | $html .= '<th>'.get_lang('Comment').'</th>'; |
||
| 53 | } |
||
| 54 | |||
| 55 | $html .= '</tr>'; |
||
| 56 | $html .= '</thead>'; |
||
| 57 | $html .= '<tbody>'; |
||
| 58 | |||
| 59 | $form->addHeader(get_lang('Answers')); |
||
| 60 | $form->addHtml($html); |
||
| 61 | |||
| 62 | $answer = null; |
||
| 63 | |||
| 64 | if (!empty($this->id)) { |
||
| 65 | $answer = new Answer($this->id); |
||
| 66 | $answer->read(); |
||
| 67 | |||
| 68 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
| 69 | $nb_answers = $answer->nbrAnswers; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | $form->addElement('hidden', 'nb_answers'); |
||
| 74 | if ($nb_answers < 1) { |
||
| 75 | $nb_answers = 1; |
||
| 76 | echo Display::return_message(get_lang('You have to create at least one answer')); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Can be more options |
||
| 80 | $optionData = Question::readQuestionOption($this->id, $course_id); |
||
| 81 | |||
| 82 | for ($i = 1; $i <= $nb_answers; $i++) { |
||
| 83 | $form->addHtml('<tr>'); |
||
| 84 | |||
| 85 | $renderer->setElementTemplate( |
||
| 86 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 87 | 'correct['.$i.']' |
||
| 88 | ); |
||
| 89 | $renderer->setElementTemplate( |
||
| 90 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 91 | 'counter['.$i.']' |
||
| 92 | ); |
||
| 93 | $renderer->setElementTemplate( |
||
| 94 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 95 | 'answer['.$i.']' |
||
| 96 | ); |
||
| 97 | $renderer->setElementTemplate( |
||
| 98 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 99 | 'comment['.$i.']' |
||
| 100 | ); |
||
| 101 | |||
| 102 | $answer_number = $form->addElement( |
||
| 103 | 'text', |
||
| 104 | 'counter['.$i.']', |
||
| 105 | null, |
||
| 106 | 'value="'.$i.'"' |
||
| 107 | ); |
||
| 108 | |||
| 109 | $answer_number->freeze(); |
||
| 110 | |||
| 111 | if (is_object($answer)) { |
||
| 112 | $defaults['answer['.$i.']'] = $answer->answer[$i]; |
||
| 113 | $defaults['comment['.$i.']'] = $answer->comment[$i]; |
||
| 114 | $correct = $answer->correct[$i]; |
||
| 115 | $defaults['correct['.$i.']'] = $correct; |
||
| 116 | |||
| 117 | $j = 1; |
||
| 118 | if (!empty($optionData)) { |
||
| 119 | foreach ($optionData as $id => $data) { |
||
| 120 | $rdoCorrect = $form->addElement('radio', 'correct['.$i.']', null, null, $id); |
||
| 121 | |||
| 122 | if (isset($_POST['correct']) && isset($_POST['correct'][$i]) && $id == $_POST['correct'][$i]) { |
||
| 123 | $rdoCorrect->setValue(Security::remove_XSS($_POST['correct'][$i])); |
||
| 124 | } |
||
| 125 | $j++; |
||
| 126 | if (3 == $j) { |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $form->addElement('radio', 'correct['.$i.']', null, null, 1); |
||
| 133 | $form->addElement('radio', 'correct['.$i.']', null, null, 2); |
||
| 134 | |||
| 135 | $defaults['answer['.$i.']'] = ''; |
||
| 136 | $defaults['comment['.$i.']'] = ''; |
||
| 137 | $defaults['correct['.$i.']'] = ''; |
||
| 138 | } |
||
| 139 | |||
| 140 | $form->addHtmlEditor( |
||
| 141 | "answer[$i]", |
||
| 142 | get_lang('Required field'), |
||
| 143 | true, |
||
| 144 | false, |
||
| 145 | ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'] |
||
| 146 | ); |
||
| 147 | |||
| 148 | if (isset($_POST['answer']) && isset($_POST['answer'][$i])) { |
||
| 149 | $form->getElement("answer[$i]")->setValue(Security::remove_XSS($_POST['answer'][$i])); |
||
| 150 | } |
||
| 151 | // show comment when feedback is enable |
||
| 152 | if (EXERCISE_FEEDBACK_TYPE_EXAM != $obj_ex->getFeedbackType()) { |
||
| 153 | $txtComment = $form->addElement( |
||
| 154 | 'html_editor', |
||
| 155 | 'comment['.$i.']', |
||
| 156 | null, |
||
| 157 | [], |
||
| 158 | [ |
||
| 159 | 'ToolbarSet' => 'TestProposedAnswer', |
||
| 160 | 'Width' => '100%', |
||
| 161 | 'Height' => '100', |
||
| 162 | ] |
||
| 163 | ); |
||
| 164 | if (isset($_POST['comment']) && isset($_POST['comment'][$i])) { |
||
| 165 | $txtComment->setValue(Security::remove_XSS($_POST['comment'][$i])); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | $form->addHtml('</tr>'); |
||
| 170 | } |
||
| 171 | |||
| 172 | $form->addHtml('</tbody></table>'); |
||
| 173 | |||
| 174 | $correctInputTemplate = '<div class="form-group">'; |
||
| 175 | $correctInputTemplate .= '<label class="col-sm-2 control-label">'; |
||
| 176 | $correctInputTemplate .= '<span class="form_required">*</span>'.get_lang('Score'); |
||
| 177 | $correctInputTemplate .= '</label>'; |
||
| 178 | $correctInputTemplate .= '<div class="col-sm-8">'; |
||
| 179 | $correctInputTemplate .= '<table>'; |
||
| 180 | $correctInputTemplate .= '<tr>'; |
||
| 181 | $correctInputTemplate .= '<td>'; |
||
| 182 | $correctInputTemplate .= get_lang('Correct').'{element}'; |
||
| 183 | $correctInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->'; |
||
| 184 | $correctInputTemplate .= '</td>'; |
||
| 185 | |||
| 186 | $wrongInputTemplate = '<td>'; |
||
| 187 | $wrongInputTemplate .= get_lang('Wrong').'{element}'; |
||
| 188 | $wrongInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->'; |
||
| 189 | $wrongInputTemplate .= '</td>'; |
||
| 190 | |||
| 191 | $doubtScoreInputTemplate = '<td>'.get_lang('Don\'t know').'<br>{element}'; |
||
| 192 | $doubtScoreInputTemplate .= '<!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->'; |
||
| 193 | $doubtScoreInputTemplate .= '</td>'; |
||
| 194 | $doubtScoreInputTemplate .= '</tr>'; |
||
| 195 | $doubtScoreInputTemplate .= '</table>'; |
||
| 196 | $doubtScoreInputTemplate .= '</div>'; |
||
| 197 | $doubtScoreInputTemplate .= '</div>'; |
||
| 198 | |||
| 199 | $renderer->setElementTemplate($correctInputTemplate, 'option[1]'); |
||
| 200 | $renderer->setElementTemplate($wrongInputTemplate, 'option[2]'); |
||
| 201 | $renderer->setElementTemplate($doubtScoreInputTemplate, 'option[3]'); |
||
| 202 | |||
| 203 | // 3 scores |
||
| 204 | $form->addElement('text', 'option[1]', get_lang('Correct'), ['class' => 'span1', 'value' => '1']); |
||
| 205 | $form->addElement('text', 'option[2]', get_lang('Wrong'), ['class' => 'span1', 'value' => '-0.5']); |
||
| 206 | $form->addElement('text', 'option[3]', get_lang('Don\'t know'), ['class' => 'span1', 'value' => '0']); |
||
| 207 | |||
| 208 | $form->addRule('option[1]', get_lang('Required field'), 'required'); |
||
| 209 | $form->addRule('option[2]', get_lang('Required field'), 'required'); |
||
| 210 | $form->addRule('option[3]', get_lang('Required field'), 'required'); |
||
| 211 | |||
| 212 | $form->addElement('hidden', 'options_count', 3); |
||
| 213 | |||
| 214 | // Extra values True, false, Dont known |
||
| 215 | if (!empty($this->extra)) { |
||
| 216 | $scores = explode(':', $this->extra); |
||
| 217 | |||
| 218 | if (!empty($scores)) { |
||
| 219 | $txtOption1->setValue($scores[0]); |
||
|
|
|||
| 220 | $txtOption2->setValue($scores[1]); |
||
| 221 | $txtOption3->setValue($scores[2]); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | global $text; |
||
| 226 | if (true == $obj_ex->edit_exercise_in_lp || |
||
| 227 | (empty($this->exerciseList) && empty($obj_ex->id)) |
||
| 228 | ) { |
||
| 229 | // setting the save button here and not in the question class.php |
||
| 230 | $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true); |
||
| 231 | $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true); |
||
| 232 | $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true); |
||
| 233 | |||
| 234 | $form->addGroup($buttonGroup); |
||
| 235 | } |
||
| 236 | |||
| 237 | if (!empty($this->id) && !$form->isSubmitted()) { |
||
| 238 | $form->setDefaults($defaults); |
||
| 239 | } |
||
| 240 | $form->setConstants(['nb_answers' => $nb_answers]); |
||
| 241 | } |
||
| 347 |