Conditions | 4 |
Paths | 6 |
Total Lines | 148 |
Code Lines | 59 |
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 |
||
31 | public function createAnswersForm($form) |
||
32 | { |
||
33 | $defaults = []; |
||
34 | $defaults['answer'] = get_lang('DefaultTextInBlanks'); |
||
35 | if (!empty($this->iid)) { |
||
36 | $objAnswer = new Answer($this->iid); |
||
37 | $preArray = explode('@@', $objAnswer->selectAnswer(1)); |
||
38 | $defaults['formula'] = array_pop($preArray); |
||
39 | $defaults['answer'] = array_shift($preArray); |
||
40 | $defaults['answer'] = preg_replace("/\[.*\]/", '', $defaults['answer']); |
||
41 | $defaults['weighting'] = $this->weighting; |
||
42 | } |
||
43 | $lowestValue = '1.00'; |
||
44 | $highestValue = '20.00'; |
||
45 | |||
46 | // javascript // |
||
47 | echo '<script> |
||
48 | function parseTextNumber(textNumber, floatValue) { |
||
49 | if (textNumber.indexOf(".") > -1) { |
||
50 | textNumber = parseFloat(textNumber); |
||
51 | floatValue.exists = "true"; |
||
52 | } else { |
||
53 | textNumber = parseInt(textNumber); |
||
54 | } |
||
55 | return textNumber; |
||
56 | } |
||
57 | |||
58 | function updateRandomValue(element) { |
||
59 | // "floatValue" helps to distinguish between an integer (10) and a float with all 0 decimals (10.00) |
||
60 | var floatValue = { exists: "false" }; |
||
61 | var index = (element.name).match(/\[[^\]]*\]/g); |
||
62 | var lowestValue = parseTextNumber(document.getElementById("lowestValue"+index).value, floatValue); |
||
63 | var highestValue = parseTextNumber(document.getElementById("highestValue"+index).value, floatValue); |
||
64 | var result = Math.random() * (highestValue - lowestValue) + lowestValue; |
||
65 | if (floatValue.exists == "true") { |
||
66 | result = parseFloat(result).toFixed(2); |
||
67 | } else { |
||
68 | result = parseInt(result); |
||
69 | } |
||
70 | document.getElementById("randomValue"+index).innerHTML = "'.get_lang("ExampleValue").': " + result; |
||
71 | } |
||
72 | |||
73 | CKEDITOR.on("instanceCreated", function(e) { |
||
74 | if (e.editor.name === "answer") { |
||
75 | e.editor.on("change", updateBlanks); |
||
76 | } |
||
77 | }); |
||
78 | |||
79 | var firstTime = true; |
||
80 | function updateBlanks(e) { |
||
81 | if (firstTime) { |
||
82 | field = document.getElementById("answer"); |
||
83 | var answer = field.value; |
||
84 | } else { |
||
85 | var answer = e.editor.getData(); |
||
86 | } |
||
87 | var blanks = answer.match(/\[[^\]]*\]/g); |
||
88 | var fields = "<div class=\"form-group\"><label class=\"col-sm-2\">'.get_lang('VariableRanges').'</label><div class=\"col-sm-8\"><table>"; |
||
89 | if (blanks!=null) { |
||
90 | if (typeof updateBlanks.randomValues === "undefined") { |
||
91 | updateBlanks.randomValues = []; |
||
92 | } |
||
93 | for (i=0 ; i<blanks.length ; i++){ |
||
94 | if (document.getElementById("lowestValue["+i+"]") && document.getElementById("highestValue["+i+"]")) { |
||
95 | lowestValue = document.getElementById("lowestValue["+i+"]").value; |
||
96 | highestValue = document.getElementById("highestValue["+i+"]").value; |
||
97 | } else { |
||
98 | lowestValue = '.$lowestValue.'.toFixed(2); |
||
99 | highestValue = '.$highestValue.'.toFixed(2); |
||
100 | for (j=0; j<blanks.length; j++) { |
||
101 | updateBlanks.randomValues[j] = parseFloat(Math.random() * (highestValue - lowestValue) + lowestValue).toFixed(2); |
||
102 | } |
||
103 | } |
||
104 | fields += "<tr><td><label>"+blanks[i]+"</label></td><td><input class=\"span1\" style=\"margin-left: 0em;\" size=\"5\" value=\""+lowestValue+"\" type=\"text\" id=\"lowestValue["+i+"]\" name=\"lowestValue["+i+"]\" onblur=\"updateRandomValue(this)\"/></td><td><input class=\"span1\" style=\"margin-left: 0em; width:80px;\" size=\"5\" value=\""+highestValue+"\" type=\"text\" id=\"highestValue["+i+"]\" name=\"highestValue["+i+"]\" onblur=\"updateRandomValue(this)\"/></td><td><label class=\"span3\" id=\"randomValue["+i+"]\"/>'.get_lang('ExampleValue').': "+updateBlanks.randomValues[i]+"</label></td></tr>"; |
||
105 | } |
||
106 | } |
||
107 | document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>"; |
||
108 | if (firstTime) { |
||
109 | firstTime = false; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | window.onload = updateBlanks; |
||
114 | |||
115 | </script>'; |
||
116 | |||
117 | // answer |
||
118 | $form->addElement('label', null, '<br /><br />'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')); |
||
119 | $form->addElement( |
||
120 | 'html_editor', |
||
121 | 'answer', |
||
122 | Display::return_icon('fill_field.png'), |
||
123 | [ |
||
124 | 'id' => 'answer', |
||
125 | 'onkeyup' => 'javascript: updateBlanks(this);', |
||
126 | ], |
||
127 | [ |
||
128 | 'ToolbarSet' => 'TestQuestionDescription', |
||
129 | 'Width' => '100%', |
||
130 | 'Height' => '350', |
||
131 | ] |
||
132 | ); |
||
133 | |||
134 | $form->addRule('answer', get_lang('GiveText'), 'required'); |
||
135 | $form->addRule('answer', get_lang('DefineBlanks'), 'regex', '/\[.*\]/'); |
||
136 | $form->applyFilter('answer', 'attr_on_filter'); |
||
137 | |||
138 | $form->addElement('label', null, get_lang('IfYouWantOnlyIntegerValuesWriteBothLimitsWithoutDecimals')); |
||
139 | $form->addElement('html', '<div id="blanks_weighting"></div>'); |
||
140 | |||
141 | $notationListButton = Display::url( |
||
142 | get_lang('NotationList'), |
||
143 | api_get_path(WEB_CODE_PATH).'exercise/evalmathnotation.php', |
||
144 | [ |
||
145 | 'class' => 'btn btn-info ajax', |
||
146 | 'data-title' => get_lang('NotationList'), |
||
147 | '_target' => '_blank', |
||
148 | ] |
||
149 | ); |
||
150 | $form->addElement( |
||
151 | 'label', |
||
152 | null, |
||
153 | $notationListButton |
||
154 | ); |
||
155 | |||
156 | $form->addElement('text', 'formula', [get_lang('Formula'), get_lang('FormulaExample')], ['id' => 'formula']); |
||
157 | $form->addRule('formula', get_lang('GiveFormula'), 'required'); |
||
158 | |||
159 | $form->addElement('text', 'weighting', get_lang('Weighting'), ['id' => 'weighting']); |
||
160 | $form->setDefaults(['weighting' => '10']); |
||
161 | |||
162 | $form->addElement('text', 'answerVariations', get_lang('AnswerVariations')); |
||
163 | $form->addRule( |
||
164 | 'answerVariations', |
||
165 | get_lang('GiveAnswerVariations'), |
||
166 | 'required' |
||
167 | ); |
||
168 | $form->setDefaults(['answerVariations' => '1']); |
||
169 | |||
170 | global $text; |
||
171 | // setting the save button here and not in the question class.php |
||
172 | $form->addButtonSave($text, 'submitQuestion'); |
||
173 | |||
174 | if (!empty($this->iid)) { |
||
175 | $form->setDefaults($defaults); |
||
176 | } else { |
||
177 | if ($this->isContent == 1) { |
||
178 | $form->setDefaults($defaults); |
||
179 | } |
||
288 |