dashboard.lib.php 21 KB

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