dashboard.lib.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * DashboardManager can be used to manage dashboard
  5. * author Christian Fasanando <christian1827@gmail.com>
  6. * @package chamilo.dashboard
  7. */
  8. class DashboardManager
  9. {
  10. /**
  11. * Constructor
  12. */
  13. public function __construct()
  14. {
  15. }
  16. /**
  17. * This function allows easy activating and inactivating of dashboard plugins
  18. * @return void
  19. */
  20. public static function handle_dashboard_plugins()
  21. {
  22. $token = Security::get_existing_token();
  23. $tokenCondition = '&sec_token='.$token;
  24. /* We scan the plugin directory. Each folder is a potential plugin. */
  25. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  26. $possibleplugins = self::getPossibleDashboardPluginsPath();
  27. $table_cols = array('name', 'version', 'description');
  28. echo Display::page_subheader(get_lang('DashboardPlugins'));
  29. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).$tokenCondition.'">';
  30. echo '<table class="data_table">';
  31. echo '<tr>';
  32. echo '<th width="50px">'.get_lang('Enabled').'</th>';
  33. echo '<th width="250px">'.get_lang('Name').'</th>';
  34. echo '<th width="100px">'.get_lang('Version').'</th>';
  35. echo '<th>'.get_lang('Description').'</th>';
  36. echo '</tr>';
  37. $disabled_blocks_data = self::get_block_data_without_plugin();
  38. // We display all the possible enabled or disabled plugins
  39. foreach ($possibleplugins as $testplugin) {
  40. $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info";
  41. if (file_exists($plugin_info_file) && is_readable($plugin_info_file)) {
  42. $plugin_info = api_parse_info_file($plugin_info_file);
  43. // change index to lower case
  44. $plugin_info = array_change_key_case($plugin_info);
  45. echo '<tr>';
  46. self::display_dashboard_plugin_checkboxes($testplugin);
  47. for ($i = 0; $i < count($table_cols); $i++) {
  48. if (isset($plugin_info[strtolower($table_cols[$i])])) {
  49. echo '<td>';
  50. echo $plugin_info[$table_cols[$i]];
  51. echo '</td>';
  52. } else {
  53. echo '<td></td>';
  54. }
  55. }
  56. echo '</tr>';
  57. } else {
  58. if ($testplugin != 'css') {
  59. echo Display::tag(
  60. 'tr',
  61. Display::tag(
  62. 'td',
  63. get_lang('CheckFilePermissions').' '.Security::remove_XSS($plugin_info_file),
  64. array('colspan' => '3')
  65. )
  66. );
  67. }
  68. }
  69. }
  70. // display all disabled block data
  71. if (count($disabled_blocks_data) > 0) {
  72. foreach ($disabled_blocks_data as $disabled_block) {
  73. echo '<tr style="background-color:#eee">';
  74. echo '<td><center><input type="checkbox" name="disabled_block" value="true" checked disabled /></center>';
  75. for ($j = 0; $j < count($table_cols); $j++) {
  76. if (isset($disabled_block[strtolower($table_cols[$j])])) {
  77. if ($j == 2) {
  78. echo '<td>';
  79. echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font><br />';
  80. echo '<font color="red">'.get_lang('ThisPluginHasbeenDeletedFromDashboardPluginDirectory').'</font>';
  81. echo '</td>';
  82. } else {
  83. echo '<td>';
  84. echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font>';
  85. echo '</td>';
  86. }
  87. } else {
  88. echo '<td>&nbsp;</td>';
  89. }
  90. }
  91. echo '</tr>';
  92. }
  93. }
  94. echo '</table>';
  95. echo '<br />';
  96. echo '<button class="btn btn-default" type="submit" name="submit_dashboard_plugins" value="'.get_lang('EnableDashboardPlugins').'">'.
  97. get_lang('EnableDashboardPlugins').'</button></form>';
  98. }
  99. /**
  100. * display checkboxes for dashboard plugin list
  101. * @param string $plugin_path
  102. *
  103. * @return void
  104. */
  105. public static function display_dashboard_plugin_checkboxes($plugin_path) {
  106. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  107. $sql = "SELECT * FROM $tbl_block
  108. WHERE path = '".Database::escape_string($plugin_path)."' AND active = 1";
  109. $rs = Database::query($sql);
  110. $checked = '';
  111. if (Database::num_rows($rs) > 0) {
  112. $checked = "checked";
  113. }
  114. echo "<td align=\"center\">";
  115. echo '<input type="checkbox" name="'.$plugin_path.'" value="true" '.$checked.'/>';
  116. echo "</td>";
  117. }
  118. /**
  119. * This function allows easy activating and inactivating
  120. * of plugins and save them inside db
  121. * @param array $plugin_paths dashboard plugin paths
  122. * return int affected rows
  123. */
  124. public static function store_dashboard_plugins($plugin_paths)
  125. {
  126. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  127. $affected_rows = 0;
  128. // get all plugins path inside plugin directory
  129. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  130. $possibleplugins = self::getPossibleDashboardPluginsPath();
  131. if (count($possibleplugins) > 0) {
  132. $selected_plugins = array_intersect(array_keys($plugin_paths), $possibleplugins);
  133. $not_selected_plugins = array_diff($possibleplugins, array_keys($plugin_paths));
  134. // get blocks id from not selected path
  135. $not_selected_blocks_id = array();
  136. foreach ($not_selected_plugins as $plugin) {
  137. $block_data = self::get_enabled_dashboard_blocks($plugin);
  138. if (!empty($block_data[$plugin])) {
  139. $not_selected_blocks_id[] = $block_data[$plugin]['id'];
  140. }
  141. }
  142. /* clean not selected plugins for extra user data and block data */
  143. // clean from extra user data
  144. $field_variable = 'dashboard';
  145. $extra_user_data = UserManager::get_extra_user_data_by_field_variable($field_variable);
  146. if (!empty($extra_user_data) && count($extra_user_data) > 0) {
  147. foreach ($extra_user_data as $key => $user_data) {
  148. $user_id = $key;
  149. $user_block_data = self::get_user_block_data($user_id);
  150. $user_block_id = array_keys($user_block_data);
  151. // clean disabled block data
  152. foreach ($user_block_id as $block_id) {
  153. if (in_array($block_id, $not_selected_blocks_id)) {
  154. unset($user_block_data[$block_id]);
  155. }
  156. }
  157. // get columns and blocks id for updating extra user data
  158. $columns = array();
  159. $user_blocks_id = array();
  160. foreach ($user_block_data as $data) {
  161. $user_blocks_id[$data['block_id']] = true;
  162. $columns[$data['block_id']] = $data['column'];
  163. }
  164. // update extra user blocks data
  165. self::store_user_blocks($user_id, $user_blocks_id, $columns);
  166. }
  167. }
  168. // clean from block data
  169. if (!empty($not_selected_blocks_id)) {
  170. $sql_check = "SELECT id FROM $tbl_block
  171. WHERE id IN(".implode(',', $not_selected_blocks_id).")";
  172. $rs_check = Database::query($sql_check);
  173. if (Database::num_rows($rs_check) > 0) {
  174. $del = "DELETE FROM $tbl_block WHERE id IN(".implode(',', $not_selected_blocks_id).")";
  175. Database::query($del);
  176. }
  177. }
  178. // store selected plugins
  179. if (!empty($selected_plugins) && count($selected_plugins) > 0) {
  180. foreach ($selected_plugins as $testplugin) {
  181. $selected_path = Database::escape_string($testplugin);
  182. // check if the path already stored inside block table for updating or adding it
  183. $sql = "SELECT path FROM $tbl_block WHERE path = '$selected_path'";
  184. $rs = Database::query($sql);
  185. if (Database::num_rows($rs) > 0) {
  186. // update
  187. $upd = "UPDATE $tbl_block SET active = 1 WHERE path = '$selected_path'";
  188. $result = Database::query($upd);
  189. $affected_rows = Database::affected_rows($result);
  190. } else {
  191. // insert
  192. $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info";
  193. $plugin_info = array();
  194. if (file_exists($plugin_info_file)) {
  195. $plugin_info = api_parse_info_file($plugin_info_file);
  196. }
  197. // change keys to lower case
  198. $plugin_info = array_change_key_case($plugin_info);
  199. // setting variables
  200. $plugin_name = $testplugin;
  201. $plugin_description = '';
  202. $plugin_controller = '';
  203. $plugin_path = $testplugin;
  204. if (isset($plugin_info['name'])) {
  205. $plugin_name = Database::escape_string($plugin_info['name']);
  206. }
  207. if (isset($plugin_info['description'])) {
  208. $plugin_description = Database::escape_string($plugin_info['description']);
  209. }
  210. if (isset($plugin_info['controller'])) {
  211. $plugin_controller = Database::escape_string($plugin_info['controller']);
  212. }
  213. $ins = "INSERT INTO $tbl_block(name, description, path, controller)
  214. VALUES ('$plugin_name', '$plugin_description', '$plugin_path', '$plugin_controller')";
  215. $result = Database::query($ins);
  216. $affected_rows = Database::affected_rows($result);
  217. }
  218. }
  219. }
  220. }
  221. return $affected_rows;
  222. }
  223. /**
  224. * Get all plugins path inside dashboard directory
  225. * @return array name plugins directories
  226. */
  227. public static function getPossibleDashboardPluginsPath()
  228. {
  229. // get all plugins path inside plugin directory
  230. /* We scan the plugin directory. Each folder is a potential plugin. */
  231. $possiblePlugins = array();
  232. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  233. $handle = @opendir($dashboard_pluginpath);
  234. while (false !== ($file = readdir($handle))) {
  235. if ($file <> '.' && $file <> '..' && is_dir($dashboard_pluginpath.$file)) {
  236. $possiblePlugins[] = $file;
  237. }
  238. }
  239. @closedir($handle);
  240. return $possiblePlugins;
  241. }
  242. /**
  243. * Get all blocks data without plugin directory
  244. * @return array Block data
  245. */
  246. public static function get_block_data_without_plugin()
  247. {
  248. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  249. $possibleplugins = self::getPossibleDashboardPluginsPath();
  250. // We check if plugin exists inside directory for updating active field
  251. $sql = "SELECT * FROM $tbl_block";
  252. $rs = Database::query($sql);
  253. if (Database::num_rows($rs) > 0) {
  254. while ($row = Database::fetch_array($rs)) {
  255. if (!in_array($row['path'], $possibleplugins)) {
  256. $active = 0;
  257. } else {
  258. $active = 1;
  259. }
  260. // update active
  261. $upd = "UPDATE $tbl_block SET active = '$active'
  262. WHERE path = '".$row['path']."'";
  263. Database::query($upd);
  264. }
  265. }
  266. // get disabled block data
  267. $block_data = array();
  268. $sql = "SELECT * FROM $tbl_block WHERE active = 0";
  269. $rs_block = Database::query($sql);
  270. if (Database::num_rows($rs_block) > 0) {
  271. while ($row_block = Database::fetch_array($rs_block)) {
  272. $block_data[] = $row_block;
  273. }
  274. }
  275. return $block_data;
  276. }
  277. /**
  278. * get data about enabled dashboard block (stored insise block table)
  279. * @param string $path plugin path
  280. * @return array data
  281. */
  282. public static function get_enabled_dashboard_blocks($path = '')
  283. {
  284. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  285. $condition_path = '';
  286. if (!empty($path)) {
  287. $path = Database::escape_string($path);
  288. $condition_path = ' AND path = "'.$path.'" ';
  289. }
  290. $sql = "SELECT * FROM $tbl_block WHERE active = 1 $condition_path ";
  291. $rs = Database::query($sql);
  292. $block_data = array();
  293. if (Database::num_rows($rs) > 0) {
  294. while ($row = Database::fetch_array($rs)) {
  295. $block_data[$row['path']] = $row;
  296. }
  297. }
  298. return $block_data;
  299. }
  300. /**
  301. * display user dashboard list
  302. * @param int User id
  303. * @return void
  304. */
  305. public static function display_user_dashboard_list($user_id)
  306. {
  307. $enabled_dashboard_plugins = self::get_enabled_dashboard_blocks();
  308. $user_block_data = self::get_user_block_data($user_id);
  309. if (count($enabled_dashboard_plugins) > 0) {
  310. echo '<div style="margin-top:20px">';
  311. echo '<div><strong>'.get_lang('SelectBlockForDisplayingInsideBlocksDashboardView').'</strong></div><br />';
  312. echo '<form name="dashboard_list" method="post" action="index.php?action=store_user_block">';
  313. echo '<table class="data_table">';
  314. echo '<tr>';
  315. echo '<th width="5%">';
  316. echo get_lang('Enabled');
  317. echo '</th>';
  318. echo '<th width="30%">';
  319. echo get_lang('Name');
  320. echo '</th>';
  321. echo '<th width="40%">';
  322. echo get_lang('Description');
  323. echo '</th>';
  324. echo '<th>';
  325. echo get_lang('ColumnPosition');
  326. echo '</th>';
  327. echo '</tr>';
  328. // We display all enabled plugins and the checkboxes
  329. foreach ($enabled_dashboard_plugins as $block) {
  330. $path = $block['path'];
  331. $controller_class = $block['controller'];
  332. $filename_controller = $path.'.class.php';
  333. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  334. require_once $dashboard_plugin_path.$filename_controller;
  335. if (class_exists($controller_class)) {
  336. $obj_block = new $controller_class($user_id);
  337. // check if user is allowed to see the block
  338. if (method_exists($obj_block, 'is_block_visible_for_user')) {
  339. $is_block_visible_for_user = $obj_block->is_block_visible_for_user($user_id);
  340. if (!$is_block_visible_for_user) {
  341. continue;
  342. }
  343. }
  344. echo '<tr>';
  345. // checkboxes
  346. self::display_user_dashboard_list_checkboxes($user_id, $block['id']);
  347. echo '<td>'.$block['name'].'</td>';
  348. echo '<td>'.$block['description'].'</td>';
  349. echo '<td>
  350. <select class="selectpicker show-tick form-control" name="columns['.$block['id'].']">
  351. <option value="1" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column'] == 1 ? 'selected' : '').' >1</option>
  352. <option value="2" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column'] == 2 ? 'selected' : '').' >2</option>
  353. </select>
  354. </td>';
  355. echo '</tr>';
  356. } else {
  357. echo Display::tag('tr', Display::tag('td', get_lang('Error').' '.$controller_class, array('colspan'=>'3')));
  358. }
  359. }
  360. echo '</table>';
  361. echo '<div class="row"><div class="col-md-12">';
  362. echo '<button class="btn btn-default" type="submit" name="submit_dashboard_list" value="'.get_lang('EnableDashboardBlock').'"><em class="fa fa-check-square"></em> '.
  363. get_lang('EnableDashboardBlock').'</button></form>';
  364. echo '</div></div>';
  365. } else {
  366. echo '<div style="margin-top:20px">'.get_lang('ThereAreNoEnabledDashboardPlugins').'</div>';
  367. if (api_is_platform_admin()) {
  368. echo '<a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'admin/settings.php?category=Plugins">'.
  369. get_lang('ConfigureDashboardPlugin').'</a>';
  370. }
  371. }
  372. }
  373. /**
  374. * display checkboxes for user dashboard list
  375. * @param int User id
  376. * @param int Block id
  377. * @return void
  378. */
  379. public static function display_user_dashboard_list_checkboxes($user_id, $block_id)
  380. {
  381. $user_id = intval($user_id);
  382. $user_block_data = self::get_user_block_data($user_id);
  383. $enabled_blocks_id = array_keys($user_block_data);
  384. $checked = '';
  385. if (in_array($block_id, $enabled_blocks_id)) {
  386. $checked = "checked";
  387. }
  388. echo "<td align=\"center\">";
  389. echo '<input type="checkbox" name="enabled_blocks['.$block_id.']" value="true" '.$checked.'/>';
  390. echo "</td>";
  391. }
  392. /**
  393. * This function store enabled blocks id with its column position (block_id1:colum;block_id2:colum; ...)
  394. * inside extra user fields
  395. * @param int $user_id User id
  396. * @param array $enabled_blocks selected blocks
  397. * @param array $columns columns position
  398. * @return bool
  399. */
  400. public static function store_user_blocks($user_id, $enabled_blocks, $columns)
  401. {
  402. $selected_blocks_id = array();
  403. if (is_array($enabled_blocks) && count($enabled_blocks) > 0) {
  404. $selected_blocks_id = array_keys($enabled_blocks);
  405. }
  406. // build data for storing inside extra user field
  407. $fname = 'dashboard';
  408. $fvalue = array();
  409. foreach ($selected_blocks_id as $block_id) {
  410. $fvalue[] = $block_id.':'.$columns[$block_id];
  411. }
  412. $upd_extra_field = UserManager::update_extra_field_value(
  413. $user_id,
  414. $fname,
  415. $fvalue
  416. );
  417. return $upd_extra_field;
  418. }
  419. /**
  420. * This function get user block data (block id with its number of column) from extra user data
  421. * @param int User id
  422. * @return array data (block_id,column)
  423. */
  424. public static function get_user_block_data($user_id)
  425. {
  426. $user_id = intval($user_id);
  427. $field_variable = 'dashboard';
  428. $extra_user_data = UserManager::get_extra_user_data_by_field($user_id, $field_variable);
  429. $extra_user_data = explode(';', $extra_user_data[$field_variable]);
  430. $data = array();
  431. foreach ($extra_user_data as $extra) {
  432. $split_extra = explode(':', $extra);
  433. if (!empty($split_extra)) {
  434. $block_id = $split_extra[0];
  435. $column = isset($split_extra[1]) ? $split_extra[1] : null;
  436. $data[$block_id] = array('block_id' => $block_id, 'column' => $column);
  437. }
  438. }
  439. return $data;
  440. }
  441. /**
  442. * This function update extra user blocks data after closing a dashboard block
  443. * @param int User id
  444. * @param string plugin path
  445. * @param integer $user_id
  446. * @return bool
  447. */
  448. public static function close_user_block($user_id, $path)
  449. {
  450. $enabled_dashboard_blocks = self::get_enabled_dashboard_blocks($path);
  451. $user_block_data = self::get_user_block_data($user_id);
  452. foreach ($enabled_dashboard_blocks as $enabled_block) {
  453. unset($user_block_data[$enabled_block['id']]);
  454. }
  455. // get columns and blocks id for updating extra user data
  456. $columns = array();
  457. $user_blocks_id = array();
  458. foreach ($user_block_data as $data) {
  459. $user_blocks_id[$data['block_id']] = true;
  460. $columns[$data['block_id']] = $data['column'];
  461. }
  462. // update extra user blocks data
  463. $upd_extra_field = self::store_user_blocks($user_id, $user_blocks_id, $columns);
  464. return $upd_extra_field;
  465. }
  466. /**
  467. * get links for styles from dashboard plugins
  468. * @return string links
  469. */
  470. public static function get_links_for_styles_from_dashboard_plugins() {
  471. return '<link rel="stylesheet" href="'.api_get_path(WEB_PLUGIN_PATH).'dashboard/css/default.css" type="text/css" />'.PHP_EOL;
  472. }
  473. }