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