| Conditions | 17 |
| Paths | 504 |
| Total Lines | 143 |
| Code Lines | 91 |
| 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 |
||
| 22 | public function createAnswersForm($form) |
||
| 23 | { |
||
| 24 | $defaults = []; |
||
| 25 | $nb_matches = $nb_options = 2; |
||
| 26 | $matches = []; |
||
| 27 | $answer = null; |
||
| 28 | if ($form->isSubmitted()) { |
||
| 29 | $nb_matches = $form->getSubmitValue('nb_matches'); |
||
| 30 | $nb_options = $form->getSubmitValue('nb_options'); |
||
| 31 | |||
| 32 | if (isset($_POST['lessMatches'])) { |
||
| 33 | $nb_matches--; |
||
| 34 | } |
||
| 35 | |||
| 36 | if (isset($_POST['moreMatches'])) { |
||
| 37 | $nb_matches++; |
||
| 38 | } |
||
| 39 | |||
| 40 | if (isset($_POST['lessOptions'])) { |
||
| 41 | $nb_options--; |
||
| 42 | } |
||
| 43 | |||
| 44 | if (isset($_POST['moreOptions'])) { |
||
| 45 | $nb_options++; |
||
| 46 | } |
||
| 47 | } elseif (!empty($this->id)) { |
||
| 48 | $defaults['orientation'] = in_array($this->extra, ['h', 'v']) ? $this->extra : 'h'; |
||
| 49 | |||
| 50 | $answer = new Answer($this->id); |
||
| 51 | $answer->read(); |
||
| 52 | |||
| 53 | if ($answer->nbrAnswers > 0) { |
||
| 54 | $nb_matches = $nb_options = 0; |
||
| 55 | for ($i = 1; $i <= $answer->nbrAnswers; $i++) { |
||
| 56 | if ($answer->isCorrect($i)) { |
||
| 57 | $nb_matches++; |
||
| 58 | $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i); |
||
| 59 | $defaults['weighting['.$nb_matches.']'] = float_format($answer->selectWeighting($i), 1); |
||
| 60 | $answerInfo = $answer->getAnswerByAutoId($answer->correct[$i]); |
||
| 61 | $defaults['matches['.$nb_matches.']'] = isset($answerInfo['answer']) ? $answerInfo['answer'] : ''; |
||
| 62 | } else { |
||
| 63 | $nb_options++; |
||
| 64 | $defaults['option['.$nb_options.']'] = $answer->selectAnswer($i); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $defaults['answer[1]'] = get_lang('First step'); |
||
| 70 | $defaults['answer[2]'] = get_lang('Second step'); |
||
| 71 | $defaults['matches[2]'] = '2'; |
||
| 72 | $defaults['option[1]'] = get_lang('Note down the address'); |
||
| 73 | $defaults['option[2]'] = get_lang('Contact the emergency services'); |
||
| 74 | $defaults['orientation'] = 'h'; |
||
| 75 | } |
||
| 76 | |||
| 77 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
| 78 | $matches[$i] = $i; |
||
| 79 | } |
||
| 80 | |||
| 81 | $form->addElement('hidden', 'nb_matches', $nb_matches); |
||
| 82 | $form->addElement('hidden', 'nb_options', $nb_options); |
||
| 83 | |||
| 84 | $form->addRadio( |
||
| 85 | 'orientation', |
||
| 86 | get_lang('Choose orientation'), |
||
| 87 | ['h' => get_lang('Horizontal'), 'v' => get_lang('Vertical')] |
||
| 88 | ); |
||
| 89 | |||
| 90 | // DISPLAY MATCHES |
||
| 91 | $html = '<table class="table table-striped table-hover"> |
||
| 92 | <thead> |
||
| 93 | <tr> |
||
| 94 | <th width="85%">'.get_lang('Answer').'</th> |
||
| 95 | <th width="15%">'.get_lang('Matches To').'</th> |
||
| 96 | <th width="10">'.get_lang('Score').'</th> |
||
| 97 | </tr> |
||
| 98 | </thead> |
||
| 99 | <tbody>'; |
||
| 100 | |||
| 101 | $form->addHeader(get_lang('Match them')); |
||
| 102 | $form->addHtml($html); |
||
| 103 | |||
| 104 | if ($nb_matches < 1) { |
||
| 105 | $nb_matches = 1; |
||
| 106 | echo Display::return_message(get_lang('You have to create at least one answer'), 'normal'); |
||
| 107 | } |
||
| 108 | |||
| 109 | for ($i = 1; $i <= $nb_matches; $i++) { |
||
| 110 | $renderer = &$form->defaultRenderer(); |
||
| 111 | $renderer->setElementTemplate( |
||
| 112 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 113 | "answer[$i]" |
||
| 114 | ); |
||
| 115 | |||
| 116 | $renderer->setElementTemplate( |
||
| 117 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 118 | "matches[$i]" |
||
| 119 | ); |
||
| 120 | |||
| 121 | $renderer->setElementTemplate( |
||
| 122 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>', |
||
| 123 | "weighting[$i]" |
||
| 124 | ); |
||
| 125 | |||
| 126 | $form->addHtml('<tr>'); |
||
| 127 | $form->addText("answer[$i]", null); |
||
| 128 | $form->addSelect("matches[$i]", null, $matches); |
||
| 129 | $form->addText("weighting[$i]", null, true, ['value' => 10, 'style' => 'width: 60px;']); |
||
| 130 | $form->addHtml('</tr>'); |
||
| 131 | } |
||
| 132 | |||
| 133 | $form->addHtml('</tbody></table>'); |
||
| 134 | |||
| 135 | $renderer->setElementTemplate( |
||
|
|
|||
| 136 | '<div class="form-group"><div class="col-sm-offset-2">{element}', |
||
| 137 | 'lessMatches' |
||
| 138 | ); |
||
| 139 | $renderer->setElementTemplate('{element}</div></div>', 'moreMatches'); |
||
| 140 | |||
| 141 | global $text; |
||
| 142 | |||
| 143 | $group = [ |
||
| 144 | $form->addButtonDelete(get_lang('Remove element'), 'lessMatches', true), |
||
| 145 | $form->addButtonCreate(get_lang('Add element'), 'moreMatches', true), |
||
| 146 | $form->addButtonSave($text, 'submitQuestion', true), |
||
| 147 | ]; |
||
| 148 | |||
| 149 | $form->addGroup($group); |
||
| 150 | |||
| 151 | if (!empty($this->id)) { |
||
| 152 | $form->setDefaults($defaults); |
||
| 153 | } else { |
||
| 154 | $form->setDefaults(['orientation' => 'h']); |
||
| 155 | |||
| 156 | if (1 == $this->isContent) { |
||
| 157 | $form->setDefaults($defaults); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $form->setConstants( |
||
| 162 | [ |
||
| 163 | 'nb_matches' => $nb_matches, |
||
| 164 | 'nb_options' => $nb_options, |
||
| 165 | ] |
||
| 221 |