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 | |||
| 76 | if ($objExercise->random_answers == '1' && $this->getQuestionType() != CALCULATED_ANSWER) { |
||
| 77 | $this->readOrderedBy('rand()', '');// randomize answers |
||
| 78 | } else { |
||
| 79 | $this->read(); // natural order |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Clears $new_* arrays |
||
| 85 | * |
||
| 86 | * @author Olivier Brouckaert |
||
| 87 | */ |
||
| 88 | public function cancel() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Reads answer information from the database |
||
| 103 | * |
||
| 104 | * @author Olivier Brouckaert |
||
| 105 | */ |
||
| 106 | public function read() |
||
| 107 | { |
||
| 108 | $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 109 | $questionId = $this->questionId; |
||
| 110 | |||
| 111 | $sql = "SELECT * FROM $TBL_ANSWER |
||
| 112 | WHERE |
||
| 113 | c_id = {$this->course_id} AND |
||
| 114 | question_id ='".$questionId."' |
||
| 115 | ORDER BY position"; |
||
| 116 | |||
| 117 | $result = Database::query($sql); |
||
| 118 | $i=1; |
||
| 119 | |||
| 120 | // while a record is found |
||
| 121 | while ($object = Database::fetch_object($result)) { |
||
| 122 | $this->id[$i] = $object->id; |
||
| 123 | $this->answer[$i] = $object->answer; |
||
| 124 | $this->correct[$i] = $object->correct; |
||
| 125 | $this->comment[$i] = $object->comment; |
||
| 126 | $this->weighting[$i] = $object->ponderation; |
||
| 127 | $this->position[$i] = $object->position; |
||
| 128 | $this->hotspot_coordinates[$i] = $object->hotspot_coordinates; |
||
| 129 | $this->hotspot_type[$i] = $object->hotspot_type; |
||
| 130 | $this->destination[$i] = $object->destination; |
||
| 131 | $this->autoId[$i] = $object->id_auto; |
||
| 132 | $this->iid[$i] = $object->iid; |
||
| 133 | $i++; |
||
| 134 | } |
||
| 135 | $this->nbrAnswers = $i-1; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param int $id |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getAnswerByAutoId($id) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * returns all answer ids from this question Id |
||
| 162 | * |
||
| 163 | * @author Yoselyn Castillo |
||
| 164 | * @return array - $id (answer ids) |
||
| 165 | */ |
||
| 166 | public function selectAnswerId() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Reads answer information from the data base ordered by parameter |
||
| 189 | * @param string Field we want to order by |
||
| 190 | * @param string DESC or ASC |
||
| 191 | * @param string $field |
||
| 192 | * @author Frederic Vauthier |
||
| 193 | */ |
||
| 194 | public function readOrderedBy($field, $order='ASC') |
||
| 195 | { |
||
| 196 | $field = Database::escape_string($field); |
||
| 197 | if (empty($field)) { |
||
| 198 | $field = 'position'; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($order != 'ASC' && $order!='DESC') { |
||
| 202 | $order = 'ASC'; |
||
| 203 | } |
||
| 204 | |||
| 205 | $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 206 | $TBL_QUIZ = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 207 | $questionId = intval($this->questionId); |
||
| 208 | |||
| 209 | $sql = "SELECT type FROM $TBL_QUIZ |
||
| 210 | WHERE c_id = {$this->course_id} AND id = $questionId"; |
||
| 211 | $result_question = Database::query($sql); |
||
| 212 | $questionType = Database::fetch_array($result_question); |
||
| 213 | |||
| 214 | if ($questionType['type'] == DRAGGABLE) { |
||
| 215 | // Random is done by submit.js.tpl |
||
| 216 | $this->read(); |
||
| 217 | |||
| 218 | return true; |
||
| 219 | } |
||
| 220 | |||
| 221 | $sql = "SELECT |
||
| 222 | answer, |
||
| 223 | correct, |
||
| 224 | comment, |
||
| 225 | ponderation, |
||
| 226 | position, |
||
| 227 | hotspot_coordinates, |
||
| 228 | hotspot_type, |
||
| 229 | destination, |
||
| 230 | id_auto, |
||
| 231 | iid |
||
| 232 | FROM $TBL_ANSWER |
||
| 233 | WHERE |
||
| 234 | c_id = {$this->course_id} AND |
||
| 235 | question_id='".$questionId."' |
||
| 236 | ORDER BY $field $order"; |
||
| 237 | $result=Database::query($sql); |
||
| 238 | |||
| 239 | $i = 1; |
||
| 240 | // while a record is found |
||
| 241 | $doubt_data = null; |
||
| 242 | while ($object = Database::fetch_object($result)) { |
||
| 243 | if ($questionType['type'] == UNIQUE_ANSWER_NO_OPTION && $object->position == 666) { |
||
| 244 | $doubt_data = $object; |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | $this->answer[$i] = $object->answer; |
||
| 248 | $this->correct[$i] = $object->correct; |
||
| 249 | $this->comment[$i] = $object->comment; |
||
| 250 | $this->weighting[$i] = $object->ponderation; |
||
| 251 | $this->position[$i] = $object->position; |
||
| 252 | $this->hotspot_coordinates[$i] = $object->hotspot_coordinates; |
||
| 253 | $this->hotspot_type[$i] = $object->hotspot_type; |
||
| 254 | $this->destination[$i] = $object->destination; |
||
| 255 | $this->autoId[$i] = $object->id_auto; |
||
| 256 | $this->iid[$i] = $object->iid; |
||
| 257 | $i++; |
||
| 258 | } |
||
| 259 | |||
| 260 | if ($questionType['type'] == UNIQUE_ANSWER_NO_OPTION && !empty($doubt_data)) { |
||
| 261 | $this->answer[$i] = $doubt_data->answer; |
||
| 262 | $this->correct[$i] = $doubt_data->correct; |
||
| 263 | $this->comment[$i] = $doubt_data->comment; |
||
| 264 | $this->weighting[$i] = $doubt_data->ponderation; |
||
| 265 | $this->position[$i] = $doubt_data->position; |
||
| 266 | $this->hotspot_coordinates[$i] = $object->hotspot_coordinates; |
||
| 267 | $this->hotspot_type[$i] = $object->hotspot_type; |
||
| 268 | $this->destination[$i] = $doubt_data->destination; |
||
| 269 | $this->autoId[$i] = $doubt_data->id_auto; |
||
| 270 | $this->iid[$i] = $doubt_data->iid; |
||
| 271 | $i++; |
||
| 272 | } |
||
| 273 | $this->nbrAnswers = $i-1; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * returns the autoincrement id identificator |
||
| 278 | * |
||
| 279 | * @author Juan Carlos Ra�a |
||
| 280 | * @return integer - answer num |
||
| 281 | */ |
||
| 282 | public function selectAutoId($id) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * returns the number of answers in this question |
||
| 289 | * |
||
| 290 | * @author Olivier Brouckaert |
||
| 291 | * @return integer - number of answers |
||
| 292 | */ |
||
| 293 | public function selectNbrAnswers() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * returns the question ID which the answers belong to |
||
| 300 | * |
||
| 301 | * @author Olivier Brouckaert |
||
| 302 | * @return integer - the question ID |
||
| 303 | */ |
||
| 304 | public function selectQuestionId() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * returns the question ID of the destination question |
||
| 311 | * |
||
| 312 | * @author Julio Montoya |
||
| 313 | * @param integer $id |
||
| 314 | * @return integer - the question ID |
||
| 315 | */ |
||
| 316 | public function selectDestination($id) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * returns the answer title |
||
| 323 | * |
||
| 324 | * @author Olivier Brouckaert |
||
| 325 | * @param - integer $id - answer ID |
||
| 326 | * @return string - answer title |
||
| 327 | */ |
||
| 328 | public function selectAnswer($id) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * return array answer by id else return a bool |
||
| 335 | * @param integer $auto_id |
||
| 336 | */ |
||
| 337 | public function selectAnswerByAutoId($auto_id) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * returns the answer title from an answer's position |
||
| 357 | * |
||
| 358 | * @author Yannick Warnier |
||
| 359 | * @param - integer $id - answer ID |
||
| 360 | * @return bool - answer title |
||
| 361 | */ |
||
| 362 | public function selectAnswerIdByPosition($pos) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns a list of answers |
||
| 377 | * @author Yannick Warnier <[email protected]> |
||
| 378 | * @return array List of answers where each answer is an array |
||
| 379 | * of (id, answer, comment, grade) and grade=weighting |
||
| 380 | */ |
||
| 381 | public function getAnswersList($decode = false) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns a list of grades |
||
| 411 | * @author Yannick Warnier <[email protected]> |
||
| 412 | * @return array List of grades where grade=weighting (?) |
||
| 413 | */ |
||
| 414 | public function getGradesList() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns the question type |
||
| 427 | * @author Yannick Warnier <[email protected]> |
||
| 428 | * @return integer The type of the question this answer is bound to |
||
| 429 | */ |
||
| 430 | public function getQuestionType() |
||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * tells if answer is correct or not |
||
| 447 | * |
||
| 448 | * @author Olivier Brouckaert |
||
| 449 | * @param - integer $id - answer ID |
||
| 450 | * @return integer - 0 if bad answer, not 0 if good answer |
||
| 451 | */ |
||
| 452 | public function isCorrect($id) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * returns answer comment |
||
| 459 | * |
||
| 460 | * @author Olivier Brouckaert |
||
| 461 | * @param - integer $id - answer ID |
||
| 462 | * @return string - answer comment |
||
| 463 | */ |
||
| 464 | public function selectComment($id) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * returns answer weighting |
||
| 471 | * |
||
| 472 | * @author Olivier Brouckaert |
||
| 473 | * @param - integer $id - answer ID |
||
| 474 | * @param integer $id |
||
| 475 | * @return integer - answer weighting |
||
| 476 | */ |
||
| 477 | public function selectWeighting($id) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * returns answer position |
||
| 484 | * |
||
| 485 | * @author Olivier Brouckaert |
||
| 486 | * @param - integer $id - answer ID |
||
| 487 | * @return integer - answer position |
||
| 488 | */ |
||
| 489 | function selectPosition($id) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * returns answer hotspot coordinates |
||
| 496 | * |
||
| 497 | * @author Olivier Brouckaert |
||
| 498 | * @param integer Answer ID |
||
| 499 | * @param integer $id |
||
| 500 | * @return integer Answer position |
||
| 501 | */ |
||
| 502 | public function selectHotspotCoordinates($id) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * returns answer hotspot type |
||
| 509 | * |
||
| 510 | * @author Toon Keppens |
||
| 511 | * @param integer Answer ID |
||
| 512 | * @param integer $id |
||
| 513 | * @return integer Answer position |
||
| 514 | */ |
||
| 515 | public function selectHotspotType($id) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Creates a new answer |
||
| 522 | * |
||
| 523 | * @author Olivier Brouckaert |
||
| 524 | * @param string $answer answer title |
||
| 525 | * @param integer $correct 0 if bad answer, not 0 if good answer |
||
| 526 | * @param string $comment answer comment |
||
| 527 | * @param integer $weighting answer weighting |
||
| 528 | * @param integer $position answer position |
||
| 529 | * @param array $new_hotspot_coordinates Coordinates for hotspot exercises (optional) |
||
| 530 | * @param integer $new_hotspot_type Type for hotspot exercises (optional) |
||
| 531 | * @param string $destination |
||
| 532 | */ |
||
| 533 | public function createAnswer( |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Updates an answer |
||
| 557 | * |
||
| 558 | * @author Toon Keppens |
||
| 559 | * @param int $iid |
||
| 560 | * @param string $answer |
||
| 561 | * @param string $comment |
||
| 562 | * @param string $correct |
||
| 563 | * @param string $weighting |
||
| 564 | * @param string $position |
||
| 565 | * @param string $destination |
||
| 566 | * @param string $hotspot_coordinates |
||
| 567 | * @param string $hotspot_type |
||
| 568 | */ |
||
| 569 | public function updateAnswers( |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Records answers into the data base |
||
| 598 | * |
||
| 599 | * @author Olivier Brouckaert |
||
| 600 | */ |
||
| 601 | public function save() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Duplicates answers by copying them into another question |
||
| 733 | * |
||
| 734 | * @author Olivier Brouckaert |
||
| 735 | * @param int question id |
||
| 736 | * @param array destination course info (result of the function api_get_course_info() ) |
||
| 737 | * @param string $newQuestionId |
||
| 738 | */ |
||
| 739 | public function duplicate($newQuestionId, $course_info = null) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Get the necessary JavaScript for some answers |
||
| 832 | * @return string |
||
| 833 | */ |
||
| 834 | public function getJs() |
||
| 845 | |||
| 846 | } |
||
| 847 |