<?php

/**
 * User Activity
 * $_SESSION[{$_sesKey}] = array(
 *   {id_zasob: int}
 *   , {id_zasob: int, task: 'edit', id_item: int}
 *   , {id_zasob: int}
 *   ...
 * );
 */
class UserActivity {

	public static $_sesKey = 'UserActivityList';
	public static $_limit = 5;

	public static function showListInContainer() {
		$list = self::showSimpleList();
		if (empty($list)) {
			return;
		}
		?>
		<div class="container">
			<h3>Ostatnia aktywność:</h3>
			<?php echo $list; ?>
		</div>
		<?php
	}

	public static function showSimpleList() {
		$out = '';
		$listaOut = array();
		Lib::loadClass('ProcesHelper');
		$list = self::getRawList();
		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>';}
		$list = array_reverse($list);
		foreach ($list as $activity) {
			$zasobObj = ProcesHelper::getZasobTableInfo($activity->id_zasob);
			if (!$zasobObj) {
				continue;
			}
			$userAcl = User::getAcl();
			$userAcl->fetchGroups();
			if (!$userAcl->hasTableAcl($zasobObj->ID)) {
				continue;
			}
			$tblAcl = $userAcl->getTableAcl($zasobObj->ID);
			if (!$tblAcl) {
				continue;
			}

			$listAdd = '';
			if (!empty($activity->task) && !empty($activity->id_item)) {
				if ($activity->task == 'edit') {
					$listAdd .= " - edycja rekordu " . '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobObj->ID}#EDIT/{$activity->id_item}" . '">' . $activity->id_item . '</a>';
				}
			}
			$listaOut[] = '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$zasobObj->ID}" . '">' . $tblAcl->getShortLabel() . '</a>';
		}

		if (!empty($listaOut)) {
			$out = '<ul><li>' . implode('</li><li>', $listaOut) . '</li></ul>';
		}
		return $out;
	}

	public static function _createSes() {
		if (!isset($_SESSION[self::$_sesKey])) {
			$_SESSION[self::$_sesKey] = array();
		}
	}

	public static function add($id_zasob, $task = '', $id_item = null) {
		//unset($_SESSION[self::$_sesKey]);
		$activity = null;

		self::_createSes();

		$activity = new stdClass();
		$activity->id_zasob = $id_zasob;
		if ($task) {
			$activity->task = $task;
		}
		if ($id_item) {
			$activity->id_item = $id_item;
		}

		if (!$activity) {
			return;
		}

		$lastActivity = self::getRawLastActivity();
		if ($lastActivity) {
			if ($lastActivity->id_zasob == $activity->id_zasob) {
				if (isset($activity->task) && isset($lastActivity->task)) {
					if ($activity->task == $lastActivity->task
						&& $activity->id_item == $lastActivity->id_item
					) {
						return;
					}
				} else {
					return;
				}
			}
		}

		$_SESSION[self::$_sesKey][] = $activity;

		self::_gc();
	}

	public static function getRawList() {
		self::_createSes();

		return $_SESSION[self::$_sesKey];
	}

	public static function getRawLastActivity() {
		$list = self::getRawList();
		if (!empty($list)) {
			$lastActivity = end($list);
			return $lastActivity;
		}
		return null;
	}

	public static function getListTotal() {
		$list = self::getRawList();
		return count($list);
	}

	public static function _gc($recLimit = 3) {
		if ($recLimit < 0) {
			return;
		}
		$total = count($_SESSION[self::$_sesKey]);
		if ($total > self::$_limit) {
			array_shift($_SESSION[self::$_sesKey]);
			self::_gc(--$recLimit);
		}
	}

}
