dashboard_controller.php 3.4 KB

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