dashboard_controller.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Controller script. Prepares the common background
  5. * variables to give to the scripts corresponding to
  6. * the requested action.
  7. *
  8. * @author Christian Fasanando <christian1827@gmail.com>
  9. *
  10. * @todo move to main/inc/lib
  11. *
  12. * @package chamilo.dashboard
  13. */
  14. class DashboardController
  15. {
  16. private $user_id;
  17. /**
  18. * Constructor.
  19. */
  20. public function __construct()
  21. {
  22. $this->user_id = api_get_user_id();
  23. }
  24. /**
  25. * Display blocks from dashboard plugin paths
  26. * render to dashboard.php view.
  27. */
  28. public function display()
  29. {
  30. $tpl = new Template(get_lang('Dashboard'));
  31. $user_id = $this->user_id;
  32. $dashboard_blocks = DashboardManager::get_enabled_dashboard_blocks();
  33. $user_block_data = DashboardManager::get_user_block_data($user_id);
  34. $user_blocks_id = array_keys($user_block_data);
  35. $blocks = null;
  36. if (!empty($dashboard_blocks)) {
  37. foreach ($dashboard_blocks as $block) {
  38. // display only user blocks
  39. if (!in_array($block['id'], $user_blocks_id)) {
  40. continue;
  41. }
  42. $path = $block['path'];
  43. $controller_class = $block['controller'];
  44. $filename_controller = $path.'.class.php';
  45. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  46. require_once $dashboard_plugin_path.$filename_controller;
  47. if (class_exists($controller_class)) {
  48. $obj = new $controller_class($user_id);
  49. // check if user is allowed to see the block
  50. if (method_exists($obj, 'is_block_visible_for_user')) {
  51. $is_block_visible_for_user = $obj->is_block_visible_for_user($user_id);
  52. if (!$is_block_visible_for_user) {
  53. continue;
  54. }
  55. }
  56. $blocks[$path] = $obj->get_block();
  57. // set user block column
  58. $blocks[$path]['column'] = $user_block_data[$block['id']]['column'];
  59. }
  60. }
  61. }
  62. $view = isset($_GET['view']) ? $_GET['view'] : 'blocks';
  63. api_block_anonymous_users();
  64. $link_blocks_view = $link_list_view = null;
  65. if ($view == 'list') {
  66. $link_blocks_view = '<a href="'.api_get_self().'?view=blocks">'.
  67. Display::return_icon('blocks.png', get_lang('Dashboard blocks'), '', ICON_SIZE_MEDIUM).'</a>';
  68. } else {
  69. $link_list_view = '<a href="'.api_get_self().'?view=list">'.
  70. Display::return_icon('edit.png', get_lang('Edit blocks'), '', ICON_SIZE_MEDIUM).'</a>';
  71. }
  72. $configuration_link = null;
  73. if (api_is_platform_admin()) {
  74. $configuration_link = '<a href="'.api_get_path(WEB_CODE_PATH).'admin/settings.php?category=Plugins">'
  75. .Display::return_icon(
  76. 'settings.png',
  77. get_lang('Configure Dashboard Plugin'),
  78. '',
  79. ICON_SIZE_MEDIUM
  80. ).'</a>';
  81. }
  82. $actions = Display::toolbarAction('toolbar', [0 => $link_blocks_view.$link_list_view.$configuration_link]);
  83. $tpl->assign('actions', $actions);
  84. // block dashboard view
  85. $columns = [];
  86. $blockList = null;
  87. if (isset($view) && $view == 'blocks') {
  88. if (isset($blocks) && count($blocks) > 0) {
  89. // group content html by number of column
  90. if (is_array($blocks)) {
  91. $tmp_columns = [];
  92. foreach ($blocks as $block) {
  93. $tmp_columns[] = $block['column'];
  94. if (in_array($block['column'], $tmp_columns)) {
  95. $columns['column_'.$block['column']][] = $block['content_html'];
  96. }
  97. }
  98. }
  99. }
  100. } else {
  101. $user_id = api_get_user_id();
  102. $blockList = DashboardManager::display_user_dashboard_list($user_id);
  103. $tpl->assign('blocklist', $blockList);
  104. }
  105. $tpl->assign('columns', $columns);
  106. $template = $tpl->get_template('dashboard/index.tpl');
  107. $content = $tpl->fetch($template);
  108. $tpl->assign('content', $content);
  109. $tpl->display_one_col_template();
  110. }
  111. /**
  112. * This method allow store user blocks from dashboard manager
  113. * render to dashboard.php view.
  114. */
  115. public function store_user_block()
  116. {
  117. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  118. $enabled_blocks = $_POST['enabled_blocks'];
  119. $columns = $_POST['columns'];
  120. DashboardManager::store_user_blocks($this->user_id, $enabled_blocks, $columns);
  121. Display::addFlash(Display::return_message(get_lang('Saved')));
  122. }
  123. header('Location: '.api_get_path(WEB_CODE_PATH).'dashboard/index.php');
  124. exit;
  125. }
  126. /**
  127. * This method is used when you close a block from dashboard block interface
  128. * render to dashboard.php view.
  129. */
  130. public function close_user_block($path)
  131. {
  132. DashboardManager::close_user_block($this->user_id, $path);
  133. Display::addFlash(Display::return_message(get_lang('Saved')));
  134. header('Location: '.api_get_path(WEB_CODE_PATH).'dashboard/index.php');
  135. exit;
  136. }
  137. }