dashboard_controller.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 message (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)) continue;
  45. $path = $block['path'];
  46. $controller_class = $block['controller'];
  47. $filename_controller = $path.'.class.php';
  48. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  49. require_once $dashboard_plugin_path.$filename_controller;
  50. if (class_exists($controller_class)) {
  51. $obj = new $controller_class($user_id);
  52. // check if user is allowed to see the block
  53. if (method_exists($obj, 'is_block_visible_for_user')) {
  54. $is_block_visible_for_user = $obj->is_block_visible_for_user($user_id);
  55. if (!$is_block_visible_for_user) continue;
  56. }
  57. $data_block[$path] = $obj->get_block();
  58. // set user block column
  59. $data_block[$path]['column'] = $user_block_data[$block['id']]['column'];
  60. }
  61. }
  62. $data['blocks'] = $data_block;
  63. $data['dashboard_view'] = 'blocks';
  64. }
  65. if ($msg) {
  66. $data['msg'] = $msg;
  67. }
  68. // render to the view
  69. $this->view->set_data($data);
  70. $this->view->set_layout('layout');
  71. $this->view->set_template('dashboard');
  72. $this->view->render();
  73. }
  74. /**
  75. * This method allow store user blocks from dashboard manager
  76. * render to dashboard.php view
  77. */
  78. public function store_user_block()
  79. {
  80. $data = array();
  81. $user_id = $this->user_id;
  82. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  83. $enabled_blocks = $_POST['enabled_blocks'];
  84. $columns = $_POST['columns'];
  85. $affected_rows = DashboardManager::store_user_blocks($user_id, $enabled_blocks, $columns);
  86. if ($affected_rows) {
  87. $data['success'] = true;
  88. }
  89. }
  90. $data['dashboard_view'] = 'list';
  91. // render to the view
  92. $this->view->set_data($data);
  93. $this->view->set_layout('layout');
  94. $this->view->set_template('dashboard');
  95. $this->view->render();
  96. }
  97. /**
  98. * This method is used when you close a block from dashboard block interface
  99. * render to dashboard.php view
  100. */
  101. public function close_user_block($path)
  102. {
  103. $user_id = $this->user_id;
  104. $result = DashboardManager::close_user_block($user_id, $path);
  105. $this->display($result);
  106. }
  107. }