display.lib.php 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CoreBundle\Entity\ExtraField;
  5. /**
  6. * Class Display
  7. * Contains several public functions dealing with the display of
  8. * table data, messages, help topics, ...
  9. *
  10. * Include/require it in your code to use its public functionality.
  11. * There are also several display public functions in the main api library.
  12. *
  13. * All public functions static public functions inside a class called Display,
  14. * so you use them like this: e.g.
  15. * Display::return_message($message)
  16. *
  17. * @package chamilo.library
  18. *
  19. */
  20. class Display
  21. {
  22. /** @var Template */
  23. public static $global_template;
  24. public static $preview_style = null;
  25. /**
  26. * Constructor
  27. */
  28. public function __construct()
  29. {
  30. }
  31. /**
  32. * @return array
  33. */
  34. public static function toolList()
  35. {
  36. return array(
  37. 'group',
  38. 'work',
  39. 'glossary',
  40. 'forum',
  41. 'course_description',
  42. 'gradebook',
  43. 'attendance',
  44. 'course_progress',
  45. 'notebook'
  46. );
  47. }
  48. /**
  49. * Displays the page header
  50. * @param string The name of the page (will be showed in the page title)
  51. * @param string Optional help file name
  52. * @param string $page_header
  53. */
  54. public static function display_header(
  55. $tool_name = '',
  56. $help = null,
  57. $page_header = null
  58. ) {
  59. $origin = api_get_origin();
  60. $showHeader = true;
  61. if (isset($origin) && $origin == 'learnpath') {
  62. $showHeader = false;
  63. }
  64. self::$global_template = new Template($tool_name, $showHeader, $showHeader);
  65. // Fixing tools with any help it takes xxx part of main/xxx/index.php
  66. if (empty($help)) {
  67. $currentURL = api_get_self();
  68. preg_match('/main\/([^*\/]+)/', $currentURL, $matches);
  69. $toolList = self::toolList();
  70. if (!empty($matches)) {
  71. foreach ($matches as $match) {
  72. if (in_array($match, $toolList)) {
  73. $help = explode('_', $match);
  74. $help = array_map('ucfirst', $help);
  75. $help = implode('', $help);
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. self::$global_template->setHelp($help);
  82. if (!empty(self::$preview_style)) {
  83. self::$global_template->preview_theme = self::$preview_style;
  84. self::$global_template->set_system_parameters();
  85. self::$global_template->setCssFiles();
  86. self::$global_template->set_js_files();
  87. self::$global_template->setCssCustomFiles();
  88. }
  89. if (!empty($page_header)) {
  90. self::$global_template->assign('header', $page_header);
  91. }
  92. echo self::$global_template->show_header_template();
  93. }
  94. /**
  95. * Displays the reduced page header (without banner)
  96. */
  97. public static function display_reduced_header()
  98. {
  99. global $show_learnpath, $tool_name;
  100. self::$global_template = new Template(
  101. $tool_name,
  102. false,
  103. false,
  104. $show_learnpath
  105. );
  106. echo self::$global_template->show_header_template();
  107. }
  108. /**
  109. * Display no header
  110. */
  111. public static function display_no_header()
  112. {
  113. global $tool_name, $show_learnpath;
  114. $disable_js_and_css_files = true;
  115. self::$global_template = new Template(
  116. $tool_name,
  117. false,
  118. false,
  119. $show_learnpath
  120. );
  121. }
  122. /**
  123. * Displays the reduced page header (without banner)
  124. */
  125. public static function set_header()
  126. {
  127. global $show_learnpath, $tool_name;
  128. self::$global_template = new Template(
  129. $tool_name,
  130. false,
  131. false,
  132. $show_learnpath
  133. );
  134. }
  135. /**
  136. * Display the page footer
  137. */
  138. public static function display_footer()
  139. {
  140. echo self::$global_template->show_footer_template();
  141. }
  142. /**
  143. * Display the page footer
  144. */
  145. public static function display_reduced_footer()
  146. {
  147. echo self::$global_template->show_footer_js_template();
  148. echo '</body></html>';
  149. }
  150. public static function page()
  151. {
  152. return new Page();
  153. }
  154. /**
  155. * Displays the tool introduction of a tool.
  156. *
  157. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  158. * @param string $tool These are the constants that are used for indicating the tools.
  159. * @param array $editor_config Optional configuration settings for the online editor.
  160. * return: $tool return a string array list with the "define" in main_api.lib
  161. * @return html code for adding an introduction
  162. */
  163. public static function display_introduction_section(
  164. $tool,
  165. $editor_config = null
  166. ) {
  167. echo self::return_introduction_section($tool, $editor_config);
  168. }
  169. /**
  170. * @param string $tool
  171. * @param array $editor_config
  172. * @return null
  173. */
  174. public static function return_introduction_section(
  175. $tool,
  176. $editor_config = null
  177. ) {
  178. $is_allowed_to_edit = api_is_allowed_to_edit();
  179. $moduleId = $tool;
  180. if (api_get_setting('enable_tool_introduction') == 'true' || $tool == TOOL_COURSE_HOMEPAGE) {
  181. $introduction_section = null;
  182. require api_get_path(SYS_INC_PATH).'introductionSection.inc.php';
  183. return $introduction_section;
  184. }
  185. }
  186. /**
  187. * Displays a table
  188. * @param array $header Titles for the table header
  189. * each item in this array can contain 3 values
  190. * - 1st element: the column title
  191. * - 2nd element: true or false (column sortable?)
  192. * - 3th element: additional attributes for
  193. * th-tag (eg for column-width)
  194. * - 4the element: additional attributes for the td-tags
  195. * @param array $content 2D-array with the tables content
  196. * @param array $sorting_options Keys are:
  197. * 'column' = The column to use as sort-key
  198. * 'direction' = SORT_ASC or SORT_DESC
  199. * @param array $paging_options Keys are:
  200. * 'per_page_default' = items per page when switching from
  201. * full- list to per-page-view
  202. * 'per_page' = number of items to show per page
  203. * 'page_nr' = The page to display
  204. * @param array $query_vars Additional variables to add in the query-string
  205. * @param string The style that the table will show. You can set 'table' or 'grid'
  206. * @author bart.mollet@hogent.be
  207. */
  208. public static function display_sortable_table(
  209. $header,
  210. $content,
  211. $sorting_options = array(),
  212. $paging_options = array(),
  213. $query_vars = null,
  214. $form_actions = array(),
  215. $style = 'table'
  216. ) {
  217. global $origin;
  218. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  219. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  220. $table = new SortableTableFromArray($content, $column, $default_items_per_page);
  221. if (is_array($query_vars)) {
  222. $table->set_additional_parameters($query_vars);
  223. }
  224. if ($style == 'table') {
  225. if (is_array($header) && count($header) > 0) {
  226. foreach ($header as $index => $header_item) {
  227. $table->set_header(
  228. $index,
  229. isset($header_item[0]) ? $header_item[0] : null,
  230. isset($header_item[1]) ? $header_item[1] : null,
  231. isset($header_item[2]) ? $header_item[2] : null,
  232. isset($header_item[3]) ? $header_item[3] : null
  233. );
  234. }
  235. }
  236. $table->set_form_actions($form_actions);
  237. $table->display();
  238. } else {
  239. $table->display_grid();
  240. }
  241. }
  242. /**
  243. * Returns an HTML table with sortable column (through complete page refresh)
  244. * @param array $header
  245. * @param array $content Array of row arrays
  246. * @param array $sorting_options
  247. * @param array $paging_options
  248. * @param array $query_vars
  249. * @param array $form_actions
  250. * @param string $style
  251. * @return string HTML string for array
  252. */
  253. public static function return_sortable_table(
  254. $header,
  255. $content,
  256. $sorting_options = array(),
  257. $paging_options = array(),
  258. $query_vars = null,
  259. $form_actions = array(),
  260. $style = 'table'
  261. ) {
  262. ob_start();
  263. self::display_sortable_table(
  264. $header,
  265. $content,
  266. $sorting_options,
  267. $paging_options,
  268. $query_vars,
  269. $form_actions,
  270. $style
  271. );
  272. $content = ob_get_contents();
  273. ob_end_clean();
  274. return $content;
  275. }
  276. /**
  277. * Shows a nice grid
  278. * @param string grid name (important to create css)
  279. * @param array header content
  280. * @param array array with the information to show
  281. * @param array $paging_options Keys are:
  282. * 'per_page_default' = items per page when switching from
  283. * full- list to per-page-view
  284. * 'per_page' = number of items to show per page
  285. * 'page_nr' = The page to display
  286. * 'hide_navigation' = true to hide the navigation
  287. * @param array $query_vars Additional variables to add in the query-string
  288. * @param array $form actions Additional variables to add in the query-string
  289. * @param mixed An array with bool values to know which columns show.
  290. * i.e: $visibility_options= array(true, false) we will only show the first column
  291. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  292. */
  293. public static function display_sortable_grid(
  294. $name,
  295. $header,
  296. $content,
  297. $paging_options = array(),
  298. $query_vars = null,
  299. $form_actions = array(),
  300. $visibility_options = true,
  301. $sort_data = true,
  302. $grid_class = array()
  303. ) {
  304. echo self::return_sortable_grid(
  305. $name,
  306. $header,
  307. $content,
  308. $paging_options,
  309. $query_vars,
  310. $form_actions,
  311. $visibility_options,
  312. $sort_data,
  313. $grid_class
  314. );
  315. }
  316. /**
  317. * Gets a nice grid in html string
  318. * @param string grid name (important to create css)
  319. * @param array header content
  320. * @param array array with the information to show
  321. * @param array $paging_options Keys are:
  322. * 'per_page_default' = items per page when switching from
  323. * full- list to per-page-view
  324. * 'per_page' = number of items to show per page
  325. * 'page_nr' = The page to display
  326. * 'hide_navigation' = true to hide the navigation
  327. * @param array $query_vars Additional variables to add in the query-string
  328. * @param array $form actions Additional variables to add in the query-string
  329. * @param mixed An array with bool values to know which columns show. i.e:
  330. * $visibility_options= array(true, false) we will only show the first column
  331. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  332. * @param bool true for sorting data or false otherwise
  333. * @param array grid classes
  334. * @return string html grid
  335. */
  336. public static function return_sortable_grid(
  337. $name,
  338. $header,
  339. $content,
  340. $paging_options = array(),
  341. $query_vars = null,
  342. $form_actions = array(),
  343. $visibility_options = true,
  344. $sort_data = true,
  345. $grid_class = array(),
  346. $elementCount = 0
  347. ) {
  348. $column = 0;
  349. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  350. $table = new SortableTableFromArray($content, $column, $default_items_per_page, $name);
  351. $table->total_number_of_items = intval($elementCount);
  352. if (is_array($query_vars)) {
  353. $table->set_additional_parameters($query_vars);
  354. }
  355. return $table->display_simple_grid(
  356. $visibility_options,
  357. $paging_options['hide_navigation'],
  358. $default_items_per_page,
  359. $sort_data,
  360. $grid_class
  361. );
  362. }
  363. /**
  364. * Displays a table with a special configuration
  365. * @param array $header Titles for the table header
  366. * each item in this array can contain 3 values
  367. * - 1st element: the column title
  368. * - 2nd element: true or false (column sortable?)
  369. * - 3th element: additional attributes for th-tag (eg for column-width)
  370. * - 4the element: additional attributes for the td-tags
  371. * @param array $content 2D-array with the tables content
  372. * @param array $sorting_options Keys are:
  373. * 'column' = The column to use as sort-key
  374. * 'direction' = SORT_ASC or SORT_DESC
  375. * @param array $paging_options Keys are:
  376. * 'per_page_default' = items per page when switching from full list to per-page-view
  377. * 'per_page' = number of items to show per page
  378. * 'page_nr' = The page to display
  379. * @param array $query_vars Additional variables to add in the query-string
  380. * @param array $column_show Array of binaries 1= show columns 0. hide a column
  381. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  382. * i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  383. * @param array $form_actions Set optional forms actions
  384. *
  385. * @author Julio Montoya
  386. */
  387. public static function display_sortable_config_table(
  388. $table_name,
  389. $header,
  390. $content,
  391. $sorting_options = array(),
  392. $paging_options = array(),
  393. $query_vars = null,
  394. $column_show = array(),
  395. $column_order = array(),
  396. $form_actions = array()
  397. ) {
  398. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  399. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  400. $table = new SortableTableFromArrayConfig(
  401. $content,
  402. $column,
  403. $default_items_per_page,
  404. $table_name,
  405. $column_show,
  406. $column_order
  407. );
  408. if (is_array($query_vars)) {
  409. $table->set_additional_parameters($query_vars);
  410. }
  411. // Show or hide the columns header
  412. if (is_array($column_show)) {
  413. for ($i = 0; $i < count($column_show); $i++) {
  414. if (!empty($column_show[$i])) {
  415. $val0 = isset($header[$i][0]) ? $header[$i][0] : null;
  416. $val1 = isset($header[$i][1]) ? $header[$i][1] : null;
  417. $val2 = isset($header[$i][2]) ? $header[$i][2] : null;
  418. $val3 = isset($header[$i][3]) ? $header[$i][3] : null;
  419. $table->set_header($i, $val0, $val1, $val2, $val3);
  420. }
  421. }
  422. }
  423. $table->set_form_actions($form_actions);
  424. $table->display();
  425. }
  426. /**
  427. * Displays a normal message. It is recommended to use this public function
  428. * to display any normal information messages.
  429. * @param string $message
  430. * @param bool $filter (true) or not (false)
  431. * @param bool $returnValue
  432. *
  433. * @deprecated Use <code>Display::addFlash(Display::return_message($message, 'normal'));</code>
  434. * Or <code>echo Display::return_message($message, 'normal')</code>
  435. *
  436. * @return void
  437. */
  438. public static function display_normal_message(
  439. $message,
  440. $filter = true,
  441. $returnValue = false
  442. ) {
  443. $message = self::return_message($message, 'normal', $filter);
  444. if ($returnValue) {
  445. return $message;
  446. } else {
  447. echo $message;
  448. }
  449. }
  450. /**
  451. * Displays an warning message. Use this if you want to draw attention to something
  452. * This can also be used for instance with the hint in the exercises
  453. *
  454. * @deprecated use Display::addFlash(Display::return_message($message, 'warning'));
  455. */
  456. public static function display_warning_message(
  457. $message,
  458. $filter = true,
  459. $returnValue = false
  460. ) {
  461. $message = self::return_message($message, 'warning', $filter);
  462. if ($returnValue) {
  463. return $message;
  464. } else {
  465. echo $message;
  466. }
  467. }
  468. /**
  469. * Displays an confirmation message. Use this if something has been done successfully
  470. * @param bool Filter (true) or not (false)
  471. * @deprecated use Display::addFlash(Display::return_message($message, 'confirm'));
  472. * @return void
  473. */
  474. public static function display_confirmation_message(
  475. $message,
  476. $filter = true,
  477. $returnValue = false
  478. ) {
  479. $message = self::return_message($message, 'confirm', $filter);
  480. if ($returnValue) {
  481. return $message;
  482. } else {
  483. echo $message;
  484. }
  485. }
  486. /**
  487. * Displays an error message. It is recommended to use this public function if an error occurs
  488. * @param string $message - include any additional html
  489. * tags if you need them
  490. * @param bool Filter (true) or not (false)
  491. * @deprecated use Display::addFlash(Display::return_message($message, 'error'));
  492. *
  493. * @return void
  494. */
  495. public static function display_error_message(
  496. $message,
  497. $filter = true,
  498. $returnValue = false
  499. ) {
  500. $message = self::return_message($message, 'error', $filter);
  501. if ($returnValue) {
  502. return $message;
  503. } else {
  504. echo $message;
  505. }
  506. }
  507. /**
  508. * @param string $message
  509. * @param string $type
  510. * @param bool $filter
  511. */
  512. public static function return_message_and_translate(
  513. $message,
  514. $type = 'normal',
  515. $filter = true
  516. ) {
  517. $message = get_lang($message);
  518. echo self::return_message($message, $type, $filter);
  519. }
  520. /**
  521. * Returns a div html string with
  522. * @param string $message
  523. * @param string $type Example: confirm, normal, warning, error
  524. * @param bool $filter Whether to XSS-filter or not
  525. * @return string Message wrapped into an HTML div
  526. */
  527. public static function return_message(
  528. $message,
  529. $type = 'normal',
  530. $filter = true
  531. ) {
  532. if (empty($message)) {
  533. return '';
  534. }
  535. if ($filter) {
  536. $message = api_htmlentities(
  537. $message,
  538. ENT_QUOTES,
  539. api_is_xml_http_request() ? 'UTF-8' : api_get_system_encoding()
  540. );
  541. }
  542. $class = '';
  543. switch ($type) {
  544. case 'warning':
  545. $class .= 'alert alert-warning';
  546. break;
  547. case 'error':
  548. $class .= 'alert alert-danger';
  549. break;
  550. case 'confirmation':
  551. case 'confirm':
  552. case 'success':
  553. $class .= 'alert alert-success';
  554. break;
  555. case 'normal':
  556. default:
  557. $class .= 'alert alert-info';
  558. }
  559. return self::div($message, array('class'=> $class));
  560. }
  561. /**
  562. * Returns an encrypted mailto hyperlink
  563. *
  564. * @param string e-mail
  565. * @param string clickable text
  566. * @param string optional, class from stylesheet
  567. * @return string encrypted mailto hyperlink
  568. */
  569. public static function encrypted_mailto_link(
  570. $email,
  571. $clickable_text = null,
  572. $style_class = ''
  573. ) {
  574. if (is_null($clickable_text)) {
  575. $clickable_text = $email;
  576. }
  577. // "mailto:" already present?
  578. if (substr($email, 0, 7) != 'mailto:') {
  579. $email = 'mailto:'.$email;
  580. }
  581. // Class (stylesheet) defined?
  582. if ($style_class != '') {
  583. $style_class = ' class="'.$style_class.'"';
  584. }
  585. // Encrypt email
  586. $hmail = '';
  587. for ($i = 0; $i < strlen($email); $i++) {
  588. $hmail .= '&#'.ord($email {
  589. $i }).';';
  590. }
  591. $hclickable_text = null;
  592. // Encrypt clickable text if @ is present
  593. if (strpos($clickable_text, '@')) {
  594. for ($i = 0; $i < strlen($clickable_text); $i++) {
  595. $hclickable_text .= '&#'.ord($clickable_text {
  596. $i }).';';
  597. }
  598. } else {
  599. $hclickable_text = @htmlspecialchars($clickable_text, ENT_QUOTES, api_get_system_encoding());
  600. }
  601. // Return encrypted mailto hyperlink
  602. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$hclickable_text.'</a>';
  603. }
  604. /**
  605. * Returns an mailto icon hyperlink
  606. *
  607. * @param string e-mail
  608. * @param string icon source file from the icon lib
  609. * @param integer icon size from icon lib
  610. * @param string optional, class from stylesheet
  611. * @return string encrypted mailto hyperlink
  612. */
  613. public static function icon_mailto_link(
  614. $email,
  615. $icon_file = "mail.png",
  616. $icon_size = 22,
  617. $style_class = ''
  618. ) {
  619. // "mailto:" already present?
  620. if (substr($email, 0, 7) != 'mailto:') {
  621. $email = 'mailto:'.$email;
  622. }
  623. // Class (stylesheet) defined?
  624. if ($style_class != '') {
  625. $style_class = ' class="'.$style_class.'"';
  626. }
  627. // Encrypt email
  628. $hmail = '';
  629. for ($i = 0; $i < strlen($email); $i++) {
  630. $hmail .= '&#'.ord($email {
  631. $i }).';';
  632. }
  633. // icon html code
  634. $icon_html_source = self::return_icon($icon_file, $hmail, '', $icon_size);
  635. // Return encrypted mailto hyperlink
  636. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$icon_html_source.'</a>';
  637. }
  638. /**
  639. * Prints an <option>-list with all letters (A-Z).
  640. * @param char $selected_letter The letter that should be selected
  641. * @todo This is English language specific implementation.
  642. * It should be adapted for the other languages.
  643. * @return string
  644. */
  645. public static function get_alphabet_options($selected_letter = '')
  646. {
  647. $result = '';
  648. for ($i = 65; $i <= 90; $i++) {
  649. $letter = chr($i);
  650. $result .= '<option value="'.$letter.'"';
  651. if ($selected_letter == $letter) {
  652. $result .= ' selected="selected"';
  653. }
  654. $result .= '>'.$letter.'</option>';
  655. }
  656. return $result;
  657. }
  658. /**
  659. * Get the options withing a select box within the given values
  660. * @param int Min value
  661. * @param int Max value
  662. * @param int Default value
  663. * @return string HTML select options
  664. */
  665. public static function get_numeric_options($min, $max, $selected_num = 0)
  666. {
  667. $result = '';
  668. for ($i = $min; $i <= $max; $i++) {
  669. $result .= '<option value="'.$i.'"';
  670. if (is_int($selected_num)) {
  671. if ($selected_num == $i) {
  672. $result .= ' selected="selected"';
  673. }
  674. }
  675. $result .= '>'.$i.'</option>';
  676. }
  677. return $result;
  678. }
  679. /**
  680. * This public function displays an icon
  681. * @param string The filename of the file (in the main/img/ folder
  682. * @param string The alt text (probably a language variable)
  683. * @param array additional attributes (for instance height, width, onclick, ...)
  684. * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  685. * @return void
  686. */
  687. public static function display_icon(
  688. $image,
  689. $alt_text = '',
  690. $additional_attributes = array(),
  691. $size = null
  692. ) {
  693. echo self::return_icon($image, $alt_text, $additional_attributes, $size);
  694. }
  695. /**
  696. * Gets the path of an icon
  697. *
  698. * @param string $icon
  699. * @param int $size
  700. *
  701. * @return string
  702. */
  703. public static function returnIconPath($icon, $size = ICON_SIZE_SMALL)
  704. {
  705. return self::return_icon($icon, null, null, $size, null, true, false);
  706. }
  707. /**
  708. * This public function returns the htmlcode for an icon
  709. *
  710. * @param string The filename of the file (in the main/img/ folder
  711. * @param string The alt text (probably a language variable)
  712. * @param array Additional attributes (for instance height, width, onclick, ...)
  713. * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  714. * @return string An HTML string of the right <img> tag
  715. *
  716. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University 2006
  717. * @author Julio Montoya 2010 Function improved, adding image constants
  718. * @author Yannick Warnier 2011 Added size handler
  719. * @version Feb 2011
  720. */
  721. public static function return_icon(
  722. $image,
  723. $alt_text = '',
  724. $additional_attributes = array(),
  725. $size = ICON_SIZE_SMALL,
  726. $show_text = true,
  727. $return_only_path = false,
  728. $loadThemeIcon = true
  729. ) {
  730. $code_path = api_get_path(SYS_CODE_PATH);
  731. $w_code_path = api_get_path(WEB_CODE_PATH);
  732. // The following path is checked to see if the file exist. It's
  733. // important to use the public path (i.e. web/css/) rather than the
  734. // internal path (/app/Resource/public/css/) because the path used
  735. // in the end must be the public path
  736. $alternateCssPath = api_get_path(SYS_PUBLIC_PATH).'css/';
  737. $alternateWebCssPath = api_get_path(WEB_PUBLIC_PATH).'css/';
  738. // Avoid issues with illegal string offset for legacy calls to this
  739. // method with an empty string rather than null or an empty array
  740. if (empty($additional_attributes)) {
  741. $additional_attributes = [];
  742. }
  743. $image = trim($image);
  744. if (isset($size)) {
  745. $size = intval($size);
  746. } else {
  747. $size = ICON_SIZE_SMALL;
  748. }
  749. $size_extra = $size.'/';
  750. $icon = $w_code_path.'img/'.$image;
  751. $theme = 'themes/chamilo/icons/';
  752. if ($loadThemeIcon) {
  753. $theme = 'themes/'.api_get_visual_theme().'/icons/';
  754. if (is_file($alternateCssPath.$theme.$image)) {
  755. $icon = $alternateWebCssPath.$theme.$image;
  756. }
  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. } 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. } 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. // Special code to enable SVG - refs #7359 - Needs more work
  771. // The code below does something else to "test out" SVG: for each icon,
  772. // it checks if there is an SVG version. If so, it uses it.
  773. // When moving this to production, the return_icon() calls should
  774. // ask for the SVG version directly
  775. $svgIcons = api_get_setting('icons_mode_svg');
  776. if ($svgIcons == 'true' && $return_only_path == false) {
  777. $svgImage = substr($image, 0, -3).'svg';
  778. if (is_file($code_path.$theme.'svg/'.$svgImage)) {
  779. $icon = $w_code_path.$theme.'svg/'.$svgImage;
  780. } elseif (is_file($code_path.'img/icons/svg/'.$svgImage)) {
  781. $icon = $w_code_path.'img/icons/svg/'.$svgImage;
  782. }
  783. if (empty($additional_attributes['height'])) {
  784. $additional_attributes['height'] = $size;
  785. }
  786. if (empty($additional_attributes['width'])) {
  787. $additional_attributes['width'] = $size;
  788. }
  789. }
  790. $icon = api_get_cdn_path($icon);
  791. if ($return_only_path) {
  792. return $icon;
  793. }
  794. $img = self::img($icon, $alt_text, $additional_attributes);
  795. if (SHOW_TEXT_NEAR_ICONS == true && !empty($alt_text)) {
  796. if ($show_text) {
  797. $img = "$img $alt_text";
  798. }
  799. }
  800. return $img;
  801. }
  802. /**
  803. * Returns the htmlcode for an image
  804. *
  805. * @param string $image_path the filename of the file (in the main/img/ folder
  806. * @param string $alt_text the alt text (probably a language variable)
  807. * @param array $additional_attributes (for instance height, width, onclick, ...)
  808. * @param boolean $filterPath Optional. Whether filter the image path. Default is true
  809. * @return string
  810. * @author Julio Montoya 2010
  811. */
  812. public static function img(
  813. $image_path,
  814. $alt_text = '',
  815. $additional_attributes = null,
  816. $filterPath = true
  817. ) {
  818. if (empty($image_path)) {
  819. // For some reason, the call to img() happened without a proper
  820. // image. Log the error and return an empty string to avoid
  821. // breaking the HTML
  822. $trace = debug_backtrace();
  823. $caller = $trace[1];
  824. error_log('No image provided in Display::img(). Caller info: '.print_r($caller, 1));
  825. return '';
  826. }
  827. // Sanitizing the parameter $image_path
  828. if ($filterPath) {
  829. $image_path = Security::filter_img_path($image_path);
  830. }
  831. // alt text = the image name if there is none provided (for XHTML compliance)
  832. if ($alt_text == '') {
  833. $alt_text = basename($image_path);
  834. }
  835. if (empty($additional_attributes)) {
  836. $additional_attributes = [];
  837. }
  838. $additional_attributes['src'] = $image_path;
  839. if (empty($additional_attributes['alt'])) {
  840. $additional_attributes['alt'] = $alt_text;
  841. }
  842. if (empty($additional_attributes['title'])) {
  843. $additional_attributes['title'] = $alt_text;
  844. }
  845. return self::tag('img', '', $additional_attributes);
  846. }
  847. /**
  848. * Returns the htmlcode for a tag (h3, h1, div, a, button), etc
  849. *
  850. * @param string $tag the tag name
  851. * @param string $content the tag's content
  852. * @param array $additional_attributes (for instance height, width, onclick, ...)
  853. *
  854. * @return string
  855. * @author Julio Montoya 2010
  856. */
  857. public static function tag($tag, $content, $additional_attributes = array())
  858. {
  859. $attribute_list = '';
  860. // Managing the additional attributes
  861. if (!empty($additional_attributes) && is_array($additional_attributes)) {
  862. $attribute_list = '';
  863. foreach ($additional_attributes as $key => & $value) {
  864. $attribute_list .= $key.'="'.$value.'" ';
  865. }
  866. }
  867. //some tags don't have this </XXX>
  868. if (in_array($tag, array('img', 'input', 'br'))) {
  869. $return_value = '<'.$tag.' '.$attribute_list.' />';
  870. } else {
  871. $return_value = '<'.$tag.' '.$attribute_list.' >'.$content.'</'.$tag.'>';
  872. }
  873. return $return_value;
  874. }
  875. /**
  876. * Creates a URL anchor
  877. * @param string $name
  878. * @param string $url
  879. * @param array $attributes
  880. *
  881. * @return string
  882. */
  883. public static function url($name, $url, $attributes = array())
  884. {
  885. if (!empty($url)) {
  886. $url = preg_replace('#&amp;#', '&', $url);
  887. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  888. $attributes['href'] = $url;
  889. }
  890. return self::tag('a', $name, $attributes);
  891. }
  892. /**
  893. * Creates a div tag
  894. *
  895. * @param string $content
  896. * @param array $attributes
  897. * @return string
  898. */
  899. public static function div($content, $attributes = array())
  900. {
  901. return self::tag('div', $content, $attributes);
  902. }
  903. /**
  904. * Creates a span tag
  905. */
  906. public static function span($content, $attributes = array())
  907. {
  908. return self::tag('span', $content, $attributes);
  909. }
  910. /**
  911. * Displays an HTML input tag
  912. */
  913. public static function input($type, $name, $value, $attributes = array())
  914. {
  915. if (isset($type)) {
  916. $attributes['type'] = $type;
  917. }
  918. if (isset($name)) {
  919. $attributes['name'] = $name;
  920. }
  921. if (isset($value)) {
  922. $attributes['value'] = $value;
  923. }
  924. return self::tag('input', '', $attributes);
  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. * Displays an HTML select tag
  941. *
  942. */
  943. public static function select(
  944. $name,
  945. $values,
  946. $default = -1,
  947. $extra_attributes = array(),
  948. $show_blank_item = true,
  949. $blank_item_text = null
  950. ) {
  951. $html = '';
  952. $extra = '';
  953. $default_id = 'id="'.$name.'" ';
  954. foreach ($extra_attributes as $key => $parameter) {
  955. if ($key == 'id') {
  956. $default_id = '';
  957. }
  958. $extra .= $key.'="'.$parameter.'" ';
  959. }
  960. $html .= '<select name="'.$name.'" '.$default_id.' '.$extra.'>';
  961. if ($show_blank_item) {
  962. if (empty($blank_item_text)) {
  963. $blank_item_text = get_lang('Select');
  964. } else {
  965. $blank_item_text = Security::remove_XSS($blank_item_text);
  966. }
  967. $html .= self::tag(
  968. 'option',
  969. '-- '.$blank_item_text.' --',
  970. array('value' => '-1')
  971. );
  972. }
  973. if ($values) {
  974. foreach ($values as $key => $value) {
  975. if (is_array($value) && isset($value['name'])) {
  976. $value = $value['name'];
  977. }
  978. $html .= '<option value="'.$key.'"';
  979. if (is_array($default)) {
  980. foreach ($default as $item) {
  981. if ($item == $key) {
  982. $html .= ' selected="selected"';
  983. break;
  984. }
  985. }
  986. } else {
  987. if ($default == $key) {
  988. $html .= ' selected="selected"';
  989. }
  990. }
  991. $html .= '>'.$value.'</option>';
  992. }
  993. }
  994. $html .= '</select>';
  995. return $html;
  996. }
  997. /**
  998. * Creates a tab menu
  999. * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css
  1000. * in the $htmlHeadXtra variable before the display_header
  1001. * Add this script
  1002. * @example
  1003. * <script>
  1004. $(function() {
  1005. $( "#tabs" ).tabs();
  1006. });
  1007. </script>
  1008. * @param array $headers list of the tab titles
  1009. * @param array $items
  1010. * @param string $id id of the container of the tab in the example "tabs"
  1011. * @param array $attributes for the ul
  1012. * @param array $ul_attributes
  1013. *
  1014. * @return string
  1015. */
  1016. public static function tabs(
  1017. $headers,
  1018. $items,
  1019. $id = 'tabs',
  1020. $attributes = array(),
  1021. $ul_attributes = array()
  1022. ) {
  1023. if (empty($headers) || count($headers) == 0) {
  1024. return '';
  1025. }
  1026. $lis = '';
  1027. $i = 1;
  1028. foreach ($headers as $item) {
  1029. $active = '';
  1030. if ($i == 1) {
  1031. $active = ' active';
  1032. }
  1033. $item = self::tag(
  1034. 'a',
  1035. $item,
  1036. array(
  1037. 'href' => '#'.$id.'-'.$i,
  1038. 'role' => 'tab',
  1039. 'data-toggle' => 'tab',
  1040. 'id' => $id.$i,
  1041. )
  1042. );
  1043. $ul_attributes['role'] = 'presentation';
  1044. $ul_attributes['class'] = $active;
  1045. $lis .= self::tag('li', $item, $ul_attributes);
  1046. $i++;
  1047. }
  1048. $ul = self::tag(
  1049. 'ul',
  1050. $lis,
  1051. [
  1052. 'class' => 'nav nav-tabs tabs-margin',
  1053. 'role' => 'tablist',
  1054. 'id' => 'ul_'.$id
  1055. ]
  1056. );
  1057. $i = 1;
  1058. $divs = '';
  1059. foreach ($items as $content) {
  1060. $active = '';
  1061. if ($i == 1) {
  1062. $active = ' active';
  1063. }
  1064. $divs .= self::tag(
  1065. 'div',
  1066. $content,
  1067. array('id' => $id.'-'.$i, 'class' => 'tab-pane '.$active, 'role' => 'tabpanel')
  1068. );
  1069. $i++;
  1070. }
  1071. $attributes['id'] = $id;
  1072. $attributes['role'] = 'tabpanel';
  1073. $attributes['class'] = 'tab-wrapper';
  1074. $main_div = self::tag(
  1075. 'div',
  1076. $ul.self::tag('div', $divs, ['class' => 'tab-content']),
  1077. $attributes
  1078. );
  1079. return $main_div;
  1080. }
  1081. /**
  1082. * @param $headers
  1083. * @param null $selected
  1084. *
  1085. * @return string
  1086. */
  1087. public static function tabsOnlyLink($headers, $selected = null)
  1088. {
  1089. $id = uniqid();
  1090. $i = 1;
  1091. $lis = null;
  1092. foreach ($headers as $item) {
  1093. $class = null;
  1094. if ($i == $selected) {
  1095. $class = 'active';
  1096. }
  1097. $item = self::tag(
  1098. 'a',
  1099. $item['content'],
  1100. array('id' => $id.'-'.$i, 'href' => $item['url'])
  1101. );
  1102. $lis .= self::tag('li', $item, array('class' => $class));
  1103. $i++;
  1104. }
  1105. return self::tag(
  1106. 'ul',
  1107. $lis,
  1108. array('class' => 'nav nav-tabs tabs-margin')
  1109. );
  1110. }
  1111. /**
  1112. * In order to display a grid using jqgrid you have to:
  1113. * @example
  1114. * After your Display::display_header function you have to add the nex javascript code: *
  1115. * <script>
  1116. * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,array());
  1117. * // for more information of this function check the grid_js() function
  1118. * </script>
  1119. * //Then you have to call the grid_html
  1120. * echo Display::grid_html('my_grid_name');
  1121. * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work
  1122. *
  1123. * @param string the div id, this value must be the same with the first parameter of Display::grid_js()
  1124. * @return string html
  1125. *
  1126. */
  1127. public static function grid_html($div_id)
  1128. {
  1129. $table = self::tag('table', '', array('id' => $div_id));
  1130. $table .= self::tag('div', '', array('id' => $div_id.'_pager'));
  1131. return $table;
  1132. }
  1133. /**
  1134. * @param string $label
  1135. * @param string $form_item
  1136. * @return string
  1137. */
  1138. public static function form_row($label, $form_item)
  1139. {
  1140. $label = self::span($label, array('class' =>'control-label'));
  1141. $form_item = self::div($form_item, array('class' =>'controls'));
  1142. return self::div($label.$form_item, array('class'=>'control-group'));
  1143. }
  1144. /**
  1145. * This is a wrapper to use the jqgrid in Chamilo.
  1146. * For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
  1147. * This function need to be in the ready jquery function
  1148. * example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> }
  1149. * In order to work this function needs the Display::grid_html function with the same div id
  1150. *
  1151. * @param string $div_id div id
  1152. * @param string $url url where the jqgrid will ask for data (if datatype = json)
  1153. * @param array $column_names Visible columns (you should use get_lang). An array in which we place the names of the columns.
  1154. * This is the text that appears in the head of the grid (Header layer).
  1155. * Example: colname {name:'date', index:'date', width:120, align:'right'},
  1156. * @param array $column_model the column model : Array which describes the parameters of the columns.This is the most important part of the grid.
  1157. * For a full description of all valid values see colModel API. See the url above.
  1158. * @param array $extra_params extra parameters
  1159. * @param array $data data that will be loaded
  1160. * @param string $formatter A string that will be appended to the JSON returned
  1161. * @param bool $fixed_width not implemented yet
  1162. * @return string the js code
  1163. *
  1164. */
  1165. public static function grid_js(
  1166. $div_id,
  1167. $url,
  1168. $column_names,
  1169. $column_model,
  1170. $extra_params,
  1171. $data = array(),
  1172. $formatter = '',
  1173. $fixed_width = false
  1174. ) {
  1175. $obj = new stdClass();
  1176. $obj->first = 'first';
  1177. if (!empty($url)) {
  1178. $obj->url = $url;
  1179. }
  1180. //This line should only be used/modified in case of having characters
  1181. // encoding problems - see #6159
  1182. //$column_names = array_map("utf8_encode", $column_names);
  1183. $obj->colNames = $column_names;
  1184. $obj->colModel = $column_model;
  1185. $obj->pager = '#'.$div_id.'_pager';
  1186. $obj->datatype = 'json';
  1187. $obj->viewrecords = 'true';
  1188. $all_value = 10000000;
  1189. // Default row quantity
  1190. if (!isset($extra_params['rowList'])) {
  1191. $extra_params['rowList'] = array(20, 50, 100, 500, 1000, $all_value);
  1192. }
  1193. $json = '';
  1194. if (!empty($extra_params['datatype'])) {
  1195. $obj->datatype = $extra_params['datatype'];
  1196. }
  1197. // Row even odd style.
  1198. $obj->altRows = true;
  1199. if (!empty($extra_params['altRows'])) {
  1200. $obj->altRows = $extra_params['altRows'];
  1201. }
  1202. if (!empty($extra_params['sortname'])) {
  1203. $obj->sortname = $extra_params['sortname'];
  1204. }
  1205. if (!empty($extra_params['sortorder'])) {
  1206. $obj->sortorder = $extra_params['sortorder'];
  1207. }
  1208. if (!empty($extra_params['rowList'])) {
  1209. $obj->rowList = $extra_params['rowList'];
  1210. }
  1211. //Sets how many records we want to view in the grid
  1212. $obj->rowNum = 20;
  1213. if (!empty($extra_params['rowNum'])) {
  1214. $obj->rowNum = $extra_params['rowNum'];
  1215. }
  1216. if (!empty($extra_params['viewrecords'])) {
  1217. $obj->viewrecords = $extra_params['viewrecords'];
  1218. }
  1219. $beforeSelectRow = null;
  1220. if (isset($extra_params['beforeSelectRow'])) {
  1221. $beforeSelectRow = "beforeSelectRow: ".$extra_params['beforeSelectRow'].", ";
  1222. unset($extra_params['beforeSelectRow']);
  1223. }
  1224. // Adding extra params
  1225. if (!empty($extra_params)) {
  1226. foreach ($extra_params as $key => $element) {
  1227. // the groupHeaders key gets a special treatment
  1228. if ($key != 'groupHeaders') {
  1229. $obj->$key = $element;
  1230. }
  1231. }
  1232. }
  1233. // Adding static data.
  1234. if (!empty($data)) {
  1235. $data_var = $div_id.'_data';
  1236. $json .= ' var '.$data_var.' = '.json_encode($data).';';
  1237. $obj->data = $data_var;
  1238. $obj->datatype = 'local';
  1239. $json .= "\n";
  1240. }
  1241. $obj->end = 'end';
  1242. $json_encode = json_encode($obj);
  1243. if (!empty($data)) {
  1244. //Converts the "data":"js_variable" to "data":js_variable,
  1245. // otherwise it will not work
  1246. $json_encode = str_replace('"data":"'.$data_var.'"', '"data":'.$data_var.'', $json_encode);
  1247. }
  1248. // Fixing true/false js values that doesn't need the ""
  1249. $json_encode = str_replace(':"true"', ':true', $json_encode);
  1250. // wrap_cell is not a valid jqgrid attributes is a hack to wrap a text
  1251. $json_encode = str_replace('"wrap_cell":true', 'cellattr : function(rowId, value, rowObject, colModel, arrData) { return \'class = "jqgrid_whitespace"\'; }', $json_encode);
  1252. $json_encode = str_replace(':"false"', ':false', $json_encode);
  1253. $json_encode = str_replace('"formatter":"action_formatter"', 'formatter:action_formatter', $json_encode);
  1254. $json_encode = str_replace(array('{"first":"first",', '"end":"end"}'), '', $json_encode);
  1255. // Creating the jqgrid element.
  1256. $json .= '$("#'.$div_id.'").jqGrid({';
  1257. //$json .= $beforeSelectRow;
  1258. $json .= $json_encode;
  1259. $json .= '});';
  1260. // Grouping headers option
  1261. if (isset($extra_params['groupHeaders'])) {
  1262. $groups = '';
  1263. foreach ($extra_params['groupHeaders'] as $group) {
  1264. //{ "startColumnName" : "courses", "numberOfColumns" : 1, "titleText" : "Order Info" },
  1265. $groups .= '{ "startColumnName" : "'.$group['startColumnName'].'", "numberOfColumns" : '.$group['numberOfColumns'].', "titleText" : "'.$group['titleText'].'" },';
  1266. }
  1267. $json .= '$("#'.$div_id.'").jqGrid("setGroupHeaders", {
  1268. "useColSpanStyle" : false,
  1269. "groupHeaders" : [
  1270. ' . $groups.'
  1271. ]
  1272. });';
  1273. }
  1274. $all_text = addslashes(get_lang('All'));
  1275. $json .= '$("'.$obj->pager.' option[value='.$all_value.']").text("'.$all_text.'");';
  1276. $json .= "\n";
  1277. // Adding edit/delete icons.
  1278. $json .= $formatter;
  1279. return $json;
  1280. }
  1281. /**
  1282. * @param array $headers
  1283. * @param array $rows
  1284. * @param array $attributes
  1285. * @return string
  1286. */
  1287. public static function table($headers, $rows, $attributes = array())
  1288. {
  1289. if (empty($attributes)) {
  1290. $attributes['class'] = 'data_table';
  1291. }
  1292. $table = new HTML_Table($attributes);
  1293. $row = 0;
  1294. $column = 0;
  1295. // Course headers
  1296. if (!empty($headers)) {
  1297. foreach ($headers as $item) {
  1298. $table->setHeaderContents($row, $column, $item);
  1299. $column++;
  1300. }
  1301. $row = 1;
  1302. $column = 0;
  1303. }
  1304. if (!empty($rows)) {
  1305. foreach ($rows as $content) {
  1306. $table->setCellContents($row, $column, $content);
  1307. $row++;
  1308. }
  1309. }
  1310. return $table->toHtml();
  1311. }
  1312. /**
  1313. * Returns the "what's new" icon notifications
  1314. *
  1315. * The general logic of this function is to track the last time the user
  1316. * entered the course and compare to what has changed inside this course
  1317. * since then, based on the item_property table inside this course. Note that,
  1318. * if the user never entered the course before, he will not see notification
  1319. * icons. This function takes session ID into account (if any) and only shows
  1320. * the corresponding notifications.
  1321. * @param array $course_info Course information array, containing at least elements 'db' and 'k'
  1322. * @param bool $loadAjax
  1323. * @return string The HTML link to be shown next to the course
  1324. */
  1325. public static function show_notification($course_info, $loadAjax = true)
  1326. {
  1327. if (empty($course_info)) {
  1328. return '';
  1329. }
  1330. $t_track_e_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1331. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  1332. $tool_edit_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1333. $course_code = Database::escape_string($course_info['code']);
  1334. $user_id = api_get_user_id();
  1335. $course_id = (int) $course_info['real_id'];
  1336. $sessionId = (int) $course_info['id_session'];
  1337. $status = (int) $course_info['status'];
  1338. $loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax');
  1339. if ($loadNotificationsByAjax) {
  1340. if ($loadAjax) {
  1341. $id = 'notification_'.$course_id.'_'.$sessionId.'_'.$status;
  1342. Session::write($id, true);
  1343. return '<span id ="'.$id.'" class="course_notification"></span>';
  1344. }
  1345. }
  1346. // Get the user's last access dates to all tools of this course
  1347. $sql = "SELECT *
  1348. FROM $t_track_e_access
  1349. WHERE
  1350. c_id = $course_id AND
  1351. access_user_id = '$user_id' AND
  1352. access_session_id ='".$sessionId."'";
  1353. $resLastTrackInCourse = Database::query($sql);
  1354. $oldestTrackDate = $oldestTrackDateOrig = '3000-01-01 00:00:00';
  1355. while ($lastTrackInCourse = Database::fetch_array($resLastTrackInCourse)) {
  1356. $lastTrackInCourseDate[$lastTrackInCourse['access_tool']] = $lastTrackInCourse['access_date'];
  1357. if ($oldestTrackDate > $lastTrackInCourse['access_date']) {
  1358. $oldestTrackDate = $lastTrackInCourse['access_date'];
  1359. }
  1360. }
  1361. if ($oldestTrackDate == $oldestTrackDateOrig) {
  1362. //if there was no connexion to the course ever, then take the
  1363. // course creation date as a reference
  1364. $oldestTrackDate = $course_info['creation_date'];
  1365. }
  1366. $sessionCondition = api_get_session_condition(
  1367. $sessionId,
  1368. true,
  1369. false,
  1370. 'session_id'
  1371. );
  1372. $hideTools = [TOOL_NOTEBOOK, TOOL_CHAT];
  1373. // Get current tools in course
  1374. $sql = "SELECT name, link, image
  1375. FROM $course_tool_table
  1376. WHERE
  1377. c_id = $course_id AND
  1378. visibility = '1' AND
  1379. name NOT IN ('".implode("','", $hideTools)."')
  1380. ";
  1381. $result = Database::query($sql);
  1382. $tools = Database::store_result($result);
  1383. $group_ids = GroupManager::get_group_ids($course_info['real_id'], $user_id);
  1384. $group_ids[] = 0; //add group 'everyone'
  1385. $notifications = array();
  1386. if ($tools) {
  1387. foreach ($tools as $tool) {
  1388. $toolName = $tool['name'];
  1389. // Fix to get student publications
  1390. if ($toolName == 'student_publication') {
  1391. $toolName = 'work';
  1392. }
  1393. $toolName = addslashes(Database::escape_string($toolName));
  1394. $sql = "SELECT * FROM $tool_edit_table
  1395. WHERE
  1396. c_id = $course_id AND
  1397. tool = '$toolName' AND
  1398. lastedit_type NOT LIKE '%Deleted%' AND
  1399. lastedit_type NOT LIKE '%deleted%' AND
  1400. lastedit_type NOT LIKE '%DocumentInvisible%' AND
  1401. lastedit_date > '$oldestTrackDate' AND
  1402. lastedit_user_id != $user_id $sessionCondition AND
  1403. visibility != 2 AND
  1404. (to_user_id IN ('$user_id', '0') OR to_user_id IS NULL) AND
  1405. (to_group_id IN ('".implode("','", $group_ids)."') OR to_group_id IS NULL)
  1406. ORDER BY lastedit_date DESC
  1407. LIMIT 1";
  1408. $result = Database::query($sql);
  1409. $latestChange = Database::fetch_array($result);
  1410. if ($latestChange) {
  1411. $latestChange['link'] = $tool['link'];
  1412. $latestChange['image'] = $tool['image'];
  1413. $latestChange['tool'] = $tool['name'];
  1414. $notifications[$toolName] = $latestChange;
  1415. }
  1416. }
  1417. }
  1418. // Show all tool icons where there is something new.
  1419. $return = '';
  1420. foreach ($notifications as $notification) {
  1421. $toolName = $notification['tool'];
  1422. if (!(
  1423. $notification['visibility'] == '1' ||
  1424. ($status == '1' && $notification['visibility'] == '0') ||
  1425. !isset($notification['visibility'])
  1426. )
  1427. ) {
  1428. continue;
  1429. }
  1430. if ($toolName == TOOL_SURVEY) {
  1431. $survey_info = SurveyManager::get_survey($notification['ref'], 0, $course_code);
  1432. if (!empty($survey_info)) {
  1433. $invited_users = SurveyUtil::get_invited_users(
  1434. $survey_info['code'],
  1435. $course_code
  1436. );
  1437. if (!in_array($user_id, $invited_users['course_users'])) {
  1438. continue;
  1439. }
  1440. }
  1441. }
  1442. if ($notification['tool'] == TOOL_LEARNPATH) {
  1443. if (!learnpath::is_lp_visible_for_student($notification['ref'], $user_id, $course_code)) {
  1444. continue;
  1445. }
  1446. }
  1447. if ($notification['tool'] == TOOL_DROPBOX) {
  1448. $notification['link'] = 'dropbox/dropbox_download.php?id='.$notification['ref'];
  1449. }
  1450. if ($notification['tool'] == 'work' &&
  1451. $notification['lastedit_type'] == 'DirectoryCreated'
  1452. ) {
  1453. $notification['lastedit_type'] = 'WorkAdded';
  1454. }
  1455. $lastDate = api_get_local_time($notification['lastedit_date']);
  1456. $type = $notification['lastedit_type'];
  1457. if ($type == 'CalendareventVisible') {
  1458. $type = 'Visible';
  1459. }
  1460. $label = get_lang('TitleNotification').": ".get_lang($type)." ($lastDate)";
  1461. if (strpos($notification['link'], '?') === false) {
  1462. $notification['link'] = $notification['link'].'?notification=1';
  1463. } else {
  1464. $notification['link'] = $notification['link'].'&notification=1';
  1465. }
  1466. $image = substr($notification['image'], 0, -4).'.png';
  1467. $return .= self::url(
  1468. self::return_icon($image, $label),
  1469. api_get_path(WEB_CODE_PATH).
  1470. $notification['link'].'&cidReq='.$course_code.
  1471. '&ref='.$notification['ref'].
  1472. '&gidReq='.$notification['to_group_id'].
  1473. '&id_session='.$sessionId
  1474. ).PHP_EOL;
  1475. }
  1476. return $return;
  1477. }
  1478. /**
  1479. * Get the session box details as an array
  1480. * @param int Session ID
  1481. * @return array Empty array or session array
  1482. * ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int]
  1483. */
  1484. public static function get_session_title_box($session_id)
  1485. {
  1486. global $nosession;
  1487. if (!$nosession) {
  1488. global $now, $date_start, $date_end;
  1489. }
  1490. $output = array();
  1491. $active = false;
  1492. if (!$nosession) {
  1493. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  1494. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1495. // Request for the name of the general coach
  1496. $sql = 'SELECT tu.lastname, tu.firstname, ts.*
  1497. FROM '.$tbl_session.' ts
  1498. LEFT JOIN '.$main_user_table.' tu
  1499. ON ts.id_coach = tu.user_id
  1500. WHERE ts.id = '.intval($session_id);
  1501. $rs = Database::query($sql);
  1502. $session_info = Database::store_result($rs, 'ASSOC');
  1503. $session_info = $session_info[0];
  1504. $session = array();
  1505. $session['category_id'] = $session_info['session_category_id'];
  1506. $session['title'] = $session_info['name'];
  1507. $session['id_coach'] = $session_info['id_coach'];
  1508. $session['coach'] = '';
  1509. $session['dates'] = '';
  1510. if (api_get_setting('show_session_coach') === 'true') {
  1511. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($session_info['firstname'], $session_info['lastname']);
  1512. }
  1513. if (
  1514. ($session_info['access_end_date'] == '0000-00-00 00:00:00' && $session_info['access_start_date'] == '0000-00-00 00:00:00') ||
  1515. (empty($session_info['access_end_date']) && empty($session_info['access_start_date']))
  1516. ) {
  1517. if (isset($session_info['duration']) && !empty($session_info['duration'])) {
  1518. $daysLeft = SessionManager::getDayLeftInSession($session_info, api_get_user_id());
  1519. $session['duration'] = $daysLeft >= 0
  1520. ? sprintf(get_lang('SessionDurationXDaysLeft'), $daysLeft)
  1521. : get_lang('YourSessionTimeHasExpired');
  1522. }
  1523. $active = true;
  1524. } else {
  1525. $dates = SessionManager::parseSessionDates($session_info, true);
  1526. $session['dates'] = $dates['access'];
  1527. if (api_get_setting('show_session_coach') === 'true') {
  1528. $session['coach'] = api_get_person_name(
  1529. $session_info['firstname'],
  1530. $session_info['lastname']
  1531. );
  1532. }
  1533. $active = $date_start <= $now && $date_end >= $now;
  1534. }
  1535. $session['active'] = $active;
  1536. $session['session_category_id'] = $session_info['session_category_id'];
  1537. $session['visibility'] = $session_info['visibility'];
  1538. $session['num_users'] = $session_info['nbr_users'];
  1539. $session['num_courses'] = $session_info['nbr_courses'];
  1540. $session['description'] = $session_info['description'];
  1541. $session['show_description'] = $session_info['show_description'];
  1542. $entityManager = Database::getManager();
  1543. $fieldValuesRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraFieldValues');
  1544. $extraFieldValues = $fieldValuesRepo->getVisibleValues(
  1545. ExtraField::SESSION_FIELD_TYPE,
  1546. $session_id
  1547. );
  1548. $session['extra_fields'] = [];
  1549. foreach ($extraFieldValues as $value) {
  1550. $session['extra_fields'][] = [
  1551. 'field' => [
  1552. 'variable' => $value->getField()->getVariable(),
  1553. 'display_text' => $value->getField()->getDisplayText()
  1554. ],
  1555. 'value' => $value->getValue()
  1556. ];
  1557. }
  1558. $output = $session;
  1559. }
  1560. return $output;
  1561. }
  1562. /**
  1563. * Return the five star HTML
  1564. *
  1565. * @param string $id of the rating ul element
  1566. * @param string $url that will be added (for jquery see hot_courses.tpl)
  1567. * @param array $point_info point info array see function CourseManager::get_course_ranking()
  1568. * @param bool $add_div_wrapper add a div wrapper
  1569. * @return string
  1570. **/
  1571. public static function return_rating_system(
  1572. $id,
  1573. $url,
  1574. $point_info = [],
  1575. $add_div_wrapper = true
  1576. ) {
  1577. $number_of_users_who_voted = isset($point_info['users_who_voted']) ? $point_info['users_who_voted'] : null;
  1578. $percentage = isset($point_info['point_average']) ? $point_info['point_average'] : 0;
  1579. if (!empty($percentage)) {
  1580. $percentage = $percentage * 125 / 100;
  1581. }
  1582. $accesses = isset($point_info['accesses']) ? $point_info['accesses'] : 0;
  1583. $star_label = sprintf(get_lang('XStarsOutOf5'), $point_info['point_average_star']);
  1584. $html = '<ul id="'.$id.'" class="star-rating">
  1585. <li class="current-rating" style="width:'.$percentage.'px;"></li>
  1586. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=1" title="'.$star_label.'" class="one-star">1</a></li>
  1587. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=2" title="'.$star_label.'" class="two-stars">2</a></li>
  1588. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=3" title="'.$star_label.'" class="three-stars">3</a></li>
  1589. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=4" title="'.$star_label.'" class="four-stars">4</a></li>
  1590. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=5" title="'.$star_label.'" class="five-stars">5</a></li>
  1591. </ul>';
  1592. $labels = array();
  1593. $labels[] = $number_of_users_who_voted == 1 ? $number_of_users_who_voted.' '.get_lang('Vote') : $number_of_users_who_voted.' '.get_lang('Votes');
  1594. $labels[] = $accesses == 1 ? $accesses.' '.get_lang('Visit') : $accesses.' '.get_lang('Visits');
  1595. $labels[] = $point_info['user_vote'] ? get_lang('YourVote').' ['.$point_info['user_vote'].']' : get_lang('YourVote').' [?] ';
  1596. if (!$add_div_wrapper && api_is_anonymous()) {
  1597. $labels[] = self::tag('span', get_lang('LoginToVote'), array('class' => 'error'));
  1598. }
  1599. $html .= self::div(implode(' | ', $labels), array('id' => 'vote_label_'.$id, 'class' => 'vote_label_info'));
  1600. $html .= ' '.self::span(' ', array('id' => 'vote_label2_'.$id));
  1601. if ($add_div_wrapper) {
  1602. $html = self::div($html, array('id' => 'rating_wrapper_'.$id));
  1603. }
  1604. return $html;
  1605. }
  1606. /**
  1607. * @param string $title
  1608. * @param string $second_title
  1609. * @param string $size
  1610. * @param bool $filter
  1611. * @return string
  1612. */
  1613. public static function page_header($title, $second_title = null, $size = 'h2', $filter = true)
  1614. {
  1615. if ($filter) {
  1616. $title = Security::remove_XSS($title);
  1617. }
  1618. if (!empty($second_title)) {
  1619. if ($filter) {
  1620. $second_title = Security::remove_XSS($second_title);
  1621. }
  1622. $title .= "<small> $second_title<small>";
  1623. }
  1624. return '<'.$size.' class="page-header">'.$title.'</'.$size.'>';
  1625. }
  1626. public static function page_header_and_translate($title, $second_title = null)
  1627. {
  1628. $title = get_lang($title);
  1629. return self::page_header($title, $second_title);
  1630. }
  1631. public static function page_subheader_and_translate($title, $second_title = null)
  1632. {
  1633. $title = get_lang($title);
  1634. return self::page_subheader($title, $second_title);
  1635. }
  1636. public static function page_subheader($title, $second_title = null, $size = 'h3')
  1637. {
  1638. if (!empty($second_title)) {
  1639. $second_title = Security::remove_XSS($second_title);
  1640. $title .= "<small> $second_title<small>";
  1641. }
  1642. return '<'.$size.'>'.Security::remove_XSS($title).'</'.$size.'>';
  1643. }
  1644. public static function page_subheader2($title, $second_title = null)
  1645. {
  1646. return self::page_header($title, $second_title, 'h4');
  1647. }
  1648. public static function page_subheader3($title, $second_title = null)
  1649. {
  1650. return self::page_header($title, $second_title, 'h5');
  1651. }
  1652. /**
  1653. * @param array $list
  1654. * @return null|string
  1655. */
  1656. public static function description($list)
  1657. {
  1658. $html = null;
  1659. if (!empty($list)) {
  1660. $html = '<dl class="dl-horizontal">';
  1661. foreach ($list as $item) {
  1662. $html .= '<dt>'.$item['title'].'</dt>';
  1663. $html .= '<dd>'.$item['content'].'</dd>';
  1664. }
  1665. $html .= '</dl>';
  1666. }
  1667. return $html;
  1668. }
  1669. /**
  1670. * @param $percentage
  1671. * @param bool $show_percentage
  1672. * @param null $extra_info
  1673. * @return string
  1674. */
  1675. public static function bar_progress($percentage, $show_percentage = true, $extra_info = null)
  1676. {
  1677. $percentage = intval($percentage);
  1678. $div = '<div class="progress">
  1679. <div
  1680. class="progress-bar progress-bar-striped"
  1681. role="progressbar"
  1682. aria-valuenow="'.$percentage.'"
  1683. aria-valuemin="0"
  1684. aria-valuemax="100"
  1685. style="width: '.$percentage.'%;"
  1686. >';
  1687. if ($show_percentage) {
  1688. $div .= $percentage.'%';
  1689. } else {
  1690. if (!empty($extra_info)) {
  1691. $div .= $extra_info;
  1692. }
  1693. }
  1694. $div .= '</div>';
  1695. return $div;
  1696. }
  1697. /**
  1698. * @param string $count
  1699. * @param string $type
  1700. * @return null|string
  1701. */
  1702. public static function badge($count, $type = "warning")
  1703. {
  1704. $class = '';
  1705. switch ($type) {
  1706. case 'success':
  1707. $class = 'badge-success';
  1708. break;
  1709. case 'warning':
  1710. $class = 'badge-warning';
  1711. break;
  1712. case 'important':
  1713. $class = 'badge-important';
  1714. break;
  1715. case 'info':
  1716. $class = 'badge-info';
  1717. break;
  1718. case 'inverse':
  1719. $class = 'badge-inverse';
  1720. break;
  1721. }
  1722. if (!empty($count)) {
  1723. return ' <span class="badge '.$class.'">'.$count.'</span>';
  1724. }
  1725. return null;
  1726. }
  1727. /**
  1728. * @param array $badge_list
  1729. * @return string
  1730. */
  1731. public static function badge_group($badge_list)
  1732. {
  1733. $html = '<div class="badge-group">';
  1734. foreach ($badge_list as $badge) {
  1735. $html .= $badge;
  1736. }
  1737. $html .= '</div>';
  1738. return $html;
  1739. }
  1740. /**
  1741. * @param string $content
  1742. * @param string $type
  1743. * @return string
  1744. */
  1745. public static function label($content, $type = 'default')
  1746. {
  1747. switch ($type) {
  1748. case 'success':
  1749. $class = 'label-success';
  1750. break;
  1751. case 'warning':
  1752. $class = 'label-warning';
  1753. break;
  1754. case 'important':
  1755. //no break
  1756. case 'danger':
  1757. $class = 'label-danger';
  1758. break;
  1759. case 'info':
  1760. $class = 'label-info';
  1761. break;
  1762. case 'primary':
  1763. $class = 'label-primary';
  1764. break;
  1765. default:
  1766. $class = 'label-default';
  1767. break;
  1768. }
  1769. $html = '';
  1770. if (!empty($content)) {
  1771. $html = '<span class="label '.$class.'">';
  1772. $html .= $content;
  1773. $html .= '</span>';
  1774. }
  1775. return $html;
  1776. }
  1777. /**
  1778. * @param array $items
  1779. * @return null|string
  1780. */
  1781. public static function actions($items, $class = 'new_actions')
  1782. {
  1783. $html = null;
  1784. if (!empty($items)) {
  1785. $html = '<div class="'.$class.'"><ul class="nav nav-pills">';
  1786. foreach ($items as $value) {
  1787. $class = null;
  1788. if (isset($value['active']) && $value['active']) {
  1789. $class = 'class ="active"';
  1790. }
  1791. if (basename($_SERVER['REQUEST_URI']) == basename($value['url'])) {
  1792. $class = 'class ="active"';
  1793. }
  1794. $html .= "<li $class >";
  1795. $attributes = isset($value['url_attributes']) ? $value['url_attributes'] : array();
  1796. $html .= self::url($value['content'], $value['url'], $attributes);
  1797. $html .= '</li>';
  1798. }
  1799. $html .= '</ul></div>';
  1800. $html .= '<br />';
  1801. }
  1802. return $html;
  1803. }
  1804. /**
  1805. * Prints a tooltip
  1806. * @param string $text
  1807. * @param string $tip
  1808. *
  1809. * @return string
  1810. */
  1811. public static function tip($text, $tip)
  1812. {
  1813. if (empty($tip)) {
  1814. return $text;
  1815. }
  1816. return self::span(
  1817. $text,
  1818. array('class' => 'boot-tooltip', 'title' => strip_tags($tip))
  1819. );
  1820. }
  1821. /**
  1822. * @param array $items
  1823. * @param string $type
  1824. * @param null $id
  1825. * @return null|string
  1826. */
  1827. public static function generate_accordion($items, $type = 'jquery', $id = null)
  1828. {
  1829. $html = null;
  1830. if (!empty($items)) {
  1831. if (empty($id)) {
  1832. $id = api_get_unique_id();
  1833. }
  1834. if ($type == 'jquery') {
  1835. $html = '<div class="accordion_jquery" id="'.$id.'">'; //using jquery
  1836. } else {
  1837. $html = '<div class="accordion" id="'.$id.'">'; //using bootstrap
  1838. }
  1839. $count = 1;
  1840. foreach ($items as $item) {
  1841. $html .= '<div class="accordion-my-group">';
  1842. $html .= '<div class="accordion-heading">
  1843. <a class="accordion-toggle" data-toggle="collapse" data-parent="#'.$id.'" href="#collapse'.$count.'">
  1844. '.$item['title'].'
  1845. </a>
  1846. </div>';
  1847. $html .= '<div id="collapse'.$count.'" class="accordion-body">';
  1848. $html .= '<div class="accordion-my-inner">
  1849. '.$item['content'].'
  1850. </div>
  1851. </div>';
  1852. }
  1853. $html .= '</div>';
  1854. }
  1855. return $html;
  1856. }
  1857. /**
  1858. * @param array $buttons
  1859. * @return string
  1860. */
  1861. public static function groupButton($buttons)
  1862. {
  1863. $html = '<div class="btn-group" role="group">';
  1864. foreach ($buttons as $button) {
  1865. $html .= $button;
  1866. }
  1867. $html .= '</div>';
  1868. return $html;
  1869. }
  1870. /**
  1871. * @todo use twig
  1872. * @param string $title
  1873. * @param array $elements
  1874. * @param bool $alignToRight
  1875. * @return string
  1876. */
  1877. public static function groupButtonWithDropDown($title, $elements, $alignToRight = false)
  1878. {
  1879. $html = '<div class="btn-group">
  1880. <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  1881. '.$title.'
  1882. <span class="caret"></span></button>
  1883. <ul class="dropdown-menu ' . ($alignToRight ? 'dropdown-menu-right' : '').'">';
  1884. foreach ($elements as $item) {
  1885. $html .= self::tag('li', self::url($item['title'], $item['href']));
  1886. }
  1887. $html .= '</ul>
  1888. </div>';
  1889. return $html;
  1890. }
  1891. /**
  1892. * @param string $file
  1893. * @param array $params
  1894. * @return null|string
  1895. */
  1896. public static function getMediaPlayer($file, $params = array())
  1897. {
  1898. $fileInfo = pathinfo($file);
  1899. switch ($fileInfo['extension']) {
  1900. case 'mp3':
  1901. case 'webm':
  1902. $autoplay = null;
  1903. if (isset($params['autoplay']) && $params['autoplay'] == 'true') {
  1904. $autoplay = 'autoplay';
  1905. }
  1906. $width = isset($params['width']) ? 'width="'.$params['width'].'"' : null;
  1907. $id = isset($params['id']) ? $params['id'] : $fileInfo['basename'];
  1908. $class = isset($params['class']) ? ' class="'.$params['class'].'"' : null;
  1909. $html = '<audio id="'.$id.'" '.$class.' controls '.$autoplay.' '.$width.' src="'.$params['url'].'" >';
  1910. $html .= '<object width="'.$width.'" height="50" type="application/x-shockwave-flash" data="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf">
  1911. <param name="movie" value="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf" />
  1912. <param name="flashvars" value="controls=true&file='.$params['url'].'" />
  1913. </object>';
  1914. $html .= '</audio>';
  1915. return $html;
  1916. break;
  1917. case 'wav':
  1918. //no break;
  1919. case 'ogg':
  1920. $html = '<audio width="300px" controls src="'.$params['url'].'" >';
  1921. return $html;
  1922. break;
  1923. }
  1924. return null;
  1925. }
  1926. /**
  1927. * @param int $nextValue
  1928. * @param array $list
  1929. * @param int $current
  1930. * @param int $fixedValue
  1931. * @param array $conditions
  1932. * @param string $link
  1933. * @param bool $isMedia
  1934. * @param bool $addHeaders
  1935. * @param array $linkAttributes
  1936. * @return string
  1937. */
  1938. public static function progressPaginationBar(
  1939. $nextValue,
  1940. $list,
  1941. $current,
  1942. $fixedValue = null,
  1943. $conditions = [],
  1944. $link = null,
  1945. $isMedia = false,
  1946. $addHeaders = true,
  1947. $linkAttributes = []
  1948. ) {
  1949. if ($addHeaders) {
  1950. $pagination_size = 'pagination-mini';
  1951. $html = '<div class="exercise_pagination pagination '.$pagination_size.'"><ul>';
  1952. } else {
  1953. $html = null;
  1954. }
  1955. $affectAllItems = false;
  1956. if ($isMedia && isset($fixedValue) && ($nextValue + 1 == $current)) {
  1957. $affectAllItems = true;
  1958. }
  1959. $localCounter = 0;
  1960. foreach ($list as $itemId) {
  1961. $isCurrent = false;
  1962. if ($affectAllItems) {
  1963. $isCurrent = true;
  1964. } else {
  1965. if (!$isMedia) {
  1966. $isCurrent = $current == ($localCounter + $nextValue + 1) ? true : false;
  1967. }
  1968. }
  1969. $html .= self::parsePaginationItem(
  1970. $itemId,
  1971. $isCurrent,
  1972. $conditions,
  1973. $link,
  1974. $nextValue,
  1975. $isMedia,
  1976. $localCounter,
  1977. $fixedValue,
  1978. $linkAttributes
  1979. );
  1980. $localCounter++;
  1981. }
  1982. if ($addHeaders) {
  1983. $html .= '</ul></div>';
  1984. }
  1985. return $html;
  1986. }
  1987. /**
  1988. *
  1989. * @param int $itemId
  1990. * @param bool $isCurrent
  1991. * @param array $conditions
  1992. * @param string $link
  1993. * @param int $nextValue
  1994. * @param bool $isMedia
  1995. * @param int $localCounter
  1996. * @param int $fixedValue
  1997. * @param array $linkAttributes
  1998. *
  1999. * @return string
  2000. */
  2001. public static function parsePaginationItem(
  2002. $itemId,
  2003. $isCurrent,
  2004. $conditions,
  2005. $link,
  2006. $nextValue = 0,
  2007. $isMedia = false,
  2008. $localCounter = null,
  2009. $fixedValue = null,
  2010. $linkAttributes = []
  2011. ) {
  2012. $defaultClass = 'before';
  2013. $class = $defaultClass;
  2014. foreach ($conditions as $condition) {
  2015. $array = isset($condition['items']) ? $condition['items'] : array();
  2016. $class_to_applied = $condition['class'];
  2017. $type = isset($condition['type']) ? $condition['type'] : 'positive';
  2018. $mode = isset($condition['mode']) ? $condition['mode'] : 'add';
  2019. switch ($type) {
  2020. case 'positive':
  2021. if (in_array($itemId, $array)) {
  2022. if ($mode == 'overwrite') {
  2023. $class = " $defaultClass $class_to_applied";
  2024. } else {
  2025. $class .= " $class_to_applied";
  2026. }
  2027. }
  2028. break;
  2029. case 'negative':
  2030. if (!in_array($itemId, $array)) {
  2031. if ($mode == 'overwrite') {
  2032. $class = " $defaultClass $class_to_applied";
  2033. } else {
  2034. $class .= " $class_to_applied";
  2035. }
  2036. }
  2037. break;
  2038. }
  2039. }
  2040. if ($isCurrent) {
  2041. $class = 'before current';
  2042. }
  2043. if ($isMedia && $isCurrent) {
  2044. $class = 'before current';
  2045. }
  2046. if (empty($link)) {
  2047. $link_to_show = '#';
  2048. } else {
  2049. $link_to_show = $link.($nextValue + $localCounter);
  2050. }
  2051. $label = $nextValue + $localCounter + 1;
  2052. if ($isMedia) {
  2053. $label = ($fixedValue + 1).' '.chr(97 + $localCounter);
  2054. $link_to_show = $link.$fixedValue.'#questionanchor'.$itemId;
  2055. }
  2056. $link = self::url($label.' ', $link_to_show, $linkAttributes);
  2057. return '<li class = "'.$class.'">'.$link.'</li>';
  2058. }
  2059. /**
  2060. * @param int $current
  2061. * @param int $total
  2062. * @return string
  2063. */
  2064. public static function paginationIndicator($current, $total)
  2065. {
  2066. $html = null;
  2067. if (!empty($current) && !empty($total)) {
  2068. $label = sprintf(get_lang('PaginationXofY'), $current, $total);
  2069. $html = self::url($label, '#', array('class' => 'btn disabled'));
  2070. }
  2071. return $html;
  2072. }
  2073. /**
  2074. * Adds a message in the queue
  2075. * @param string $message
  2076. */
  2077. public static function addFlash($message)
  2078. {
  2079. $messages = Session::read('flash_messages');
  2080. if (empty($messages)) {
  2081. $messages[] = $message;
  2082. } else {
  2083. array_push($messages, $message);
  2084. }
  2085. Session::write('flash_messages', $messages);
  2086. }
  2087. /**
  2088. * @return string
  2089. */
  2090. public static function getFlashToString()
  2091. {
  2092. $messages = Session::read('flash_messages');
  2093. $messageToString = '';
  2094. if (!empty($messages)) {
  2095. foreach ($messages as $message) {
  2096. $messageToString .= $message;
  2097. }
  2098. }
  2099. return $messageToString;
  2100. }
  2101. /**
  2102. * Shows the message from the session
  2103. */
  2104. public static function showFlash()
  2105. {
  2106. echo self::getFlashToString();
  2107. }
  2108. /**
  2109. * Destroys the message session
  2110. */
  2111. public static function cleanFlashMessages()
  2112. {
  2113. Session::erase('flash_messages');
  2114. }
  2115. /**
  2116. * Get the profile edition link for a user
  2117. * @param int $userId The user id
  2118. * @param boolean $asAdmin Optional. Whether get the URL for the platform admin
  2119. * @return string The link
  2120. */
  2121. public static function getProfileEditionLink($userId, $asAdmin = false)
  2122. {
  2123. $editProfileUrl = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  2124. if ($asAdmin) {
  2125. $editProfileUrl = api_get_path(WEB_CODE_PATH)."admin/user_edit.php?user_id=".intval($userId);
  2126. }
  2127. if (api_get_setting('sso_authentication') === 'true') {
  2128. $subSSOClass = api_get_setting('sso_authentication_subclass');
  2129. $objSSO = null;
  2130. if (!empty($subSSOClass)) {
  2131. $file = api_get_path(SYS_CODE_PATH)."auth/sso/sso.$subSSOClass.class.php";
  2132. if (file_exists($file)) {
  2133. require_once $file;
  2134. $subSSOClass = 'sso'.$subSSOClass;
  2135. $objSSO = new $subSSOClass();
  2136. } else {
  2137. throw new Exception("$subSSOClass file not set");
  2138. }
  2139. } else {
  2140. $objSSO = new sso();
  2141. }
  2142. $editProfileUrl = $objSSO->generateProfileEditingURL(
  2143. $userId,
  2144. $asAdmin
  2145. );
  2146. }
  2147. return $editProfileUrl;
  2148. }
  2149. /**
  2150. * Get the vCard for a user
  2151. * @param int $userId The user id
  2152. * @return string *.*vcf file
  2153. */
  2154. public static function getVCardUserLink($userId)
  2155. {
  2156. $vCardUrl = api_get_path(WEB_PATH).'main/social/vcard_export.php?userId='.intval($userId);
  2157. return $vCardUrl;
  2158. }
  2159. /**
  2160. * @param string $content
  2161. * @param string $title
  2162. * @param string $footer
  2163. * @param string $type primary|success|info|warning|danger
  2164. * @param string $extra
  2165. * @param string $id
  2166. * @param string $customColor
  2167. *
  2168. * @return string
  2169. */
  2170. public static function panel(
  2171. $content,
  2172. $title = '',
  2173. $footer = '',
  2174. $type = 'default',
  2175. $extra = '',
  2176. $id = '',
  2177. $customColor = ''
  2178. ) {
  2179. $headerStyle = '';
  2180. if (!empty($customColor)) {
  2181. $headerStyle = 'style = "color: white; background-color: '.$customColor.'" ';
  2182. }
  2183. $title = !empty($title) ? '<div class="panel-heading" '.$headerStyle.' ><h3 class="panel-title">'.$title.'</h3>'.$extra.'</div>' : '';
  2184. $footer = !empty($footer) ? '<div class="panel-footer">'.$footer.'</div>' : '';
  2185. $typeList = ['primary', 'success', 'info', 'warning', 'danger'];
  2186. $style = !in_array($type, $typeList) ? 'default' : $type;
  2187. if (!empty($id)) {
  2188. $id = " id = $id ";
  2189. }
  2190. return '
  2191. <div '.$id.' class="panel panel-'.$style.'">
  2192. '.$title.'
  2193. '.self::contentPanel($content).'
  2194. '.$footer.'
  2195. </div>'
  2196. ;
  2197. }
  2198. /**
  2199. * @param string $content
  2200. * @return string
  2201. */
  2202. public static function contentPanel($content)
  2203. {
  2204. if (empty($content)) {
  2205. return '';
  2206. }
  2207. return '<div class="panel-body">'.$content.'</div>';
  2208. }
  2209. /**
  2210. * Get the button HTML with an Awesome Font icon
  2211. * @param string $text The button content
  2212. * @param string $url The url to button
  2213. * @param string $icon The Awesome Font class for icon
  2214. * @param string $type Optional. The button Bootstrap class. Default 'default' class
  2215. * @param array $attributes The additional attributes
  2216. * @param bool $includeText
  2217. *
  2218. * @return string The button HTML
  2219. */
  2220. public static function toolbarButton(
  2221. $text,
  2222. $url,
  2223. $icon = 'check',
  2224. $type = 'default',
  2225. array $attributes = [],
  2226. $includeText = true
  2227. ) {
  2228. $buttonClass = "btn btn-$type";
  2229. $icon = self::tag('i', null, ['class' => "fa fa-$icon fa-fw", 'aria-hidden' => 'true']);
  2230. $attributes['class'] = isset($attributes['class']) ? "$buttonClass {$attributes['class']}" : $buttonClass;
  2231. $attributes['title'] = isset($attributes['title']) ? $attributes['title'] : $text;
  2232. if (!$includeText) {
  2233. $text = '<span class="sr-only">'.$text.'</span>';
  2234. }
  2235. return self::url("$icon $text", $url, $attributes);
  2236. }
  2237. /**
  2238. * @param string $id
  2239. * @param array $content
  2240. * @param array $colsWidth Optional. Columns width
  2241. * @return string
  2242. */
  2243. public static function toolbarAction($id, $content, $colsWidth = [])
  2244. {
  2245. $col = count($content);
  2246. if (!$colsWidth) {
  2247. $width = 12 / $col;
  2248. array_walk($content, function() use ($width, &$colsWidth) {
  2249. $colsWidth[] = $width;
  2250. });
  2251. }
  2252. $html = '<div id="'.$id.'" class="actions">';
  2253. $html .= '<div class="row">';
  2254. for ($i = 0; $i < $col; $i++) {
  2255. $class = 'col-sm-'.$colsWidth[$i];
  2256. if ($col > 1) {
  2257. if ($i > 0 && $i < count($content) - 1) {
  2258. $class .= ' text-center';
  2259. } elseif ($i === count($content) - 1) {
  2260. $class .= ' text-right';
  2261. }
  2262. }
  2263. $html .= '<div class="'.$class.'">'.$content[$i].'</div>';
  2264. }
  2265. $html .= '</div>';
  2266. $html .= '</div>';
  2267. return $html;
  2268. }
  2269. /**
  2270. * Get a HTML code for a icon by Font Awesome
  2271. * @param string $name The icon name
  2272. * @param int|string $size Optional. The size for the icon. (Example: lg, 2, 3, 4, 5)
  2273. * @param boolean $fixWidth Optional. Whether add the fw class
  2274. * @param string $additionalClass Optional. Additional class
  2275. *
  2276. * @return string
  2277. */
  2278. public static function returnFontAwesomeIcon(
  2279. $name,
  2280. $size = '',
  2281. $fixWidth = false,
  2282. $additionalClass = ''
  2283. ) {
  2284. $className = "fa fa-$name";
  2285. if ($fixWidth) {
  2286. $className .= ' fa-fw';
  2287. }
  2288. switch ($size) {
  2289. case 'lg':
  2290. $className .= ' fa-lg';
  2291. break;
  2292. case 2:
  2293. //no break
  2294. case 3:
  2295. //no break
  2296. case 4:
  2297. //no break
  2298. case 5:
  2299. $className .= " fa-{$size}x";
  2300. break;
  2301. }
  2302. if (!empty($additionalClass)) {
  2303. $className .= " $additionalClass";
  2304. }
  2305. $icon = self::tag('em', null, ['class' => $className]);
  2306. return "$icon ";
  2307. }
  2308. /**
  2309. * @param string $title
  2310. * @param string $content
  2311. * @param null $id
  2312. * @param array $params
  2313. * @param null $idAccordion
  2314. * @param null $idCollapse
  2315. * @param bool|true $open
  2316. * @param bool|false $fullClickable
  2317. * @return null|string
  2318. * @todo rework function to easy use
  2319. */
  2320. public static function panelCollapse(
  2321. $title,
  2322. $content,
  2323. $id = null,
  2324. $params = array(),
  2325. $idAccordion = null,
  2326. $idCollapse = null,
  2327. $open = true,
  2328. $fullClickable = false
  2329. ) {
  2330. if (!empty($idAccordion)) {
  2331. $headerClass = '';
  2332. $headerClass .= $fullClickable ? 'center-block ' : '';
  2333. $headerClass .= $open ? '' : 'collapsed';
  2334. $contentClass = 'panel-collapse collapse ';
  2335. $contentClass .= $open ? 'in' : '';
  2336. $ariaExpanded = $open ? 'true' : 'false';
  2337. $html = <<<HTML
  2338. <div class="panel-group" id="$idAccordion" role="tablist" aria-multiselectable="true">
  2339. <div class="panel panel-default" id="$id">
  2340. <div class="panel-heading" role="tab">
  2341. <h4 class="panel-title">
  2342. <a class="$headerClass" role="button" data-toggle="collapse" data-parent="#$idAccordion" href="#$idCollapse" aria-expanded="$ariaExpanded" aria-controls="$idCollapse">$title</a>
  2343. </h4>
  2344. </div>
  2345. <div id="$idCollapse" class="$contentClass" role="tabpanel">
  2346. <div class="panel-body">$content</div>
  2347. </div>
  2348. </div>
  2349. </div>
  2350. HTML;
  2351. } else {
  2352. if (!empty($id)) {
  2353. $params['id'] = $id;
  2354. }
  2355. $params['class'] = 'panel panel-default';
  2356. $html = null;
  2357. if (!empty($title)) {
  2358. $html .= '<div class="panel-heading">'.$title.'</div>'.PHP_EOL;
  2359. }
  2360. $html .= '<div class="panel-body">'.$content.'</div>'.PHP_EOL;
  2361. $html = self::div($html, $params);
  2362. }
  2363. return $html;
  2364. }
  2365. /**
  2366. * Returns the string "1 day ago" with a link showing the exact date time.
  2367. * @param string $dateTime in UTC or a DateTime in UTC
  2368. *
  2369. * @return string
  2370. */
  2371. public static function dateToStringAgoAndLongDate($dateTime)
  2372. {
  2373. if (empty($dateTime) || $dateTime === '0000-00-00 00:00:00') {
  2374. return '';
  2375. }
  2376. if ($dateTime instanceof \DateTime) {
  2377. $dateTime = $dateTime->format('Y-m-d H:i:s');
  2378. }
  2379. return self::tip(
  2380. date_to_str_ago($dateTime),
  2381. api_convert_and_format_date($dateTime, DATE_TIME_FORMAT_LONG)
  2382. //api_get_local_time($dateTime)
  2383. );
  2384. }
  2385. /**
  2386. * @param array $userInfo
  2387. * @param string $status
  2388. * @param string $toolbar
  2389. *
  2390. * @return string
  2391. */
  2392. public static function getUserCard($userInfo, $status = '', $toolbar = '')
  2393. {
  2394. if (empty($userInfo)) {
  2395. return '';
  2396. }
  2397. if (!empty($status)) {
  2398. $status = '<div class="items-user-status">'.$status.'</div>';
  2399. }
  2400. if (!empty($toolbar)) {
  2401. $toolbar = '<div class="btn-group pull-right">'.$toolbar.'</div>';
  2402. }
  2403. return '<div id="user_card_'.$userInfo['id'].'" class="col-md-12">
  2404. <div class="row">
  2405. <div class="col-md-2">
  2406. <img src="'.$userInfo['avatar'].'" class="img-responsive img-circle">
  2407. </div>
  2408. <div class="col-md-10">
  2409. <p>'.$userInfo['complete_name'].'</p>
  2410. <div class="row">
  2411. <div class="col-md-2">
  2412. '.$status.'
  2413. </div>
  2414. <div class="col-md-10">
  2415. '.$toolbar.'
  2416. </div>
  2417. </div>
  2418. </div>
  2419. </div>
  2420. <hr />
  2421. </div>';
  2422. }
  2423. }