dashboard_controller.php 3.3 KB

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