| Total Complexity | 82 |
| Total Lines | 467 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like survey_question often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use survey_question, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class survey_question |
||
| 10 | { |
||
| 11 | public $buttonList = []; |
||
| 12 | /** @var FormValidator */ |
||
| 13 | private $form; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param array $surveyData |
||
| 17 | */ |
||
| 18 | public function addParentMenu(FormValidator $form, $surveyData) |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $type |
||
| 63 | * |
||
| 64 | * @return survey_question |
||
| 65 | */ |
||
| 66 | public static function createQuestion($type) |
||
| 67 | { |
||
| 68 | switch ($type) { |
||
| 69 | case 'comment': |
||
| 70 | return new ch_comment(); |
||
| 71 | case 'dropdown': |
||
| 72 | return new ch_dropdown(); |
||
| 73 | case 'multiplechoice': |
||
| 74 | return new ch_multiplechoice(); |
||
| 75 | case 'multipleresponse': |
||
| 76 | return new ch_multipleresponse(); |
||
| 77 | case 'open': |
||
| 78 | return new ch_open(); |
||
| 79 | case 'pagebreak': |
||
| 80 | return new ch_pagebreak(); |
||
| 81 | case 'percentage': |
||
| 82 | return new ch_percentage(); |
||
| 83 | case 'personality': |
||
| 84 | return new ch_personality(); |
||
| 85 | case 'score': |
||
| 86 | return new ch_score(); |
||
| 87 | case 'yesno': |
||
| 88 | return new ch_yesno(); |
||
| 89 | case 'selectivedisplay': |
||
| 90 | return new ch_selectivedisplay(); |
||
| 91 | default: |
||
| 92 | api_not_allowed(true); |
||
| 93 | break; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Generic part of any survey question: the question field. |
||
| 99 | * |
||
| 100 | * @param array $surveyData |
||
| 101 | * @param array $formData |
||
| 102 | * |
||
| 103 | * @return FormValidator |
||
| 104 | */ |
||
| 105 | public function createForm($surveyData, $formData) |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Adds submit button. |
||
| 235 | */ |
||
| 236 | public function renderForm() |
||
| 237 | { |
||
| 238 | if (isset($_GET['question_id']) && !empty($_GET['question_id'])) { |
||
| 239 | /** |
||
| 240 | * Check if survey has answers first before update it, this is because if you update it, the question |
||
| 241 | * options will delete and re-insert in database loosing the iid and question_id to verify the correct answers. |
||
| 242 | */ |
||
| 243 | $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; |
||
| 244 | $answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId); |
||
| 245 | if (!$answersChecker) { |
||
| 246 | $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true); |
||
| 247 | } else { |
||
| 248 | $this->getForm()->addHtml(' |
||
| 249 | <div class="form-group"> |
||
| 250 | <label class="col-sm-2 control-label"></label> |
||
| 251 | <div class="col-sm-8"> |
||
| 252 | <div class="alert alert-info">'. |
||
| 253 | get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers').'</div> |
||
| 254 | </div> |
||
| 255 | <div class="col-sm-2"></div> |
||
| 256 | </div> |
||
| 257 | '); |
||
| 258 | } |
||
| 259 | } else { |
||
| 260 | $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true); |
||
| 261 | } |
||
| 262 | |||
| 263 | $this->getForm()->addGroup($this->buttonList, 'buttons'); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return FormValidator |
||
| 268 | */ |
||
| 269 | public function getForm() |
||
| 270 | { |
||
| 271 | return $this->form; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param FormValidator $form |
||
| 276 | */ |
||
| 277 | public function setForm($form) |
||
| 278 | { |
||
| 279 | $this->form = $form; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param array $formData |
||
| 284 | * |
||
| 285 | * @return mixed |
||
| 286 | */ |
||
| 287 | public function preSave($formData) |
||
| 288 | { |
||
| 289 | $counter = Session::read('answer_count'); |
||
| 290 | $answerList = Session::read('answer_list'); |
||
| 291 | |||
| 292 | if (empty($answerList)) { |
||
| 293 | $answerList = isset($formData['answers']) ? $formData['answers'] : []; |
||
| 294 | Session::write('answer_list', $answerList); |
||
| 295 | } |
||
| 296 | |||
| 297 | if (isset($_POST['answers'])) { |
||
| 298 | $formData['answers'] = $_POST['answers']; |
||
| 299 | } |
||
| 300 | |||
| 301 | if (empty($counter)) { |
||
| 302 | $counter = count($answerList) - 1; |
||
| 303 | Session::write('answer_count', $counter); |
||
| 304 | } |
||
| 305 | |||
| 306 | // Moving an answer up |
||
| 307 | if (isset($_POST['move_up']) && $_POST['move_up']) { |
||
| 308 | foreach ($_POST['move_up'] as $key => &$value) { |
||
| 309 | $id1 = $key; |
||
| 310 | $content1 = $formData['answers'][$id1]; |
||
| 311 | $id2 = $key - 1; |
||
| 312 | $content2 = $formData['answers'][$id2]; |
||
| 313 | $formData['answers'][$id1] = $content2; |
||
| 314 | $formData['answers'][$id2] = $content1; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | // Moving an answer down |
||
| 319 | if (isset($_POST['move_down']) && $_POST['move_down']) { |
||
| 320 | foreach ($_POST['move_down'] as $key => &$value) { |
||
| 321 | $id1 = $key; |
||
| 322 | $content1 = $formData['answers'][$id1]; |
||
| 323 | $id2 = $key + 1; |
||
| 324 | $content2 = $formData['answers'][$id2]; |
||
| 325 | $formData['answers'][$id1] = $content2; |
||
| 326 | $formData['answers'][$id2] = $content1; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Deleting a specific answer is only saved in the session until the |
||
| 332 | * "Save question" button is pressed. This means all options are kept |
||
| 333 | * in the survey_question_option table until the question is saved. |
||
| 334 | */ |
||
| 335 | if (isset($_POST['delete_answer'])) { |
||
| 336 | $deleted = false; |
||
| 337 | foreach ($_POST['delete_answer'] as $key => &$value) { |
||
| 338 | $deleted = $key; |
||
| 339 | $counter--; |
||
| 340 | Session::write('answer_count', $counter); |
||
| 341 | } |
||
| 342 | |||
| 343 | $newAnswers = []; |
||
| 344 | $newAnswersId = []; |
||
| 345 | foreach ($formData['answers'] as $key => &$value) { |
||
| 346 | if ($key > $deleted) { |
||
| 347 | // swap with previous (deleted) option slot |
||
| 348 | $newAnswers[$key - 1] = $formData['answers'][$key]; |
||
| 349 | $newAnswersId[$key - 1] = $formData['answersid'][$key]; |
||
| 350 | unset($formData['answers'][$key]); |
||
| 351 | unset($formData['answersid'][$key]); |
||
| 352 | } elseif ($key === $deleted) { |
||
| 353 | // delete option |
||
| 354 | unset($formData['answers'][$deleted]); |
||
| 355 | unset($formData['answersid'][$deleted]); |
||
| 356 | } else { |
||
| 357 | // keep as is |
||
| 358 | $newAnswers[$key] = $value; |
||
| 359 | $newAnswersId[$key] = $formData['answersid'][$key]; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | unset($formData['answers']); |
||
| 363 | unset($formData['answersid']); |
||
| 364 | $formData['answers'] = $newAnswers; |
||
| 365 | $formData['answersid'] = $newAnswersId; |
||
| 366 | } |
||
| 367 | |||
| 368 | // Adding an answer |
||
| 369 | if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) { |
||
| 370 | $counter++; |
||
| 371 | Session::write('answer_count', $counter); |
||
| 372 | } |
||
| 373 | |||
| 374 | // Removing an answer |
||
| 375 | if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) { |
||
| 376 | $counter--; |
||
| 377 | Session::write('answer_count', $counter); |
||
| 378 | foreach ($formData['answers'] as $index => &$data) { |
||
| 379 | if ($index > $counter) { |
||
| 380 | unset($formData['answers'][$index]); |
||
| 381 | unset($formData['answersid'][$index]); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | if (!isset($_POST['delete_answer'])) { |
||
| 387 | // Make sure we have an array of answers |
||
| 388 | if (!isset($formData['answers'])) { |
||
| 389 | $formData['answers'] = []; |
||
| 390 | } |
||
| 391 | // Check if no deleted answer remains at the end of the answers |
||
| 392 | // array and add empty answers if the array is too short |
||
| 393 | foreach ($formData['answers'] as $index => $data) { |
||
| 394 | if ($index > $counter) { |
||
| 395 | unset($formData['answers'][$index]); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | for ($i = 0; $i <= $counter; $i++) { |
||
| 400 | if (!isset($formData['answers'][$i])) { |
||
| 401 | $formData['answers'][$i] = ''; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : []; |
||
| 407 | Session::write('answer_list', $formData['answers']); |
||
| 408 | |||
| 409 | if (!isset($formData['is_required']) && api_get_configuration_value('survey_mark_question_as_required')) { |
||
| 410 | $formData['is_required'] = true; |
||
| 411 | } |
||
| 412 | |||
| 413 | return $formData; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param array $surveyData |
||
| 418 | * @param array $formData |
||
| 419 | * |
||
| 420 | * @return mixed |
||
| 421 | */ |
||
| 422 | public function save($surveyData, $formData, $dataFromDatabase = []) |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Adds two buttons. One to add an option, one to remove an option. |
||
| 441 | * |
||
| 442 | * @param array $data |
||
| 443 | */ |
||
| 444 | public function addRemoveButtons($data) |
||
| 466 | ); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param array $questionData |
||
| 471 | * @param array $answers |
||
| 472 | */ |
||
| 473 | public function render(FormValidator $form, $questionData = [], $answers = []) |
||
| 476 | } |
||
| 477 | } |
||
| 478 |