Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | class LearnpathLink extends AbstractLink |
||
| 11 | { |
||
| 12 | private $course_info = null; |
||
|
|
|||
| 13 | private $learnpath_table = null; |
||
| 14 | private $learnpath_data = null; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Constructor |
||
| 18 | */ |
||
| 19 | public function __construct() |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Generate an array of learnpaths that a teacher hasn't created a link for. |
||
| 27 | * @return array 2-dimensional array - every element contains 2 subelements (id, name) |
||
| 28 | */ |
||
| 29 | public function get_not_created_links() |
||
| 30 | { |
||
| 31 | return false; |
||
| 32 | if (empty($this->course_code)) { |
||
| 33 | die('Error in get_not_created_links() : course code not set'); |
||
| 34 | } |
||
| 35 | |||
| 36 | $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK); |
||
| 37 | |||
| 38 | $sql = 'SELECT id, name FROM '.$this->get_learnpath_table().' lp |
||
| 39 | WHERE c_id = ' . $this->course_id.' AND id NOT IN ' |
||
| 40 | . ' (SELECT ref_id FROM '.$tbl_grade_links |
||
| 41 | . ' WHERE type = '.LINK_LEARNPATH |
||
| 42 | . " AND course_code = '".$this->get_course_code()."'" |
||
| 43 | . ') AND lp.session_id='.api_get_session_id().''; |
||
| 44 | |||
| 45 | $result = Database::query($sql); |
||
| 46 | |||
| 47 | $cats = array(); |
||
| 48 | while ($data = Database::fetch_array($result)) { |
||
| 49 | $cats[] = array($data['id'], $data['name']); |
||
| 50 | } |
||
| 51 | |||
| 52 | return $cats; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Generate an array of all learnpaths available. |
||
| 57 | * @return array 2-dimensional array - every element contains 2 subelements (id, name) |
||
| 58 | */ |
||
| 59 | public function get_all_links() |
||
| 60 | { |
||
| 61 | if (empty($this->course_code)) { |
||
| 62 | die('Error in get_not_created_links() : course code not set'); |
||
| 63 | } |
||
| 64 | |||
| 65 | $session_id = api_get_session_id(); |
||
| 66 | View Code Duplication | if (empty($session_id)) { |
|
| 67 | $session_condition = api_get_session_condition(0, true); |
||
| 68 | } else { |
||
| 69 | $session_condition = api_get_session_condition($session_id, true, true); |
||
| 70 | } |
||
| 71 | |||
| 72 | $sql = 'SELECT id, name FROM '.$this->get_learnpath_table().' |
||
| 73 | WHERE c_id = ' . $this->course_id.' '.$session_condition.' '; |
||
| 74 | $result = Database::query($sql); |
||
| 75 | |||
| 76 | $cats = array(); |
||
| 77 | while ($data = Database::fetch_array($result)) { |
||
| 78 | $cats[] = array($data['id'], $data['name']); |
||
| 79 | } |
||
| 80 | |||
| 81 | return $cats; |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Has anyone used this learnpath yet ? |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function has_results() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get the progress of this learnpath. Only the last attempt are taken into account. |
||
| 100 | * @param $stud_id student id (default: all students who have results - then the average is returned) |
||
| 101 | * @param $type The type of score we want to get: best|average|ranking |
||
| 102 | * @return array (score, max) if student is given |
||
| 103 | * array (sum of scores, number of scores) otherwise |
||
| 104 | * or null if no scores available |
||
| 105 | */ |
||
| 106 | public function calc_score($stud_id = null, $type = null) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get URL where to go to if the user clicks on the link. |
||
| 180 | */ |
||
| 181 | public function get_link() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get name to display: same as learnpath title |
||
| 197 | */ |
||
| 198 | public function get_name() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get description to display: same as learnpath description |
||
| 206 | */ |
||
| 207 | public function get_description() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Check if this still links to a learnpath |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function is_valid_link() |
|
| 224 | |||
| 225 | public function get_type_name() |
||
| 229 | |||
| 230 | public function needs_name_and_description() |
||
| 234 | |||
| 235 | public function needs_max() |
||
| 239 | |||
| 240 | public function needs_results() |
||
| 244 | |||
| 245 | public function is_allowed_to_change_name() |
||
| 249 | |||
| 250 | // INTERNAL FUNCTIONS |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Lazy load function to get the database table of the learnpath |
||
| 254 | */ |
||
| 255 | private function get_learnpath_table() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Lazy load function to get the database contents of this learnpath |
||
| 263 | */ |
||
| 264 | private function get_learnpath_data() |
||
| 274 | |||
| 275 | public function get_icon_name() |
||
| 279 | } |
||
| 280 |
This check marks private properties in classes that are never used. Those properties can be removed.