display.lib.php 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use \ChamiloSession as Session;
  4. /**
  5. * Class Display
  6. * Contains several public functions dealing with the display of
  7. * table data, messages, help topics, ...
  8. *
  9. * Include/require it in your code to use its public functionality.
  10. * There are also several display public functions in the main api library.
  11. *
  12. * All public functions static public functions inside a class called Display,
  13. * so you use them like this: e.g.
  14. * Display::display_normal_message($message)
  15. *
  16. * @package chamilo.library
  17. *
  18. */
  19. class Display
  20. {
  21. /* The main template*/
  22. public static $global_template;
  23. public static $preview_style = null;
  24. public function __construct()
  25. {
  26. }
  27. /**
  28. * @return array
  29. */
  30. public static function toolList()
  31. {
  32. return array(
  33. 'group',
  34. 'work',
  35. 'glossary',
  36. 'forum',
  37. 'course_description',
  38. 'gradebook',
  39. 'attendance',
  40. 'course_progress',
  41. 'notebook'
  42. );
  43. }
  44. /**
  45. * Displays the page header
  46. * @param string The name of the page (will be showed in the page title)
  47. * @param string Optional help file name
  48. * @param string $page_header
  49. */
  50. public static function display_header($tool_name ='', $help = null, $page_header = null)
  51. {
  52. $origin = api_get_origin();
  53. $showHeader = true;
  54. if (isset($origin) && $origin == 'learnpath') {
  55. $showHeader = false;
  56. }
  57. self::$global_template = new Template($tool_name, $showHeader, $showHeader);
  58. // Fixing tools with any help it takes xxx part of main/xxx/index.php
  59. if (empty($help)) {
  60. $currentURL = api_get_self();
  61. preg_match('/main\/([^*\/]+)/', $currentURL, $matches);
  62. $toolList = self::toolList();
  63. if (!empty($matches)) {
  64. foreach ($matches as $match) {
  65. if (in_array($match, $toolList)) {
  66. $help = explode('_', $match);
  67. $help = array_map('ucfirst', $help);
  68. $help = implode('', $help);
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. self::$global_template->set_help($help);
  75. if (!empty(self::$preview_style)) {
  76. self::$global_template->preview_theme = self::$preview_style;
  77. self::$global_template->set_css_files();
  78. self::$global_template->set_js_files();
  79. }
  80. if (!empty($page_header)) {
  81. self::$global_template->assign('header', $page_header);
  82. }
  83. echo self::$global_template->show_header_template();
  84. }
  85. /**
  86. * Displays the reduced page header (without banner)
  87. */
  88. public static function display_reduced_header()
  89. {
  90. global $show_learnpath, $tool_name;
  91. self::$global_template = new Template($tool_name, false, false, $show_learnpath);
  92. echo self::$global_template ->show_header_template();
  93. }
  94. public static function display_no_header()
  95. {
  96. global $tool_name, $show_learnpath;
  97. $disable_js_and_css_files = true;
  98. self::$global_template = new Template($tool_name, false, false, $show_learnpath);
  99. //echo self::$global_template->show_header_template();
  100. }
  101. /**
  102. * Displays the reduced page header (without banner)
  103. */
  104. public static function set_header() {
  105. global $show_learnpath;
  106. self::$global_template = new Template($tool_name, false, false, $show_learnpath);
  107. }
  108. /**
  109. * Display the page footer
  110. */
  111. public static function display_footer() {
  112. echo self::$global_template ->show_footer_template();
  113. }
  114. public static function page()
  115. {
  116. return new Page();
  117. }
  118. /**
  119. * Displays the tool introduction of a tool.
  120. *
  121. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  122. * @param string $tool These are the constants that are used for indicating the tools.
  123. * @param array $editor_config Optional configuration settings for the online editor.
  124. * return: $tool return a string array list with the "define" in main_api.lib
  125. * @return html code for adding an introduction
  126. */
  127. public static function display_introduction_section($tool, $editor_config = null) {
  128. echo self::return_introduction_section($tool, $editor_config);
  129. }
  130. /**
  131. * @param string $tool
  132. * @param array $editor_config
  133. * @return null
  134. */
  135. public static function return_introduction_section($tool, $editor_config = null)
  136. {
  137. $is_allowed_to_edit = api_is_allowed_to_edit();
  138. $moduleId = $tool;
  139. if (api_get_setting('enable_tool_introduction') == 'true' || $tool == TOOL_COURSE_HOMEPAGE) {
  140. $introduction_section = null;
  141. require api_get_path(INCLUDE_PATH).'introductionSection.inc.php';
  142. return $introduction_section;
  143. }
  144. }
  145. /**
  146. * Displays a localised html file
  147. * tries to show the file "$full_file_name"."_".$language_interface.".html"
  148. * and if this does not exist, shows the file "$full_file_name".".html"
  149. * warning this public function defines a global
  150. * @param $full_file_name, the (path) name of the file, without .html
  151. * @return return a string with the path
  152. */
  153. public static function display_localised_html_file($full_file_name) {
  154. global $language_interface;
  155. $localised_file_name = $full_file_name.'_'.$language_interface.'.html';
  156. $default_file_name = $full_file_name.'.html';
  157. if (file_exists($localised_file_name)) {
  158. include $localised_file_name;
  159. } else {
  160. include ($default_file_name);
  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. $header_item[0],
  207. $header_item[1],
  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. public static function return_sortable_table(
  220. $header,
  221. $content,
  222. $sorting_options = array(),
  223. $paging_options = array(),
  224. $query_vars = null,
  225. $form_actions = array(),
  226. $style = 'table'
  227. ) {
  228. ob_start();
  229. self::display_sortable_table(
  230. $header,
  231. $content,
  232. $sorting_options,
  233. $paging_options,
  234. $query_vars,
  235. $form_actions,
  236. $style
  237. );
  238. $content = ob_get_contents();
  239. ob_end_clean();
  240. return $content;
  241. }
  242. /**
  243. * Shows a nice grid
  244. * @param string grid name (important to create css)
  245. * @param array header content
  246. * @param array array with the information to show
  247. * @param array $paging_options Keys are:
  248. * 'per_page_default' = items per page when switching from
  249. * full- list to per-page-view
  250. * 'per_page' = number of items to show per page
  251. * 'page_nr' = The page to display
  252. * 'hide_navigation' = true to hide the navigation
  253. * @param array $query_vars Additional variables to add in the query-string
  254. * @param array $form actions Additional variables to add in the query-string
  255. * @param mixed An array with bool values to know which columns show. i.e: $visibility_options= array(true, false) we will only show the first column
  256. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  257. */
  258. public static function display_sortable_grid(
  259. $name,
  260. $header,
  261. $content,
  262. $paging_options = array(),
  263. $query_vars = null,
  264. $form_actions = array(),
  265. $visibility_options = true,
  266. $sort_data = true,
  267. $grid_class = array()
  268. ) {
  269. echo self::return_sortable_grid(
  270. $name,
  271. $header,
  272. $content,
  273. $paging_options,
  274. $query_vars,
  275. $form_actions,
  276. $visibility_options,
  277. $sort_data,
  278. $grid_class
  279. );
  280. }
  281. /**
  282. * Gets a nice grid in html string
  283. * @param string grid name (important to create css)
  284. * @param array header content
  285. * @param array array with the information to show
  286. * @param array $paging_options Keys are:
  287. * 'per_page_default' = items per page when switching from
  288. * full- list to per-page-view
  289. * 'per_page' = number of items to show per page
  290. * 'page_nr' = The page to display
  291. * 'hide_navigation' = true to hide the navigation
  292. * @param array $query_vars Additional variables to add in the query-string
  293. * @param array $form actions Additional variables to add in the query-string
  294. * @param mixed An array with bool values to know which columns show. i.e:
  295. * $visibility_options= array(true, false) we will only show the first column
  296. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  297. * @param bool true for sorting data or false otherwise
  298. * @param array grid classes
  299. * @return string html grid
  300. */
  301. public static function return_sortable_grid(
  302. $name,
  303. $header,
  304. $content,
  305. $paging_options = array(),
  306. $query_vars = null,
  307. $form_actions = array(),
  308. $visibility_options = true,
  309. $sort_data = true,
  310. $grid_class = array(),
  311. $elementCount = 0
  312. ) {
  313. $column = 0;
  314. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  315. $table = new SortableTableFromArray($content, $column, $default_items_per_page, $name);
  316. $table->total_number_of_items = intval($elementCount);
  317. if (is_array($query_vars)) {
  318. $table->set_additional_parameters($query_vars);
  319. }
  320. return $table->display_simple_grid(
  321. $visibility_options,
  322. $paging_options['hide_navigation'],
  323. $default_items_per_page,
  324. $sort_data,
  325. $grid_class
  326. );
  327. }
  328. /**
  329. * Displays a table with a special configuration
  330. * @param array $header Titles for the table header
  331. * each item in this array can contain 3 values
  332. * - 1st element: the column title
  333. * - 2nd element: true or false (column sortable?)
  334. * - 3th element: additional attributes for
  335. * th-tag (eg for column-width)
  336. * - 4the element: additional attributes for the td-tags
  337. * @param array $content 2D-array with the tables content
  338. * @param array $sorting_options Keys are:
  339. * 'column' = The column to use as sort-key
  340. * 'direction' = SORT_ASC or SORT_DESC
  341. * @param array $paging_options Keys are:
  342. * 'per_page_default' = items per page when switching from
  343. * full- list to per-page-view
  344. * 'per_page' = number of items to show per page
  345. * 'page_nr' = The page to display
  346. * @param array $query_vars Additional variables to add in the query-string
  347. * @param array $column_show Array of binaries 1= show columns 0. hide a column
  348. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  349. * i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  350. * @param array $form_actions Set optional forms actions
  351. *
  352. * @author Julio Montoya
  353. */
  354. public static function display_sortable_config_table(
  355. $table_name,
  356. $header,
  357. $content,
  358. $sorting_options = array(),
  359. $paging_options = array(),
  360. $query_vars = null,
  361. $column_show = array(),
  362. $column_order = array(),
  363. $form_actions = array()
  364. ) {
  365. global $origin;
  366. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  367. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  368. $table = new SortableTableFromArrayConfig($content, $column, $default_items_per_page, $table_name, $column_show, $column_order);
  369. if (is_array($query_vars)) {
  370. $table->set_additional_parameters($query_vars);
  371. }
  372. // Show or hide the columns header
  373. if (is_array($column_show)) {
  374. for ($i = 0; $i < count($column_show); $i++) {
  375. if (!empty($column_show[$i])) {
  376. $val0 = isset($header[$i][0]) ? $header[$i][0] : null;
  377. $val1 = isset($header[$i][1]) ? $header[$i][1] : null;
  378. $val2 = isset($header[$i][2]) ? $header[$i][2] : null;
  379. $val3 = isset($header[$i][3]) ? $header[$i][3] : null;
  380. $table->set_header($i, $val0, $val1, $val2, $val3);
  381. }
  382. }
  383. }
  384. $table->set_form_actions($form_actions);
  385. $table->display();
  386. }
  387. /**
  388. * Displays a normal message. It is recommended to use this public function
  389. * to display any normal information messages.
  390. *
  391. * @param bool Filter (true) or not (false)
  392. * @return void
  393. */
  394. public static function display_normal_message($message, $filter = true, $returnValue = false)
  395. {
  396. $message = self::return_message($message, 'normal', $filter);
  397. if ($returnValue) {
  398. return $message;
  399. } else {
  400. echo $message;
  401. }
  402. }
  403. /**
  404. * Displays an warning message. Use this if you want to draw attention to something
  405. * This can also be used for instance with the hint in the exercises
  406. *
  407. */
  408. public static function display_warning_message($message, $filter = true, $returnValue = false)
  409. {
  410. $message = self::return_message($message, 'warning', $filter);
  411. if ($returnValue) {
  412. return $message;
  413. } else {
  414. echo $message;
  415. }
  416. }
  417. /**
  418. * Displays an confirmation message. Use this if something has been done successfully
  419. * @param bool Filter (true) or not (false)
  420. * @return void
  421. */
  422. public static function display_confirmation_message ($message, $filter = true, $returnValue = false)
  423. {
  424. $message = self::return_message($message, 'confirm', $filter);
  425. if ($returnValue) {
  426. return $message;
  427. } else {
  428. echo $message;
  429. }
  430. }
  431. /**
  432. * Displays an error message. It is recommended to use this public function if an error occurs
  433. * @param string $message - include any additional html
  434. * tags if you need them
  435. * @param bool Filter (true) or not (false)
  436. * @return void
  437. */
  438. public static function display_error_message ($message, $filter = true, $returnValue = false)
  439. {
  440. $message = self::return_message($message, 'error', $filter);
  441. if ($returnValue) {
  442. return $message;
  443. } else {
  444. echo $message;
  445. }
  446. }
  447. /**
  448. * @param string $message
  449. * @param string $type
  450. * @param bool $filter
  451. */
  452. public static function return_message_and_translate($message, $type='normal', $filter = true)
  453. {
  454. $message = get_lang($message);
  455. echo self::return_message($message, $type, $filter);
  456. }
  457. /**
  458. * Returns a div html string with
  459. * @param string The message
  460. * @param string The message type (confirm,normal,warning,error)
  461. * @param bool Whether to XSS-filter or not
  462. * @return string Message wrapped into an HTML div
  463. */
  464. public static function return_message($message, $type = 'normal', $filter = true)
  465. {
  466. if ($filter) {
  467. $message = api_htmlentities($message, ENT_QUOTES, api_is_xml_http_request() ? 'UTF-8' : api_get_system_encoding());
  468. //$message = Security::remove_XSS($message);
  469. }
  470. $class = "";
  471. switch($type) {
  472. case 'warning':
  473. $class .= 'warning-message';
  474. break;
  475. case 'error':
  476. $class .= 'error-message';
  477. break;
  478. case 'confirmation':
  479. case 'confirm':
  480. case 'success':
  481. $class .= 'confirmation-message';
  482. break;
  483. case 'normal':
  484. default:
  485. $class .= 'normal-message';
  486. }
  487. return self::div($message, array('class'=> $class));
  488. }
  489. /**
  490. * Returns an encrypted mailto hyperlink
  491. *
  492. * @param string e-mail
  493. * @param string clickable text
  494. * @param string optional, class from stylesheet
  495. * @return string encrypted mailto hyperlink
  496. */
  497. public static function encrypted_mailto_link ($email, $clickable_text = null, $style_class = '')
  498. {
  499. if (is_null($clickable_text)) {
  500. $clickable_text = $email;
  501. }
  502. // "mailto:" already present?
  503. if (substr($email, 0, 7) != 'mailto:') {
  504. $email = 'mailto:'.$email;
  505. }
  506. // Class (stylesheet) defined?
  507. if ($style_class != '') {
  508. $style_class = ' class="'.$style_class.'"';
  509. }
  510. // Encrypt email
  511. $hmail = '';
  512. for ($i = 0; $i < strlen($email); $i ++) {
  513. $hmail .= '&#'.ord($email {
  514. $i }).';';
  515. }
  516. $hclickable_text = null;
  517. // Encrypt clickable text if @ is present
  518. if (strpos($clickable_text, '@')) {
  519. for ($i = 0; $i < strlen($clickable_text); $i ++) {
  520. $hclickable_text .= '&#'.ord($clickable_text {
  521. $i }).';';
  522. }
  523. } else {
  524. $hclickable_text = @htmlspecialchars($clickable_text, ENT_QUOTES, api_get_system_encoding());
  525. }
  526. // Return encrypted mailto hyperlink
  527. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$hclickable_text.'</a>';
  528. }
  529. /**
  530. * Returns an mailto icon hyperlink
  531. *
  532. * @param string e-mail
  533. * @param string icon source file from the icon lib
  534. * @param integer icon size from icon lib
  535. * @param string optional, class from stylesheet
  536. * @return string encrypted mailto hyperlink
  537. */
  538. public static function icon_mailto_link($email, $icon_file = "mail.png", $icon_size = 22, $style_class = '')
  539. {
  540. // "mailto:" already present?
  541. if (substr($email, 0, 7) != 'mailto:') {
  542. $email = 'mailto:'.$email;
  543. }
  544. // Class (stylesheet) defined?
  545. if ($style_class != '') {
  546. $style_class = ' class="'.$style_class.'"';
  547. }
  548. // Encrypt email
  549. $hmail = '';
  550. for ($i = 0; $i < strlen($email); $i ++) {
  551. $hmail .= '&#'.ord($email {
  552. $i }).';';
  553. }
  554. // icon html code
  555. $icon_html_source = self::return_icon($icon_file, $hmail, '', $icon_size);
  556. // Return encrypted mailto hyperlink
  557. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$icon_html_source.'</a>';
  558. }
  559. /**
  560. * Creates a hyperlink to the platform homepage.
  561. * @param string $name, the visible name of the hyperlink, default is sitename
  562. * @return string with html code for hyperlink
  563. */
  564. public static function get_platform_home_link_html($name = '')
  565. {
  566. if ($name == '') {
  567. $name = api_get_setting('siteName');
  568. }
  569. return '<a href="'.api_get_path(WEB_PATH).'index.php">'.$name.'</a>';
  570. }
  571. /**
  572. * Prints an <option>-list with all letters (A-Z).
  573. * @param char $selected_letter The letter that should be selected
  574. * @todo This is English language specific implementation.
  575. * It should be adapted for the other languages.
  576. */
  577. public static function get_alphabet_options($selected_letter = '')
  578. {
  579. $result = '';
  580. for ($i = 65; $i <= 90; $i ++) {
  581. $letter = chr($i);
  582. $result .= '<option value="'.$letter.'"';
  583. if ($selected_letter == $letter) {
  584. $result .= ' selected="selected"';
  585. }
  586. $result .= '>'.$letter.'</option>';
  587. }
  588. return $result;
  589. }
  590. /**
  591. * Get the options withing a select box within the given values
  592. * @param int Min value
  593. * @param int Max value
  594. * @param int Default value
  595. * @return string HTML select options
  596. */
  597. public static function get_numeric_options($min, $max, $selected_num = 0)
  598. {
  599. $result = '';
  600. for ($i = $min; $i <= $max; $i ++) {
  601. $result .= '<option value="'.$i.'"';
  602. if (is_int($selected_num))
  603. if ($selected_num == $i) {
  604. $result .= ' selected="selected"';
  605. }
  606. $result .= '>'.$i.'</option>';
  607. }
  608. return $result;
  609. }
  610. /**
  611. * Shows the so-called "left" menu for navigating
  612. */
  613. public static function show_course_navigation_menu($isHidden = false)
  614. {
  615. global $output_string_menu;
  616. global $_setting;
  617. // Check if the $_SERVER['REQUEST_URI'] contains already url parameters (thus a questionmark)
  618. if (strpos($_SERVER['REQUEST_URI'], '?') === false) {
  619. $sourceurl = api_get_self().'?';
  620. } else {
  621. $sourceurl = $_SERVER['REQUEST_URI'];
  622. }
  623. $output_string_menu = '';
  624. if ($isHidden == 'true' and $_SESSION['hideMenu']) {
  625. $_SESSION['hideMenu'] = 'hidden';
  626. $sourceurl = str_replace('&isHidden=true', '', $sourceurl);
  627. $sourceurl = str_replace('&isHidden=false', '', $sourceurl);
  628. $output_string_menu .= ' <a href="'.$sourceurl.'&isHidden=false"><img src="../../main/img/expand.gif" alt="'.'Show menu1'.'" padding:"2px"/></a>';
  629. } elseif ($isHidden == 'false' && $_SESSION['hideMenu']) {
  630. $sourceurl = str_replace('&isHidden=true', '', $sourceurl);
  631. $sourceurl = str_replace('&isHidden=false', '', $sourceurl);
  632. $_SESSION['hideMenu'] = 'shown';
  633. $output_string_menu .= '<div id="leftimg"><a href="'.$sourceurl.'&isHidden=true"><img src="../../main/img/collapse.gif" alt="'.'Hide menu2'.'" padding:"2px"/></a></div>';
  634. } elseif ($_SESSION['hideMenu']) {
  635. if ($_SESSION['hideMenu'] == 'shown') {
  636. $output_string_menu .= '<div id="leftimg"><a href="'.$sourceurl.'&isHidden=true"><img src="../../main/img/collapse.gif" alt="'.'Hide menu3'.' padding:"2px"/></a></div>';
  637. }
  638. if ($_SESSION['hideMenu'] == 'hidden') {
  639. $sourceurl = str_replace('&isHidden=true', '', $sourceurl);
  640. $output_string_menu .= '<a href="'.$sourceurl.'&isHidden=false"><img src="../../main/img/expand.gif" alt="'.'Hide menu4'.' padding:"2px"/></a>';
  641. }
  642. } elseif (!$_SESSION['hideMenu']) {
  643. $_SESSION['hideMenu'] = 'shown';
  644. if (isset($_cid)) {
  645. $output_string_menu .= '<div id="leftimg"><a href="'.$sourceurl.'&isHidden=true"><img src="main/img/collapse.gif" alt="'.'Hide menu5'.' padding:"2px"/></a></div>';
  646. }
  647. }
  648. }
  649. /**
  650. * This public function displays an icon
  651. * @param string The filename of the file (in the main/img/ folder
  652. * @param string The alt text (probably a language variable)
  653. * @param array additional attributes (for instance height, width, onclick, ...)
  654. * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  655. * @return void
  656. */
  657. public static function display_icon(
  658. $image,
  659. $alt_text = '',
  660. $additional_attributes = array(),
  661. $size = null
  662. ) {
  663. echo self::return_icon($image, $alt_text, $additional_attributes, $size);
  664. }
  665. /**
  666. * This public function returns the htmlcode for an icon
  667. *
  668. * @param string The filename of the file (in the main/img/ folder
  669. * @param string The alt text (probably a language variable)
  670. * @param array Additional attributes (for instance height, width, onclick, ...)
  671. * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  672. * @return string An HTML string of the right <img> tag
  673. *
  674. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University 2006
  675. * @author Julio Montoya 2010 Function improved, adding image constants
  676. * @author Yannick Warnier 2011 Added size handler
  677. * @version Feb 2011
  678. */
  679. public static function return_icon(
  680. $image,
  681. $alt_text = '',
  682. $additional_attributes = array(),
  683. $size = ICON_SIZE_SMALL,
  684. $show_text = true,
  685. $return_only_path = false
  686. ) {
  687. $code_path = api_get_path(SYS_CODE_PATH);
  688. $w_code_path = api_get_path(WEB_CODE_PATH);
  689. $image = trim($image);
  690. $theme = 'css/'.api_get_visual_theme().'/icons/';
  691. $icon = '';
  692. $size_extra = '';
  693. if (isset($size)) {
  694. $size = intval($size);
  695. $size_extra = $size.'/';
  696. } else {
  697. $size = ICON_SIZE_SMALL;
  698. }
  699. //Checking the theme icons folder example: main/css/chamilo/icons/XXX
  700. if (is_file($code_path.$theme.$size_extra.$image)) {
  701. $icon = $w_code_path.$theme.$size_extra.$image;
  702. } elseif (is_file($code_path.'img/icons/'.$size_extra.$image)) {
  703. //Checking the main/img/icons/XXX/ folder
  704. $icon = $w_code_path.'img/icons/'.$size_extra.$image;
  705. } else {
  706. //Checking the img/ folder
  707. $icon = $w_code_path.'img/'.$image;
  708. }
  709. $icon = api_get_cdn_path($icon);
  710. if ($return_only_path) {
  711. return $icon;
  712. }
  713. $img = self::img($icon, $alt_text, $additional_attributes);
  714. if (SHOW_TEXT_NEAR_ICONS == true and !empty($alt_text)) {
  715. if ($show_text) {
  716. $img = "$img $alt_text";
  717. }
  718. }
  719. return $img;
  720. }
  721. /**
  722. * Returns the htmlcode for an image
  723. *
  724. * @param string $image the filename of the file (in the main/img/ folder
  725. * @param string $alt_text the alt text (probably a language variable)
  726. * @param array additional attributes (for instance height, width, onclick, ...)
  727. * @author Julio Montoya 2010
  728. */
  729. public static function img($image_path, $alt_text = '', $additional_attributes = array())
  730. {
  731. // Sanitizing the parameter $image_path
  732. $image_path = Security::filter_img_path($image_path);
  733. // alt text = the image name if there is none provided (for XHTML compliance)
  734. if ($alt_text == '') {
  735. $alt_text = basename($image_path);
  736. }
  737. $additional_attributes['src'] = $image_path;
  738. if (empty($additional_attributes['alt'])) {
  739. $additional_attributes['alt'] = $alt_text;
  740. }
  741. if (empty($additional_attributes['title'])) {
  742. $additional_attributes['title'] = $alt_text;
  743. }
  744. //return '<img src="'.$image_path.'" alt="'.$alt_text.'" title="'.$alt_text.'" '.$attribute_list.' />';
  745. return self::tag('img', '', $additional_attributes);
  746. }
  747. /**
  748. * Returns the htmlcode for a tag (h3, h1, div, a, button), etc
  749. *
  750. * @param string $tag the tag name
  751. * @param string $content the tag's content
  752. * @param array $additional_attributes (for instance height, width, onclick, ...)
  753. * @author Julio Montoya 2010
  754. */
  755. public static function tag($tag, $content, $additional_attributes = array())
  756. {
  757. $attribute_list = '';
  758. // Managing the additional attributes
  759. if (!empty($additional_attributes) && is_array($additional_attributes)) {
  760. $attribute_list = '';
  761. foreach ($additional_attributes as $key => & $value) {
  762. $attribute_list .= $key.'="'.$value.'" ';
  763. }
  764. }
  765. //some tags don't have this </XXX>
  766. if (in_array($tag, array('img','input','br'))) {
  767. $return_value = '<'.$tag.' '.$attribute_list.' />';
  768. } else {
  769. $return_value = '<'.$tag.' '.$attribute_list.' > '.$content.'</'.$tag.'>';
  770. }
  771. return $return_value;
  772. }
  773. /**
  774. * Creates a URL anchor
  775. */
  776. public static function url($name, $url, $extra_attributes = array())
  777. {
  778. if (!empty($url)) {
  779. $url = preg_replace('#&amp;#', '&', $url);
  780. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  781. $extra_attributes['href'] = $url;
  782. }
  783. return self::tag('a', $name, $extra_attributes);
  784. }
  785. /**
  786. * Creates a div tag
  787. *
  788. * @param $content
  789. * @param array $extra_attributes
  790. * @return string
  791. */
  792. public static function div($content, $extra_attributes = array())
  793. {
  794. return self::tag('div', $content, $extra_attributes);
  795. }
  796. /**
  797. * Creates a span tag
  798. */
  799. public static function span($content, $extra_attributes = array())
  800. {
  801. return self::tag('span', $content, $extra_attributes);
  802. }
  803. /**
  804. * Displays an HTML input tag
  805. *
  806. */
  807. public static function input($type, $name, $value, $extra_attributes = array())
  808. {
  809. if (isset($type)) {
  810. $extra_attributes['type']= $type;
  811. }
  812. if (isset($name)) {
  813. $extra_attributes['name']= $name;
  814. }
  815. if (isset($value)) {
  816. $extra_attributes['value']= $value;
  817. }
  818. return self::tag('input', '', $extra_attributes);
  819. }
  820. /**
  821. * @param $name
  822. * @param $value
  823. * @param array $extra_attributes
  824. * @return string
  825. */
  826. public static function button($name, $value, $extra_attributes = array())
  827. {
  828. if (!empty($name)) {
  829. $extra_attributes['name']= $name;
  830. }
  831. return self::tag('button', $value, $extra_attributes);
  832. }
  833. /**
  834. * Displays an HTML select tag
  835. *
  836. */
  837. public static function select(
  838. $name,
  839. $values,
  840. $default = -1,
  841. $extra_attributes = array(),
  842. $show_blank_item = true,
  843. $blank_item_text = null
  844. ) {
  845. $html = '';
  846. $extra = '';
  847. $default_id = 'id="'.$name.'" ';
  848. foreach($extra_attributes as $key=>$parameter) {
  849. if ($key == 'id') {
  850. $default_id = '';
  851. }
  852. $extra .= $key.'="'.$parameter.'"';
  853. }
  854. $html .= '<select name="'.$name.'" '.$default_id.' '.$extra.'>';
  855. if ($show_blank_item) {
  856. if (empty($blank_item_text)) {
  857. $blank_item_text = get_lang('Select');
  858. } else {
  859. $blank_item_text = Security::remove_XSS($blank_item_text);
  860. }
  861. $html .= self::tag('option', '-- '.$blank_item_text.' --', array('value'=>'-1'));
  862. }
  863. if ($values) {
  864. foreach ($values as $key => $value) {
  865. if(is_array($value) && isset($value['name'])) {
  866. $value = $value['name'];
  867. }
  868. $html .= '<option value="'.$key.'"';
  869. if (is_array($default)) {
  870. foreach($default as $item) {
  871. if ($item == $key) {
  872. $html .= 'selected="selected"';
  873. break;
  874. }
  875. }
  876. } else {
  877. if ($default == $key) {
  878. $html .= 'selected="selected"';
  879. }
  880. }
  881. $html .= '>'.$value.'</option>';
  882. }
  883. }
  884. $html .= '</select>';
  885. return $html;
  886. }
  887. /**
  888. * Creates a tab menu
  889. * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css
  890. * in the $htmlHeadXtra variable before the display_header
  891. * Add this script
  892. * @example
  893. * <script>
  894. $(function() {
  895. $( "#tabs" ).tabs();
  896. });
  897. </script>
  898. * @param array list of the tab titles
  899. * @param array content that will be showed
  900. * @param string the id of the container of the tab in the example "tabs"
  901. * @param array attributes for the ul
  902. *
  903. */
  904. public static function tabs($header_list, $content_list, $id = 'tabs', $attributes = array(), $ul_attributes = array())
  905. {
  906. if (empty($header_list) || count($header_list) == 0 ) {
  907. return '';
  908. }
  909. $lis = '';
  910. $i = 1;
  911. foreach ($header_list as $item) {
  912. $item =self::tag('a', $item, array('href'=>'#'.$id.'-'.$i));
  913. $lis .=self::tag('li', $item, $ul_attributes);
  914. $i++;
  915. }
  916. $ul = self::tag('ul',$lis);
  917. $i = 1;
  918. $divs = '';
  919. foreach ($content_list as $content) {
  920. $content = self::tag('p',$content);
  921. $divs .=self::tag('div', $content, array('id'=>$id.'-'.$i));
  922. $i++;
  923. }
  924. $attributes['id'] = $id;
  925. $main_div = self::tag('div',$ul.$divs, $attributes);
  926. return $main_div ;
  927. }
  928. public static function tabs_only_link($header_list, $selected = null)
  929. {
  930. $id = uniqid();
  931. $i = 1;
  932. $lis = null;
  933. foreach ($header_list as $item) {
  934. $class = null;
  935. if ($i == $selected) {
  936. $class = 'active';
  937. }
  938. $item =self::tag('a', $item['content'], array('id'=>$id.'-'.$i, 'href' => $item['url']));
  939. $lis .=self::tag('li', $item, array('class' => $class));
  940. $i++;
  941. }
  942. return self::tag('ul',$lis, array('class' => 'nav nav-tabs'));
  943. }
  944. /**
  945. * In order to display a grid using jqgrid you have to:
  946. * @example
  947. * After your Display::display_header function you have to add the nex javascript code: *
  948. * <script>
  949. * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,array()); // for more information of this function check the grid_js() function
  950. * </script>
  951. * //Then you have to call the grid_html
  952. * echo Display::grid_html('my_grid_name');
  953. * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work
  954. *
  955. * @param string the div id, this value must be the same with the first parameter of Display::grid_js()
  956. * @return string html
  957. *
  958. */
  959. public static function grid_html($div_id)
  960. {
  961. $table = self::tag('table','', array('id' => $div_id));
  962. $table .= self::tag('div','', array('id' => $div_id.'_pager'));
  963. return $table;
  964. }
  965. /**
  966. * @param string $label
  967. * @param string $form_item
  968. * @return string
  969. */
  970. public static function form_row($label, $form_item)
  971. {
  972. $label = self::span($label, array('class' =>'control-label'));
  973. $form_item = self::div($form_item, array('class' =>'controls'));
  974. return self::div($label.$form_item, array('class'=>'control-group'));
  975. }
  976. /**
  977. * This is a wrapper to use the jqgrid in Chamilo.
  978. * For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
  979. * This function need to be in the ready jquery function
  980. * example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> }
  981. * In order to work this function needs the Display::grid_html function with the same div id
  982. *
  983. * @param string $div_id div id
  984. * @param string $url url where the jqgrid will ask for data (if datatype = json)
  985. * @param array $column_names Visible columns (you should use get_lang). An array in which we place the names of the columns.
  986. * This is the text that appears in the head of the grid (Header layer).
  987. * Example: colname {name:'date', index:'date', width:120, align:'right'},
  988. * @param array $column_model the column model : Array which describes the parameters of the columns.This is the most important part of the grid.
  989. * For a full description of all valid values see colModel API. See the url above.
  990. * @param array $extra_params extra parameters
  991. * @param array $data data that will be loaded
  992. * @param string $formatter A string that will be appended to the JSON returned
  993. * @param bool $fixed_width not implemented yet
  994. * @return string the js code
  995. *
  996. */
  997. public static function grid_js(
  998. $div_id,
  999. $url,
  1000. $column_names,
  1001. $column_model,
  1002. $extra_params,
  1003. $data = array(),
  1004. $formatter = '',
  1005. $fixed_width = false
  1006. ) {
  1007. $obj = new stdClass();
  1008. $obj->first = 'first';
  1009. if (!empty($url)) {
  1010. $obj->url = $url;
  1011. }
  1012. //This line should only be used/modified in case of having characters
  1013. // encoding problems - see #6159
  1014. //$column_names = array_map("utf8_encode", $column_names);
  1015. $obj->colNames = $column_names;
  1016. $obj->colModel = $column_model;
  1017. $obj->pager = '#'.$div_id.'_pager';
  1018. $obj->datatype = 'json';
  1019. $obj->viewrecords = 'true';
  1020. $all_value = 10000000;
  1021. // Default row quantity
  1022. if (!isset($extra_params['rowList'])) {
  1023. $extra_params['rowList'] = array(20, 50, 100, 500, 1000, $all_value);
  1024. }
  1025. $json = '';
  1026. if (!empty($extra_params['datatype'])) {
  1027. $obj->datatype = $extra_params['datatype'];
  1028. }
  1029. // Row even odd style.
  1030. $obj->altRows = true;
  1031. if (!empty($extra_params['altRows'])) {
  1032. $obj->altRows = $extra_params['altRows'];
  1033. }
  1034. if (!empty($extra_params['sortname'])) {
  1035. $obj->sortname = $extra_params['sortname'];
  1036. }
  1037. if (!empty($extra_params['sortorder'])) {
  1038. $obj->sortorder = $extra_params['sortorder'];
  1039. }
  1040. if (!empty($extra_params['rowList'])) {
  1041. $obj->rowList = $extra_params['rowList'];
  1042. }
  1043. //Sets how many records we want to view in the grid
  1044. $obj->rowNum = 20;
  1045. if (!empty($extra_params['rowNum'])) {
  1046. $obj->rowNum = $extra_params['rowNum'];
  1047. }
  1048. if (!empty($extra_params['viewrecords'])) {
  1049. $obj->viewrecords = $extra_params['viewrecords'];
  1050. }
  1051. $beforeSelectRow = null;
  1052. if (isset($extra_params['beforeSelectRow'])) {
  1053. $beforeSelectRow = "beforeSelectRow: ".$extra_params['beforeSelectRow'].", ";
  1054. unset($extra_params['beforeSelectRow']);
  1055. }
  1056. // Adding extra params
  1057. if (!empty($extra_params)) {
  1058. foreach ($extra_params as $key => $element) {
  1059. // the groupHeaders key gets a special treatment
  1060. if ($key != 'groupHeaders') {
  1061. $obj->$key = $element;
  1062. }
  1063. }
  1064. }
  1065. // Adding static data.
  1066. if (!empty($data)) {
  1067. $data_var = $div_id.'_data';
  1068. $json.=' var '.$data_var.' = '.json_encode($data).';';
  1069. $obj->data = $data_var;
  1070. $obj->datatype = 'local';
  1071. $json.="\n";
  1072. }
  1073. $obj->end = 'end';
  1074. $json_encode = json_encode($obj);
  1075. if (!empty($data)) {
  1076. //Converts the "data":"js_variable" to "data":js_variable,
  1077. // otherwise it will not work
  1078. $json_encode = str_replace('"data":"'.$data_var.'"', '"data":'.$data_var.'', $json_encode);
  1079. }
  1080. // Fixing true/false js values that doesn't need the ""
  1081. $json_encode = str_replace(':"true"',':true',$json_encode);
  1082. // wrap_cell is not a valid jqgrid attributes is a hack to wrap a text
  1083. $json_encode = str_replace('"wrap_cell":true', 'cellattr : function(rowId, value, rowObject, colModel, arrData) { return \'class = "jqgrid_whitespace"\'; }', $json_encode);
  1084. $json_encode = str_replace(':"false"',':false',$json_encode);
  1085. $json_encode = str_replace('"formatter":"action_formatter"', 'formatter:action_formatter', $json_encode);
  1086. $json_encode = str_replace(array('{"first":"first",','"end":"end"}'), '', $json_encode);
  1087. // Creating the jqgrid element.
  1088. $json .= '$("#'.$div_id.'").jqGrid({';
  1089. //$json .= $beforeSelectRow;
  1090. $json .= $json_encode;
  1091. $json .= '});';
  1092. // Grouping headers option
  1093. if (isset($extra_params['groupHeaders'])) {
  1094. $groups = '';
  1095. foreach ($extra_params['groupHeaders'] as $group) {
  1096. //{ "startColumnName" : "courses", "numberOfColumns" : 1, "titleText" : "Order Info" },
  1097. $groups .= '{ "startColumnName" : "' . $group['startColumnName'] . '", "numberOfColumns" : ' . $group['numberOfColumns'] . ', "titleText" : "' . $group['titleText'] . '" },';
  1098. }
  1099. $json .= '$("#'.$div_id.'").jqGrid("setGroupHeaders", {
  1100. "useColSpanStyle" : false,
  1101. "groupHeaders" : [
  1102. ' . $groups . '
  1103. ]
  1104. });';
  1105. }
  1106. $all_text = addslashes(get_lang('All'));
  1107. $json .= '$("'.$obj->pager.' option[value='.$all_value.']").text("'.$all_text.'");';
  1108. $json.= "\n";
  1109. // Adding edit/delete icons.
  1110. $json.= $formatter;
  1111. return $json;
  1112. }
  1113. /**
  1114. * @param array $headers
  1115. * @param array $rows
  1116. * @param array $attributes
  1117. * @return string
  1118. */
  1119. public static function table($headers, $rows, $attributes = array())
  1120. {
  1121. if (empty($attributes)) {
  1122. $attributes['class'] = 'data_table';
  1123. }
  1124. $table = new HTML_Table($attributes);
  1125. $row = 0;
  1126. $column = 0;
  1127. //Course headers
  1128. if (!empty($headers)) {
  1129. foreach ($headers as $item) {
  1130. $table->setHeaderContents($row, $column, $item);
  1131. $column++;
  1132. }
  1133. $row = 1;
  1134. $column = 0;
  1135. }
  1136. if (!empty($rows)) {
  1137. foreach($rows as $content) {
  1138. $table->setCellContents($row, $column, $content);
  1139. $row++;
  1140. //$column++;
  1141. }
  1142. }
  1143. return $table->toHtml();
  1144. }
  1145. /**
  1146. * Returns the "what's new" icon notifications
  1147. *
  1148. * The general logic of this function is to track the last time the user
  1149. * entered the course and compare to what has changed inside this course
  1150. * since then, based on the item_property table inside this course. Note that,
  1151. * if the user never entered the course before, he will not see notification
  1152. * icons. This function takes session ID into account (if any) and only shows
  1153. * the corresponding notifications.
  1154. * @param array Course information array, containing at least elements 'db' and 'k'
  1155. * @return string The HTML link to be shown next to the course
  1156. */
  1157. public static function show_notification($course_info)
  1158. {
  1159. $t_track_e_access = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1160. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  1161. $tool_edit_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1162. $course_code = Database::escape_string($course_info['code']);
  1163. $user_id = api_get_user_id();
  1164. $course_id = $course_info['real_id'];
  1165. $course_info['id_session'] = intval($course_info['id_session']);
  1166. // Get the user's last access dates to all tools of this course
  1167. $sql = "SELECT *
  1168. FROM $t_track_e_access USE INDEX (access_cours_code, access_user_id)
  1169. WHERE
  1170. access_cours_code = '".$course_code."' AND
  1171. access_user_id = '$user_id' AND
  1172. access_session_id ='".$course_info['id_session']."'";
  1173. $resLastTrackInCourse = Database::query($sql);
  1174. $oldestTrackDate = $oldestTrackDateOrig = '3000-01-01 00:00:00';
  1175. while ($lastTrackInCourse = Database::fetch_array($resLastTrackInCourse)) {
  1176. $lastTrackInCourseDate[$lastTrackInCourse['access_tool']] = $lastTrackInCourse['access_date'];
  1177. if ($oldestTrackDate > $lastTrackInCourse['access_date']) {
  1178. $oldestTrackDate = $lastTrackInCourse['access_date'];
  1179. }
  1180. }
  1181. if ($oldestTrackDate == $oldestTrackDateOrig) {
  1182. //if there was no connexion to the course ever, then take the
  1183. // course creation date as a reference
  1184. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1185. $sql = "SELECT course.creation_date ".
  1186. "FROM $course_table course ".
  1187. "WHERE course.code = '".$course_code."'";
  1188. $res = Database::query($sql);
  1189. if ($res && Database::num_rows($res)>0) {
  1190. $row = Database::fetch_array($res);
  1191. }
  1192. $oldestTrackDate = $row['creation_date'];
  1193. }
  1194. // Get the last edits of all tools of this course.
  1195. $sql = "SELECT
  1196. tet.*,
  1197. tet.lastedit_date last_date,
  1198. tet.tool tool,
  1199. tet.ref ref,
  1200. tet.lastedit_type type,
  1201. tet.to_group_id group_id,
  1202. ctt.image image,
  1203. ctt.link link
  1204. FROM $tool_edit_table tet, $course_tool_table ctt
  1205. WHERE
  1206. tet.c_id = $course_id AND
  1207. ctt.c_id = $course_id AND
  1208. tet.lastedit_date > '$oldestTrackDate' ".
  1209. // Special hack for work tool, which is called student_publication in c_tool and work in c_item_property :-/ BT#7104
  1210. " AND (ctt.name = tet.tool OR (ctt.name = 'student_publication' AND tet.tool = 'work')) ".
  1211. " AND ctt.visibility = '1' ".
  1212. " AND tet.lastedit_user_id != $user_id AND tet.id_session = '".$course_info['id_session']."'
  1213. ORDER BY tet.lastedit_date";
  1214. $res = Database::query($sql);
  1215. // Get the group_id's with user membership.
  1216. $group_ids = GroupManager :: get_group_ids($course_info['real_id'], $user_id);
  1217. $group_ids[] = 0; //add group 'everyone'
  1218. $notifications = array();
  1219. // Filter all last edits of all tools of the course
  1220. while ($res && ($item_property = Database::fetch_array($res))) {
  1221. // First thing to check is if the user never entered the tool
  1222. // or if his last visit was earlier than the last modification.
  1223. if ((!isset($lastTrackInCourseDate[$item_property['tool']])
  1224. || $lastTrackInCourseDate[$item_property['tool']] < $item_property['lastedit_date'])
  1225. // Drop the tool elements that are part of a group that the
  1226. // user is not part of.
  1227. && ((in_array($item_property['to_group_id'], $group_ids)
  1228. // Drop the dropbox, notebook and chat tools (we don't care)
  1229. && ($item_property['tool'] != TOOL_DROPBOX
  1230. && $item_property['tool'] != TOOL_NOTEBOOK
  1231. && $item_property['tool'] != TOOL_CHAT)
  1232. )
  1233. )
  1234. // Take only what's visible or "invisible but where the user is a teacher" or where the visibility is unset.
  1235. && ($item_property['visibility'] == '1'
  1236. || ($course_info['status'] == '1' && $item_property['visibility'] == '0')
  1237. || !isset($item_property['visibility']))
  1238. ) {
  1239. if ($course_info['real_id'] == 1) {
  1240. // var_dump($item_property);
  1241. }
  1242. // Also drop announcements and events that are not for the user or his group.
  1243. if ((
  1244. $item_property['tool'] == TOOL_ANNOUNCEMENT ||
  1245. $item_property['tool'] == TOOL_CALENDAR_EVENT
  1246. ) &&
  1247. (
  1248. ($item_property['to_user_id'] != $user_id) &&
  1249. (!isset($item_property['to_group_id']) || !in_array($item_property['to_group_id'], $group_ids)))
  1250. ) {
  1251. continue;
  1252. }
  1253. // If it's a survey, make sure the user's invited. Otherwise drop it.
  1254. if ($item_property['tool'] == TOOL_SURVEY) {
  1255. $survey_info = survey_manager::get_survey($item_property['ref'], 0, $course_code);
  1256. if (!empty($survey_info)) {
  1257. $invited_users = SurveyUtil::get_invited_users(
  1258. $survey_info['code'],
  1259. $course_code
  1260. );
  1261. if (!in_array($user_id, $invited_users['course_users'])) {
  1262. continue;
  1263. }
  1264. }
  1265. }
  1266. // If it's a learning path, ensure it is currently visible to the user
  1267. if ($item_property['tool'] == TOOL_LEARNPATH) {
  1268. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpath.class.php';
  1269. if (!learnpath::is_lp_visible_for_student($item_property['ref'], $user_id, $course_code)) {
  1270. continue;
  1271. }
  1272. }
  1273. if ($item_property['tool'] == 'work' && $item_property['type'] == 'DirectoryCreated') {
  1274. $item_property['lastedit_type'] = 'WorkAdded';
  1275. }
  1276. $notifications[$item_property['tool']] = $item_property;
  1277. }
  1278. }
  1279. // Show all tool icons where there is something new.
  1280. $retvalue = '&nbsp;';
  1281. while (list($key, $notification) = each($notifications)) {
  1282. $lastDate = date('d/m/Y H:i', convert_sql_date($notification['lastedit_date']));
  1283. $type = $notification['lastedit_type'];
  1284. if (empty($course_info['id_session'])) {
  1285. $my_course['id_session'] = 0;
  1286. } else {
  1287. $my_course['id_session'] = $course_info['id_session'];
  1288. }
  1289. $label = get_lang('TitleNotification').": ".get_lang($type)." ($lastDate)";
  1290. $retvalue .= '<a href="'.api_get_path(WEB_CODE_PATH).$notification['link'].'?cidReq='.$course_code.'&amp;ref='.$notification['ref'].'&amp;gidReq='.$notification['to_group_id'].'&amp;id_session='.$my_course['id_session'].'">'.
  1291. Display::return_icon($notification['image'], $label).'</a>&nbsp;';
  1292. }
  1293. return $retvalue;
  1294. }
  1295. /**
  1296. * Displays a digest e.g. short summary of new agenda and announcements items.
  1297. * This used to be displayed in the right hand menu, but is now
  1298. * disabled by default (see config settings in this file) because most people like
  1299. * the what's new icons better.
  1300. *
  1301. * @version 1.0
  1302. */
  1303. public static function display_digest($toolsList, $digest, $orderKey, $courses)
  1304. {
  1305. $html = '';
  1306. if (is_array($digest) && (CONFVAL_showExtractInfo == SCRIPTVAL_UnderCourseList || CONFVAL_showExtractInfo == SCRIPTVAL_Both)) {
  1307. // // // LEVEL 1 // // //
  1308. reset($digest);
  1309. $html .= "<br /><br />\n";
  1310. while (list($key1) = each($digest)) {
  1311. if (is_array($digest[$key1])) {
  1312. // // // Title of LEVEL 1 // // //
  1313. $html .= "<strong>\n";
  1314. if ($orderKey[0] == 'keyTools') {
  1315. $tools = $key1;
  1316. $html .= $toolsList[$key1]['name'];
  1317. } elseif ($orderKey[0] == 'keyCourse') {
  1318. $courseSysCode = $key1;
  1319. $html .= "<a href=\"".api_get_path(WEB_COURSE_PATH). $courses[$key1]['coursePath']. "\">".$courses[$key1]['courseCode']. "</a>\n";
  1320. } elseif ($orderKey[0] == 'keyTime') {
  1321. $html .= api_convert_and_format_date($digest[$key1], DATE_FORMAT_LONG, date_default_timezone_get());
  1322. }
  1323. $html .= "</strong>\n";
  1324. // // // End Of Title of LEVEL 1 // // //
  1325. // // // LEVEL 2 // // //
  1326. reset($digest[$key1]);
  1327. while (list ($key2) = each($digest[$key1])) {
  1328. // // // Title of LEVEL 2 // // //
  1329. $html .= "<p>\n". "\n";
  1330. if ($orderKey[1] == 'keyTools') {
  1331. $tools = $key2;
  1332. $html .= $toolsList[$key2][name];
  1333. } elseif ($orderKey[1] == 'keyCourse') {
  1334. $courseSysCode = $key2;
  1335. $html .= "<a href=\"". api_get_path(WEB_COURSE_PATH). $courses[$key2]['coursePath']. "\">". $courses[$key2]['courseCode']. "</a>\n";
  1336. } elseif ($orderKey[1] == 'keyTime') {
  1337. $html .= api_convert_and_format_date($key2, DATE_FORMAT_LONG, date_default_timezone_get());
  1338. }
  1339. $html .= "\n";
  1340. $html .= "</p>";
  1341. // // // End Of Title of LEVEL 2 // // //
  1342. // // // LEVEL 3 // // //
  1343. reset($digest[$key1][$key2]);
  1344. while (list ($key3, $dataFromCourse) = each($digest[$key1][$key2])) {
  1345. // // // Title of LEVEL 3 // // //
  1346. if ($orderKey[2] == 'keyTools') {
  1347. $level3title = "<a href=\"".$toolsList[$key3]["path"].$courseSysCode."\">".$toolsList[$key3]['name']."</a>";
  1348. } elseif ($orderKey[2] == 'keyCourse') {
  1349. $level3title = "&#8226; <a href=\"".$toolsList[$tools]["path"].$key3."\">".$courses[$key3]['courseCode']."</a>\n";
  1350. } elseif ($orderKey[2] == 'keyTime') {
  1351. $level3title = "&#8226; <a href=\"".$toolsList[$tools]["path"].$courseSysCode."\">".api_convert_and_format_date($key3, DATE_FORMAT_LONG, date_default_timezone_get())."</a>";
  1352. }
  1353. // // // End Of Title of LEVEL 3 // // //
  1354. // // // LEVEL 4 (data) // // //
  1355. reset($digest[$key1][$key2][$key3]);
  1356. while (list ($key4, $dataFromCourse) = each($digest[$key1][$key2][$key3])) {
  1357. $html .= $level3title. ' &ndash; '. api_substr(strip_tags($dataFromCourse), 0, CONFVAL_NB_CHAR_FROM_CONTENT);
  1358. //adding ... (three dots) if the texts are too large and they are shortened
  1359. if (api_strlen($dataFromCourse) >= CONFVAL_NB_CHAR_FROM_CONTENT) {
  1360. $html .= '...';
  1361. }
  1362. }
  1363. $html .= "<br />\n";
  1364. }
  1365. }
  1366. }
  1367. }
  1368. return $html;
  1369. }
  1370. } // End function display_digest
  1371. /**
  1372. * Get the session box details as an array
  1373. * @param int Session ID
  1374. * @return array Empty array or session array ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int]
  1375. */
  1376. public static function get_session_title_box($session_id)
  1377. {
  1378. global $nosession;
  1379. if (!$nosession) {
  1380. global $now, $date_start, $date_end;
  1381. }
  1382. $output = array();
  1383. if (!$nosession) {
  1384. $main_user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1385. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1386. $active = false;
  1387. // Request for the name of the general coach
  1388. $sql ='SELECT tu.lastname, tu.firstname, ts.*
  1389. FROM '.$tbl_session.' ts LEFT JOIN '.$main_user_table .' tu ON ts.id_coach = tu.user_id
  1390. WHERE ts.id = '.intval($session_id);
  1391. $rs = Database::query($sql);
  1392. $session_info = Database::store_result($rs, 'ASSOC');
  1393. $session_info = $session_info[0];
  1394. $session = array();
  1395. $session['title'] = $session_info['name'];
  1396. $session['coach'] = '';
  1397. $session['dates'] = '';
  1398. if ($session_info['date_end'] == '0000-00-00' && $session_info['date_start'] == '0000-00-00') {
  1399. if (api_get_setting('show_session_coach') === 'true') {
  1400. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($session_info['firstname'], $session_info['lastname']);
  1401. }
  1402. if (isset($session_info['duration']) && !empty($session_info['duration'])) {
  1403. $userDurationData = SessionManager::getUserSession(
  1404. api_get_user_id(),
  1405. $session_id
  1406. );
  1407. $userDuration = 0;
  1408. if (isset($userDurationData['duration'])) {
  1409. $userDuration = intval($userDurationData['duration']);
  1410. }
  1411. $totalDuration = $session_info['duration'] + $userDuration;
  1412. $daysLeft = SessionManager::getDayLeftInSession(
  1413. $session_id,
  1414. api_get_user_id(),
  1415. $totalDuration
  1416. );
  1417. $session['duration'] = sprintf(get_lang('SessionDurationXDaysLeft'), $daysLeft);
  1418. }
  1419. $active = true;
  1420. } else {
  1421. $start = $stop = false;
  1422. $start_buffer = $stop_buffer = '';
  1423. if ($session_info['date_start'] == '0000-00-00') {
  1424. $session_info['date_start'] = '';
  1425. } else {
  1426. $start = true;
  1427. $start_buffer = $session_info['date_start'];
  1428. $session_info['date_start'] = get_lang('From').' '.$session_info['date_start'];
  1429. }
  1430. if ($session_info['date_end'] == '0000-00-00') {
  1431. $session_info['date_end'] = '';
  1432. } else {
  1433. $stop = true;
  1434. $stop_buffer = $session_info['date_end'];
  1435. $session_info['date_end'] = get_lang('Until').' '.$session_info['date_end'];
  1436. }
  1437. if ($start && $stop) {
  1438. $session['dates'] = Display::tag('i', sprintf(get_lang('FromDateXToDateY'), $start_buffer, $stop_buffer));
  1439. } else {
  1440. $session['dates'] = Display::tag('i', $session_info['date_start'].' '.$session_info['date_end']);
  1441. }
  1442. if ( api_get_setting('show_session_coach') === 'true' ) {
  1443. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($session_info['firstname'], $session_info['lastname']);
  1444. }
  1445. $active = ($date_start <= $now && $date_end >= $now);
  1446. }
  1447. $session['active'] = $active;
  1448. $session['session_category_id'] = $session_info['session_category_id'];
  1449. if (array_key_exists('show_description', $session_info)) {
  1450. if (!empty($session_info['show_description'])) {
  1451. $session['description'] = $session_info['description'];
  1452. }
  1453. }
  1454. $output = $session;
  1455. }
  1456. return $output;
  1457. }
  1458. /**
  1459. * Return the five star HTML
  1460. *
  1461. * @param string id of the rating ul element
  1462. * @param string url that will be added (for jquery see hot_courses.tpl)
  1463. * @param string point info array see function CourseManager::get_course_ranking()
  1464. * @param bool add a div wrapper
  1465. * @todo use templates
  1466. **/
  1467. public static function return_rating_system($id, $url, $point_info = array(), $add_div_wrapper = true)
  1468. {
  1469. $number_of_users_who_voted = isset($point_info['users_who_voted']) ? $point_info['users_who_voted'] : null;
  1470. $percentage = isset($point_info['point_average']) ? $point_info['point_average'] : 0;
  1471. if (!empty($percentage)) {
  1472. $percentage = $percentage*125/100;
  1473. }
  1474. $accesses = isset($point_info['accesses']) ? $point_info['accesses'] : 0;
  1475. $star_label = sprintf(get_lang('XStarsOutOf5'), $point_info['point_average_star']);
  1476. $html = '<ul id="'.$id.'" class="star-rating">
  1477. <li class="current-rating" style="width:'.$percentage.'px;"></li>
  1478. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=1" title="'.$star_label.'" class="one-star">1</a></li>
  1479. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=2" title="'.$star_label.'" class="two-stars">2</a></li>
  1480. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=3" title="'.$star_label.'" class="three-stars">3</a></li>
  1481. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=4" title="'.$star_label.'" class="four-stars">4</a></li>
  1482. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=5" title="'.$star_label.'" class="five-stars">5</a></li>
  1483. </ul>';
  1484. $labels = array();
  1485. $labels[]= $number_of_users_who_voted == 1 ? $number_of_users_who_voted.' '.get_lang('Vote') : $number_of_users_who_voted.' '.get_lang('Votes');
  1486. $labels[]= $accesses == 1 ? $accesses.' '.get_lang('Visit') : $accesses.' '.get_lang('Visits');
  1487. if (!empty($number_of_users_who_voted)) {
  1488. $labels[]= get_lang('Average').' '.$point_info['point_average_star'].'/5';
  1489. }
  1490. $labels[]= $point_info['user_vote'] ? get_lang('YourVote').' ['.$point_info['user_vote'].']' : get_lang('YourVote'). ' [?] ';
  1491. if (!$add_div_wrapper && api_is_anonymous()) {
  1492. $labels[]= Display::tag('span', get_lang('LoginToVote'), array('class' => 'error'));
  1493. }
  1494. $html .= Display::span(implode(' | ', $labels) , array('id' => 'vote_label_'.$id, 'class' => 'vote_label_info'));
  1495. $html .= ' '.Display::span(' ', array('id' => 'vote_label2_'.$id));
  1496. if ($add_div_wrapper) {
  1497. $html = Display::div($html, array('id' => 'rating_wrapper_'.$id));
  1498. }
  1499. return $html;
  1500. }
  1501. public static function return_default_table_class()
  1502. {
  1503. return 'data_table';
  1504. }
  1505. /**
  1506. * @param string $title
  1507. * @param string $second_title
  1508. * @param string $size
  1509. * @param bool $filter
  1510. * @return string
  1511. */
  1512. public static function page_header($title, $second_title = null, $size = 'h2', $filter = true)
  1513. {
  1514. if ($filter) {
  1515. $title = Security::remove_XSS($title);
  1516. }
  1517. if (!empty($second_title)) {
  1518. if ($filter) {
  1519. $second_title = Security::remove_XSS($second_title);
  1520. }
  1521. $title .= "<small> $second_title<small>";
  1522. }
  1523. return '<div class="page-header"><'.$size.'>'.$title.'</'.$size.'></div>';
  1524. }
  1525. public static function page_header_and_translate($title, $second_title = null)
  1526. {
  1527. $title = get_lang($title);
  1528. return self::page_header($title, $second_title);
  1529. }
  1530. public static function page_subheader_and_translate($title, $second_title = null)
  1531. {
  1532. $title = get_lang($title);
  1533. return self::page_subheader($title, $second_title);
  1534. }
  1535. public static function page_subheader($title, $second_title = null)
  1536. {
  1537. if (!empty($second_title)) {
  1538. $second_title = Security::remove_XSS($second_title);
  1539. $title .= "<small> $second_title<small>";
  1540. }
  1541. return '<h3>'.Security::remove_XSS($title).'</h3>';
  1542. }
  1543. public static function page_subheader2($title, $second_title = null)
  1544. {
  1545. return self::page_header($title, $second_title, 'h3');
  1546. }
  1547. public static function page_subheader3($title, $second_title = null)
  1548. {
  1549. return self::page_header($title, $second_title, 'h4');
  1550. }
  1551. public static function description($list)
  1552. {
  1553. $html = null;
  1554. if (!empty($list)) {
  1555. $html = '<dl class="dl-horizontal">';
  1556. foreach ($list as $item) {
  1557. $html .= '<dt>'.$item['title'].'</dd>';
  1558. $html .= '<dd>'.$item['content'].'</dt>';
  1559. }
  1560. $html .= '</dl>';
  1561. }
  1562. return $html;
  1563. }
  1564. public static function bar_progress($percentage, $show_percentage = true, $extra_info = null)
  1565. {
  1566. $percentage = intval($percentage);
  1567. $div = '<div class="progress progress-striped">
  1568. <div class="bar" style="width: '.$percentage.'%;"></div>
  1569. </div>';
  1570. if ($show_percentage) {
  1571. $div .= '<div class="progresstext">'.$percentage.'%</div>';
  1572. } else {
  1573. if (!empty($extra_info)) {
  1574. $div .= '<div class="progresstext">'.$extra_info.'</div>';
  1575. }
  1576. }
  1577. return $div;
  1578. }
  1579. public static function badge($count, $type ="warning") {
  1580. $class = '';
  1581. switch ($type) {
  1582. case 'success':
  1583. $class = 'badge-success';
  1584. break;
  1585. case 'warning':
  1586. $class = 'badge-warning';
  1587. break;
  1588. case 'important':
  1589. $class = 'badge-important';
  1590. break;
  1591. case 'info':
  1592. $class = 'badge-info';
  1593. break;
  1594. case 'inverse':
  1595. $class = 'badge-inverse';
  1596. break;
  1597. }
  1598. if (!empty($count)) {
  1599. return ' <span class="badge '.$class.'">'.$count.'</span>';
  1600. }
  1601. return null;
  1602. }
  1603. /**
  1604. * @param array $badge_list
  1605. * @return string
  1606. */
  1607. public static function badge_group($badge_list)
  1608. {
  1609. $html = '<div class="badge-group">';
  1610. foreach ($badge_list as $badge) {
  1611. $html .= $badge;
  1612. }
  1613. $html .= '</div>';
  1614. return $html;
  1615. }
  1616. /**
  1617. * @param string $content
  1618. * @param string $type
  1619. * @return string
  1620. */
  1621. public static function label($content, $type = null)
  1622. {
  1623. $class = '';
  1624. switch ($type) {
  1625. case 'success':
  1626. $class = 'label-success';
  1627. break;
  1628. case 'warning':
  1629. $class = 'label-warning';
  1630. break;
  1631. case 'important':
  1632. $class = 'label-important';
  1633. break;
  1634. case 'info':
  1635. $class = 'label-info';
  1636. break;
  1637. case 'inverse':
  1638. $class = 'label-inverse';
  1639. break;
  1640. }
  1641. $html = '';
  1642. if (!empty($content)) {
  1643. $html = '<span class="label '.$class.'">';
  1644. $html .= $content;
  1645. $html .='</span>';
  1646. }
  1647. return $html;
  1648. }
  1649. /**
  1650. * @param array $items
  1651. * @return null|string
  1652. */
  1653. public static function actions($items, $class = 'new_actions')
  1654. {
  1655. $html = null;
  1656. if (!empty($items)) {
  1657. $html = '<div class="'.$class.'"><ul class="nav nav-pills">';
  1658. foreach ($items as $value) {
  1659. $class = null;
  1660. if (isset($value['active']) && $value['active']) {
  1661. $class = 'class ="active"';
  1662. }
  1663. if (basename($_SERVER['REQUEST_URI']) == basename($value['url']) ) {
  1664. $class = 'class ="active"';
  1665. }
  1666. $html .= "<li $class >";
  1667. $html .= self::url($value['content'], $value['url']);
  1668. $html .= '</li>';
  1669. }
  1670. $html .= '</ul></div>';
  1671. }
  1672. return $html;
  1673. }
  1674. /**
  1675. * Prints a tooltip
  1676. */
  1677. public static function tip($text, $tip)
  1678. {
  1679. if (empty($tip)) {
  1680. return $text;
  1681. }
  1682. return self::span($text, array('class' => 'boot-tooltip', 'title' => strip_tags($tip)));
  1683. }
  1684. public static function generate_accordion($items, $type = 'jquery', $id = null)
  1685. {
  1686. $html = null;
  1687. if (!empty($items)) {
  1688. if (empty($id)) {
  1689. $id = api_get_unique_id();
  1690. }
  1691. if ($type == 'jquery') {
  1692. $html = '<div class="accordion_jquery" id="'.$id.'">'; //using jquery
  1693. } else {
  1694. $html = '<div class="accordion" id="'.$id.'">'; //using bootstrap
  1695. }
  1696. $count = 1;
  1697. foreach ($items as $item) {
  1698. $html .= '<div class="accordion-my-group">';
  1699. $html .= '<div class="accordion-heading">
  1700. <a class="accordion-toggle" data-toggle="collapse" data-parent="#'.$id.'" href="#collapse'.$count.'">
  1701. '.$item['title'].'
  1702. </a>
  1703. </div>';
  1704. $html .= '<div id="collapse'.$count.'" class="accordion-body">';
  1705. $html .= '<div class="accordion-my-inner">
  1706. '.$item['content'].'
  1707. </div>
  1708. </div>';
  1709. }
  1710. $html .= '</div>';
  1711. }
  1712. return $html;
  1713. }
  1714. /**
  1715. * @todo use twig
  1716. */
  1717. public static function group_button($title, $elements)
  1718. {
  1719. $html = '<div class="btn-toolbar">
  1720. <div class="btn-group">
  1721. <button class="btn dropdown-toggle" data-toggle="dropdown">'.$title.' <span class="caret"></span></button>
  1722. <ul class="dropdown-menu">';
  1723. foreach ($elements as $item) {
  1724. $html .= Display::tag('li', Display::url($item['title'], $item['href']));
  1725. }
  1726. $html .= '</ul>
  1727. </div> </div>';
  1728. return $html;
  1729. }
  1730. /**
  1731. * @param string $file
  1732. * @param array $params
  1733. * @return null|string
  1734. */
  1735. public static function getMediaPlayer($file, $params = array())
  1736. {
  1737. $fileInfo = pathinfo($file);
  1738. switch ($fileInfo['extension']) {
  1739. case 'wav':
  1740. if (isset($params['url'])) {
  1741. $url = DocumentManager::generateAudioTempFile(basename($file), $file);
  1742. return DocumentManager::readNanogongFile($url);
  1743. }
  1744. break;
  1745. case 'mp3':
  1746. case 'webm':
  1747. $autoplay = null;
  1748. if (isset($params['autoplay']) && $params['autoplay'] == 'true') {
  1749. $autoplay = 'autoplay';
  1750. }
  1751. $width = isset($params['width']) ? 'width="'.$params['width'].'"' : null;
  1752. $id = isset($params['id']) ? $params['id'] : $fileInfo['basename'];
  1753. $class = isset($params['class']) ? ' class="'.$params['class'].'"' : null;
  1754. $html = '<audio id="'.$id.'" '.$class.' controls '.$autoplay.' '.$width.' src="'.$params['url'].'" >';
  1755. $html .= '<object width="'.$width.'" height="50" type="application/x-shockwave-flash" data="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf">
  1756. <param name="movie" value="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf" />
  1757. <param name="flashvars" value="controls=true&file='.$params['url'].'" />
  1758. </object>';
  1759. $html .= '</audio>';
  1760. return $html;
  1761. break;
  1762. }
  1763. return null;
  1764. }
  1765. /**
  1766. *
  1767. * @param int $nextValue
  1768. * @param array $list
  1769. * @param int $current
  1770. * @param int $fixedValue
  1771. * @param array $conditions
  1772. * @param string $link
  1773. * @param bool $isMedia
  1774. * @param bool $addHeaders
  1775. * @return string
  1776. */
  1777. public static function progressPaginationBar(
  1778. $nextValue,
  1779. $list,
  1780. $current,
  1781. $fixedValue = null,
  1782. $conditions = array(),
  1783. $link = null,
  1784. $isMedia = false,
  1785. $addHeaders = true,
  1786. $linkAttributes = array()
  1787. ) {
  1788. if ($addHeaders) {
  1789. $pagination_size = 'pagination-mini';
  1790. $html = '<div class="exercise_pagination pagination '.$pagination_size.'"><ul>';
  1791. } else {
  1792. $html = null;
  1793. }
  1794. $affectAllItems = false;
  1795. if ($isMedia && isset($fixedValue) && ($nextValue + 1 == $current)) {
  1796. $affectAllItems = true;
  1797. }
  1798. $localCounter = 0;
  1799. foreach ($list as $itemId) {
  1800. $isCurrent = false;
  1801. if ($affectAllItems) {
  1802. $isCurrent = true;
  1803. } else {
  1804. if (!$isMedia) {
  1805. $isCurrent = $current == ($localCounter + $nextValue + 1) ? true : false;
  1806. }
  1807. }
  1808. $html .= self::parsePaginationItem(
  1809. $itemId,
  1810. $isCurrent,
  1811. $conditions,
  1812. $link,
  1813. $nextValue,
  1814. $isMedia,
  1815. $localCounter,
  1816. $fixedValue,
  1817. $linkAttributes
  1818. );
  1819. $localCounter++;
  1820. }
  1821. if ($addHeaders) {
  1822. $html .= '</ul></div>';
  1823. }
  1824. return $html;
  1825. }
  1826. /**
  1827. *
  1828. * @param int $itemId
  1829. * @param bool $isCurrent
  1830. * @param array $conditions
  1831. * @param string $link
  1832. * @param int $nextValue
  1833. * @param bool $isMedia
  1834. * @param int $localCounter
  1835. * @param int $fixedValue
  1836. * @return string
  1837. */
  1838. static function parsePaginationItem(
  1839. $itemId,
  1840. $isCurrent,
  1841. $conditions,
  1842. $link,
  1843. $nextValue = 0,
  1844. $isMedia = false,
  1845. $localCounter = null,
  1846. $fixedValue = null,
  1847. $linkAttributes = array())
  1848. {
  1849. $defaultClass = "before";
  1850. $class = $defaultClass;
  1851. foreach ($conditions as $condition) {
  1852. $array = isset($condition['items']) ? $condition['items'] : array();
  1853. $class_to_applied = $condition['class'];
  1854. $type = isset($condition['type']) ? $condition['type'] : 'positive';
  1855. $mode = isset($condition['mode']) ? $condition['mode'] : 'add';
  1856. switch ($type) {
  1857. case 'positive':
  1858. if (in_array($itemId, $array)) {
  1859. if ($mode == 'overwrite') {
  1860. $class = " $defaultClass $class_to_applied";
  1861. } else {
  1862. $class .= " $class_to_applied";
  1863. }
  1864. }
  1865. break;
  1866. case 'negative':
  1867. if (!in_array($itemId, $array)) {
  1868. if ($mode == 'overwrite') {
  1869. $class = " $defaultClass $class_to_applied";
  1870. } else {
  1871. $class .= " $class_to_applied";
  1872. }
  1873. }
  1874. break;
  1875. }
  1876. }
  1877. if ($isCurrent) {
  1878. $class = "before current";
  1879. }
  1880. if ($isMedia && $isCurrent) {
  1881. $class = "before current";
  1882. }
  1883. if (empty($link)) {
  1884. $link_to_show = "#";
  1885. } else {
  1886. $link_to_show = $link.($nextValue + $localCounter);
  1887. }
  1888. $label = $nextValue + $localCounter + 1;
  1889. if ($isMedia) {
  1890. $label = ($fixedValue + 1) .' '.chr(97 + $localCounter);
  1891. $link_to_show = $link.$fixedValue.'#questionanchor'.$itemId;
  1892. }
  1893. $link = Display::url($label.' ', $link_to_show, $linkAttributes);
  1894. return '<li class = "'.$class.'">'.$link.'</li>';
  1895. }
  1896. /**
  1897. * @param int $current
  1898. * @param int $total
  1899. * @return string
  1900. */
  1901. public static function paginationIndicator($current, $total)
  1902. {
  1903. $html = null;
  1904. if (!empty($current) && !empty($total)) {
  1905. $label = sprintf(get_lang('PaginationXofY'), $current, $total);
  1906. $html = self::url($label, '#', array('class' => 'btn disabled'));
  1907. }
  1908. return $html;
  1909. }
  1910. /**
  1911. * Adds a message in the queue
  1912. * @param string $message
  1913. */
  1914. public static function addFlash($message)
  1915. {
  1916. $messages = Session::read('flash_messages');
  1917. if (empty($messages)) {
  1918. $messages[] = $message;
  1919. } else {
  1920. array_push($messages, $message);
  1921. }
  1922. Session::write('flash_messages', $messages);
  1923. }
  1924. /**
  1925. * @return string
  1926. */
  1927. public static function getFlashToString()
  1928. {
  1929. $messages = Session::read('flash_messages');
  1930. $messageToString = '';
  1931. if (!empty($messages)) {
  1932. foreach ($messages as $message) {
  1933. $messageToString .= $message;
  1934. }
  1935. }
  1936. return $messageToString;
  1937. }
  1938. /**
  1939. * Shows the message from the session
  1940. */
  1941. public static function showFlash()
  1942. {
  1943. echo self::getFlashToString();
  1944. }
  1945. /**
  1946. * Destroys the message session
  1947. */
  1948. public static function cleanFlashMessages()
  1949. {
  1950. Session::erase('flash_messages');
  1951. }
  1952. /**
  1953. * @param string $content
  1954. * @param string $title
  1955. * @param string $footer
  1956. * @param string $style primary|success|info|warning|danger
  1957. * @param string $extra
  1958. *
  1959. * @return string
  1960. */
  1961. public static function panel($content, $title = '', $footer = '', $style = '', $extra = '')
  1962. {
  1963. $title = !empty($title) ? '<div class="panel-heading"><h3 class="panel-title">'.$title.'</h3>'.$extra.'</div>' : '';
  1964. $footer = !empty($footer) ? '<div class="panel-footer ">'.$footer.'</div>' : '';
  1965. $styles = ['primary','success','info','warning','danger'];
  1966. $style = !in_array($style, $styles) ? 'default' : $style;
  1967. return '
  1968. <div class="panel panel-'.$style.'">
  1969. '.$title.'
  1970. '.self::contentPanel($content).'
  1971. '.$footer.'
  1972. </div>'
  1973. ;
  1974. }
  1975. /**
  1976. * @param string $content
  1977. * @return string
  1978. */
  1979. public static function contentPanel($content)
  1980. {
  1981. return '<div class="panel-body">
  1982. '.$content.'
  1983. </div>';
  1984. }
  1985. /**
  1986. * Get the button HTML with an Awesome Font icon
  1987. * @param string $text The button content
  1988. * @param string $url The url to button
  1989. * @param string $icon The Awesome Font class for icon
  1990. * @param string $type Optional. The button Bootstrap class. Default 'default' class
  1991. * @param array $attributes The additional attributes
  1992. * @return string The button HTML
  1993. */
  1994. public static function toolbarButton(
  1995. $text,
  1996. $url,
  1997. $icon = 'check',
  1998. $type = 'default',
  1999. array $attributes = [],
  2000. $includeText = true
  2001. ) {
  2002. $buttonClass = "btn btn-$type";
  2003. $icon = self::tag('i', null, ['class' => "fa fa-$icon fa-fw", 'aria-hidden' => 'true']);
  2004. $attributes['class'] = isset($attributes['class']) ? "$buttonClass {$attributes['class']}" : $buttonClass;
  2005. if (!$includeText) {
  2006. $text = '<span class="sr-only">' . $text . '</span>';
  2007. }
  2008. return self::url("$icon $text", $url, $attributes);
  2009. }
  2010. /**
  2011. * @param int $id
  2012. * @param array $content
  2013. * @param int $col
  2014. * @param bool|true $right
  2015. * @return string
  2016. */
  2017. public static function toolbarAction($id, $content = array(), $col = 2, $right = true)
  2018. {
  2019. $columns = 12/$col;
  2020. $html = '';
  2021. $html .= '<div id="' . $id . '" class="actions">';
  2022. $html .= '<div class="row-fluid">';
  2023. if ($col > 4) {
  2024. $html = '<div class="alert alert-warning" role="alert">Action toolbar design does not work when exceeding four columns - check Display::toolbarAction()</div>';
  2025. } else {
  2026. for ( $i = 0; $i < $col; $i++ ) {
  2027. $html .= '<div class="span' . $columns . '">';
  2028. if ( $col == 2 && $i == 1 ) {
  2029. if ($right === true) {
  2030. $html .= '<div class="pull-right">';
  2031. $html .= (isset($content[$i]) ? $content[$i] : '');
  2032. $html .= '</div>';
  2033. } else {
  2034. $html .= $content[$i];
  2035. }
  2036. } else {
  2037. $html .= $content[$i];
  2038. }
  2039. $html .= '</div>';
  2040. }
  2041. }
  2042. $html .= '</div>';
  2043. $html .= '</div>';
  2044. return $html;
  2045. }
  2046. }