Complex classes like Answer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Answer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Answer |
||
| 13 | { |
||
| 14 | public $questionId; |
||
| 15 | |||
| 16 | // these are arrays |
||
| 17 | public $answer; |
||
| 18 | public $correct; |
||
| 19 | public $comment; |
||
| 20 | public $weighting; |
||
| 21 | public $position; |
||
| 22 | public $hotspot_coordinates; |
||
| 23 | public $hotspot_type; |
||
| 24 | public $destination; |
||
| 25 | // these arrays are used to save temporarily new answers |
||
| 26 | // then they are moved into the arrays above or deleted in the event of cancellation |
||
| 27 | public $new_answer; |
||
| 28 | public $new_correct; |
||
| 29 | public $new_comment; |
||
| 30 | public $new_weighting; |
||
| 31 | public $new_position; |
||
| 32 | public $new_hotspot_coordinates; |
||
| 33 | public $new_hotspot_type; |
||
| 34 | public $autoId; |
||
| 35 | public $nbrAnswers; |
||
| 36 | public $new_nbrAnswers; |
||
| 37 | public $new_destination; // id of the next question if feedback option is set to Directfeedback |
||
| 38 | public $course; //Course information |
||
| 39 | public $iid; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * constructor of the class |
||
| 43 | * |
||
| 44 | * @author Olivier Brouckaert |
||
| 45 | * @param int $questionId that answers belong to |
||
| 46 | * @param int $course_id |
||
| 47 | */ |
||
| 48 | public function __construct($questionId, $course_id = null) |
||
| 49 | { |
||
| 50 | $this->questionId = intval($questionId); |
||
| 51 | $this->answer = array(); |
||
| 52 | $this->correct = array(); |
||
| 53 | $this->comment = array(); |
||
| 54 | $this->weighting = array(); |
||
| 55 | $this->position = array(); |
||
| 56 | $this->hotspot_coordinates = array(); |
||
| 57 | $this->hotspot_type = array(); |
||
| 58 | $this->destination = array(); |
||
| 59 | // clears $new_* arrays |
||
| 60 | $this->cancel(); |
||
| 61 | |||
| 62 | if (!empty($course_id)) { |
||
| 63 | $courseInfo = api_get_course_info_by_id($course_id); |
||
| 64 | } else { |
||
| 65 | $courseInfo = api_get_course_info(); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->course = $courseInfo; |
||
| 69 | $this->course_id = $courseInfo['real_id']; |
||
| 70 | |||
| 71 | // fills arrays |
||
| 72 | $objExercise = new Exercise($this->course_id); |
||
| 73 | $exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null; |
||
| 74 | $objExercise->read($exerciseId); |
||
| 75 | if ($objExercise->random_answers == '1') { |
||
| 76 | $this->readOrderedBy('rand()', '');// randomize answers |
||
| 77 | } else { |
||
| 78 | $this->read(); // natural order |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Clears $new_* arrays |
||
| 84 | * |
||
| 85 | * @author Olivier Brouckaert |
||
| 86 | */ |
||
| 87 | public function cancel() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Reads answer information from the database |
||
| 102 | * |
||
| 103 | * @author Olivier Brouckaert |
||
| 104 | */ |
||
| 105 | public function read() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param int $id |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function getAnswerByAutoId($id) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * returns all answer ids from this question Id |
||
| 161 | * |
||
| 162 | * @author Yoselyn Castillo |
||
| 163 | * @return array - $id (answer ids) |
||
| 164 | */ |
||
| 165 | public function selectAnswerId() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Reads answer information from the data base ordered by parameter |
||
| 188 | * @param string Field we want to order by |
||
| 189 | * @param string DESC or ASC |
||
| 190 | * @author Frederic Vauthier |
||
| 191 | */ |
||
| 192 | public function readOrderedBy($field, $order='ASC') |
||
| 273 | |||
| 274 | /** |
||
| 275 | * returns the autoincrement id identificator |
||
| 276 | * |
||
| 277 | * @author Juan Carlos Ra�a |
||
| 278 | * @return integer - answer num |
||
| 279 | */ |
||
| 280 | public function selectAutoId($id) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * returns the number of answers in this question |
||
| 287 | * |
||
| 288 | * @author Olivier Brouckaert |
||
| 289 | * @return integer - number of answers |
||
| 290 | */ |
||
| 291 | public function selectNbrAnswers() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * returns the question ID which the answers belong to |
||
| 298 | * |
||
| 299 | * @author Olivier Brouckaert |
||
| 300 | * @return integer - the question ID |
||
| 301 | */ |
||
| 302 | public function selectQuestionId() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * returns the question ID of the destination question |
||
| 309 | * |
||
| 310 | * @author Julio Montoya |
||
| 311 | * @return integer - the question ID |
||
| 312 | */ |
||
| 313 | public function selectDestination($id) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * returns the answer title |
||
| 320 | * |
||
| 321 | * @author Olivier Brouckaert |
||
| 322 | * @param - integer $id - answer ID |
||
| 323 | * @return string - answer title |
||
| 324 | */ |
||
| 325 | public function selectAnswer($id) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * return array answer by id else return a bool |
||
| 332 | */ |
||
| 333 | public function selectAnswerByAutoId($auto_id) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * returns the answer title from an answer's position |
||
| 353 | * |
||
| 354 | * @author Yannick Warnier |
||
| 355 | * @param - integer $id - answer ID |
||
| 356 | * @return bool - answer title |
||
| 357 | */ |
||
| 358 | public function selectAnswerIdByPosition($pos) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns a list of answers |
||
| 373 | * @author Yannick Warnier <[email protected]> |
||
| 374 | * @return array List of answers where each answer is an array |
||
| 375 | * of (id, answer, comment, grade) and grade=weighting |
||
| 376 | */ |
||
| 377 | public function getAnswersList($decode = false) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns a list of grades |
||
| 407 | * @author Yannick Warnier <[email protected]> |
||
| 408 | * @return array List of grades where grade=weighting (?) |
||
| 409 | */ |
||
| 410 | public function getGradesList() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the question type |
||
| 423 | * @author Yannick Warnier <[email protected]> |
||
| 424 | * @return integer The type of the question this answer is bound to |
||
| 425 | */ |
||
| 426 | public function getQuestionType() |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * tells if answer is correct or not |
||
| 443 | * |
||
| 444 | * @author Olivier Brouckaert |
||
| 445 | * @param - integer $id - answer ID |
||
| 446 | * @return integer - 0 if bad answer, not 0 if good answer |
||
| 447 | */ |
||
| 448 | public function isCorrect($id) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * returns answer comment |
||
| 455 | * |
||
| 456 | * @author Olivier Brouckaert |
||
| 457 | * @param - integer $id - answer ID |
||
| 458 | * @return string - answer comment |
||
| 459 | */ |
||
| 460 | public function selectComment($id) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * returns answer weighting |
||
| 467 | * |
||
| 468 | * @author Olivier Brouckaert |
||
| 469 | * @param - integer $id - answer ID |
||
| 470 | * @return integer - answer weighting |
||
| 471 | */ |
||
| 472 | public function selectWeighting($id) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * returns answer position |
||
| 479 | * |
||
| 480 | * @author Olivier Brouckaert |
||
| 481 | * @param - integer $id - answer ID |
||
| 482 | * @return integer - answer position |
||
| 483 | */ |
||
| 484 | function selectPosition($id) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * returns answer hotspot coordinates |
||
| 491 | * |
||
| 492 | * @author Olivier Brouckaert |
||
| 493 | * @param integer Answer ID |
||
| 494 | * @return integer Answer position |
||
| 495 | */ |
||
| 496 | public function selectHotspotCoordinates($id) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * returns answer hotspot type |
||
| 503 | * |
||
| 504 | * @author Toon Keppens |
||
| 505 | * @param integer Answer ID |
||
| 506 | * @return integer Answer position |
||
| 507 | */ |
||
| 508 | public function selectHotspotType($id) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Creates a new answer |
||
| 515 | * |
||
| 516 | * @author Olivier Brouckaert |
||
| 517 | * @param string $answer answer title |
||
| 518 | * @param integer $correct 0 if bad answer, not 0 if good answer |
||
| 519 | * @param string $comment answer comment |
||
| 520 | * @param integer $weighting answer weighting |
||
| 521 | * @param integer $position answer position |
||
| 522 | * @param array $new_hotspot_coordinates Coordinates for hotspot exercises (optional) |
||
| 523 | * @param integer $new_hotspot_type Type for hotspot exercises (optional) |
||
| 524 | * @param string $destination |
||
| 525 | */ |
||
| 526 | public function createAnswer( |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Updates an answer |
||
| 550 | * |
||
| 551 | * @author Toon Keppens |
||
| 552 | * @param int $iid |
||
| 553 | * @param string $answer |
||
| 554 | * @param string $comment |
||
| 555 | * @param string $correct |
||
| 556 | * @param string $weighting |
||
| 557 | * @param string $position |
||
| 558 | * @param string $destination |
||
| 559 | * @param string $hotspot_coordinates |
||
| 560 | * @param string $hotspot_type |
||
| 561 | */ |
||
| 562 | public function updateAnswers( |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Records answers into the data base |
||
| 591 | * |
||
| 592 | * @author Olivier Brouckaert |
||
| 593 | */ |
||
| 594 | public function save() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Duplicates answers by copying them into another question |
||
| 728 | * |
||
| 729 | * @author Olivier Brouckaert |
||
| 730 | * @param int question id |
||
| 731 | * @param array destination course info (result of the function api_get_course_info() ) |
||
| 732 | */ |
||
| 733 | public function duplicate($newQuestionId, $course_info = null) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Get the necessary JavaScript for some answers |
||
| 826 | * @return string |
||
| 827 | */ |
||
| 828 | public function getJs() |
||
| 839 | |||
| 840 | } |
||
| 841 |