dashboard_controller.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * @author Christian Fasanando <christian1827@gmail.com>
  8. * @todo move to main/inc/lib
  9. * @package chamilo.dashboard
  10. */
  11. class DashboardController
  12. {
  13. private $user_id;
  14. /**
  15. * Constructor
  16. */
  17. public function __construct()
  18. {
  19. $this->user_id = api_get_user_id();
  20. }
  21. /**
  22. * Display blocks from dashboard plugin paths
  23. * render to dashboard.php view
  24. */
  25. public function display()
  26. {
  27. $user_id = $this->user_id;
  28. $dashboard_blocks = DashboardManager::get_enabled_dashboard_blocks();
  29. $user_block_data = DashboardManager::get_user_block_data($user_id);
  30. $user_blocks_id = array_keys($user_block_data);
  31. $blocks = null;
  32. if (!empty($dashboard_blocks)) {
  33. foreach ($dashboard_blocks as $block) {
  34. // display only user blocks
  35. if (!in_array($block['id'], $user_blocks_id)) {
  36. continue;
  37. }
  38. $path = $block['path'];
  39. $controller_class = $block['controller'];
  40. $filename_controller = $path.'.class.php';
  41. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  42. require_once $dashboard_plugin_path.$filename_controller;
  43. if (class_exists($controller_class)) {
  44. $obj = new $controller_class($user_id);
  45. // check if user is allowed to see the block
  46. if (method_exists($obj, 'is_block_visible_for_user')) {
  47. $is_block_visible_for_user = $obj->is_block_visible_for_user($user_id);
  48. if (!$is_block_visible_for_user) {
  49. continue;
  50. }
  51. }
  52. $blocks[$path] = $obj->get_block();
  53. // set user block column
  54. $blocks[$path]['column'] = $user_block_data[$block['id']]['column'];
  55. }
  56. }
  57. }
  58. $view = isset($_GET['view']) ? $_GET['view'] : 'blocks';
  59. api_block_anonymous_users();
  60. $link_blocks_view = $link_list_view = null;
  61. if ($view == 'list') {
  62. $link_blocks_view = '<a href="'.api_get_self().'?view=blocks">'.
  63. Display::return_icon('blocks.png', get_lang('DashboardBlocks'), '', ICON_SIZE_MEDIUM).'</a>';
  64. } else {
  65. $link_list_view = '<a href="'.api_get_self().'?view=list">'.
  66. Display::return_icon('edit.png', get_lang('EditBlocks'), '', ICON_SIZE_MEDIUM).'</a>';
  67. }
  68. $configuration_link = null;
  69. if (api_is_platform_admin()) {
  70. $configuration_link = '<a href="'.api_get_path(WEB_CODE_PATH).'admin/settings.php?category=Plugins">'
  71. .Display::return_icon('settings.png', get_lang('ConfigureDashboardPlugin'), '', ICON_SIZE_MEDIUM).'</a>';
  72. }
  73. $content = '<div class="actions">';
  74. $content .= $link_blocks_view.$link_list_view.$configuration_link;
  75. $content .= '</div>';
  76. // block dashboard view
  77. if (isset($view) && $view == 'blocks') {
  78. if (isset($blocks) && count($blocks) > 0) {
  79. $columns = array();
  80. // group content html by number of column
  81. if (is_array($blocks)) {
  82. $tmp_columns = array();
  83. foreach ($blocks as $block) {
  84. $tmp_columns[] = $block['column'];
  85. if (in_array($block['column'], $tmp_columns)) {
  86. $columns['column_'.$block['column']][] = $block['content_html'];
  87. }
  88. }
  89. }
  90. $content .= '<div id="columns" class="row">';
  91. if (count($columns) > 0) {
  92. $columns_name = array_keys($columns);
  93. // blocks for column 1
  94. if (in_array('column_1', $columns_name)) {
  95. $content .= '<div id="column1" class="col-md-6">';
  96. foreach ($columns['column_1'] as $data) {
  97. $content .= $data;
  98. }
  99. $content .= '</div>';
  100. } else {
  101. $content .= '<div id="column1" class="col-md-6">';
  102. $content .= '&nbsp;';
  103. $content .= '</div>';
  104. }
  105. // blocks for column 2
  106. if (in_array('column_2', $columns_name)) {
  107. // blocks for column 1
  108. $content .= '<div id="column2" class="col-md-6">';
  109. foreach ($columns['column_2'] as $data) {
  110. $content .= $data;
  111. }
  112. $content .= '</div>';
  113. } else {
  114. $content .= '<div id="column2" class="col-md-6">';
  115. $content .= '&nbsp;';
  116. $content .= '</div>';
  117. }
  118. }
  119. $content .= '</div>';
  120. } else {
  121. $content .= '<div style="margin-top:20px;">'.get_lang('YouHaveNotEnabledBlocks').'</div>';
  122. }
  123. } else {
  124. // block dashboard list
  125. if (isset($success)) {
  126. Display::addFlash(Display::return_message(get_lang('BlocksHaveBeenUpdatedSuccessfully'), 'confirm'));
  127. }
  128. $user_id = api_get_user_id();
  129. $content .= DashboardManager::display_user_dashboard_list($user_id);
  130. }
  131. $tpl = new Template(get_lang('Dashboard'));
  132. $tpl->assign('content', $content);
  133. $tpl->display_one_col_template();
  134. }
  135. /**
  136. * This method allow store user blocks from dashboard manager
  137. * render to dashboard.php view
  138. */
  139. public function store_user_block()
  140. {
  141. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  142. $enabled_blocks = $_POST['enabled_blocks'];
  143. $columns = $_POST['columns'];
  144. DashboardManager::store_user_blocks($this->user_id, $enabled_blocks, $columns);
  145. Display::addFlash(Display::return_message(get_lang('Saved')));
  146. }
  147. header('Location: '.api_get_path(WEB_CODE_PATH).'dashboard/index.php');
  148. exit;
  149. }
  150. /**
  151. * This method is used when you close a block from dashboard block interface
  152. * render to dashboard.php view
  153. */
  154. public function close_user_block($path)
  155. {
  156. DashboardManager::close_user_block($this->user_id, $path);
  157. Display::addFlash(Display::return_message(get_lang('Saved')));
  158. header('Location: '.api_get_path(WEB_CODE_PATH).'dashboard/index.php');
  159. exit;
  160. }
  161. }