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:
Complex classes like GlossaryManager 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 GlossaryManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class GlossaryManager |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Get all glossary terms |
||
| 21 | * @author Isaac Flores <[email protected]> |
||
| 22 | * @return array Contain glossary terms |
||
| 23 | */ |
||
| 24 | View Code Duplication | public static function get_glossary_terms() |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Get glossary term by glossary id |
||
| 45 | * @author Isaac Flores <[email protected]> |
||
| 46 | * @param int $glossary_id |
||
| 47 | * |
||
| 48 | * @return string The glossary description |
||
| 49 | */ |
||
| 50 | View Code Duplication | public static function get_glossary_term_by_glossary_id($glossary_id) |
|
| 51 | { |
||
| 52 | $glossary_table = Database::get_course_table(TABLE_GLOSSARY); |
||
| 53 | $course_id = api_get_course_int_id(); |
||
| 54 | $sql = "SELECT description FROM $glossary_table |
||
| 55 | WHERE c_id = $course_id AND glossary_id =".intval($glossary_id); |
||
| 56 | $rs = Database::query($sql); |
||
| 57 | if (Database::num_rows($rs) > 0) { |
||
| 58 | $row = Database::fetch_array($rs); |
||
| 59 | |||
| 60 | return $row['description']; |
||
| 61 | } else { |
||
| 62 | return ''; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get glossary term by glossary id |
||
| 68 | * @author Isaac Flores <[email protected]> |
||
| 69 | * @param string $glossary_name The glossary term name |
||
| 70 | * |
||
| 71 | * @return array The glossary info |
||
| 72 | */ |
||
| 73 | public static function get_glossary_term_by_glossary_name($glossary_name) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * This functions stores the glossary in the database |
||
| 95 | * |
||
| 96 | * @param array $values Array of title + description (name => $title, description => $comment) |
||
| 97 | * |
||
| 98 | * @return mixed Term id on success, false on failure |
||
| 99 | * |
||
| 100 | */ |
||
| 101 | public static function save_glossary($values, $showMessage = true) |
||
| 102 | { |
||
| 103 | if (!is_array($values) || !isset($values['name'])) { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Database table definition |
||
| 108 | $t_glossary = Database::get_course_table(TABLE_GLOSSARY); |
||
| 109 | |||
| 110 | // get the maximum display order of all the glossary items |
||
| 111 | $max_glossary_item = self::get_max_glossary_item(); |
||
| 112 | |||
| 113 | // session_id |
||
| 114 | $session_id = api_get_session_id(); |
||
| 115 | |||
| 116 | // check if the glossary term already exists |
||
| 117 | if (self::glossary_exists($values['name'])) { |
||
| 118 | // display the feedback message |
||
| 119 | if ($showMessage) { |
||
| 120 | Display::addFlash( |
||
| 121 | Display::return_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'), 'error') |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | return false; |
||
| 125 | } else { |
||
| 126 | $params = [ |
||
| 127 | 'glossary_id' => 0, |
||
| 128 | 'c_id' => api_get_course_int_id(), |
||
| 129 | 'name' => $values['name'], |
||
| 130 | 'description' => $values['description'], |
||
| 131 | 'display_order' => $max_glossary_item + 1, |
||
| 132 | 'session_id' => $session_id, |
||
| 133 | ]; |
||
| 134 | $id = Database::insert($t_glossary, $params); |
||
| 135 | |||
| 136 | View Code Duplication | if ($id) { |
|
| 137 | $sql = "UPDATE $t_glossary SET glossary_id = $id WHERE iid = $id"; |
||
| 138 | Database::query($sql); |
||
| 139 | |||
| 140 | //insert into item_property |
||
| 141 | api_item_property_update( |
||
| 142 | api_get_course_info(), |
||
| 143 | TOOL_GLOSSARY, |
||
| 144 | $id, |
||
| 145 | 'GlossaryAdded', |
||
| 146 | api_get_user_id() |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | // display the feedback message |
||
| 150 | if ($showMessage) { |
||
| 151 | Display::addFlash( |
||
| 152 | Display::return_message(get_lang('TermAdded')) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $id; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * update the information of a glossary term in the database |
||
| 162 | * |
||
| 163 | * @param array $values an array containing all the form elements |
||
| 164 | * @return boolean True on success, false on failure |
||
| 165 | */ |
||
| 166 | public static function update_glossary($values, $showMessage = true) |
||
| 167 | { |
||
| 168 | // Database table definition |
||
| 169 | $t_glossary = Database::get_course_table(TABLE_GLOSSARY); |
||
| 170 | $course_id = api_get_course_int_id(); |
||
| 171 | |||
| 172 | // check if the glossary term already exists |
||
| 173 | if (self::glossary_exists($values['name'], $values['glossary_id'])) { |
||
| 174 | // display the feedback message |
||
| 175 | if ($showMessage) { |
||
| 176 | Display::addFlash( |
||
| 177 | Display::return_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'), 'error') |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | return false; |
||
| 182 | } else { |
||
| 183 | $sql = "UPDATE $t_glossary SET |
||
| 184 | name = '".Database::escape_string($values['name'])."', |
||
| 185 | description = '".Database::escape_string($values['description'])."' |
||
| 186 | WHERE |
||
| 187 | c_id = $course_id AND |
||
| 188 | glossary_id = ".intval($values['glossary_id']); |
||
| 189 | $result = Database::query($sql); |
||
| 190 | if ($result === false) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | //update glossary into item_property |
||
| 195 | api_item_property_update( |
||
| 196 | api_get_course_info(), |
||
| 197 | TOOL_GLOSSARY, |
||
| 198 | intval($values['glossary_id']), |
||
| 199 | 'GlossaryUpdated', |
||
| 200 | api_get_user_id() |
||
| 201 | ); |
||
| 202 | |||
| 203 | if ($showMessage) { |
||
| 204 | // display the feedback message |
||
| 205 | Display::addFlash( |
||
| 206 | Display::return_message(get_lang('TermUpdated')) |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return true; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the maximum display order of the glossary item |
||
| 216 | * @return integer Maximum glossary display order |
||
| 217 | */ |
||
| 218 | public static function get_max_glossary_item() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * check if the glossary term exists or not |
||
| 239 | * |
||
| 240 | * @param string $term Term to look for |
||
| 241 | * @param integer $not_id ID to counter-check if the term exists with this ID as well (optional) |
||
| 242 | * @return bool True if term exists |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | public static function glossary_exists($term, $not_id = '') |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get one specific glossary term data |
||
| 269 | * |
||
| 270 | * @param integer $glossary_id ID of the flossary term |
||
| 271 | * @return mixed Array(glossary_id,name,description,glossary_display_order) or false on error |
||
| 272 | * |
||
| 273 | */ |
||
| 274 | public static function get_glossary_information($glossary_id) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Delete a glossary term (and re-order all the others) |
||
| 308 | * |
||
| 309 | * @param integer $glossary_id |
||
| 310 | * @param bool $showMessage |
||
| 311 | * |
||
| 312 | * @return bool True on success, false on failure |
||
| 313 | */ |
||
| 314 | public static function delete_glossary($glossary_id, $showMessage = true) |
||
| 315 | { |
||
| 316 | // Database table definition |
||
| 317 | $t_glossary = Database::get_course_table(TABLE_GLOSSARY); |
||
| 318 | $course_id = api_get_course_int_id(); |
||
| 319 | $glossaryInfo = self::get_glossary_information($glossary_id); |
||
| 320 | |||
| 321 | if (empty($glossaryInfo)) { |
||
| 322 | return false; |
||
| 323 | } |
||
| 324 | |||
| 325 | $glossary_id = (int) $glossary_id; |
||
| 326 | |||
| 327 | $sql = "DELETE FROM $t_glossary |
||
| 328 | WHERE |
||
| 329 | c_id = $course_id AND |
||
| 330 | glossary_id='".$glossary_id."'"; |
||
| 331 | $result = Database::query($sql); |
||
| 332 | if ($result === false || Database::affected_rows($result) < 1) { |
||
| 333 | return false; |
||
| 334 | } |
||
| 335 | |||
| 336 | // update item_property (delete) |
||
| 337 | api_item_property_update( |
||
| 338 | api_get_course_info(), |
||
| 339 | TOOL_GLOSSARY, |
||
| 340 | $glossary_id, |
||
| 341 | 'delete', |
||
| 342 | api_get_user_id() |
||
| 343 | ); |
||
| 344 | |||
| 345 | // reorder the remaining terms |
||
| 346 | self::reorder_glossary(); |
||
| 347 | |||
| 348 | if ($showMessage) { |
||
| 349 | Display::addFlash( |
||
| 350 | Display::return_message(get_lang('TermDeleted').': '.$glossaryInfo['name']) |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | |||
| 354 | return true; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * This is the main function that displays the list or the table with all |
||
| 359 | * the glossary terms |
||
| 360 | * @param string View ('table' or 'list'). Optional parameter. |
||
| 361 | * Defaults to 'table' and prefers glossary_view from the session by default. |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public static function display_glossary($view = 'table') |
||
| 366 | { |
||
| 367 | // This function should always be called with the corresponding |
||
| 368 | // parameter for view type. Meanwhile, use this cheap trick. |
||
| 369 | $view = Session::read('glossary_view'); |
||
| 370 | if (empty($view)) { |
||
| 371 | Session::write('glossary_view', $view); |
||
| 372 | } |
||
| 373 | // action links |
||
| 374 | //echo '<div class="actions">'; |
||
| 375 | $actionsLeft = ''; |
||
| 376 | View Code Duplication | if (api_is_allowed_to_edit(null, true)) { |
|
| 377 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=addglossary&msg=add?'.api_get_cidreq().'">'. |
||
| 378 | Display::return_icon('new_glossary_term.png', get_lang('TermAddNew'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 379 | } |
||
| 380 | |||
| 381 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=export">'. |
||
| 382 | Display::return_icon('export_csv.png', get_lang('ExportGlossaryAsCSV'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 383 | View Code Duplication | if (api_is_allowed_to_edit(null, true)) { |
|
| 384 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=import">'. |
||
| 385 | Display::return_icon('import_csv.png', get_lang('ImportGlossary'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 386 | } |
||
| 387 | |||
| 388 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=export_to_pdf">'. |
||
| 389 | Display::return_icon('pdf.png', get_lang('ExportToPDF'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 390 | |||
| 391 | if (($view == 'table') || (!isset($view))) { |
||
| 392 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=list">'. |
||
| 393 | Display::return_icon('view_detailed.png', get_lang('ListView'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 394 | } else { |
||
| 395 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=table">'. |
||
| 396 | Display::return_icon('view_text.png', get_lang('TableView'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 397 | } |
||
| 398 | |||
| 399 | /* BUILD SEARCH FORM */ |
||
| 400 | $form = new FormValidator( |
||
| 401 | 'search', |
||
| 402 | 'get', |
||
| 403 | api_get_self().'?'.api_get_cidreq(), |
||
| 404 | '', |
||
| 405 | array(), |
||
| 406 | FormValidator::LAYOUT_INLINE |
||
| 407 | ); |
||
| 408 | $form->addText('keyword', '', false, array('class' => 'col-md-2')); |
||
| 409 | $form->addElement('hidden', 'cidReq', api_get_course_id()); |
||
| 410 | $form->addElement('hidden', 'id_session', api_get_session_id()); |
||
| 411 | $form->addButtonSearch(get_lang('Search')); |
||
| 412 | $actionsRight = $form->returnForm(); |
||
| 413 | |||
| 414 | $toolbar = Display::toolbarAction( |
||
| 415 | 'toolbar-document', |
||
| 416 | array($actionsLeft, $actionsRight) |
||
| 417 | ); |
||
| 418 | |||
| 419 | $content = $toolbar; |
||
| 420 | |||
| 421 | if (!$view || $view === 'table') { |
||
| 422 | $table = new SortableTable( |
||
| 423 | 'glossary', |
||
| 424 | array('GlossaryManager', 'get_number_glossary_terms'), |
||
| 425 | array('GlossaryManager', 'get_glossary_data'), |
||
| 426 | 0 |
||
| 427 | ); |
||
| 428 | //$table->set_header(0, '', false); |
||
| 429 | $table->set_header(0, get_lang('TermName'), true); |
||
| 430 | $table->set_header(1, get_lang('TermDefinition'), true); |
||
| 431 | if (api_is_allowed_to_edit(null, true)) { |
||
| 432 | $table->set_header(2, get_lang('Actions'), false, 'width=90px', array('class' => 'td_actions')); |
||
| 433 | $table->set_column_filter(2, array('GlossaryManager', 'actions_filter')); |
||
| 434 | } |
||
| 435 | $content .= $table->return_table(); |
||
| 436 | } |
||
| 437 | |||
| 438 | if ($view === 'list') { |
||
| 439 | $content .= self::displayGlossaryList(); |
||
| 440 | } |
||
| 441 | |||
| 442 | return $content; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Display the glossary terms in a list |
||
| 447 | * @return bool true |
||
| 448 | */ |
||
| 449 | public static function displayGlossaryList() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Get the number of glossary terms in the course (or course+session) |
||
| 465 | * @param int Session ID filter (optional) |
||
| 466 | * @return integer Count of glossary terms |
||
| 467 | * |
||
| 468 | */ |
||
| 469 | public static function get_number_glossary_terms($session_id = 0) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get all the data of a glossary |
||
| 498 | * |
||
| 499 | * @param int $from From which item |
||
| 500 | * @param int $number_of_items Number of items to collect |
||
| 501 | * @param string $column Name of column on which to order |
||
| 502 | * @param string $direction Whether to sort in ascending (ASC) or descending (DESC) |
||
| 503 | * |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | public static function get_glossary_data($from, $number_of_items, $column, $direction) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Update action icons column |
||
| 582 | * |
||
| 583 | * @param integer $glossary_id |
||
| 584 | * @param array $url_params Parameters to use to affect links |
||
| 585 | * @param array $row The line of results from a query on the glossary table |
||
| 586 | * |
||
| 587 | * @return string HTML string for the action icons columns |
||
| 588 | */ |
||
| 589 | public static function actions_filter($glossary_id, $url_params, $row) |
||
| 590 | { |
||
| 591 | $glossary_id = $row[2]; |
||
| 592 | $return = '<a href="'.api_get_self().'?action=edit_glossary&glossary_id='.$glossary_id.'&'.api_get_cidreq().'&msg=edit">'. |
||
| 593 | Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>'; |
||
| 594 | $glossary_data = self::get_glossary_information($glossary_id); |
||
| 595 | $glossary_term = $glossary_data['name']; |
||
| 596 | if (api_is_allowed_to_edit(null, true)) { |
||
| 597 | View Code Duplication | if ($glossary_data['session_id'] == api_get_session_id()) { |
|
| 598 | $return .= '<a href="'.api_get_self().'?action=delete_glossary&glossary_id='.$glossary_id.'&'.api_get_cidreq().'" onclick="return confirmation(\''.$glossary_term.'\');">'. |
||
| 599 | Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a>'; |
||
| 600 | } else { |
||
| 601 | $return = get_lang('EditionNotAvailableFromSession'); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | |||
| 605 | return $return; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * a little bit of javascript to display a prettier warning when deleting a term |
||
| 610 | * |
||
| 611 | * @return string HTML string including JavaScript |
||
| 612 | * |
||
| 613 | */ |
||
| 614 | public static function javascript_glossary() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Re-order glossary |
||
| 629 | */ |
||
| 630 | public static function reorder_glossary() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Move a glossary term |
||
| 651 | * |
||
| 652 | * @param string $direction |
||
| 653 | * @param string $glossary_id |
||
| 654 | */ |
||
| 655 | public static function move_glossary($direction, $glossary_id) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Export to pdf |
||
| 697 | */ |
||
| 698 | public static function export_to_pdf() |
||
| 718 | } |
||
| 719 |