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 Display 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 Display, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Display |
||
| 24 | { |
||
| 25 | /** @var Template */ |
||
| 26 | public static $global_template; |
||
| 27 | public static $preview_style = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public static function toolList() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Displays the page header |
||
| 56 | * |
||
| 57 | * @deprecated See template.lib.php class documentation |
||
| 58 | * |
||
| 59 | * @param string The name of the page (will be showed in the page title) |
||
| 60 | * @param string Optional help file name |
||
| 61 | * @param string $page_header |
||
| 62 | */ |
||
| 63 | public static function display_header($tool_name ='', $help = null, $page_header = null) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Displays the reduced page header (without banner) |
||
| 109 | * @deprecated See template.lib.php class documentation |
||
| 110 | */ |
||
| 111 | View Code Duplication | public static function display_reduced_header() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Display no header |
||
| 121 | * @deprecated |
||
| 122 | */ |
||
| 123 | View Code Duplication | public static function display_no_header() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Displays the reduced page header (without banner) |
||
| 133 | * @deprecated See template.lib.php class documentation |
||
| 134 | */ |
||
| 135 | public static function set_header() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Display the page footer |
||
| 144 | * @deprecated See template.lib.php class documentation |
||
| 145 | */ |
||
| 146 | public static function display_footer() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Display the page footer |
||
| 154 | * @deprecated See template.lib.php class documentation |
||
| 155 | */ |
||
| 156 | public static function display_reduced_footer() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Displays the tool introduction of a tool. |
||
| 165 | * |
||
| 166 | * @author Patrick Cool <[email protected]>, Ghent University |
||
| 167 | * @param string $tool These are the constants that are used for indicating the tools. |
||
| 168 | * @param array $editor_config Optional configuration settings for the online editor. |
||
| 169 | * return: $tool return a string array list with the "define" in main_api.lib |
||
| 170 | * @return html code for adding an introduction |
||
| 171 | */ |
||
| 172 | public static function display_introduction_section($tool, $editor_config = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $tool |
||
| 179 | * @param array $editor_config |
||
| 180 | * @return null |
||
| 181 | */ |
||
| 182 | public static function return_introduction_section($tool, $toolList = null) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Displays a localised html file |
||
| 357 | * tries to show the file "$full_file_name"."_".$language_interface.".html" |
||
| 358 | * and if this does not exist, shows the file "$full_file_name".".html" |
||
| 359 | * warning this public function defines a global |
||
| 360 | * @param $full_file_name, the (path) name of the file, without .html |
||
| 361 | * @return return a string with the path |
||
| 362 | */ |
||
| 363 | public static function display_localised_html_file($full_file_name) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Displays a table |
||
| 377 | * @param array $header Titles for the table header |
||
| 378 | * each item in this array can contain 3 values |
||
| 379 | * - 1st element: the column title |
||
| 380 | * - 2nd element: true or false (column sortable?) |
||
| 381 | * - 3th element: additional attributes for |
||
| 382 | * th-tag (eg for column-width) |
||
| 383 | * - 4the element: additional attributes for the td-tags |
||
| 384 | * @param array $content 2D-array with the tables content |
||
| 385 | * @param array $sorting_options Keys are: |
||
| 386 | * 'column' = The column to use as sort-key |
||
| 387 | * 'direction' = SORT_ASC or SORT_DESC |
||
| 388 | * @param array $paging_options Keys are: |
||
| 389 | * 'per_page_default' = items per page when switching from |
||
| 390 | * full- list to per-page-view |
||
| 391 | * 'per_page' = number of items to show per page |
||
| 392 | * 'page_nr' = The page to display |
||
| 393 | * @param array $query_vars Additional variables to add in the query-string |
||
| 394 | * @param string The style that the table will show. You can set 'table' or 'grid' |
||
| 395 | * @author [email protected] |
||
| 396 | */ |
||
| 397 | public static function display_sortable_table( |
||
| 431 | |||
| 432 | public static function return_sortable_table( |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Shows a nice grid |
||
| 458 | * @param string grid name (important to create css) |
||
| 459 | * @param array header content |
||
| 460 | * @param array array with the information to show |
||
| 461 | * @param array $paging_options Keys are: |
||
| 462 | * 'per_page_default' = items per page when switching from |
||
| 463 | * full- list to per-page-view |
||
| 464 | * 'per_page' = number of items to show per page |
||
| 465 | * 'page_nr' = The page to display |
||
| 466 | * 'hide_navigation' = true to hide the navigation |
||
| 467 | * @param array $query_vars Additional variables to add in the query-string |
||
| 468 | * @param array $form actions Additional variables to add in the query-string |
||
| 469 | * @param mixed An array with bool values to know which columns show. |
||
| 470 | * i.e: $visibility_options= array(true, false) we will only show the first column |
||
| 471 | * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing |
||
| 472 | */ |
||
| 473 | public static function display_sortable_grid( |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Gets a nice grid in html string |
||
| 499 | * @param string grid name (important to create css) |
||
| 500 | * @param array header content |
||
| 501 | * @param array array with the information to show |
||
| 502 | * @param array $paging_options Keys are: |
||
| 503 | * 'per_page_default' = items per page when switching from |
||
| 504 | * full- list to per-page-view |
||
| 505 | * 'per_page' = number of items to show per page |
||
| 506 | * 'page_nr' = The page to display |
||
| 507 | * 'hide_navigation' = true to hide the navigation |
||
| 508 | * @param array $query_vars Additional variables to add in the query-string |
||
| 509 | * @param array $form actions Additional variables to add in the query-string |
||
| 510 | * @param mixed An array with bool values to know which columns show. i.e: |
||
| 511 | * $visibility_options= array(true, false) we will only show the first column |
||
| 512 | * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing |
||
| 513 | * @param bool true for sorting data or false otherwise |
||
| 514 | * @param array grid classes |
||
| 515 | * @return string html grid |
||
| 516 | */ |
||
| 517 | public static function return_sortable_grid( |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Displays a table with a special configuration |
||
| 549 | * @param array $header Titles for the table header |
||
| 550 | * each item in this array can contain 3 values |
||
| 551 | * - 1st element: the column title |
||
| 552 | * - 2nd element: true or false (column sortable?) |
||
| 553 | * - 3th element: additional attributes for |
||
| 554 | * th-tag (eg for column-width) |
||
| 555 | * - 4the element: additional attributes for the td-tags |
||
| 556 | * @param array $content 2D-array with the tables content |
||
| 557 | * @param array $sorting_options Keys are: |
||
| 558 | * 'column' = The column to use as sort-key |
||
| 559 | * 'direction' = SORT_ASC or SORT_DESC |
||
| 560 | * @param array $paging_options Keys are: |
||
| 561 | * 'per_page_default' = items per page when switching from |
||
| 562 | * full- list to per-page-view |
||
| 563 | * 'per_page' = number of items to show per page |
||
| 564 | * 'page_nr' = The page to display |
||
| 565 | * @param array $query_vars Additional variables to add in the query-string |
||
| 566 | * @param array $column_show Array of binaries 1= show columns 0. hide a column |
||
| 567 | * @param array $column_order An array of integers that let us decide how the columns are going to be sort. |
||
| 568 | * i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column |
||
| 569 | * @param array $form_actions Set optional forms actions |
||
| 570 | * |
||
| 571 | * @author Julio Montoya |
||
| 572 | */ |
||
| 573 | public static function display_sortable_config_table( |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Displays a normal message. It is recommended to use this public function |
||
| 617 | * to display any normal information messages. |
||
| 618 | * @param string $message |
||
| 619 | * @param bool $filter (true) or not (false) |
||
| 620 | * @param bool $returnValue |
||
| 621 | * |
||
| 622 | * @deprecated use Display::addFlash with Display::return_message($message, 'normal'); |
||
| 623 | * |
||
| 624 | * @return void |
||
| 625 | */ |
||
| 626 | View Code Duplication | public static function display_normal_message($message, $filter = true, $returnValue = false) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Displays an warning message. Use this if you want to draw attention to something |
||
| 638 | * This can also be used for instance with the hint in the exercises |
||
| 639 | * |
||
| 640 | * @deprecated use Display::addFlash with Display::return_message |
||
| 641 | */ |
||
| 642 | View Code Duplication | public static function display_warning_message($message, $filter = true, $returnValue = false) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Displays an confirmation message. Use this if something has been done successfully |
||
| 654 | * @param bool Filter (true) or not (false) |
||
| 655 | * @deprecated use Display::addFlash with Display::return_message |
||
| 656 | * @return void |
||
| 657 | */ |
||
| 658 | View Code Duplication | public static function display_confirmation_message ($message, $filter = true, $returnValue = false) |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Displays an error message. It is recommended to use this public function if an error occurs |
||
| 670 | * @param string $message - include any additional html |
||
| 671 | * tags if you need them |
||
| 672 | * @param bool Filter (true) or not (false) |
||
| 673 | * @deprecated use Display::addFlash with Display::return_message |
||
| 674 | * |
||
| 675 | * @return void |
||
| 676 | */ |
||
| 677 | View Code Duplication | public static function display_error_message ($message, $filter = true, $returnValue = false) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * @param string $message |
||
| 689 | * @param string $type |
||
| 690 | * @param bool $filter |
||
| 691 | */ |
||
| 692 | public static function return_message_and_translate($message, $type='normal', $filter = true) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Returns a div html string with |
||
| 700 | * @param string $message |
||
| 701 | * @param string $type Example: confirm, normal, warning, error |
||
| 702 | * @param bool $filter Whether to XSS-filter or not |
||
| 703 | * @return string Message wrapped into an HTML div |
||
| 704 | */ |
||
| 705 | public static function return_message($message, $type = 'normal', $filter = true) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Returns an encrypted mailto hyperlink |
||
| 738 | * |
||
| 739 | * @param string e-mail |
||
| 740 | * @param string clickable text |
||
| 741 | * @param string optional, class from stylesheet |
||
| 742 | * @return string encrypted mailto hyperlink |
||
| 743 | */ |
||
| 744 | public static function encrypted_mailto_link($email, $clickable_text = null, $style_class = '') |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Returns an mailto icon hyperlink |
||
| 779 | * |
||
| 780 | * @param string e-mail |
||
| 781 | * @param string icon source file from the icon lib |
||
| 782 | * @param integer icon size from icon lib |
||
| 783 | * @param string optional, class from stylesheet |
||
| 784 | * @return string encrypted mailto hyperlink |
||
| 785 | */ |
||
| 786 | public static function icon_mailto_link($email, $icon_file = "mail.png", $icon_size = 22, $style_class = '') |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Prints an <option>-list with all letters (A-Z). |
||
| 811 | * @param char $selected_letter The letter that should be selected |
||
| 812 | * @todo This is English language specific implementation. |
||
| 813 | * It should be adapted for the other languages. |
||
| 814 | */ |
||
| 815 | View Code Duplication | public static function get_alphabet_options($selected_letter = '') |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Get the options withing a select box within the given values |
||
| 831 | * @param int Min value |
||
| 832 | * @param int Max value |
||
| 833 | * @param int Default value |
||
| 834 | * @return string HTML select options |
||
| 835 | */ |
||
| 836 | View Code Duplication | public static function get_numeric_options($min, $max, $selected_num = 0) |
|
| 849 | |||
| 850 | /** |
||
| 851 | * This public function displays an icon |
||
| 852 | * @param string The filename of the file (in the main/img/ folder |
||
| 853 | * @param string The alt text (probably a language variable) |
||
| 854 | * @param array additional attributes (for instance height, width, onclick, ...) |
||
| 855 | * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder) |
||
| 856 | * @return void |
||
| 857 | */ |
||
| 858 | public static function display_icon( |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Gets the path of an icon |
||
| 869 | * |
||
| 870 | * @param string $icon |
||
| 871 | * @param int $size |
||
| 872 | * |
||
| 873 | * @return string |
||
| 874 | */ |
||
| 875 | public static function returnIconPath($icon, $size = ICON_SIZE_SMALL) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * This public function returns the htmlcode for an icon |
||
| 882 | * |
||
| 883 | * @param string The filename of the file (in the main/img/ folder |
||
| 884 | * @param string The alt text (probably a language variable) |
||
| 885 | * @param array Additional attributes (for instance height, width, onclick, ...) |
||
| 886 | * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder) |
||
| 887 | * @return string An HTML string of the right <img> tag |
||
| 888 | * |
||
| 889 | * @author Patrick Cool <[email protected]>, Ghent University 2006 |
||
| 890 | * @author Julio Montoya 2010 Function improved, adding image constants |
||
| 891 | * @author Yannick Warnier 2011 Added size handler |
||
| 892 | * @version Feb 2011 |
||
| 893 | */ |
||
| 894 | public static function return_icon( |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Returns the htmlcode for an image |
||
| 928 | * |
||
| 929 | * @param string $image_path the filename of the file (in the main/img/ folder |
||
| 930 | * @param string $alt_text the alt text (probably a language variable) |
||
| 931 | * @param array $additional_attributes (for instance height, width, onclick, ...) |
||
| 932 | * @param boolean $filterPath Optional. Whether filter the image path. Default is true |
||
| 933 | * @author Julio Montoya 2010 |
||
| 934 | */ |
||
| 935 | public static function img($image_path, $alt_text = '', $additional_attributes = array(), $filterPath = true) |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Returns the htmlcode for a tag (h3, h1, div, a, button), etc |
||
| 961 | * |
||
| 962 | * @param string $tag the tag name |
||
| 963 | * @param string $content the tag's content |
||
| 964 | * @param array $additional_attributes (for instance height, width, onclick, ...) |
||
| 965 | * @author Julio Montoya 2010 |
||
| 966 | */ |
||
| 967 | public static function tag($tag, $content, $additional_attributes = array()) |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Creates a URL anchor |
||
| 988 | * @param string $name |
||
| 989 | * @param string $url |
||
| 990 | * @param array $attributes |
||
| 991 | * |
||
| 992 | * @return string |
||
| 993 | */ |
||
| 994 | public static function url($name, $url, $attributes = array()) |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Creates a div tag |
||
| 1006 | * |
||
| 1007 | * @param string $content |
||
| 1008 | * @param array $attributes |
||
| 1009 | * @return string |
||
| 1010 | */ |
||
| 1011 | public static function div($content, $attributes = array()) |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Creates a span tag |
||
| 1018 | */ |
||
| 1019 | public static function span($content, $attributes = array()) |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Displays an HTML input tag |
||
| 1026 | * |
||
| 1027 | */ |
||
| 1028 | public static function input($type, $name, $value, $attributes = array()) |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * @param $name |
||
| 1044 | * @param $value |
||
| 1045 | * @param array $attributes |
||
| 1046 | * @return string |
||
| 1047 | */ |
||
| 1048 | public static function button($name, $value, $attributes = array()) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Displays an HTML select tag |
||
| 1058 | * |
||
| 1059 | */ |
||
| 1060 | public static function select( |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Creates a tab menu |
||
| 1116 | * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css |
||
| 1117 | * in the $htmlHeadXtra variable before the display_header |
||
| 1118 | * Add this script |
||
| 1119 | * @example |
||
| 1120 | * <script> |
||
| 1121 | $(function() { |
||
| 1122 | $( "#tabs" ).tabs(); |
||
| 1123 | }); |
||
| 1124 | </script> |
||
| 1125 | * @param array $headers list of the tab titles |
||
| 1126 | * @param array $items |
||
| 1127 | * @param string $id id of the container of the tab in the example "tabs" |
||
| 1128 | * @param array $attributes for the ul |
||
| 1129 | * @param array $ul_attributes |
||
| 1130 | * |
||
| 1131 | * @return string |
||
| 1132 | */ |
||
| 1133 | public static function tabs($headers, $items, $id = 'tabs', $attributes = array(), $ul_attributes = array()) |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * @param $headers |
||
| 1180 | * @param null $selected |
||
| 1181 | * |
||
| 1182 | * @return string |
||
| 1183 | */ |
||
| 1184 | public static function tabsOnlyLink($headers, $selected = null) |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * In order to display a grid using jqgrid you have to: |
||
| 1207 | * @example |
||
| 1208 | * After your Display::display_header function you have to add the nex javascript code: * |
||
| 1209 | * <script> |
||
| 1210 | * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,array()); |
||
| 1211 | * // for more information of this function check the grid_js() function |
||
| 1212 | * </script> |
||
| 1213 | * //Then you have to call the grid_html |
||
| 1214 | * echo Display::grid_html('my_grid_name'); |
||
| 1215 | * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work |
||
| 1216 | * |
||
| 1217 | * @param string the div id, this value must be the same with the first parameter of Display::grid_js() |
||
| 1218 | * @return string html |
||
| 1219 | * |
||
| 1220 | */ |
||
| 1221 | public static function grid_html($div_id) |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * @param string $label |
||
| 1230 | * @param string $form_item |
||
| 1231 | * @return string |
||
| 1232 | */ |
||
| 1233 | public static function form_row($label, $form_item) |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * This is a wrapper to use the jqgrid in Chamilo. |
||
| 1242 | * For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options |
||
| 1243 | * This function need to be in the ready jquery function |
||
| 1244 | * example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> } |
||
| 1245 | * In order to work this function needs the Display::grid_html function with the same div id |
||
| 1246 | * |
||
| 1247 | * @param string $div_id div id |
||
| 1248 | * @param string $url url where the jqgrid will ask for data (if datatype = json) |
||
| 1249 | * @param array $column_names Visible columns (you should use get_lang). An array in which we place the names of the columns. |
||
| 1250 | * This is the text that appears in the head of the grid (Header layer). |
||
| 1251 | * Example: colname {name:'date', index:'date', width:120, align:'right'}, |
||
| 1252 | * @param array $column_model the column model : Array which describes the parameters of the columns.This is the most important part of the grid. |
||
| 1253 | * For a full description of all valid values see colModel API. See the url above. |
||
| 1254 | * @param array $extra_params extra parameters |
||
| 1255 | * @param array $data data that will be loaded |
||
| 1256 | * @param string $formatter A string that will be appended to the JSON returned |
||
| 1257 | * @param bool $fixed_width not implemented yet |
||
| 1258 | * @return string the js code |
||
| 1259 | * |
||
| 1260 | */ |
||
| 1261 | public static function grid_js( |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * @param array $headers |
||
| 1405 | * @param array $rows |
||
| 1406 | * @param array $attributes |
||
| 1407 | * @return string |
||
| 1408 | */ |
||
| 1409 | public static function table($headers, $rows, $attributes = array()) |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Returns the "what's new" icon notifications |
||
| 1439 | * |
||
| 1440 | * The general logic of this function is to track the last time the user |
||
| 1441 | * entered the course and compare to what has changed inside this course |
||
| 1442 | * since then, based on the item_property table inside this course. Note that, |
||
| 1443 | * if the user never entered the course before, he will not see notification |
||
| 1444 | * icons. This function takes session ID into account (if any) and only shows |
||
| 1445 | * the corresponding notifications. |
||
| 1446 | * @param array $course_info Course information array, containing at least elements 'db' and 'k' |
||
| 1447 | * @param bool $loadAjax |
||
| 1448 | * @return string The HTML link to be shown next to the course |
||
| 1449 | */ |
||
| 1450 | public static function show_notification($course_info, $loadAjax = true) |
||
| 1451 | { |
||
| 1452 | if (empty($course_info)) { |
||
| 1453 | return ''; |
||
| 1454 | } |
||
| 1455 | |||
| 1456 | $t_track_e_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); |
||
| 1457 | $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST); |
||
| 1458 | $tool_edit_table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1459 | $course_code = Database::escape_string($course_info['code']); |
||
| 1460 | |||
| 1461 | $user_id = api_get_user_id(); |
||
| 1462 | $course_id = (int) $course_info['real_id']; |
||
| 1463 | $sessionId = (int) $course_info['id_session']; |
||
| 1464 | $status = (int) $course_info['status']; |
||
| 1465 | |||
| 1466 | $loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax'); |
||
| 1467 | |||
| 1468 | if ($loadNotificationsByAjax) { |
||
| 1469 | if ($loadAjax) { |
||
| 1470 | $id = 'notification_'.$course_id.'_'.$sessionId.'_'.$status; |
||
| 1471 | Session::write($id, true); |
||
| 1472 | |||
| 1473 | return '<span id ="'.$id.'" class="course_notification"></span>'; |
||
| 1474 | } |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | // Get the user's last access dates to all tools of this course |
||
| 1478 | $sql = "SELECT * |
||
| 1479 | FROM $t_track_e_access |
||
| 1480 | WHERE |
||
| 1481 | c_id = $course_id AND |
||
| 1482 | access_user_id = '$user_id' AND |
||
| 1483 | access_session_id ='".$sessionId."'"; |
||
| 1484 | $resLastTrackInCourse = Database::query($sql); |
||
| 1485 | |||
| 1486 | $oldestTrackDate = $oldestTrackDateOrig = '3000-01-01 00:00:00'; |
||
| 1487 | while ($lastTrackInCourse = Database::fetch_array($resLastTrackInCourse)) { |
||
| 1488 | $lastTrackInCourseDate[$lastTrackInCourse['access_tool']] = $lastTrackInCourse['access_date']; |
||
| 1489 | if ($oldestTrackDate > $lastTrackInCourse['access_date']) { |
||
| 1490 | $oldestTrackDate = $lastTrackInCourse['access_date']; |
||
| 1491 | } |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | if ($oldestTrackDate == $oldestTrackDateOrig) { |
||
| 1495 | //if there was no connexion to the course ever, then take the |
||
| 1496 | // course creation date as a reference |
||
| 1497 | $oldestTrackDate = $course_info['creation_date']; |
||
| 1498 | } |
||
| 1499 | |||
| 1500 | $sessionCondition = api_get_session_condition( |
||
| 1501 | $sessionId, |
||
| 1502 | true, |
||
| 1503 | false, |
||
| 1504 | 'session_id' |
||
| 1505 | ); |
||
| 1506 | |||
| 1507 | $hideTools = [TOOL_NOTEBOOK, TOOL_CHAT]; |
||
| 1508 | // Get current tools in course |
||
| 1509 | $sql = "SELECT name, link, image |
||
| 1510 | FROM $course_tool_table |
||
| 1511 | WHERE |
||
| 1512 | c_id = $course_id AND |
||
| 1513 | visibility = '1' AND |
||
| 1514 | name NOT IN ('".implode("','", $hideTools)."') |
||
| 1515 | "; |
||
| 1516 | $result = Database::query($sql); |
||
| 1517 | $tools = Database::store_result($result); |
||
| 1518 | |||
| 1519 | $group_ids = GroupManager::get_group_ids($course_info['real_id'], $user_id); |
||
| 1520 | $group_ids[] = 0; //add group 'everyone' |
||
| 1521 | $notifications = array(); |
||
| 1522 | if ($tools) { |
||
| 1523 | foreach ($tools as $tool) { |
||
| 1524 | $toolName = $tool['name']; |
||
| 1525 | // Fix to get student publications |
||
| 1526 | if ($toolName == 'student_publication') { |
||
| 1527 | $toolName = 'work'; |
||
| 1528 | } |
||
| 1529 | $toolName = Database::escape_string($toolName); |
||
| 1530 | $sql = "SELECT * FROM $tool_edit_table |
||
| 1531 | WHERE |
||
| 1532 | c_id = $course_id AND |
||
| 1533 | tool = '$toolName' AND |
||
| 1534 | lastedit_type NOT LIKE '%Deleted%' AND |
||
| 1535 | lastedit_type NOT LIKE '%deleted%' AND |
||
| 1536 | lastedit_type NOT LIKE '%DocumentInvisible%' AND |
||
| 1537 | lastedit_date > '$oldestTrackDate' AND |
||
| 1538 | lastedit_user_id != $user_id $sessionCondition AND |
||
| 1539 | visibility != 2 AND |
||
| 1540 | (to_user_id IN ('$user_id', '0') OR to_user_id IS NULL) AND |
||
| 1541 | (to_group_id IN ('".implode("','",$group_ids)."') OR to_group_id IS NULL) |
||
| 1542 | ORDER BY lastedit_date DESC |
||
| 1543 | LIMIT 1"; |
||
| 1544 | |||
| 1545 | $result = Database::query($sql); |
||
| 1546 | $latestChange = Database::fetch_array($result); |
||
| 1547 | if ($latestChange) { |
||
| 1548 | $latestChange['link'] = $tool['link']; |
||
| 1549 | $latestChange['image'] = $tool['image']; |
||
| 1550 | $latestChange['tool'] = $tool['name']; |
||
| 1551 | $notifications[$toolName] = $latestChange; |
||
| 1552 | } |
||
| 1553 | } |
||
| 1554 | } |
||
| 1555 | |||
| 1556 | // Show all tool icons where there is something new. |
||
| 1557 | $return = ' '; |
||
| 1558 | foreach ($notifications as $notification) { |
||
| 1559 | $toolName = $notification['tool']; |
||
| 1560 | if (! |
||
| 1561 | ( |
||
| 1562 | $notification['visibility'] == '1' || |
||
| 1563 | ($status == '1' && $notification['visibility'] == '0') || |
||
| 1564 | !isset($notification['visibility']) |
||
| 1565 | ) |
||
| 1566 | ) { |
||
| 1567 | continue; |
||
| 1568 | } |
||
| 1569 | |||
| 1570 | if ($toolName == TOOL_SURVEY) { |
||
| 1571 | $survey_info = SurveyManager::get_survey($notification['ref'], 0, $course_code); |
||
| 1572 | if (!empty($survey_info)) { |
||
| 1573 | $invited_users = SurveyUtil::get_invited_users( |
||
| 1574 | $survey_info['code'], |
||
| 1575 | $course_code |
||
| 1576 | ); |
||
| 1577 | if (!in_array($user_id, $invited_users['course_users'])) { |
||
| 1578 | continue; |
||
| 1579 | } |
||
| 1580 | } |
||
| 1581 | } |
||
| 1582 | |||
| 1583 | if ($notification['tool'] == TOOL_LEARNPATH) { |
||
| 1584 | if (!learnpath::is_lp_visible_for_student($notification['ref'], $user_id, $course_code)) { |
||
| 1585 | continue; |
||
| 1586 | } |
||
| 1587 | } |
||
| 1588 | |||
| 1589 | if ($notification['tool'] == TOOL_DROPBOX) { |
||
| 1590 | $notification['link'] = 'dropbox/dropbox_download.php?id='.$notification['ref']; |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | if ($notification['tool'] == 'work' && |
||
| 1594 | $notification['lastedit_type'] == 'DirectoryCreated' |
||
| 1595 | ) { |
||
| 1596 | $notification['lastedit_type'] = 'WorkAdded'; |
||
| 1597 | } |
||
| 1598 | |||
| 1599 | $lastDate = api_get_local_time($notification['lastedit_date']); |
||
| 1600 | $type = $notification['lastedit_type']; |
||
| 1601 | if ($type == 'CalendareventVisible') { |
||
| 1602 | $type = 'Visible'; |
||
| 1603 | } |
||
| 1604 | $label = get_lang('TitleNotification').": ".get_lang($type)." ($lastDate)"; |
||
| 1605 | |||
| 1606 | if (strpos($notification['link'], '?') === false) { |
||
| 1607 | $notification['link'] = $notification['link'].'?notification=1'; |
||
| 1608 | } else { |
||
| 1609 | $notification['link'] = $notification['link'].'¬ification=1'; |
||
| 1610 | } |
||
| 1611 | |||
| 1612 | $image = substr($notification['image'], 0, -4).'.png'; |
||
| 1613 | |||
| 1614 | $return .= Display::url( |
||
| 1615 | Display::return_icon($image, $label), |
||
| 1616 | api_get_path(WEB_CODE_PATH). |
||
| 1617 | $notification['link'].'&cidReq='.$course_code. |
||
| 1618 | '&ref='.$notification['ref']. |
||
| 1619 | '&gidReq='.$notification['to_group_id']. |
||
| 1620 | '&id_session='.$sessionId |
||
| 1621 | ).' '; |
||
| 1622 | } |
||
| 1623 | |||
| 1624 | return $return; |
||
| 1625 | } |
||
| 1626 | |||
| 1627 | /** |
||
| 1628 | * Get the session box details as an array |
||
| 1629 | * @param int Session ID |
||
| 1630 | * @return array Empty array or session array |
||
| 1631 | * ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int] |
||
| 1632 | */ |
||
| 1633 | public static function get_session_title_box($session_id) |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Return the five star HTML |
||
| 1773 | * |
||
| 1774 | * @param string id of the rating ul element |
||
| 1775 | * @param string url that will be added (for jquery see hot_courses.tpl) |
||
| 1776 | * @param string point info array see function CourseManager::get_course_ranking() |
||
| 1777 | * @param bool add a div wrapper |
||
| 1778 | * @todo use templates |
||
| 1779 | **/ |
||
| 1780 | public static function return_rating_system($id, $url, $point_info = array(), $add_div_wrapper = true) |
||
| 1824 | |||
| 1825 | /** |
||
| 1826 | * @param string $title |
||
| 1827 | * @param string $second_title |
||
| 1828 | * @param string $size |
||
| 1829 | * @param bool $filter |
||
| 1830 | * @return string |
||
| 1831 | */ |
||
| 1832 | public static function page_header($title, $second_title = null, $size = 'h2', $filter = true) |
||
| 1846 | |||
| 1847 | public static function page_header_and_translate($title, $second_title = null) |
||
| 1852 | |||
| 1853 | public static function page_subheader_and_translate($title, $second_title = null) |
||
| 1858 | |||
| 1859 | public static function page_subheader($title, $second_title = null, $size = 'h3') |
||
| 1867 | |||
| 1868 | public static function page_subheader2($title, $second_title = null) |
||
| 1872 | |||
| 1873 | public static function page_subheader3($title, $second_title = null) |
||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * @param array $list |
||
| 1880 | * @return null|string |
||
| 1881 | */ |
||
| 1882 | public static function description($list) |
||
| 1895 | |||
| 1896 | /** |
||
| 1897 | * @param $percentage |
||
| 1898 | * @param bool $show_percentage |
||
| 1899 | * @param null $extra_info |
||
| 1900 | * @return string |
||
| 1901 | */ |
||
| 1902 | public static function bar_progress($percentage, $show_percentage = true, $extra_info = null) |
||
| 1925 | |||
| 1926 | /** |
||
| 1927 | * @param string $count |
||
| 1928 | * @param string $type |
||
| 1929 | * @return null|string |
||
| 1930 | */ |
||
| 1931 | public static function badge($count, $type ="warning") |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * @param array $badge_list |
||
| 1961 | * @return string |
||
| 1962 | */ |
||
| 1963 | public static function badge_group($badge_list) |
||
| 1972 | |||
| 1973 | /** |
||
| 1974 | * @param string $content |
||
| 1975 | * @param string $type |
||
| 1976 | * @return string |
||
| 1977 | */ |
||
| 1978 | public static function label($content, $type = 'default') |
||
| 2012 | |||
| 2013 | /** |
||
| 2014 | * @param array $items |
||
| 2015 | * @return null|string |
||
| 2016 | */ |
||
| 2017 | public static function actions($items, $class = 'new_actions') |
||
| 2042 | |||
| 2043 | /** |
||
| 2044 | * Prints a tooltip |
||
| 2045 | * @param string $text |
||
| 2046 | * @param string $tip |
||
| 2047 | * |
||
| 2048 | * @return string |
||
| 2049 | */ |
||
| 2050 | public static function tip($text, $tip) |
||
| 2057 | |||
| 2058 | /** |
||
| 2059 | * @param array $items |
||
| 2060 | * @param string $type |
||
| 2061 | * @param null $id |
||
| 2062 | * @return null|string |
||
| 2063 | */ |
||
| 2064 | public static function generate_accordion($items, $type = 'jquery', $id = null) |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * @param array $buttons |
||
| 2100 | * @return string |
||
| 2101 | */ |
||
| 2102 | public static function groupButton($buttons) |
||
| 2112 | |||
| 2113 | /** |
||
| 2114 | * @todo use twig |
||
| 2115 | */ |
||
| 2116 | public static function groupButtonWithDropDown($title, $elements) |
||
| 2130 | |||
| 2131 | /** |
||
| 2132 | * @param string $file |
||
| 2133 | * @param array $params |
||
| 2134 | * @return null|string |
||
| 2135 | */ |
||
| 2136 | public static function getMediaPlayer($file, $params = array()) |
||
| 2171 | |||
| 2172 | /** |
||
| 2173 | * |
||
| 2174 | * @param int $nextValue |
||
| 2175 | * @param array $list |
||
| 2176 | * @param int $current |
||
| 2177 | * @param int $fixedValue |
||
| 2178 | * @param array $conditions |
||
| 2179 | * @param string $link |
||
| 2180 | * @param bool $isMedia |
||
| 2181 | * @param bool $addHeaders |
||
| 2182 | * @return string |
||
| 2183 | */ |
||
| 2184 | public static function progressPaginationBar( |
||
| 2233 | /** |
||
| 2234 | * |
||
| 2235 | * @param int $itemId |
||
| 2236 | * @param bool $isCurrent |
||
| 2237 | * @param array $conditions |
||
| 2238 | * @param string $link |
||
| 2239 | * @param int $nextValue |
||
| 2240 | * @param bool $isMedia |
||
| 2241 | * @param int $localCounter |
||
| 2242 | * @param int $fixedValue |
||
| 2243 | * @return string |
||
| 2244 | */ |
||
| 2245 | public static function parsePaginationItem( |
||
| 2303 | |||
| 2304 | /** |
||
| 2305 | * @param int $current |
||
| 2306 | * @param int $total |
||
| 2307 | * @return string |
||
| 2308 | */ |
||
| 2309 | public static function paginationIndicator($current, $total) |
||
| 2318 | |||
| 2319 | /** |
||
| 2320 | * Adds a message in the queue |
||
| 2321 | * |
||
| 2322 | * @param string $message |
||
| 2323 | * @param string $type |
||
| 2324 | */ |
||
| 2325 | public static function addFlash($message, $type = 'no_layout') |
||
| 2329 | |||
| 2330 | /** |
||
| 2331 | * @deprecated |
||
| 2332 | * @return string |
||
| 2333 | */ |
||
| 2334 | public static function getFlashToString() |
||
| 2346 | |||
| 2347 | /** |
||
| 2348 | * @deprecated |
||
| 2349 | * Shows the message from the session |
||
| 2350 | */ |
||
| 2351 | public static function showFlash() |
||
| 2355 | |||
| 2356 | /** |
||
| 2357 | * Destroys the message session |
||
| 2358 | * @deprecated |
||
| 2359 | */ |
||
| 2360 | public static function cleanFlashMessages() |
||
| 2364 | |||
| 2365 | /** |
||
| 2366 | * Get the profile edition link for a user |
||
| 2367 | * @param int $userId The user id |
||
| 2368 | * @param boolean $asAdmin Optional. Whether get the URL for the platform admin |
||
| 2369 | * @return string The link |
||
| 2370 | */ |
||
| 2371 | public static function getProfileEditionLink($userId, $asAdmin = false) |
||
| 2405 | |||
| 2406 | /** |
||
| 2407 | * Get the vCard for a user |
||
| 2408 | * @param int $userId The user id |
||
| 2409 | * @return string *.*vcf file |
||
| 2410 | */ |
||
| 2411 | public static function getVCardUserLink($userId) |
||
| 2417 | |||
| 2418 | /** |
||
| 2419 | * @param string $content |
||
| 2420 | * @param string $title |
||
| 2421 | * @param string $footer |
||
| 2422 | * @param string $style primary|success|info|warning|danger |
||
| 2423 | * @param string $extra |
||
| 2424 | * |
||
| 2425 | * @return string |
||
| 2426 | */ |
||
| 2427 | public static function panel($content, $title = '', $footer = '', $style = '', $extra = '') |
||
| 2442 | |||
| 2443 | /** |
||
| 2444 | * @param string $content |
||
| 2445 | * @return string |
||
| 2446 | */ |
||
| 2447 | public static function contentPanel($content) |
||
| 2453 | |||
| 2454 | /** |
||
| 2455 | * Get the button HTML with an Awesome Font icon |
||
| 2456 | * @param string $text The button content |
||
| 2457 | * @param string $url The url to button |
||
| 2458 | * @param string $icon The Awesome Font class for icon |
||
| 2459 | * @param string $type Optional. The button Bootstrap class. Default 'default' class |
||
| 2460 | * @param array $attributes The additional attributes |
||
| 2461 | * @param bool $includeText |
||
| 2462 | * |
||
| 2463 | * @return string The button HTML |
||
| 2464 | */ |
||
| 2465 | public static function toolbarButton( |
||
| 2484 | |||
| 2485 | /** |
||
| 2486 | * @param int $id |
||
| 2487 | * @param array $content |
||
| 2488 | * @param int $col |
||
| 2489 | * @param bool|true $right |
||
| 2490 | * @return string |
||
| 2491 | */ |
||
| 2492 | public static function toolbarAction($id, $content = array(), $col = 2, $right = true) |
||
| 2524 | |||
| 2525 | /** |
||
| 2526 | * Get a HTML code for a icon by Font Awesome |
||
| 2527 | * @param string $name The icon name |
||
| 2528 | * @param int|string $size Optional. The size for the icon. (Example: lg, 2, 3, 4, 5) |
||
| 2529 | * @param boolean $fixWidth Optional. Whether add the fw class |
||
| 2530 | * @param string $additionalClass Optional. Additional class |
||
| 2531 | * |
||
| 2532 | * @return string |
||
| 2533 | */ |
||
| 2534 | public static function returnFontAwesomeIcon( |
||
| 2569 | |||
| 2570 | /** |
||
| 2571 | * @param string $title |
||
| 2572 | * @param string $content |
||
| 2573 | * @param null $id |
||
| 2574 | * @param array $params |
||
| 2575 | * @param null $idAccordion |
||
| 2576 | * @param null $idCollapse |
||
| 2577 | * @param bool|true $open |
||
| 2578 | * @param bool|false $fullClickable |
||
| 2579 | * @return null|string |
||
| 2580 | */ |
||
| 2581 | public static function panelCollapse( |
||
| 2627 | |||
| 2628 | /** |
||
| 2629 | * Returns the string "1 day ago" with a link showing the exact date time. |
||
| 2630 | * @param string $dateTime in UTC or a DateTime in UTC |
||
| 2631 | * |
||
| 2632 | * @return string |
||
| 2633 | */ |
||
| 2634 | public static function dateToStringAgoAndLongDate($dateTime) |
||
| 2649 | } |
||
| 2650 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: