dashboard_controller.php 6.9 KB

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