dashboard_controller.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * This file contains class used like controller,
  8. * it should be included inside a dispatcher file (e.g: index.php)
  9. * @author Christian Fasanando <christian1827@gmail.com>
  10. *
  11. * @package chamilo.dashboard
  12. */
  13. class DashboardController
  14. {
  15. private $toolname;
  16. private $view;
  17. private $user_id;
  18. /**
  19. * Constructor
  20. */
  21. public function __construct()
  22. {
  23. $this->user_id = api_get_user_id();
  24. $this->toolname = 'dashboard';
  25. $this->view = new View($this->toolname);
  26. }
  27. /**
  28. * Display blocks from dashboard plugin paths
  29. * @param string $msg (optional)
  30. * render to dashboard.php view
  31. */
  32. public function display($msg = false)
  33. {
  34. $data = array();
  35. $user_id = $this->user_id;
  36. $block_data_without_plugin = DashboardManager::get_block_data_without_plugin();
  37. $dashboard_blocks = DashboardManager::get_enabled_dashboard_blocks();
  38. $user_block_data = DashboardManager::get_user_block_data($user_id);
  39. $user_blocks_id = array_keys($user_block_data);
  40. $data_block = null;
  41. if (!empty($dashboard_blocks)) {
  42. foreach ($dashboard_blocks as $block) {
  43. // display only user blocks
  44. if (!in_array($block['id'], $user_blocks_id)) {
  45. continue;
  46. }
  47. $path = $block['path'];
  48. $controller_class = $block['controller'];
  49. $filename_controller = $path.'.class.php';
  50. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  51. require_once $dashboard_plugin_path.$filename_controller;
  52. if (class_exists($controller_class)) {
  53. $obj = new $controller_class($user_id);
  54. // check if user is allowed to see the block
  55. if (method_exists($obj, 'is_block_visible_for_user')) {
  56. $is_block_visible_for_user = $obj->is_block_visible_for_user($user_id);
  57. if (!$is_block_visible_for_user) {
  58. continue;
  59. }
  60. }
  61. $data_block[$path] = $obj->get_block();
  62. // set user block column
  63. $data_block[$path]['column'] = $user_block_data[$block['id']]['column'];
  64. }
  65. }
  66. $data['blocks'] = $data_block;
  67. $data['dashboard_view'] = 'blocks';
  68. }
  69. if ($msg) {
  70. $data['msg'] = $msg;
  71. }
  72. // render to the view
  73. $this->view->set_data($data);
  74. $this->view->set_layout('layout');
  75. $this->view->set_template('dashboard');
  76. $this->view->render();
  77. }
  78. /**
  79. * This method allow store user blocks from dashboard manager
  80. * render to dashboard.php view
  81. */
  82. public function store_user_block()
  83. {
  84. $data = array();
  85. $user_id = $this->user_id;
  86. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  87. $enabled_blocks = $_POST['enabled_blocks'];
  88. $columns = $_POST['columns'];
  89. $affected_rows = DashboardManager::store_user_blocks($user_id, $enabled_blocks, $columns);
  90. if ($affected_rows) {
  91. $data['success'] = true;
  92. }
  93. }
  94. $data['dashboard_view'] = 'list';
  95. // render to the view
  96. $this->view->set_data($data);
  97. $this->view->set_layout('layout');
  98. $this->view->set_template('dashboard');
  99. $this->view->render();
  100. }
  101. /**
  102. * This method is used when you close a block from dashboard block interface
  103. * render to dashboard.php view
  104. */
  105. public function close_user_block($path)
  106. {
  107. $user_id = $this->user_id;
  108. $result = DashboardManager::close_user_block($user_id, $path);
  109. $this->display($result);
  110. }
  111. }