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