| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 298 |
| Code Lines | 174 |
| 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 |
||
| 32 | public function createAnswersForm($form) |
||
| 33 | { |
||
| 34 | $objExercise = Session::read('objExercise'); |
||
| 35 | $editorConfig = [ |
||
| 36 | 'ToolbarSet' => 'TestProposedAnswer', |
||
| 37 | 'Width' => '100%', |
||
| 38 | 'Height' => '125', |
||
| 39 | ]; |
||
| 40 | |||
| 41 | //this line defines how many questions by default appear when creating a choice question |
||
| 42 | // The previous default value was 2. See task #1759. |
||
| 43 | $numberAnswers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4; |
||
| 44 | $numberAnswers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0)); |
||
| 45 | |||
| 46 | $feedbackTitle = ''; |
||
| 47 | switch ($objExercise->getFeedbackType()) { |
||
| 48 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 49 | // Scenario |
||
| 50 | $commentTitle = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 51 | $feedbackTitle = '<th width="20%">'.get_lang('Scenario').'</th>'; |
||
| 52 | break; |
||
| 53 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 54 | $commentTitle = '<th width="20%">'.get_lang('Comment').'</th>'; |
||
| 55 | break; |
||
| 56 | default: |
||
| 57 | $commentTitle = '<th width="40%">'.get_lang('Comment').'</th>'; |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | |||
| 61 | $html = '<div class="alert alert-success" role="alert">'. |
||
| 62 | get_lang('UniqueAnswerImagePreferredSize200x150').'</div>'; |
||
| 63 | |||
| 64 | $zoomOptions = api_get_configuration_value('quiz_image_zoom'); |
||
| 65 | if (isset($zoomOptions['options'])) { |
||
| 66 | $finderFolder = api_get_path(WEB_PATH).'vendor/studio-42/elfinder/'; |
||
| 67 | $html .= '<!-- elFinder CSS (REQUIRED) -->'; |
||
| 68 | $html .= '<link rel="stylesheet" type="text/css" media="screen" |
||
| 69 | href="'.$finderFolder.'css/elfinder.full.css">'; |
||
| 70 | $html .= '<link rel="stylesheet" type="text/css" media="screen" href="'.$finderFolder.'css/theme.css">'; |
||
| 71 | $html .= '<!-- elFinder JS (REQUIRED) -->'; |
||
| 72 | $html .= '<script type="text/javascript" src="'.$finderFolder.'js/elfinder.full.js"></script>'; |
||
| 73 | $html .= '<!-- elFinder translation (OPTIONAL) -->'; |
||
| 74 | $language = 'en'; |
||
| 75 | $platformLanguage = api_get_interface_language(); |
||
| 76 | $iso = api_get_language_isocode($platformLanguage); |
||
| 77 | $filePart = "vendor/studio-42/elfinder/js/i18n/elfinder.$iso.js"; |
||
| 78 | $file = api_get_path(SYS_PATH).$filePart; |
||
| 79 | $includeFile = ''; |
||
| 80 | if (file_exists($file)) { |
||
| 81 | $includeFile = '<script type="text/javascript" src="'.api_get_path(WEB_PATH).$filePart.'"></script>'; |
||
| 82 | $language = $iso; |
||
| 83 | } |
||
| 84 | $html .= $includeFile; |
||
| 85 | |||
| 86 | $html .= '<script type="text/javascript" charset="utf-8"> |
||
| 87 | $(function() { |
||
| 88 | $(".add_img_link").on("click", function(e){ |
||
| 89 | e.preventDefault(); |
||
| 90 | e.stopPropagation(); |
||
| 91 | |||
| 92 | var name = $(this).prop("name"); |
||
| 93 | var id = parseInt(name.match(/[0-9]+/)); |
||
| 94 | |||
| 95 | $([document.documentElement, document.body]).animate({ |
||
| 96 | scrollTop: $("#elfinder").offset().top |
||
| 97 | }, 1000); |
||
| 98 | |||
| 99 | var elf = $("#elfinder").elfinder({ |
||
| 100 | url : "'.api_get_path(WEB_LIBRARY_PATH).'elfinder/connectorAction.php?'.api_get_cidreq().'", |
||
| 101 | getFileCallback: function(file) { |
||
| 102 | var filePath = file; //file contains the relative url. |
||
| 103 | var imageZoom = filePath.url; |
||
| 104 | var iname = "answer["+id+"]"; |
||
| 105 | |||
| 106 | CKEDITOR.instances[iname].insertHtml(\' |
||
| 107 | <img |
||
| 108 | id="zoom_picture" |
||
| 109 | class="zoom_picture" |
||
| 110 | src="\'+imageZoom+\'" |
||
| 111 | data-zoom-image="\'+imageZoom+\'" |
||
| 112 | width="200px" |
||
| 113 | height="150px" |
||
| 114 | />\'); |
||
| 115 | |||
| 116 | $("#elfinder").elfinder("destroy"); //close the window after image is selected |
||
| 117 | }, |
||
| 118 | startPathHash: "l2_Lw", // Sets the course driver as default |
||
| 119 | resizable: false, |
||
| 120 | lang: "'.$language.'" |
||
| 121 | }).elfinder("instance"+id); |
||
| 122 | }); |
||
| 123 | }); |
||
| 124 | </script>'; |
||
| 125 | $html .= '<div id="elfinder"></div>'; |
||
| 126 | } |
||
| 127 | |||
| 128 | $html .= '<table class="table table-striped table-hover"> |
||
| 129 | <thead> |
||
| 130 | <tr style="text-align: center;"> |
||
| 131 | <th width="10">'.get_lang('Number').'</th> |
||
| 132 | <th>'.get_lang('True').'</th> |
||
| 133 | <th>'.get_lang('Answer').'</th> |
||
| 134 | '.$commentTitle.' |
||
| 135 | '.$feedbackTitle.' |
||
| 136 | <th width="15">'.get_lang('Weighting').'</th> |
||
| 137 | </tr> |
||
| 138 | </thead> |
||
| 139 | <tbody>'; |
||
| 140 | |||
| 141 | $form->addHeader(get_lang('Answers')); |
||
| 142 | $form->addHtml($html); |
||
| 143 | |||
| 144 | $defaults = []; |
||
| 145 | $correct = 0; |
||
| 146 | |||
| 147 | if (!empty($this->iid)) { |
||
| 148 | $answer = new Answer($this->iid); |
||
| 149 | $answer->read(); |
||
| 150 | |||
| 151 | if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) { |
||
| 152 | $numberAnswers = $answer->nbrAnswers; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $form->addElement('hidden', 'nb_answers'); |
||
| 157 | |||
| 158 | //Feedback SELECT |
||
| 159 | $questionList = $objExercise->selectQuestionList(); |
||
| 160 | $selectQuestion = []; |
||
| 161 | $selectQuestion[0] = get_lang('SelectTargetQuestion'); |
||
| 162 | |||
| 163 | if (is_array($questionList)) { |
||
| 164 | foreach ($questionList as $key => $questionid) { |
||
| 165 | //To avoid warning messages |
||
| 166 | if (!is_numeric($questionid)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | $question = Question::read($questionid); |
||
| 171 | $questionTitle = strip_tags($question->selectTitle()); |
||
| 172 | $selectQuestion[$questionid] = "Q$key: $questionTitle"; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | $selectQuestion[-1] = get_lang('ExitTest'); |
||
| 177 | |||
| 178 | $list = new LearnpathList(api_get_user_id()); |
||
| 179 | $flatList = $list->get_flat_list(); |
||
| 180 | $selectLpId = []; |
||
| 181 | $selectLpId[0] = get_lang('SelectTargetLP'); |
||
| 182 | |||
| 183 | foreach ($flatList as $id => $details) { |
||
| 184 | $selectLpId[$id] = cut($details['lp_name'], 20); |
||
| 185 | } |
||
| 186 | |||
| 187 | $tempScenario = []; |
||
| 188 | if ($numberAnswers < 1) { |
||
| 189 | $numberAnswers = 1; |
||
| 190 | echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer')); |
||
| 191 | } |
||
| 192 | |||
| 193 | for ($i = 1; $i <= $numberAnswers; $i++) { |
||
| 194 | $form->addHtml('<tr>'); |
||
| 195 | if (isset($answer) && is_object($answer)) { |
||
| 196 | if ($answer->correct[$i]) { |
||
| 197 | $correct = $i; |
||
| 198 | } |
||
| 199 | |||
| 200 | $defaults['answer['.$i.']'] = $answer->answer[$i]; |
||
| 201 | $defaults['comment['.$i.']'] = $answer->comment[$i]; |
||
| 202 | $defaults['weighting['.$i.']'] = float_format( |
||
| 203 | $answer->weighting[$i], |
||
| 204 | 1 |
||
| 205 | ); |
||
| 206 | |||
| 207 | $itemList = explode('@@', $answer->destination[$i]); |
||
| 208 | |||
| 209 | $try = $itemList[0]; |
||
| 210 | $lp = $itemList[1]; |
||
| 211 | $listDestination = $itemList[2]; |
||
| 212 | $url = $itemList[3]; |
||
| 213 | |||
| 214 | $tryResult = 0; |
||
| 215 | if (0 != $try) { |
||
| 216 | $tryResult = 1; |
||
| 217 | } |
||
| 218 | |||
| 219 | $urlResult = ''; |
||
| 220 | if (0 != $url) { |
||
| 221 | $urlResult = $url; |
||
| 222 | } |
||
| 223 | |||
| 224 | $tempScenario['url'.$i] = $urlResult; |
||
| 225 | $tempScenario['try'.$i] = $tryResult; |
||
| 226 | $tempScenario['lp'.$i] = $lp; |
||
| 227 | $tempScenario['destination'.$i] = $listDestination; |
||
| 228 | } else { |
||
| 229 | $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1'); |
||
| 230 | $defaults['weighting[1]'] = 10; |
||
| 231 | $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2'); |
||
| 232 | $defaults['weighting[2]'] = 0; |
||
| 233 | |||
| 234 | $tempScenario['destination'.$i] = ['0']; |
||
| 235 | $tempScenario['lp'.$i] = ['0']; |
||
| 236 | } |
||
| 237 | |||
| 238 | $defaults['scenario'] = $tempScenario; |
||
| 239 | $renderer = $form->defaultRenderer(); |
||
| 240 | $renderer->setElementTemplate( |
||
| 241 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 242 | 'correct' |
||
| 243 | ); |
||
| 244 | $renderer->setElementTemplate( |
||
| 245 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 246 | 'counter['.$i.']' |
||
| 247 | ); |
||
| 248 | $renderer->setElementTemplate( |
||
| 249 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}'. |
||
| 250 | (isset($zoomOptions['options']) ? |
||
| 251 | '<br><div class="form-group "> |
||
| 252 | <label for="question_admin_form_btn_add_img['.$i.']" class="col-sm-2 control-label"></label> |
||
| 253 | <div class="col-sm-8"> |
||
| 254 | <button class="add_img_link btn btn-info btn-sm" |
||
| 255 | name="btn_add_img['.$i.']" |
||
| 256 | type="submit" |
||
| 257 | id="question_admin_form_btn_add_img['.$i.']"> |
||
| 258 | <em class="fa fa-plus"></em> '.get_lang('AddImageWithZoom').' |
||
| 259 | </button> |
||
| 260 | </div> |
||
| 261 | <div class="col-sm-2"></div> |
||
| 262 | </div>' : '').'</td>', |
||
| 263 | 'answer['.$i.']' |
||
| 264 | ); |
||
| 265 | $renderer->setElementTemplate( |
||
| 266 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 267 | 'comment['.$i.']' |
||
| 268 | ); |
||
| 269 | $renderer->setElementTemplate( |
||
| 270 | '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', |
||
| 271 | 'weighting['.$i.']' |
||
| 272 | ); |
||
| 273 | |||
| 274 | $answerNumber = $form->addElement('text', 'counter['.$i.']', null, ' value = "'.$i.'"'); |
||
| 275 | $answerNumber->freeze(); |
||
| 276 | |||
| 277 | $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox"'); |
||
| 278 | $form->addHtmlEditor('answer['.$i.']', null, null, false, $editorConfig); |
||
| 279 | |||
| 280 | $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 281 | |||
| 282 | switch ($objExercise->getFeedbackType()) { |
||
| 283 | case EXERCISE_FEEDBACK_TYPE_DIRECT: |
||
| 284 | $this->setDirectOptions($i, $form, $renderer, $selectLpId, $selectQuestion); |
||
| 285 | break; |
||
| 286 | case EXERCISE_FEEDBACK_TYPE_POPUP: |
||
| 287 | default: |
||
| 288 | $form->addHtmlEditor('comment['.$i.']', null, null, false, $editorConfig); |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | |||
| 292 | $form->addText('weighting['.$i.']', null, null, ['class' => 'col-md-1', 'value' => '0']); |
||
| 293 | $form->addHtml('</tr>'); |
||
| 294 | } |
||
| 295 | |||
| 296 | $form->addHtml('</tbody>'); |
||
| 297 | $form->addHtml('</table>'); |
||
| 298 | |||
| 299 | global $text; |
||
| 300 | $buttonGroup = []; |
||
| 301 | if ($objExercise->edit_exercise_in_lp == true || |
||
| 302 | (empty($this->exerciseList) && empty($objExercise->iid)) |
||
| 303 | ) { |
||
| 304 | //setting the save button here and not in the question class.php |
||
| 305 | $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true); |
||
| 306 | $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true); |
||
| 307 | $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true); |
||
| 308 | $form->addGroup($buttonGroup); |
||
| 309 | } |
||
| 310 | |||
| 311 | // We check the first radio button to be sure a radio button will be check |
||
| 312 | if (0 == $correct) { |
||
| 313 | $correct = 1; |
||
| 314 | } |
||
| 315 | |||
| 316 | $defaults['correct'] = $correct; |
||
| 317 | |||
| 318 | if (!empty($this->iid)) { |
||
| 319 | $form->setDefaults($defaults); |
||
| 320 | } else { |
||
| 321 | if (1 == $this->isContent) { |
||
| 322 | // Default sample content. |
||
| 323 | $form->setDefaults($defaults); |
||
| 324 | } else { |
||
| 325 | $form->setDefaults(['correct' => 1]); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | $form->setConstants(['nb_answers' => $numberAnswers]); |
||
| 330 | } |
||
| 450 |