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 |
||
21 | class Display |
||
22 | { |
||
23 | /** @var Template */ |
||
24 | public static $global_template; |
||
25 | public static $preview_style = null; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | */ |
||
30 | public function __construct() |
||
33 | |||
34 | /** |
||
35 | * @return array |
||
36 | */ |
||
37 | public static function toolList() |
||
51 | |||
52 | /** |
||
53 | * Displays the page header |
||
54 | * @param string The name of the page (will be showed in the page title) |
||
55 | * @param string Optional help file name |
||
56 | * @param string $page_header |
||
57 | */ |
||
58 | public static function display_header($tool_name ='', $help = null, $page_header = null) |
||
99 | |||
100 | /** |
||
101 | * Displays the reduced page header (without banner) |
||
102 | */ |
||
103 | public static function display_reduced_header() |
||
109 | |||
110 | /** |
||
111 | * Display no header |
||
112 | */ |
||
113 | public static function display_no_header() |
||
119 | |||
120 | /** |
||
121 | * Displays the reduced page header (without banner) |
||
122 | */ |
||
123 | public static function set_header() |
||
128 | |||
129 | /** |
||
130 | * Display the page footer |
||
131 | */ |
||
132 | public static function display_footer() |
||
136 | |||
137 | /** |
||
138 | * Display the page footer |
||
139 | */ |
||
140 | public static function display_reduced_footer() |
||
141 | { |
||
142 | echo self::$global_template->show_footer_js_template(); |
||
143 | } |
||
144 | |||
145 | public static function page() |
||
149 | |||
150 | /** |
||
151 | * Displays the tool introduction of a tool. |
||
152 | * |
||
153 | * @author Patrick Cool <[email protected]>, Ghent University |
||
154 | * @param string $tool These are the constants that are used for indicating the tools. |
||
155 | * @param array $editor_config Optional configuration settings for the online editor. |
||
156 | * return: $tool return a string array list with the "define" in main_api.lib |
||
157 | * @return html code for adding an introduction |
||
158 | */ |
||
159 | public static function display_introduction_section($tool, $editor_config = null) |
||
163 | |||
164 | /** |
||
165 | * @param string $tool |
||
166 | * @param array $editor_config |
||
167 | * @return null |
||
168 | */ |
||
169 | public static function return_introduction_section($tool, $editor_config = null) |
||
179 | |||
180 | /** |
||
181 | * Displays a localised html file |
||
182 | * tries to show the file "$full_file_name"."_".$language_interface.".html" |
||
183 | * and if this does not exist, shows the file "$full_file_name".".html" |
||
184 | * warning this public function defines a global |
||
185 | * @param $full_file_name, the (path) name of the file, without .html |
||
186 | * @return return a string with the path |
||
187 | */ |
||
188 | public static function display_localised_html_file($full_file_name) |
||
199 | |||
200 | /** |
||
201 | * Displays a table |
||
202 | * @param array $header Titles for the table header |
||
203 | * each item in this array can contain 3 values |
||
204 | * - 1st element: the column title |
||
205 | * - 2nd element: true or false (column sortable?) |
||
206 | * - 3th element: additional attributes for |
||
207 | * th-tag (eg for column-width) |
||
208 | * - 4the element: additional attributes for the td-tags |
||
209 | * @param array $content 2D-array with the tables content |
||
210 | * @param array $sorting_options Keys are: |
||
211 | * 'column' = The column to use as sort-key |
||
212 | * 'direction' = SORT_ASC or SORT_DESC |
||
213 | * @param array $paging_options Keys are: |
||
214 | * 'per_page_default' = items per page when switching from |
||
215 | * full- list to per-page-view |
||
216 | * 'per_page' = number of items to show per page |
||
217 | * 'page_nr' = The page to display |
||
218 | * @param array $query_vars Additional variables to add in the query-string |
||
219 | * @param string The style that the table will show. You can set 'table' or 'grid' |
||
220 | * @author [email protected] |
||
221 | */ |
||
222 | public static function display_sortable_table( |
||
256 | |||
257 | public static function return_sortable_table( |
||
280 | |||
281 | /** |
||
282 | * Shows a nice grid |
||
283 | * @param string grid name (important to create css) |
||
284 | * @param array header content |
||
285 | * @param array array with the information to show |
||
286 | * @param array $paging_options Keys are: |
||
287 | * 'per_page_default' = items per page when switching from |
||
288 | * full- list to per-page-view |
||
289 | * 'per_page' = number of items to show per page |
||
290 | * 'page_nr' = The page to display |
||
291 | * 'hide_navigation' = true to hide the navigation |
||
292 | * @param array $query_vars Additional variables to add in the query-string |
||
293 | * @param array $form actions Additional variables to add in the query-string |
||
|
|||
294 | * @param mixed An array with bool values to know which columns show. |
||
295 | * i.e: $visibility_options= array(true, false) we will only show the first column |
||
296 | * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing |
||
297 | */ |
||
298 | public static function display_sortable_grid( |
||
321 | |||
322 | /** |
||
323 | * Gets a nice grid in html string |
||
324 | * @param string grid name (important to create css) |
||
325 | * @param array header content |
||
326 | * @param array array with the information to show |
||
327 | * @param array $paging_options Keys are: |
||
328 | * 'per_page_default' = items per page when switching from |
||
329 | * full- list to per-page-view |
||
330 | * 'per_page' = number of items to show per page |
||
331 | * 'page_nr' = The page to display |
||
332 | * 'hide_navigation' = true to hide the navigation |
||
333 | * @param array $query_vars Additional variables to add in the query-string |
||
334 | * @param array $form actions Additional variables to add in the query-string |
||
335 | * @param mixed An array with bool values to know which columns show. i.e: |
||
336 | * $visibility_options= array(true, false) we will only show the first column |
||
337 | * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing |
||
338 | * @param bool true for sorting data or false otherwise |
||
339 | * @param array grid classes |
||
340 | * @return string html grid |
||
341 | */ |
||
342 | public static function return_sortable_grid( |
||
371 | |||
372 | /** |
||
373 | * Displays a table with a special configuration |
||
374 | * @param array $header Titles for the table header |
||
375 | * each item in this array can contain 3 values |
||
376 | * - 1st element: the column title |
||
377 | * - 2nd element: true or false (column sortable?) |
||
378 | * - 3th element: additional attributes for |
||
379 | * th-tag (eg for column-width) |
||
380 | * - 4the element: additional attributes for the td-tags |
||
381 | * @param array $content 2D-array with the tables content |
||
382 | * @param array $sorting_options Keys are: |
||
383 | * 'column' = The column to use as sort-key |
||
384 | * 'direction' = SORT_ASC or SORT_DESC |
||
385 | * @param array $paging_options Keys are: |
||
386 | * 'per_page_default' = items per page when switching from |
||
387 | * full- list to per-page-view |
||
388 | * 'per_page' = number of items to show per page |
||
389 | * 'page_nr' = The page to display |
||
390 | * @param array $query_vars Additional variables to add in the query-string |
||
391 | * @param array $column_show Array of binaries 1= show columns 0. hide a column |
||
392 | * @param array $column_order An array of integers that let us decide how the columns are going to be sort. |
||
393 | * i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column |
||
394 | * @param array $form_actions Set optional forms actions |
||
395 | * |
||
396 | * @author Julio Montoya |
||
397 | */ |
||
398 | public static function display_sortable_config_table( |
||
439 | |||
440 | /** |
||
441 | * Displays a normal message. It is recommended to use this public function |
||
442 | * to display any normal information messages. |
||
443 | * @param string $message |
||
444 | * @param bool $filter (true) or not (false) |
||
445 | * @param bool $returnValue |
||
446 | * |
||
447 | * @return void |
||
448 | */ |
||
449 | View Code Duplication | public static function display_normal_message($message, $filter = true, $returnValue = false) |
|
458 | |||
459 | /** |
||
460 | * Displays an warning message. Use this if you want to draw attention to something |
||
461 | * This can also be used for instance with the hint in the exercises |
||
462 | * |
||
463 | */ |
||
464 | View Code Duplication | public static function display_warning_message($message, $filter = true, $returnValue = false) |
|
473 | |||
474 | /** |
||
475 | * Displays an confirmation message. Use this if something has been done successfully |
||
476 | * @param bool Filter (true) or not (false) |
||
477 | * @return void |
||
478 | */ |
||
479 | View Code Duplication | public static function display_confirmation_message ($message, $filter = true, $returnValue = false) |
|
488 | |||
489 | /** |
||
490 | * Displays an error message. It is recommended to use this public function if an error occurs |
||
491 | * @param string $message - include any additional html |
||
492 | * tags if you need them |
||
493 | * @param bool Filter (true) or not (false) |
||
494 | * @return void |
||
495 | */ |
||
496 | View Code Duplication | public static function display_error_message ($message, $filter = true, $returnValue = false) |
|
505 | |||
506 | /** |
||
507 | * @param string $message |
||
508 | * @param string $type |
||
509 | * @param bool $filter |
||
510 | */ |
||
511 | public static function return_message_and_translate($message, $type='normal', $filter = true) |
||
516 | |||
517 | /** |
||
518 | * Returns a div html string with |
||
519 | * @param string The message |
||
520 | * @param string The message type (confirm,normal,warning,error) |
||
521 | * @param bool Whether to XSS-filter or not |
||
522 | * @return string Message wrapped into an HTML div |
||
523 | */ |
||
524 | public static function return_message($message, $type = 'normal', $filter = true) |
||
525 | { |
||
526 | if (empty($message)) { |
||
527 | return ''; |
||
528 | } |
||
529 | |||
530 | if ($filter) { |
||
531 | $message = api_htmlentities($message, ENT_QUOTES, api_is_xml_http_request() ? 'UTF-8' : api_get_system_encoding()); |
||
532 | } |
||
533 | |||
534 | $class = ""; |
||
535 | switch($type) { |
||
536 | case 'warning': |
||
537 | $class .= 'alert alert-warning'; |
||
538 | break; |
||
539 | case 'error': |
||
540 | $class .= 'alert alert-danger'; |
||
541 | break; |
||
542 | case 'confirmation': |
||
543 | case 'confirm': |
||
544 | case 'success': |
||
545 | $class .= 'alert alert-success'; |
||
546 | break; |
||
547 | case 'normal': |
||
548 | default: |
||
549 | $class .= 'alert alert-info'; |
||
550 | } |
||
551 | |||
552 | return self::div($message, array('class'=> $class)); |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * Returns an encrypted mailto hyperlink |
||
557 | * |
||
558 | * @param string e-mail |
||
559 | * @param string clickable text |
||
560 | * @param string optional, class from stylesheet |
||
561 | * @return string encrypted mailto hyperlink |
||
562 | */ |
||
563 | public static function encrypted_mailto_link ($email, $clickable_text = null, $style_class = '') |
||
595 | |||
596 | /** |
||
597 | * Returns an mailto icon hyperlink |
||
598 | * |
||
599 | * @param string e-mail |
||
600 | * @param string icon source file from the icon lib |
||
601 | * @param integer icon size from icon lib |
||
602 | * @param string optional, class from stylesheet |
||
603 | * @return string encrypted mailto hyperlink |
||
604 | */ |
||
605 | public static function icon_mailto_link($email, $icon_file = "mail.png", $icon_size = 22, $style_class = '') |
||
627 | |||
628 | /** |
||
629 | * Creates a hyperlink to the platform homepage. |
||
630 | * @param string $name, the visible name of the hyperlink, default is sitename |
||
631 | * @return string with html code for hyperlink |
||
632 | */ |
||
633 | public static function get_platform_home_link_html($name = '') |
||
640 | |||
641 | /** |
||
642 | * Prints an <option>-list with all letters (A-Z). |
||
643 | * @param char $selected_letter The letter that should be selected |
||
644 | * @todo This is English language specific implementation. |
||
645 | * It should be adapted for the other languages. |
||
646 | */ |
||
647 | View Code Duplication | public static function get_alphabet_options($selected_letter = '') |
|
660 | |||
661 | /** |
||
662 | * Get the options withing a select box within the given values |
||
663 | * @param int Min value |
||
664 | * @param int Max value |
||
665 | * @param int Default value |
||
666 | * @return string HTML select options |
||
667 | */ |
||
668 | View Code Duplication | public static function get_numeric_options($min, $max, $selected_num = 0) |
|
681 | |||
682 | /** |
||
683 | * This public function displays an icon |
||
684 | * @param string The filename of the file (in the main/img/ folder |
||
685 | * @param string The alt text (probably a language variable) |
||
686 | * @param array additional attributes (for instance height, width, onclick, ...) |
||
687 | * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder) |
||
688 | * @return void |
||
689 | */ |
||
690 | public static function display_icon( |
||
698 | |||
699 | /** |
||
700 | * Gets the path of an icon |
||
701 | * |
||
702 | * @param string $icon |
||
703 | * @param string $size |
||
704 | * |
||
705 | * @return string |
||
706 | */ |
||
707 | public static function returnIconPath($icon, $size = ICON_SIZE_SMALL) |
||
708 | { |
||
709 | return Display::return_icon($icon, null, null, $size, null, true, false); |
||
710 | } |
||
711 | |||
712 | /** |
||
713 | * This public function returns the htmlcode for an icon |
||
714 | * |
||
715 | * @param string The filename of the file (in the main/img/ folder |
||
716 | * @param string The alt text (probably a language variable) |
||
717 | * @param array Additional attributes (for instance height, width, onclick, ...) |
||
718 | * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder) |
||
719 | * @return string An HTML string of the right <img> tag |
||
720 | * |
||
721 | * @author Patrick Cool <[email protected]>, Ghent University 2006 |
||
722 | * @author Julio Montoya 2010 Function improved, adding image constants |
||
723 | * @author Yannick Warnier 2011 Added size handler |
||
724 | * @version Feb 2011 |
||
725 | */ |
||
726 | public static function return_icon( |
||
727 | $image, |
||
728 | $alt_text = '', |
||
729 | $additional_attributes = array(), |
||
730 | $size = ICON_SIZE_SMALL, |
||
731 | $show_text = true, |
||
732 | $return_only_path = false, |
||
733 | $loadThemeIcon = true |
||
734 | ) { |
||
735 | $code_path = api_get_path(SYS_CODE_PATH); |
||
736 | $w_code_path = api_get_path(WEB_CODE_PATH); |
||
737 | $alternateCssPath = api_get_path(SYS_CSS_PATH); |
||
738 | $alternateWebCssPath = api_get_path(WEB_CSS_PATH); |
||
739 | |||
740 | $image = trim($image); |
||
741 | |||
742 | if (isset($size)) { |
||
743 | $size = intval($size); |
||
744 | } else { |
||
745 | $size = ICON_SIZE_SMALL; |
||
746 | } |
||
747 | |||
748 | $size_extra = $size . '/'; |
||
749 | |||
750 | // Checking the img/ folder |
||
751 | $icon = $w_code_path.'img/'.$image; |
||
752 | |||
753 | $theme = 'themes/chamilo/icons/'; |
||
754 | |||
755 | if ($loadThemeIcon) { |
||
756 | $theme = 'themes/' . api_get_visual_theme() . '/icons/'; |
||
757 | // Checking the theme icons folder example: app/Resources/public/css/themes/chamilo/icons/XXX |
||
758 | if (is_file($alternateCssPath.$theme.$size_extra.$image)) { |
||
759 | $icon = $alternateWebCssPath.$theme.$size_extra.$image; |
||
760 | View Code Duplication | } elseif (is_file($code_path.'img/icons/'.$size_extra.$image)) { |
|
761 | //Checking the main/img/icons/XXX/ folder |
||
762 | $icon = $w_code_path.'img/icons/'.$size_extra.$image; |
||
763 | } |
||
764 | View Code Duplication | } else { |
|
765 | if (is_file($code_path.'img/icons/'.$size_extra.$image)) { |
||
766 | // Checking the main/img/icons/XXX/ folder |
||
767 | $icon = $w_code_path.'img/icons/'.$size_extra.$image; |
||
768 | } |
||
769 | } |
||
770 | |||
771 | // Special code to enable SVG - refs #7359 - Needs more work |
||
772 | // The code below does something else to "test out" SVG: for each icon, |
||
773 | // it checks if there is an SVG version. If so, it uses it. |
||
774 | // When moving this to production, the return_icon() calls should |
||
775 | // ask for the SVG version directly |
||
776 | $testServer = api_get_setting('server_type'); |
||
777 | if ($testServer == 'test' && $return_only_path == false) { |
||
778 | $svgImage = substr($image, 0, -3) . 'svg'; |
||
779 | if (is_file($code_path . $theme . 'svg/' . $svgImage)) { |
||
780 | $icon = $w_code_path . $theme . 'svg/' . $svgImage; |
||
781 | } elseif (is_file($code_path . 'img/icons/svg/' . $svgImage)) { |
||
782 | $icon = $w_code_path . 'img/icons/svg/' . $svgImage; |
||
783 | } |
||
784 | |||
785 | if (empty($additional_attributes['height'])) { |
||
786 | $additional_attributes['height'] = $size; |
||
787 | } |
||
788 | if (empty($additional_attributes['width'])) { |
||
789 | $additional_attributes['width'] = $size; |
||
790 | } |
||
791 | } |
||
792 | |||
793 | $icon = api_get_cdn_path($icon); |
||
794 | |||
795 | if ($return_only_path) { |
||
796 | return $icon; |
||
797 | |||
798 | } |
||
799 | |||
800 | $img = self::img($icon, $alt_text, $additional_attributes); |
||
801 | if (SHOW_TEXT_NEAR_ICONS == true && !empty($alt_text)) { |
||
802 | if ($show_text) { |
||
803 | $img = "$img $alt_text"; |
||
804 | } |
||
805 | } |
||
806 | |||
807 | return $img; |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * Returns the htmlcode for an image |
||
812 | * |
||
813 | * @param string $image_path the filename of the file (in the main/img/ folder |
||
814 | * @param string $alt_text the alt text (probably a language variable) |
||
815 | * @param array $additional_attributes (for instance height, width, onclick, ...) |
||
816 | * @param boolean $filterPath Optional. Whether filter the image path. Default is true |
||
817 | * @author Julio Montoya 2010 |
||
818 | */ |
||
819 | public static function img($image_path, $alt_text = '', $additional_attributes = array(), $filterPath = true) |
||
820 | { |
||
821 | // Sanitizing the parameter $image_path |
||
822 | if ($filterPath) { |
||
823 | $image_path = Security::filter_img_path($image_path); |
||
824 | } |
||
825 | |||
826 | // alt text = the image name if there is none provided (for XHTML compliance) |
||
827 | if ($alt_text == '') { |
||
828 | $alt_text = basename($image_path); |
||
829 | } |
||
830 | |||
831 | $additional_attributes['src'] = $image_path; |
||
832 | |||
833 | if (empty($additional_attributes['alt'])) { |
||
834 | $additional_attributes['alt'] = $alt_text; |
||
835 | } |
||
836 | if (empty($additional_attributes['title'])) { |
||
837 | $additional_attributes['title'] = $alt_text; |
||
838 | } |
||
839 | |||
840 | return self::tag('img', '', $additional_attributes); |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * Returns the htmlcode for a tag (h3, h1, div, a, button), etc |
||
845 | * |
||
846 | * @param string $tag the tag name |
||
847 | * @param string $content the tag's content |
||
848 | * @param array $additional_attributes (for instance height, width, onclick, ...) |
||
849 | * @author Julio Montoya 2010 |
||
850 | */ |
||
851 | public static function tag($tag, $content, $additional_attributes = array()) |
||
852 | { |
||
853 | $attribute_list = ''; |
||
854 | // Managing the additional attributes |
||
855 | if (!empty($additional_attributes) && is_array($additional_attributes)) { |
||
856 | $attribute_list = ''; |
||
857 | foreach ($additional_attributes as $key => & $value) { |
||
858 | $attribute_list .= $key.'="'.$value.'" '; |
||
859 | } |
||
860 | } |
||
861 | //some tags don't have this </XXX> |
||
862 | if (in_array($tag, array('img','input','br'))) { |
||
863 | $return_value = '<'.$tag.' '.$attribute_list.' />'; |
||
864 | } else { |
||
865 | $return_value = '<'.$tag.' '.$attribute_list.' > '.$content.'</'.$tag.'>'; |
||
866 | } |
||
867 | return $return_value; |
||
868 | } |
||
869 | |||
870 | /** |
||
871 | * Creates a URL anchor |
||
872 | * @param string $name |
||
873 | * @param string $url |
||
874 | * @param array $attributes |
||
875 | * |
||
876 | * @return string |
||
877 | */ |
||
878 | public static function url($name, $url, $attributes = array()) |
||
879 | { |
||
880 | if (!empty($url)) { |
||
881 | $url = preg_replace('#&#', '&', $url); |
||
882 | $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); |
||
883 | $attributes['href'] = $url; |
||
884 | } |
||
885 | return self::tag('a', $name, $attributes); |
||
886 | } |
||
887 | |||
888 | /** |
||
889 | * Creates a div tag |
||
890 | * |
||
891 | * @param string $content |
||
892 | * @param array $attributes |
||
893 | * @return string |
||
894 | */ |
||
895 | public static function div($content, $attributes = array()) |
||
896 | { |
||
897 | return self::tag('div', $content, $attributes); |
||
898 | } |
||
899 | |||
900 | /** |
||
901 | * Creates a span tag |
||
902 | */ |
||
903 | public static function span($content, $attributes = array()) |
||
904 | { |
||
905 | return self::tag('span', $content, $attributes); |
||
906 | } |
||
907 | |||
908 | /** |
||
909 | * Displays an HTML input tag |
||
910 | * |
||
911 | */ |
||
912 | public static function input($type, $name, $value, $attributes = array()) |
||
913 | { |
||
914 | if (isset($type)) { |
||
915 | $attributes['type']= $type; |
||
916 | } |
||
917 | if (isset($name)) { |
||
918 | $attributes['name']= $name; |
||
919 | } |
||
920 | if (isset($value)) { |
||
921 | $attributes['value']= $value; |
||
922 | } |
||
923 | return self::tag('input', '', $attributes); |
||
924 | } |
||
925 | |||
926 | /** |
||
927 | * @param $name |
||
928 | * @param $value |
||
929 | * @param array $attributes |
||
930 | * @return string |
||
931 | */ |
||
932 | public static function button($name, $value, $attributes = array()) |
||
933 | { |
||
934 | if (!empty($name)) { |
||
935 | $attributes['name'] = $name; |
||
936 | } |
||
937 | return self::tag('button', $value, $attributes); |
||
938 | } |
||
939 | |||
940 | /** |
||
941 | * Displays an HTML select tag |
||
942 | * |
||
943 | */ |
||
944 | public static function select( |
||
945 | $name, |
||
946 | $values, |
||
947 | $default = -1, |
||
948 | $extra_attributes = array(), |
||
949 | $show_blank_item = true, |
||
950 | $blank_item_text = null |
||
951 | ) { |
||
952 | $html = ''; |
||
953 | $extra = ''; |
||
954 | $default_id = 'id="' . $name . '" '; |
||
955 | foreach ($extra_attributes as $key => $parameter) { |
||
956 | if ($key == 'id') { |
||
957 | $default_id = ''; |
||
958 | } |
||
959 | $extra .= $key . '="' . $parameter . '" '; |
||
960 | } |
||
961 | $html .= '<select name="' . $name . '" ' . $default_id . ' ' . $extra . '>'; |
||
962 | |||
963 | if ($show_blank_item) { |
||
964 | if (empty($blank_item_text)) { |
||
965 | $blank_item_text = get_lang('Select'); |
||
966 | } else { |
||
967 | $blank_item_text = Security::remove_XSS($blank_item_text); |
||
968 | } |
||
969 | $html .= self::tag('option', '-- ' . $blank_item_text . ' --', array('value' => '-1')); |
||
970 | } |
||
971 | if ($values) { |
||
972 | foreach ($values as $key => $value) { |
||
973 | if (is_array($value) && isset($value['name'])) { |
||
974 | $value = $value['name']; |
||
975 | } |
||
976 | $html .= '<option value="' . $key . '"'; |
||
977 | |||
978 | if (is_array($default)) { |
||
979 | foreach ($default as $item) { |
||
980 | if ($item == $key) { |
||
981 | $html .= ' selected="selected"'; |
||
982 | break; |
||
983 | } |
||
984 | } |
||
985 | } else { |
||
986 | if ($default == $key) { |
||
987 | $html .= ' selected="selected"'; |
||
988 | } |
||
989 | } |
||
990 | |||
991 | $html .= '>' . $value . '</option>'; |
||
992 | } |
||
993 | } |
||
994 | $html .= '</select>'; |
||
995 | return $html; |
||
996 | } |
||
997 | |||
998 | /** |
||
999 | * Creates a tab menu |
||
1000 | * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css |
||
1001 | * in the $htmlHeadXtra variable before the display_header |
||
1002 | * Add this script |
||
1003 | * @example |
||
1004 | * <script> |
||
1005 | $(function() { |
||
1006 | $( "#tabs" ).tabs(); |
||
1007 | }); |
||
1008 | </script> |
||
1009 | * @param array list of the tab titles |
||
1010 | * @param array content that will be showed |
||
1011 | * @param string the id of the container of the tab in the example "tabs" |
||
1012 | * @param array attributes for the ul |
||
1013 | * |
||
1014 | */ |
||
1015 | public static function tabs($header_list, $content_list, $id = 'tabs', $attributes = array(), $ul_attributes = array()) |
||
1016 | { |
||
1017 | if (empty($header_list) || count($header_list) == 0 ) { |
||
1018 | return ''; |
||
1019 | } |
||
1020 | |||
1021 | $lis = ''; |
||
1022 | $i = 1; |
||
1023 | foreach ($header_list as $item) { |
||
1024 | $active = ''; |
||
1025 | if ($i == 1) { |
||
1026 | $active = ' active'; |
||
1027 | } |
||
1028 | $item = self::tag('a', $item, array('href'=>'#'.$id.'-'.$i, 'role'=> 'tab')); |
||
1029 | $ul_attributes['data-toggle'] = 'tab'; |
||
1030 | $ul_attributes['role'] = 'presentation'; |
||
1031 | $ul_attributes['class'] = $active; |
||
1032 | $lis .= self::tag('li', $item, $ul_attributes); |
||
1033 | $i++; |
||
1034 | } |
||
1035 | $ul = self::tag('ul', $lis, ['class' => 'nav nav-tabs', 'role'=> 'tablist']); |
||
1036 | |||
1037 | $i = 1; |
||
1038 | $divs = ''; |
||
1039 | foreach ($content_list as $content) { |
||
1040 | $active = ''; |
||
1041 | if ($i == 1) { |
||
1042 | $active = ' active'; |
||
1043 | } |
||
1044 | $divs .= self::tag('div', $content, array('id'=> $id.'-'.$i, 'class' => 'tab-pane '.$active, 'role' => 'tabpanel')); |
||
1045 | $i++; |
||
1046 | } |
||
1047 | |||
1048 | $attributes['id'] = $id; |
||
1049 | $attributes['role'] = 'tabpanel'; |
||
1050 | $attributes['class'] = 'tab-wrapper'; |
||
1051 | |||
1052 | $main_div = self::tag('div', $ul.self::tag('div', $divs, ['class' => 'tab-content']), $attributes); |
||
1053 | |||
1054 | return $main_div ; |
||
1055 | } |
||
1056 | |||
1057 | /** |
||
1058 | * @param $headers |
||
1059 | * @param null $selected |
||
1060 | * |
||
1061 | * @return string |
||
1062 | */ |
||
1063 | public static function tabsOnlyLink($headers, $selected = null) |
||
1083 | |||
1084 | /** |
||
1085 | * In order to display a grid using jqgrid you have to: |
||
1086 | * @example |
||
1087 | * After your Display::display_header function you have to add the nex javascript code: * |
||
1088 | * <script> |
||
1089 | * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,array()); |
||
1090 | * // for more information of this function check the grid_js() function |
||
1091 | * </script> |
||
1092 | * //Then you have to call the grid_html |
||
1093 | * echo Display::grid_html('my_grid_name'); |
||
1094 | * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work |
||
1095 | * |
||
1096 | * @param string the div id, this value must be the same with the first parameter of Display::grid_js() |
||
1097 | * @return string html |
||
1098 | * |
||
1099 | */ |
||
1100 | public static function grid_html($div_id) |
||
1106 | |||
1107 | /** |
||
1108 | * @param string $label |
||
1109 | * @param string $form_item |
||
1110 | * @return string |
||
1111 | */ |
||
1112 | public static function form_row($label, $form_item) |
||
1118 | |||
1119 | /** |
||
1120 | * This is a wrapper to use the jqgrid in Chamilo. |
||
1121 | * For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options |
||
1122 | * This function need to be in the ready jquery function |
||
1123 | * example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> } |
||
1124 | * In order to work this function needs the Display::grid_html function with the same div id |
||
1125 | * |
||
1126 | * @param string $div_id div id |
||
1127 | * @param string $url url where the jqgrid will ask for data (if datatype = json) |
||
1128 | * @param array $column_names Visible columns (you should use get_lang). An array in which we place the names of the columns. |
||
1129 | * This is the text that appears in the head of the grid (Header layer). |
||
1130 | * Example: colname {name:'date', index:'date', width:120, align:'right'}, |
||
1131 | * @param array $column_model the column model : Array which describes the parameters of the columns.This is the most important part of the grid. |
||
1132 | * For a full description of all valid values see colModel API. See the url above. |
||
1133 | * @param array $extra_params extra parameters |
||
1134 | * @param array $data data that will be loaded |
||
1135 | * @param string $formatter A string that will be appended to the JSON returned |
||
1136 | * @param bool $fixed_width not implemented yet |
||
1137 | * @return string the js code |
||
1138 | * |
||
1139 | */ |
||
1140 | public static function grid_js( |
||
1281 | |||
1282 | /** |
||
1283 | * @param array $headers |
||
1284 | * @param array $rows |
||
1285 | * @param array $attributes |
||
1286 | * @return string |
||
1287 | */ |
||
1288 | public static function table($headers, $rows, $attributes = array()) |
||
1315 | |||
1316 | /** |
||
1317 | * Returns the "what's new" icon notifications |
||
1318 | * |
||
1319 | * The general logic of this function is to track the last time the user |
||
1320 | * entered the course and compare to what has changed inside this course |
||
1321 | * since then, based on the item_property table inside this course. Note that, |
||
1322 | * if the user never entered the course before, he will not see notification |
||
1323 | * icons. This function takes session ID into account (if any) and only shows |
||
1324 | * the corresponding notifications. |
||
1325 | * @param array Course information array, containing at least elements 'db' and 'k' |
||
1326 | * @return string The HTML link to be shown next to the course |
||
1327 | */ |
||
1328 | public static function show_notification($course_info) |
||
1492 | |||
1493 | /** |
||
1494 | * Get the session box details as an array |
||
1495 | * @param int Session ID |
||
1496 | * @return array Empty array or session array |
||
1497 | * ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int] |
||
1498 | */ |
||
1499 | public static function get_session_title_box($session_id) |
||
1644 | |||
1645 | /** |
||
1646 | * Return the five star HTML |
||
1647 | * |
||
1648 | * @param string id of the rating ul element |
||
1649 | * @param string url that will be added (for jquery see hot_courses.tpl) |
||
1650 | * @param string point info array see function CourseManager::get_course_ranking() |
||
1651 | * @param bool add a div wrapper |
||
1652 | * @todo use templates |
||
1653 | **/ |
||
1654 | public static function return_rating_system($id, $url, $point_info = array(), $add_div_wrapper = true) |
||
1698 | |||
1699 | public static function return_default_table_class() |
||
1703 | |||
1704 | /** |
||
1705 | * @param string $title |
||
1706 | * @param string $second_title |
||
1707 | * @param string $size |
||
1708 | * @param bool $filter |
||
1709 | * @return string |
||
1710 | */ |
||
1711 | public static function page_header($title, $second_title = null, $size = 'h2', $filter = true) |
||
1725 | |||
1726 | public static function page_header_and_translate($title, $second_title = null) |
||
1731 | |||
1732 | public static function page_subheader_and_translate($title, $second_title = null) |
||
1737 | |||
1738 | public static function page_subheader($title, $second_title = null, $size = 'h3') |
||
1746 | |||
1747 | public static function page_subheader2($title, $second_title = null) |
||
1751 | |||
1752 | public static function page_subheader3($title, $second_title = null) |
||
1756 | |||
1757 | /** |
||
1758 | * @param array $list |
||
1759 | * @return null|string |
||
1760 | */ |
||
1761 | public static function description($list) |
||
1774 | |||
1775 | /** |
||
1776 | * @param $percentage |
||
1777 | * @param bool $show_percentage |
||
1778 | * @param null $extra_info |
||
1779 | * @return string |
||
1780 | */ |
||
1781 | public static function bar_progress($percentage, $show_percentage = true, $extra_info = null) |
||
1804 | |||
1805 | /** |
||
1806 | * @param string $count |
||
1807 | * @param string $type |
||
1808 | * @return null|string |
||
1809 | */ |
||
1810 | public static function badge($count, $type ="warning") |
||
1837 | |||
1838 | /** |
||
1839 | * @param array $badge_list |
||
1840 | * @return string |
||
1841 | */ |
||
1842 | public static function badge_group($badge_list) |
||
1851 | |||
1852 | /** |
||
1853 | * @param string $content |
||
1854 | * @param string $type |
||
1855 | * @return string |
||
1856 | */ |
||
1857 | public static function label($content, $type = 'default') |
||
1891 | |||
1892 | /** |
||
1893 | * @param array $items |
||
1894 | * @return null|string |
||
1895 | */ |
||
1896 | public static function actions($items, $class = 'new_actions') |
||
1919 | |||
1920 | /** |
||
1921 | * Prints a tooltip |
||
1922 | * @param string $text |
||
1923 | * @param string $tip |
||
1924 | * |
||
1925 | * @return string |
||
1926 | */ |
||
1927 | public static function tip($text, $tip) |
||
1934 | |||
1935 | /** |
||
1936 | * @param array $items |
||
1937 | * @param string $type |
||
1938 | * @param null $id |
||
1939 | * @return null|string |
||
1940 | */ |
||
1941 | public static function generate_accordion($items, $type = 'jquery', $id = null) |
||
1974 | |||
1975 | /** |
||
1976 | * @todo use twig |
||
1977 | */ |
||
1978 | public static function group_button($title, $elements) |
||
1992 | |||
1993 | /** |
||
1994 | * @param string $file |
||
1995 | * @param array $params |
||
1996 | * @return null|string |
||
1997 | */ |
||
1998 | public static function getMediaPlayer($file, $params = array()) |
||
2032 | |||
2033 | /** |
||
2034 | * |
||
2035 | * @param int $nextValue |
||
2036 | * @param array $list |
||
2037 | * @param int $current |
||
2038 | * @param int $fixedValue |
||
2039 | * @param array $conditions |
||
2040 | * @param string $link |
||
2041 | * @param bool $isMedia |
||
2042 | * @param bool $addHeaders |
||
2043 | * @return string |
||
2044 | */ |
||
2045 | public static function progressPaginationBar( |
||
2046 | $nextValue, |
||
2047 | $list, |
||
2048 | $current, |
||
2049 | $fixedValue = null, |
||
2050 | $conditions = array(), |
||
2051 | $link = null, |
||
2052 | $isMedia = false, |
||
2053 | $addHeaders = true, |
||
2054 | $linkAttributes = array() |
||
2055 | ) { |
||
2056 | if ($addHeaders) { |
||
2057 | $pagination_size = 'pagination-mini'; |
||
2058 | $html = '<div class="exercise_pagination pagination '.$pagination_size.'"><ul>'; |
||
2059 | } else { |
||
2060 | $html = null; |
||
2061 | } |
||
2062 | $affectAllItems = false; |
||
2063 | if ($isMedia && isset($fixedValue) && ($nextValue + 1 == $current)) { |
||
2064 | $affectAllItems = true; |
||
2065 | } |
||
2066 | $localCounter = 0; |
||
2067 | foreach ($list as $itemId) { |
||
2068 | $isCurrent = false; |
||
2069 | if ($affectAllItems) { |
||
2070 | $isCurrent = true; |
||
2071 | } else { |
||
2072 | if (!$isMedia) { |
||
2073 | $isCurrent = $current == ($localCounter + $nextValue + 1) ? true : false; |
||
2074 | } |
||
2075 | } |
||
2076 | $html .= self::parsePaginationItem( |
||
2077 | $itemId, |
||
2078 | $isCurrent, |
||
2079 | $conditions, |
||
2080 | $link, |
||
2081 | $nextValue, |
||
2082 | $isMedia, |
||
2083 | $localCounter, |
||
2084 | $fixedValue, |
||
2085 | $linkAttributes |
||
2086 | ); |
||
2087 | $localCounter++; |
||
2088 | } |
||
2089 | if ($addHeaders) { |
||
2090 | $html .= '</ul></div>'; |
||
2091 | } |
||
2092 | return $html; |
||
2093 | } |
||
2094 | /** |
||
2095 | * |
||
2096 | * @param int $itemId |
||
2097 | * @param bool $isCurrent |
||
2098 | * @param array $conditions |
||
2099 | * @param string $link |
||
2100 | * @param int $nextValue |
||
2101 | * @param bool $isMedia |
||
2102 | * @param int $localCounter |
||
2103 | * @param int $fixedValue |
||
2104 | * @return string |
||
2105 | */ |
||
2106 | static function parsePaginationItem( |
||
2107 | $itemId, |
||
2108 | $isCurrent, |
||
2109 | $conditions, |
||
2110 | $link, |
||
2111 | $nextValue = 0, |
||
2112 | $isMedia = false, |
||
2113 | $localCounter = null, |
||
2114 | $fixedValue = null, |
||
2115 | $linkAttributes = array()) |
||
2116 | { |
||
2117 | $defaultClass = "before"; |
||
2118 | $class = $defaultClass; |
||
2119 | foreach ($conditions as $condition) { |
||
2120 | $array = isset($condition['items']) ? $condition['items'] : array(); |
||
2121 | $class_to_applied = $condition['class']; |
||
2122 | $type = isset($condition['type']) ? $condition['type'] : 'positive'; |
||
2123 | $mode = isset($condition['mode']) ? $condition['mode'] : 'add'; |
||
2124 | switch ($type) { |
||
2125 | View Code Duplication | case 'positive': |
|
2126 | if (in_array($itemId, $array)) { |
||
2127 | if ($mode == 'overwrite') { |
||
2128 | $class = " $defaultClass $class_to_applied"; |
||
2129 | } else { |
||
2130 | $class .= " $class_to_applied"; |
||
2131 | } |
||
2132 | } |
||
2133 | break; |
||
2134 | View Code Duplication | case 'negative': |
|
2135 | if (!in_array($itemId, $array)) { |
||
2136 | if ($mode == 'overwrite') { |
||
2137 | $class = " $defaultClass $class_to_applied"; |
||
2138 | } else { |
||
2139 | $class .= " $class_to_applied"; |
||
2140 | } |
||
2141 | } |
||
2142 | break; |
||
2143 | } |
||
2144 | } |
||
2145 | if ($isCurrent) { |
||
2146 | $class = "before current"; |
||
2147 | } |
||
2148 | if ($isMedia && $isCurrent) { |
||
2149 | $class = "before current"; |
||
2150 | } |
||
2151 | if (empty($link)) { |
||
2152 | $link_to_show = "#"; |
||
2153 | } else { |
||
2154 | $link_to_show = $link.($nextValue + $localCounter); |
||
2155 | } |
||
2156 | $label = $nextValue + $localCounter + 1; |
||
2157 | if ($isMedia) { |
||
2158 | $label = ($fixedValue + 1) .' '.chr(97 + $localCounter); |
||
2159 | $link_to_show = $link.$fixedValue.'#questionanchor'.$itemId; |
||
2160 | } |
||
2161 | $link = Display::url($label.' ', $link_to_show, $linkAttributes); |
||
2162 | return '<li class = "'.$class.'">'.$link.'</li>'; |
||
2163 | } |
||
2164 | |||
2165 | /** |
||
2166 | * @param int $current |
||
2167 | * @param int $total |
||
2168 | * @return string |
||
2169 | */ |
||
2170 | public static function paginationIndicator($current, $total) |
||
2171 | { |
||
2172 | $html = null; |
||
2173 | if (!empty($current) && !empty($total)) { |
||
2174 | $label = sprintf(get_lang('PaginationXofY'), $current, $total); |
||
2175 | $html = self::url($label, '#', array('class' => 'btn disabled')); |
||
2176 | } |
||
2177 | return $html; |
||
2178 | } |
||
2179 | |||
2180 | /** |
||
2181 | * Adds a message in the queue |
||
2182 | * @param string $message |
||
2183 | */ |
||
2184 | public static function addFlash($message) |
||
2194 | |||
2195 | /** |
||
2196 | * @return string |
||
2197 | */ |
||
2198 | public static function getFlashToString() |
||
2210 | |||
2211 | /** |
||
2212 | * Shows the message from the session |
||
2213 | */ |
||
2214 | public static function showFlash() |
||
2218 | |||
2219 | /** |
||
2220 | * Destroys the message session |
||
2221 | */ |
||
2222 | public static function cleanFlashMessages() |
||
2226 | |||
2227 | /** |
||
2228 | * Get the profile edition link for a user |
||
2229 | * @param int $userId The user id |
||
2230 | * @param boolean $asAdmin Optional. Whether get the URL for the platform admin |
||
2231 | * @return string The link |
||
2232 | */ |
||
2233 | public static function getProfileEditionLink($userId, $asAdmin = false) |
||
2262 | |||
2263 | /** |
||
2264 | * Get the vCard for a user |
||
2265 | * @param int $userId The user id |
||
2266 | * @return string *.*vcf file |
||
2267 | */ |
||
2268 | public static function getVCardUserLink($userId) |
||
2274 | |||
2275 | /** |
||
2276 | * @param string $content |
||
2277 | * @param string $title |
||
2278 | * @param string $footer |
||
2279 | * @param string $style primary|success|info|warning|danger |
||
2280 | * @param string $extra |
||
2281 | * |
||
2282 | * @return string |
||
2283 | */ |
||
2284 | public static function panel($content, $title = '', $footer = '', $style = '', $extra = '') |
||
2299 | |||
2300 | /** |
||
2301 | * @param string $content |
||
2302 | * @return string |
||
2303 | */ |
||
2304 | public static function contentPanel($content) |
||
2310 | |||
2311 | /** |
||
2312 | * Get the button HTML with an Awesome Font icon |
||
2313 | * @param string $text The button content |
||
2314 | * @param string $url The url to button |
||
2315 | * @param string $icon The Awesome Font class for icon |
||
2316 | * @param string $type Optional. The button Bootstrap class. Default 'default' class |
||
2317 | * @param array $attributes The additional attributes |
||
2318 | * @return string The button HTML |
||
2319 | */ |
||
2320 | public static function toolbarButton( |
||
2338 | |||
2339 | /** |
||
2340 | * @param int $id |
||
2341 | * @param array $content |
||
2342 | * @param int $col |
||
2343 | * @param bool|true $right |
||
2344 | * @return string |
||
2345 | */ |
||
2346 | public static function toolbarAction($id, $content = array(), $col = 2, $right = true) |
||
2376 | |||
2377 | /** |
||
2378 | * Get a HTML code for a icon by Font Awesome |
||
2379 | * @param string $name The icon name |
||
2380 | * @param boolean $fixWidth Optional. Whether add the fw class |
||
2381 | * @param int|string $size Optional. The size for the icon. |
||
2382 | * @param string $additionalClass Optional. Additional class |
||
2383 | * |
||
2384 | * @return string |
||
2385 | */ |
||
2386 | public static function returnFontAwesomeIcon( |
||
2421 | |||
2422 | /** |
||
2423 | * @param string $title |
||
2424 | * @param string $content |
||
2425 | * @param null $id |
||
2426 | * @param array $params |
||
2427 | * @param null $idAccordion |
||
2428 | * @param null $idCollapse |
||
2429 | * @param bool|true $open |
||
2430 | * @param bool|false $fullClickable |
||
2431 | * @return null|string |
||
2432 | */ |
||
2433 | public static function panelCollapse( |
||
2479 | } |
||
2480 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.