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