UserActivity.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * User Activity
  4. * $_SESSION[{$_sesKey}] = array(
  5. * {id_zasob: int}
  6. * , {id_zasob: int, task: 'edit', id_item: int}
  7. * , {id_zasob: int}
  8. * ...
  9. * );
  10. */
  11. class UserActivity {
  12. public static $_sesKey = 'UserActivityList';
  13. public static $_limit = 5;
  14. public static function showListInContainer() {
  15. $list = self::showSimpleList();
  16. if (empty($list)) {
  17. return;
  18. }
  19. ?>
  20. <div class="container">
  21. <h3>Ostatnia aktywność:</h3>
  22. <?php echo $list; ?>
  23. </div>
  24. <?php
  25. }
  26. public static function showSimpleList() {
  27. $out = '';
  28. $listaOut = array();
  29. Lib::loadClass('ProcesHelper');
  30. $list = self::getRawList();
  31. if(V::get('DBG_ACT', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($list);echo'</pre>';}
  32. $list = array_reverse($list);
  33. foreach ($list as $activity) {
  34. $zasobObj = ProcesHelper::getZasobTableInfo($activity->id_zasob);
  35. if (!$zasobObj) {
  36. continue;
  37. }
  38. $userAcl = User::getAcl();
  39. $userAcl->fetchGroups();
  40. if (!$userAcl->hasTableAcl($zasobObj->ID)) {
  41. continue;
  42. }
  43. $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
  44. if (!$tblAcl) {
  45. continue;
  46. }
  47. $listAdd = '';
  48. if (!empty($activity->task) && !empty($activity->id_item)) {
  49. if ($activity->task == 'edit') {
  50. $listAdd .= " - edycja rekordu " . '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobObj->ID}#EDIT/{$activity->id_item}" . '">' . $activity->id_item . '</a>';
  51. }
  52. }
  53. $listaOut[] = '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobObj->ID}" . '">' . $tblAcl->getShortLabel() . '</a>';
  54. }
  55. if (!empty($listaOut)) {
  56. $out = '<ul><li>' . implode('</li><li>', $listaOut) . '</li></ul>';
  57. }
  58. return $out;
  59. }
  60. public static function _createSes() {
  61. if (!isset($_SESSION[self::$_sesKey])) {
  62. $_SESSION[self::$_sesKey] = array();
  63. }
  64. }
  65. public static function add($id_zasob, $task = '', $id_item = null) {
  66. //unset($_SESSION[self::$_sesKey]);
  67. $activity = null;
  68. self::_createSes();
  69. $activity = new stdClass();
  70. $activity->id_zasob = $id_zasob;
  71. if ($task) {
  72. $activity->task = $task;
  73. }
  74. if ($id_item) {
  75. $activity->id_item = $id_item;
  76. }
  77. if (!$activity) {
  78. return;
  79. }
  80. $lastActivity = self::getRawLastActivity();
  81. if ($lastActivity) {
  82. if ($lastActivity->id_zasob == $activity->id_zasob) {
  83. if (isset($activity->task) && isset($lastActivity->task)) {
  84. if ($activity->task == $lastActivity->task
  85. && $activity->id_item == $lastActivity->id_item
  86. ) {
  87. return;
  88. }
  89. } else {
  90. return;
  91. }
  92. }
  93. }
  94. $_SESSION[self::$_sesKey][] = $activity;
  95. self::_gc();
  96. }
  97. public static function getRawList() {
  98. self::_createSes();
  99. return $_SESSION[self::$_sesKey];
  100. }
  101. public static function getRawLastActivity() {
  102. $list = self::getRawList();
  103. if (!empty($list)) {
  104. $lastActivity = end($list);
  105. return $lastActivity;
  106. }
  107. return null;
  108. }
  109. public static function getListTotal() {
  110. $list = self::getRawList();
  111. return count($list);
  112. }
  113. public static function _gc($recLimit = 3) {
  114. if ($recLimit < 0) {
  115. return;
  116. }
  117. $total = count($_SESSION[self::$_sesKey]);
  118. if ($total > self::$_limit) {
  119. array_shift($_SESSION[self::$_sesKey]);
  120. self::_gc(--$recLimit);
  121. }
  122. }
  123. }