display.lib.php 69 KB

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