Commit 8c53afcd authored by mabolek's avatar mabolek Committed by Thomas Löffler
Browse files

[TASK] It is possible to add karma to a user and cache it

parent 10efc348
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace T3o\Karma\Controller; namespace T3o\Karma\Controller;
use T3o\Karma\Domain\Model\KarmaSource;
use T3o\Karma\Domain\Repository\KarmaSourceRepository;
use T3o\Karma\Service\KarmaService; use T3o\Karma\Service\KarmaService;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository; use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
...@@ -40,6 +42,11 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll ...@@ -40,6 +42,11 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll
*/ */
protected $frontendUserRepository; protected $frontendUserRepository;
/**
* @var KarmaSourceRepository
*/
protected $karmaSourceRepository;
/** /**
* @param KarmaService $karmaService * @param KarmaService $karmaService
*/ */
...@@ -56,6 +63,12 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll ...@@ -56,6 +63,12 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll
$this->frontendUserRepository = $frontendUserRepository; $this->frontendUserRepository = $frontendUserRepository;
} }
/**
* @param KarmaSourceRepository $karmaSourceRepository
*/
public function injectKarmaSourceRepository(KarmaSourceRepository $karmaSourceRepository) {
$this->karmaSourceRepository = $karmaSourceRepository;
}
/** /**
* The test action * The test action
...@@ -64,7 +77,13 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll ...@@ -64,7 +77,13 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll
{ {
$frontendUser = $this->frontendUserRepository->findByUid(1); $frontendUser = $this->frontendUserRepository->findByUid(1);
$valueReturned = $this->karmaService->getImmutableKarmaForUser($frontendUser, true, true); $karmaSource = $this->karmaSourceRepository->findByUid(1);
$this->karmaService->addKarmaToUser(12, $frontendUser, $karmaSource);
$valueReturned = $this->karmaService->getImmutableKarmaForUser($frontendUser);
var_dump($valueReturned); var_dump($valueReturned);
} }
......
...@@ -32,14 +32,14 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser ...@@ -32,14 +32,14 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
* *
* @var int * @var int
*/ */
protected $karmaImmutableValue = 0; protected $karmaImmutableValueCache = 0;
/** /**
* Cached mutable karma total * Cached mutable karma total
* *
* @var int * @var int
*/ */
protected $karmaMutableValue = 0; protected $karmaMutableValueCache = 0;
/** /**
* Karma total cache generation timestamp * Karma total cache generation timestamp
...@@ -53,19 +53,19 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser ...@@ -53,19 +53,19 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
* *
* @return int * @return int
*/ */
public function getKarmaImmutableValue(): int public function getKarmaImmutableValueCache(): int
{ {
return $this->karmaImmutableValue; return $this->karmaImmutableValueCache;
} }
/** /**
* Set cached immutable karma total * Set cached immutable karma total
* *
* @param int $karmaImmutableValue * @param int $karmaImmutableValueCache
*/ */
public function setKarmaImmutableValue(int $karmaImmutableValue) public function setKarmaImmutableValueCache(int $karmaImmutableValueCache)
{ {
$this->karmaImmutableValue = $karmaImmutableValue; $this->karmaImmutableValueCache = $karmaImmutableValueCache;
} }
/** /**
...@@ -73,9 +73,9 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser ...@@ -73,9 +73,9 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
* *
* @return int * @return int
*/ */
public function getKarmaMutableValue(): int public function getKarmaMutableValueCache(): int
{ {
return $this->karmaMutableValue; return $this->karmaMutableValueCache;
} }
/** /**
...@@ -83,9 +83,9 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser ...@@ -83,9 +83,9 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
* *
* @param int $karmaMutableValue * @param int $karmaMutableValue
*/ */
public function setKarmaMutableValue(int $karmaMutableValue) public function setKarmaMutableValueCache(int $karmaMutableValue)
{ {
$this->karmaMutableValue = $karmaMutableValue; $this->karmaMutableValueCache = $karmaMutableValue;
} }
/** /**
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace T3o\Karma\Domain\Model; namespace T3o\Karma\Domain\Model;
use In2code\Userprofile\Domain\Model\FrontendUser; use T3o\Karma\Domain\Model\FrontendUser;
/*************************************************************** /***************************************************************
* Copyright notice * Copyright notice
......
...@@ -2,9 +2,13 @@ ...@@ -2,9 +2,13 @@
namespace T3o\Karma\Service; namespace T3o\Karma\Service;
use T3o\Karma\Domain\Model\Campaign;
use T3o\Karma\Domain\Model\FrontendUser; use T3o\Karma\Domain\Model\FrontendUser;
use T3o\Karma\Domain\Model\KarmaSource;
use T3o\Karma\Domain\Model\LedgerEntry;
use T3o\Karma\Domain\Repository\FrontendUserRepository; use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository; use T3o\Karma\Domain\Repository\LedgerEntryRepository;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
/*************************************************************** /***************************************************************
* Copyright notice * Copyright notice
...@@ -60,17 +64,17 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface ...@@ -60,17 +64,17 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
/** /**
* Get the imimmutable karma total from the user * Get the imimmutable karma total from the user
* *
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser * @param ExtbaseFrontendUser $frontendUser
* @param bool $ignoreCache Fetch value directly from ledger * @param bool $ignoreCache Fetch value directly from ledger
* @param bool $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update immutable cache. * @param bool $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update immutable cache.
* @return int Immutable Karma Total * @return int Immutable Karma Total
*/ */
public function getImmutableKarmaForUser(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true) public function getImmutableKarmaForUser(ExtbaseFrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true)
{ {
$frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser); $frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser);
if (!$ignoreCache) { if (!$ignoreCache) {
return $frontendUser->getKarmaImmutableValue(); return $frontendUser->getKarmaImmutableValueCache();
} }
$immutableKarma = $this->ledgerEntryRepository->calculateImmutableKarmaTotalForUser($frontendUser); $immutableKarma = $this->ledgerEntryRepository->calculateImmutableKarmaTotalForUser($frontendUser);
...@@ -78,8 +82,8 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface ...@@ -78,8 +82,8 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
if ($updateCacheEntry) { if ($updateCacheEntry) {
$immutableKarma = $this->getMutableKarmaForUser($frontendUser, true, false); $immutableKarma = $this->getMutableKarmaForUser($frontendUser, true, false);
$frontendUser->setKarmaImmutableValue($immutableKarma); $frontendUser->setKarmaImmutableValueCache($immutableKarma);
$frontendUser->setKarmaImmutableValue($immutableKarma); $frontendUser->setKarmaImmutableValueCache($immutableKarma);
$frontendUser->setKarmaCacheTimestamp(time()); $frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser); $this->frontendUserRepository->update($frontendUser);
...@@ -91,12 +95,12 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface ...@@ -91,12 +95,12 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
/** /**
* Get the mutable karma total from the user * Get the mutable karma total from the user
* *
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser * @param ExtbaseFrontendUser $frontendUser
* @param bool $ignoreCache Fetch value directly from ledger * @param bool $ignoreCache Fetch value directly from ledger
* @param bool $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update mutable cache. * @param bool $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update mutable cache.
* @return int Mutable Karma Total * @return int Mutable Karma Total
*/ */
public function getMutableKarmaForUser(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true) public function getMutableKarmaForUser(ExtbaseFrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true)
{ {
$frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser); $frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser);
...@@ -109,8 +113,8 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface ...@@ -109,8 +113,8 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
if ($updateCacheEntry) { if ($updateCacheEntry) {
$mutableKarma = $this->getImmutableKarmaForUser($frontendUser, true, false); $mutableKarma = $this->getImmutableKarmaForUser($frontendUser, true, false);
$frontendUser->setKarmaMutableValue($mutableKarma); $frontendUser->setKarmaMutableValueCache($mutableKarma);
$frontendUser->setKarmaMutableValue($mutableKarma); $frontendUser->setKarmaMutableValueCache($mutableKarma);
$frontendUser->setKarmaCacheTimestamp(time()); $frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser); $this->frontendUserRepository->update($frontendUser);
...@@ -119,14 +123,44 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface ...@@ -119,14 +123,44 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
return $mutableKarma; return $mutableKarma;
} }
/**
* Add karma to a user
*
* @param int $karmaValue to add to the user
* @param ExtbaseFrontendUser $frontendUser to add the value to
* @param KarmaSource $karmaSource for the karma value
* @param Campaign $campaign for the value (optional)
*/
public function addKarmaToUser(int $karmaValue, ExtbaseFrontendUser $frontendUser, KarmaSource $karmaSource, Campaign $campaign = null)
{
$frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser);
$ledgerEntry = new LedgerEntry();
$ledgerEntry->setImmutableValue($karmaValue);
$ledgerEntry->setMutableValue($karmaValue);
$ledgerEntry->setUser($frontendUser);
$ledgerEntry->setKarmaSource($karmaSource);
if ($campaign !== null) {
$ledgerEntry->setCampaign($campaign);
}
$this->ledgerEntryRepository->add($ledgerEntry);
$frontendUser->setKarmaImmutableValueCache($frontendUser->getKarmaImmutableValueCache() + $karmaValue);
$frontendUser->setKarmaMutableValueCache($frontendUser->getKarmaMutableValueCache() + $karmaValue);
$frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser);
}
/** /**
* Will take any FrontendUser object and return a karma extension FrontendUser subclass with the data we need * Will take any FrontendUser object and return a karma extension FrontendUser subclass with the data we need
* *
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser * @param ExtbaseFrontendUser $frontendUser
* @return FrontendUser * @return FrontendUser
* @internal * @internal
*/ */
public function ensureCorrectFrontendUserSubclass(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser) public function ensureCorrectFrontendUserSubclass(ExtbaseFrontendUser $frontendUser)
{ {
//If user is not an instance of our own frontend user class we have to fetch it again //If user is not an instance of our own frontend user class we have to fetch it again
if (!($frontendUser instanceof FrontendUser)) { if (!($frontendUser instanceof FrontendUser)) {
......
<?php <?php
declare(strict_types = 1); declare(strict_types = 1);
$ll = 'LLL:EXT:karma/Resources/Private/Language/locallang_db.xlf:';
/* /*
* Table configuration fe_users * Table configuration fe_users
*/ */
$feUsersColumns = [ $feUsersColumns = [
'tx_karma_immutable_value_cache' => [ 'karma_immutable_value_cache' => [
'exclude' => 0, 'exclude' => 0,
'config' => [ 'config' => [
'type' => 'passthrough', 'label' => $ll . 'fe_users.karma_immutable_value_cache',
'type' => 'input',
'eval' => 'int',
'readOnly' => true,
], ],
], ],
'tx_karma_mutable_value_cache' => [ 'karma_mutable_value_cache' => [
'exclude' => 0, 'exclude' => 0,
'config' => [ 'config' => [
'type' => 'passthrough', 'label' => $ll . 'fe_users.karma_mutable_value_cache',
'type' => 'input',
'eval' => 'int',
'readOnly' => true,
], ],
], ],
'tx_karma_cache_timestamp' => [ 'karma_cache_timestamp' => [
'exclude' => 0, 'exclude' => 0,
'config' => [ 'config' => [
'type' => 'passthrough', 'label' => $ll . 'fe_users.karma_cache_timestamp',
'type' => 'input',
'eval' => 'datetime',
'readOnly' => true,
], ],
], ],
]; ];
...@@ -29,8 +40,8 @@ $fields = 'tx_karma_immutable_value_cache,tx_karma_mutable_value_cache,tx_karma_ ...@@ -29,8 +40,8 @@ $fields = 'tx_karma_immutable_value_cache,tx_karma_mutable_value_cache,tx_karma_
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes( \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users', 'fe_users',
'tx_karma_immutable_value_cache,tx_karma_mutable_value_cache,tx_karma_cache_timestamp,', 'karma_immutable_value_cache,karma_mutable_value_cache,karma_cache_timestamp,',
'', '',
'' 'after:tx_extbase_type'
); );
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('fe_users', $feUsersColumns); \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('fe_users', $feUsersColumns);
...@@ -18,7 +18,7 @@ $tx_karma_domain_model_ledgerentry = [ ...@@ -18,7 +18,7 @@ $tx_karma_domain_model_ledgerentry = [
'iconfile' => 'EXT:karma/Resources/Public/Icons/tx_karma_domain_model_ledgerentry.svg', 'iconfile' => 'EXT:karma/Resources/Public/Icons/tx_karma_domain_model_ledgerentry.svg',
], ],
'interface' => [ 'interface' => [
'showRecordFieldList' => 'pid,hidden,starttime,endtime,expired,immutable_value,mutable_value,type,campaign,user' 'showRecordFieldList' => 'pid,hidden,starttime,endtime,expired,immutable_value,mutable_value,karma_source,campaign,user'
], ],
'columns' => [ 'columns' => [
'hidden' => [ 'hidden' => [
...@@ -77,14 +77,14 @@ $tx_karma_domain_model_ledgerentry = [ ...@@ -77,14 +77,14 @@ $tx_karma_domain_model_ledgerentry = [
'default' => 0, 'default' => 0,
] ]
], ],
'type' => [ 'karma_source' => [
'exclude' => true, 'exclude' => true,
'label' => $ll . 'tx_karma_domain_model_ledgerentry.type', 'label' => $ll . 'tx_karma_domain_model_ledgerentry.karma_source',
'config' => [ 'config' => [
'type' => 'group', 'type' => 'group',
'internal_type' => 'db', 'internal_type' => 'db',
'foreign_table' => 'tx_karma_domain_model_type', 'foreign_table' => 'tx_karma_domain_model_karmasource',
'allowed' => 'tx_karma_domain_model_type', 'allowed' => 'tx_karma_domain_model_karmasource',
'size' => 1, 'size' => 1,
'minitems' => 1, 'minitems' => 1,
'maxitems' => 1, 'maxitems' => 1,
...@@ -119,7 +119,7 @@ $tx_karma_domain_model_ledgerentry = [ ...@@ -119,7 +119,7 @@ $tx_karma_domain_model_ledgerentry = [
], ],
'types' => [ 'types' => [
'0' => [ '0' => [
'showitem' => 'hidden,expired,immutable_value,mutable_value,type,campaign,user' 'showitem' => 'hidden,expired,immutable_value,mutable_value,karma_source,campaign,user'
], ],
], ],
]; ];
......
plugin.tx_karma { plugin.tx_karma {
view { view {
templateRootPath = {$plugin.tx_userprofile.view.templateRootPath} templateRootPath = {$plugin.tx_karma.view.templateRootPath}
partialRootPath = {$plugin.tx_userprofile.view.partialRootPath} partialRootPath = {$plugin.tx_karma.view.partialRootPath}
layoutRootPath = {$plugin.tx_userprofile.view.layoutRootPath} layoutRootPath = {$plugin.tx_karma.view.layoutRootPath}
} }
persistence { persistence {
storagePid = {$plugin.tx_userprofile.persistence.storagePid} storagePid = {$plugin.tx_karma.persistence.storagePid}
} }
features { features {
......
...@@ -27,8 +27,8 @@ ...@@ -27,8 +27,8 @@
<trans-unit id="tx_karma_domain_model_ledgerentry.mutable_value"> <trans-unit id="tx_karma_domain_model_ledgerentry.mutable_value">
<source>Mutable Value</source> <source>Mutable Value</source>
</trans-unit> </trans-unit>
<trans-unit id="tx_karma_domain_model_ledgerentry.type"> <trans-unit id="tx_karma_domain_model_ledgerentry.karma_source">
<source>Type</source> <source>Karma Source</source>
</trans-unit> </trans-unit>
<trans-unit id="tx_karma_domain_model_ledgerentry.campaign"> <trans-unit id="tx_karma_domain_model_ledgerentry.campaign">
<source>Campaign</source> <source>Campaign</source>
...@@ -45,6 +45,15 @@ ...@@ -45,6 +45,15 @@
<trans-unit id="tx_karma_domain_model_karmasource.code"> <trans-unit id="tx_karma_domain_model_karmasource.code">
<source>Unique Alphanumeric Code</source> <source>Unique Alphanumeric Code</source>
</trans-unit> </trans-unit>
<trans-unit id="fe_users.tx_karma_immutable_value_cache">
<source>Immutable Karma Value Cache</source>
</trans-unit>
<trans-unit id="fe_users.tx_karma_mutable_value_cache">
<source>Mutable Karma Value Cache</source>
</trans-unit>
<trans-unit id="fe_users.tx_karma_cache_timestamp">
<source>Karma Cache Timestamp</source>
</trans-unit>
</body> </body>
</file> </file>
</xliff> </xliff>
\ No newline at end of file
...@@ -61,7 +61,7 @@ CREATE TABLE tx_karma_domain_model_campaign ( ...@@ -61,7 +61,7 @@ CREATE TABLE tx_karma_domain_model_campaign (
# Table structure for table 'fe_users' # Table structure for table 'fe_users'
# #
CREATE TABLE fe_users ( CREATE TABLE fe_users (
tx_karma_immutable_value_cache int(11) DEFAULT '0', karma_immutable_value_cache int(11) DEFAULT '0',
tx_karma_mutable_value_cache int(11) DEFAULT '0', karma_mutable_value_cache int(11) DEFAULT '0',
tx_karma_cache_timestamp int(11) DEFAULT '0' NOT NULL, karma_cache_timestamp int(11) DEFAULT '0' NOT NULL,
); );
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment