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 | |||
| 40 | /** |
||
| 41 | * constructor of the class |
||
| 42 | * |
||
| 43 | * @author Olivier Brouckaert |
||
| 44 | * @param int $questionId that answers belong to |
||
| 45 | * @param int $course_id |
||
| 46 | */ |
||
| 47 | public function __construct($questionId, $course_id = null) |
||
| 48 | { |
||
| 49 | $this->questionId = intval($questionId); |
||
| 50 | $this->answer = array(); |
||
| 51 | $this->correct = array(); |
||
| 52 | $this->comment = array(); |
||
| 53 | $this->weighting = array(); |
||
| 54 | $this->position = array(); |
||
| 55 | $this->hotspot_coordinates = array(); |
||
| 56 | $this->hotspot_type = array(); |
||
| 57 | $this->destination = array(); |
||
| 58 | // clears $new_* arrays |
||
| 59 | $this->cancel(); |
||
| 60 | |||
| 61 | if (!empty($course_id)) { |
||
| 62 | $courseInfo = api_get_course_info_by_id($course_id); |
||
| 63 | } else { |
||
| 64 | $courseInfo = api_get_course_info(); |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->course = $courseInfo; |
||
| 68 | $this->course_id = $courseInfo['real_id']; |
||
| 69 | |||
| 70 | // fills arrays |
||
| 71 | $objExercise = new Exercise($this->course_id); |
||
| 72 | $exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null; |
||
| 73 | $objExercise->read($exerciseId); |
||
| 74 | |||
| 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() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param int $id |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function getAnswerByAutoId($id) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * returns all answer ids from this question Id |
||
| 160 | * |
||
| 161 | * @author Yoselyn Castillo |
||
| 162 | * @return array - $id (answer ids) |
||
| 163 | */ |
||
| 164 | public function selectAnswerId() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Reads answer information from the data base ordered by parameter |
||
| 187 | * @param string Field we want to order by |
||
| 188 | * @param string DESC or ASC |
||
| 189 | * @author Frederic Vauthier |
||
| 190 | */ |
||
| 191 | public function readOrderedBy($field, $order='ASC') |
||
| 192 | { |
||
| 193 | $field = Database::escape_string($field); |
||
| 194 | if (empty($field)) { |
||
| 195 | $field = 'position'; |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($order != 'ASC' && $order!='DESC') { |
||
| 199 | $order = 'ASC'; |
||
| 200 | } |
||
| 201 | |||
| 202 | $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 203 | $TBL_QUIZ = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 204 | $questionId = intval($this->questionId); |
||
| 205 | |||
| 206 | $sql = "SELECT type FROM $TBL_QUIZ |
||
| 207 | WHERE c_id = {$this->course_id} AND id = $questionId"; |
||
| 208 | $result_question = Database::query($sql); |
||
| 209 | $questionType = Database::fetch_array($result_question); |
||
| 210 | |||
| 211 | if ($questionType['type'] == DRAGGABLE) { |
||
| 212 | // Random is done by submit.js.tpl |
||
| 213 | $this->read(); |
||
| 214 | |||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | $sql = "SELECT |
||
| 219 | answer, |
||
| 220 | correct, |
||
| 221 | comment, |
||
| 222 | ponderation, |
||
| 223 | position, |
||
| 224 | hotspot_coordinates, |
||
| 225 | hotspot_type, |
||
| 226 | destination, |
||
| 227 | id_auto |
||
| 228 | FROM $TBL_ANSWER |
||
| 229 | WHERE |
||
| 230 | c_id = {$this->course_id} AND |
||
| 231 | question_id='".$questionId."' |
||
| 232 | ORDER BY $field $order"; |
||
| 233 | $result=Database::query($sql); |
||
| 234 | |||
| 235 | $i = 1; |
||
| 236 | // while a record is found |
||
| 237 | $doubt_data = null; |
||
| 238 | while ($object = Database::fetch_object($result)) { |
||
| 239 | if ($questionType['type'] == UNIQUE_ANSWER_NO_OPTION && $object->position == 666) { |
||
| 240 | $doubt_data = $object; |
||
| 241 | continue; |
||
| 242 | } |
||
| 243 | $this->answer[$i] = $object->answer; |
||
| 244 | $this->correct[$i] = $object->correct; |
||
| 245 | $this->comment[$i] = $object->comment; |
||
| 246 | $this->weighting[$i] = $object->ponderation; |
||
| 247 | $this->position[$i] = $object->position; |
||
| 248 | $this->hotspot_coordinates[$i] = $object->hotspot_coordinates; |
||
| 249 | $this->hotspot_type[$i] = $object->hotspot_type; |
||
| 250 | $this->destination[$i] = $object->destination; |
||
| 251 | $this->autoId[$i] = $object->id_auto; |
||
| 252 | $i++; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($questionType['type'] == UNIQUE_ANSWER_NO_OPTION && !empty($doubt_data)) { |
||
| 256 | $this->answer[$i] = $doubt_data->answer; |
||
| 257 | $this->correct[$i] = $doubt_data->correct; |
||
| 258 | $this->comment[$i] = $doubt_data->comment; |
||
| 259 | $this->weighting[$i] = $doubt_data->ponderation; |
||
| 260 | $this->position[$i] = $doubt_data->position; |
||
| 261 | $this->hotspot_coordinates[$i] = $object->hotspot_coordinates; |
||
| 262 | $this->hotspot_type[$i] = $object->hotspot_type; |
||
| 263 | $this->destination[$i] = $doubt_data->destination; |
||
| 264 | $this->autoId[$i] = $doubt_data->id_auto; |
||
| 265 | $i++; |
||
| 266 | } |
||
| 267 | $this->nbrAnswers = $i-1; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * returns the autoincrement id identificator |
||
| 272 | * |
||
| 273 | * @author Juan Carlos Ra�a |
||
| 274 | * @return integer - answer num |
||
| 275 | */ |
||
| 276 | public function selectAutoId($id) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * returns the number of answers in this question |
||
| 283 | * |
||
| 284 | * @author Olivier Brouckaert |
||
| 285 | * @return integer - number of answers |
||
| 286 | */ |
||
| 287 | public function selectNbrAnswers() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * returns the question ID which the answers belong to |
||
| 294 | * |
||
| 295 | * @author Olivier Brouckaert |
||
| 296 | * @return integer - the question ID |
||
| 297 | */ |
||
| 298 | public function selectQuestionId() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * returns the question ID of the destination question |
||
| 305 | * |
||
| 306 | * @author Julio Montoya |
||
| 307 | * @return integer - the question ID |
||
| 308 | */ |
||
| 309 | public function selectDestination($id) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * returns the answer title |
||
| 316 | * |
||
| 317 | * @author Olivier Brouckaert |
||
| 318 | * @param - integer $id - answer ID |
||
| 319 | * @return string - answer title |
||
| 320 | */ |
||
| 321 | public function selectAnswer($id) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * return array answer by id else return a bool |
||
| 328 | */ |
||
| 329 | public function selectAnswerByAutoId($auto_id) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * returns the answer title from an answer's position |
||
| 349 | * |
||
| 350 | * @author Yannick Warnier |
||
| 351 | * @param - integer $id - answer ID |
||
| 352 | * @return bool - answer title |
||
| 353 | */ |
||
| 354 | public function selectAnswerIdByPosition($pos) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns a list of answers |
||
| 369 | * @author Yannick Warnier <[email protected]> |
||
| 370 | * @return array List of answers where each answer is an array |
||
| 371 | * of (id, answer, comment, grade) and grade=weighting |
||
| 372 | */ |
||
| 373 | public function getAnswersList($decode = false) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Returns a list of grades |
||
| 403 | * @author Yannick Warnier <[email protected]> |
||
| 404 | * @return array List of grades where grade=weighting (?) |
||
| 405 | */ |
||
| 406 | public function getGradesList() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Returns the question type |
||
| 419 | * @author Yannick Warnier <[email protected]> |
||
| 420 | * @return integer The type of the question this answer is bound to |
||
| 421 | */ |
||
| 422 | public function getQuestionType() |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * tells if answer is correct or not |
||
| 439 | * |
||
| 440 | * @author Olivier Brouckaert |
||
| 441 | * @param - integer $id - answer ID |
||
| 442 | * @return integer - 0 if bad answer, not 0 if good answer |
||
| 443 | */ |
||
| 444 | public function isCorrect($id) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * returns answer comment |
||
| 451 | * |
||
| 452 | * @author Olivier Brouckaert |
||
| 453 | * @param - integer $id - answer ID |
||
| 454 | * @return string - answer comment |
||
| 455 | */ |
||
| 456 | public function selectComment($id) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * returns answer weighting |
||
| 463 | * |
||
| 464 | * @author Olivier Brouckaert |
||
| 465 | * @param - integer $id - answer ID |
||
| 466 | * @return integer - answer weighting |
||
| 467 | */ |
||
| 468 | public function selectWeighting($id) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * returns answer position |
||
| 475 | * |
||
| 476 | * @author Olivier Brouckaert |
||
| 477 | * @param - integer $id - answer ID |
||
| 478 | * @return integer - answer position |
||
| 479 | */ |
||
| 480 | function selectPosition($id) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * returns answer hotspot coordinates |
||
| 487 | * |
||
| 488 | * @author Olivier Brouckaert |
||
| 489 | * @param integer Answer ID |
||
| 490 | * @return integer Answer position |
||
| 491 | */ |
||
| 492 | public function selectHotspotCoordinates($id) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * returns answer hotspot type |
||
| 499 | * |
||
| 500 | * @author Toon Keppens |
||
| 501 | * @param integer Answer ID |
||
| 502 | * @return integer Answer position |
||
| 503 | */ |
||
| 504 | public function selectHotspotType($id) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Creates a new answer |
||
| 511 | * |
||
| 512 | * @author Olivier Brouckaert |
||
| 513 | * @param string $answer answer title |
||
| 514 | * @param integer $correct 0 if bad answer, not 0 if good answer |
||
| 515 | * @param string $comment answer comment |
||
| 516 | * @param integer $weighting answer weighting |
||
| 517 | * @param integer $position answer position |
||
| 518 | * @param array $new_hotspot_coordinates Coordinates for hotspot exercises (optional) |
||
| 519 | * @param integer $new_hotspot_type Type for hotspot exercises (optional) |
||
| 520 | * @param string $destination |
||
| 521 | */ |
||
| 522 | public function createAnswer( |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Updates an answer |
||
| 546 | * |
||
| 547 | * @author Toon Keppens |
||
| 548 | * |
||
| 549 | * @param string $answer |
||
| 550 | * @param string $comment |
||
| 551 | * @param string $correct |
||
| 552 | * @param string $weighting |
||
| 553 | * @param string $position |
||
| 554 | * @param string $destination |
||
| 555 | * @param string $hotspot_coordinates |
||
| 556 | * @param string $hotspot_type |
||
| 557 | */ |
||
| 558 | public function updateAnswers( |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Records answers into the data base |
||
| 588 | * |
||
| 589 | * @author Olivier Brouckaert |
||
| 590 | */ |
||
| 591 | public function save() |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Duplicates answers by copying them into another question |
||
| 723 | * |
||
| 724 | * @author Olivier Brouckaert |
||
| 725 | * @param int question id |
||
| 726 | * @param array destination course info (result of the function api_get_course_info() ) |
||
| 727 | */ |
||
| 728 | public function duplicate($newQuestionId, $course_info = null) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Get the necessary JavaScript for some answers |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | public function getJs() |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Check if a answer is correct by an answer auto id |
||
| 837 | * @param $needle int The answer auto id |
||
| 838 | * @return bool |
||
| 839 | */ |
||
| 840 | public function isCorrectByAutoId($needle) |
||
| 856 | } |
||
| 857 |