display.lib.php 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  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. }