display.lib.php 87 KB

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