| Conditions | 26 |
| Paths | > 20000 |
| Total Lines | 222 |
| Code Lines | 143 |
| 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 | $defaults = []; |
||
| 32 | $nb_matches = $nb_options = 2; |
||
| 33 | $matches = []; |
||
| 34 | $answer = null; |
||
| 35 | $counter = 1; |
||
| 36 | |||
| 37 | if (isset($this->id)) { |
||
| 38 | $answer = new Answer($this->id); |
||
| 39 | $answer->read(); |
||
| 40 | if ($answer->nbrAnswers > 0) { |
||
| 41 | for ($i = 1; $i <= $answer->nbrAnswers; $i++) { |
||
| 42 | $correct = $answer->isCorrect($i); |
||
| 43 | if (empty($correct)) { |
||
| 44 | $matches[$answer->selectAutoId($i)] = chr(64 + $counter); |
||
| 45 | $counter++; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($form->isSubmitted()) { |
||
| 52 | $nb_matches = $form->getSubmitValue('nb_matches'); |
||
| 53 | $nb_options = $form->getSubmitValue('nb_options'); |
||
| 54 | if (isset($_POST['lessOptions'])) { |
||
| 55 | $nb_matches--; |
||
| 56 | $nb_options--; |
||
| 57 | } |
||
| 58 | if (isset($_POST['moreOptions'])) { |
||
| 59 | $nb_matches++; |
||
| 60 | $nb_options++; |
||
| 61 | } |
||
| 62 | } elseif (!empty($this->id)) { |
||
| 63 | if ($answer->nbrAnswers > 0) { |
||
| 64 | $nb_matches = $nb_options = 0; |
||
| 65 | for ($i = 1; $i <= $answer->nbrAnswers; $i++) { |
||
| 66 | if ($answer->isCorrect($i)) { |
||
| 67 | $nb_matches++; |
||
| 68 | $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i); |
||
| 69 | if (MATCHING === $this->type) { |
||
| 70 | $defaults['weighting['.$nb_matches.']'] = float_format($answer->selectWeighting($i), 1); |
||
| 71 | } |
||
| 72 | $defaults['matches['.$nb_matches.']'] = $answer->correct[$i]; |
||
| 73 | } else { |
||
| 74 | $nb_options++; |
||
| 75 | $defaults['option['.$nb_options.']'] = $answer->selectAnswer($i); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | $defaults['answer[1]'] = get_lang('First step'); |
||
| 81 | $defaults['answer[2]'] = get_lang('Second step'); |
||
| 82 | $defaults['matches[2]'] = '2'; |
||
| 83 | $defaults['option[1]'] = get_lang('Note down the address'); |
||
| 84 | $defaults['option[2]'] = get_lang('Contact the emergency services'); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (empty($matches)) { |
||
| 88 | for ($i = 1; $i <= $nb_options; $i++) { |
||
| 89 | // fill the array with A, B, C..... |
||
| 90 | $matches[$i] = chr(64 + $i); |
||
| 91 | } |
||
| 92 | } else { |
||
| 93 | for ($i = $counter; $i <= $nb_options; $i++) { |
||
| 94 | // fill the array with A, B, C..... |
||
| 95 | $matches[$i] = chr(64 + $i); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $form->addElement('hidden', 'nb_matches', $nb_matches); |
||
| 100 | $form->addElement('hidden', 'nb_options', $nb_options); |
||
| 101 | |||
| 102 | $thWeighting = ''; |
||
| 103 | if (MATCHING === $this->type) { |
||
| 104 | $thWeighting = '<th width="10%">'.get_lang('Score').'</th>'; |
||
| 105 | } |
||
| 106 | |||
| 107 | // DISPLAY MATCHES |
||
| 108 | $html = '<table class="table table-striped table-hover"> |
||
| 109 | <thead> |
||
| 110 | <tr> |
||
| 111 | <th width="5%">'.get_lang('N°').'</th> |
||
| 112 | <th width="70%">'.get_lang('Answer').'</th> |
||
| 113 | <th width="15%">'.get_lang('Matches To').'</th> |
||
| 114 | '.$thWeighting.' |
||
| 115 | </tr> |
||
| 116 | </thead> |
||
| 117 | <tbody>'; |
||
| 118 | |||
| 119 | $form->addHeader(get_lang('Match them')); |
||
| 120 | $form->addHtml($html); |
||
| 121 | |||
| 122 | if ($nb_matches < 1) { |
||
| 123 | $nb_matches = 1; |
||
| 124 | Display::addFlash( |
||
| 125 | Display::return_message(get_lang('You have to create at least one answer')) |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | $editorConfig = [ |
||
| 130 | 'ToolbarSet' => 'TestMatching', |
||
| 131 | 'Width' => '100%', |
||
| 132 | 'Height' => '125', |
||
| 133 | ]; |
||
| 134 | |||
| 135 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
| 136 | $renderer = &$form->defaultRenderer(); |
||
| 137 | $renderer->setElementTemplate( |
||
| 138 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 139 | "answer[$i]" |
||
| 140 | ); |
||
| 141 | |||
| 142 | $renderer->setElementTemplate( |
||
| 143 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 144 | "matches[$i]" |
||
| 145 | ); |
||
| 146 | |||
| 147 | $renderer->setElementTemplate( |
||
| 148 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 149 | "weighting[$i]" |
||
| 150 | ); |
||
| 151 | |||
| 152 | $form->addHtml('<tr>'); |
||
| 153 | $form->addHtml("<td>$i</td>"); |
||
| 154 | $form->addHtmlEditor( |
||
| 155 | "answer[$i]", |
||
| 156 | null, |
||
| 157 | null, |
||
| 158 | false, |
||
| 159 | $editorConfig |
||
| 160 | ); |
||
| 161 | $form->addSelect( |
||
| 162 | "matches[$i]", |
||
| 163 | null, |
||
| 164 | $matches, |
||
| 165 | ['id' => 'matches_'.$i] |
||
| 166 | ); |
||
| 167 | if (MATCHING === $this->type) { |
||
| 168 | $form->addText( |
||
| 169 | "weighting[$i]", |
||
| 170 | null, |
||
| 171 | true, |
||
| 172 | ['id' => 'weighting_'.$i, 'value' => 10] |
||
| 173 | ); |
||
| 174 | } else { |
||
| 175 | $form->addHidden("weighting[$i]", "0"); |
||
| 176 | } |
||
| 177 | $form->addHtml('</tr>'); |
||
| 178 | } |
||
| 179 | |||
| 180 | $form->addHtml('</tbody></table>'); |
||
| 181 | |||
| 182 | // DISPLAY OPTIONS |
||
| 183 | $html = '<table class="table table-striped table-hover"> |
||
| 184 | <thead> |
||
| 185 | <tr> |
||
| 186 | <th width="15%">'.get_lang('N°').'</th> |
||
| 187 | <th width="85%">'.get_lang('Answer').'</th> |
||
| 188 | </tr> |
||
| 189 | </thead> |
||
| 190 | <tbody>'; |
||
| 191 | |||
| 192 | $form->addHtml($html); |
||
| 193 | |||
| 194 | if ($nb_options < 1) { |
||
| 195 | $nb_options = 1; |
||
| 196 | Display::addFlash( |
||
| 197 | Display::return_message(get_lang('You have to create at least one answer')) |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | |||
| 201 | for ($i = 1; $i <= $nb_options; $i++) { |
||
| 202 | $renderer = &$form->defaultRenderer(); |
||
| 203 | $renderer->setElementTemplate( |
||
| 204 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 205 | "option[$i]" |
||
| 206 | ); |
||
| 207 | |||
| 208 | $form->addHtml('<tr>'); |
||
| 209 | $form->addHtml('<td>'.chr(64 + $i).'</td>'); |
||
| 210 | $form->addHtmlEditor( |
||
| 211 | "option[$i]", |
||
| 212 | null, |
||
| 213 | null, |
||
| 214 | false, |
||
| 215 | $editorConfig |
||
| 216 | ); |
||
| 217 | |||
| 218 | $form->addHtml('</tr>'); |
||
| 219 | } |
||
| 220 | |||
| 221 | $form->addHtml('</table>'); |
||
| 222 | |||
| 223 | if (MATCHING_COMBINATION === $this->type) { |
||
| 224 | $form->addText('questionWeighting', get_lang('Score'), true, ['value' => 10]); |
||
| 225 | |||
| 226 | if (!empty($this->id)) { |
||
| 227 | $defaults['questionWeighting'] = $this->weighting; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | global $text; |
||
| 232 | $group = []; |
||
| 233 | // setting the save button here and not in the question class.php |
||
| 234 | $group[] = $form->addButtonDelete(get_lang('Remove element'), 'lessOptions', true); |
||
| 235 | $group[] = $form->addButtonCreate(get_lang('Add element'), 'moreOptions', true); |
||
| 236 | $group[] = $form->addButtonSave($text, 'submitQuestion', true); |
||
| 237 | $form->addGroup($group); |
||
| 238 | |||
| 239 | if (!empty($this->id)) { |
||
| 240 | $form->setDefaults($defaults); |
||
| 241 | } else { |
||
| 242 | if (1 == $this->isContent) { |
||
| 243 | $form->setDefaults($defaults); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | $form->setConstants( |
||
| 248 | [ |
||
| 249 | 'nb_matches' => $nb_matches, |
||
| 250 | 'nb_options' => $nb_options, |
||
| 251 | ] |
||
| 342 |