From 542f62dd5e70dc9557d1d840c0a292a2f9809e2c Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 12 Jul 2016 14:12:48 +0200 Subject: [PATCH] Added 'who was online' box --- com.woltlab.wcf/objectType.xml | 5 + com.woltlab.wcf/templates/boxWhoWasOnline.tpl | 13 ++ .../box/WhoWasOnlineBoxController.class.php | 113 ++++++++++++++++++ .../WhoWasOnlineCacheBuilder.class.php | 35 ++++++ 4 files changed, 166 insertions(+) create mode 100644 com.woltlab.wcf/templates/boxWhoWasOnline.tpl create mode 100644 wcfsetup/install/files/lib/system/box/WhoWasOnlineBoxController.class.php create mode 100644 wcfsetup/install/files/lib/system/cache/builder/WhoWasOnlineCacheBuilder.class.php diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index c27854df96..92f027c6b6 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -1052,6 +1052,11 @@ com.woltlab.wcf.boxController wcf\system\box\ArticleListBoxController + + com.woltlab.wcf.whoWasOnline + com.woltlab.wcf.boxController + wcf\system\box\WhoWasOnlineBoxController + diff --git a/com.woltlab.wcf/templates/boxWhoWasOnline.tpl b/com.woltlab.wcf/templates/boxWhoWasOnline.tpl new file mode 100644 index 0000000000..0f7e59aa7b --- /dev/null +++ b/com.woltlab.wcf/templates/boxWhoWasOnline.tpl @@ -0,0 +1,13 @@ +{if $whoWasOnlineList|count < 29} + +{else} + +{/if} diff --git a/wcfsetup/install/files/lib/system/box/WhoWasOnlineBoxController.class.php b/wcfsetup/install/files/lib/system/box/WhoWasOnlineBoxController.class.php new file mode 100644 index 0000000000..629b38513b --- /dev/null +++ b/wcfsetup/install/files/lib/system/box/WhoWasOnlineBoxController.class.php @@ -0,0 +1,113 @@ + + * @package WoltLabSuite\Core\System\Box + * @since 3.0 + */ +class WhoWasOnlineBoxController extends AbstractDatabaseObjectListBoxController { + /** + * @inheritDoc + */ + protected static $supportedPositions = ['footerBoxes', 'sidebarLeft', 'sidebarRight']; + + /** + * @inheritDoc + */ + protected $sortFieldLanguageItemPrefix = 'wcf.user'; + + /** + * @inheritDoc + */ + public $validSortFields = [ + 'username', + 'lastActivityTime' + ]; + + /** + * users loaded from cache + * @var UserProfile[] + */ + public $users = []; + + /** + * @inheritDoc + */ + protected function getObjectList() { + return null; + } + + /** + * @inheritDoc + */ + protected function getTemplate() { + return WCF::getTPL()->fetch('boxWhoWasOnline', 'wcf', [ + 'whoWasOnlineList' => $this->users, + 'boxPosition' => $this->box->position, + 'whoWasOnlineTimeFormat' => WCF::getLanguage()->get('wcf.date.timeFormat') + ]); + } + + /** + * @inheritDoc + */ + public function hasContent() { + if (!MODULE_USERS_ONLINE || !WCF::getSession()->getPermission('user.profile.canViewUsersOnlineList')) { + return false; + } + + parent::hasContent(); + + return count($this->users) > 0; + } + + /** + * @inheritDoc + */ + protected function loadContent() { + $this->readObjects(); + + $this->content = $this->getTemplate(); + } + + /** + * @inheritDoc + */ + protected function readObjects() { + EventHandler::getInstance()->fireAction($this, 'readObjects'); + + $userIDs = WhoWasOnlineCacheBuilder::getInstance()->getData(); + + if (!empty($userIDs)) { + if (WCF::getUser()->userID && !in_array(WCF::getUser()->userID, $userIDs)) { + // current user is missing in cache -> reset cache + WhoWasOnlineCacheBuilder::getInstance()->reset(); + } + + $this->users = UserProfileRuntimeCache::getInstance()->getObjects($userIDs); + foreach ($this->users as $key => $user) { + // remove invisible users + if (!UsersOnlineList::isVisible($user->userID, $user->canViewOnlineStatus)) { + unset($this->users[$key]); + } + } + + // sort users + if (!empty($this->users)) { + DatabaseObject::sort($this->users, $this->sortField, $this->sortOrder); + } + } + } +} diff --git a/wcfsetup/install/files/lib/system/cache/builder/WhoWasOnlineCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/WhoWasOnlineCacheBuilder.class.php new file mode 100644 index 0000000000..e37cb8f675 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cache/builder/WhoWasOnlineCacheBuilder.class.php @@ -0,0 +1,35 @@ + + * @package WoltLabSuite\Core\System\Cache\Builder + */ +class WhoWasOnlineCacheBuilder extends AbstractCacheBuilder { + /** + * @inheritDoc + */ + protected $maxLifetime = 600; + + /** + * @inheritDoc + */ + protected function rebuild(array $parameters) { + $userIDs = []; + $sql = "(SELECT userID FROM wcf".WCF_N."_user WHERE lastActivityTime > ?) + UNION + (SELECT userID FROM wcf".WCF_N."_session WHERE userID IS NOT NULL AND lastActivityTime > ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([TIME_NOW - 86400, TIME_NOW - USER_ONLINE_TIMEOUT]); + while ($userID = $statement->fetchColumn()) { + $userIDs[] = $userID; + } + + return $userIDs; + } +} -- 2.20.1