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 @@
namespace T3o\Karma\Controller;
use T3o\Karma\Domain\Model\KarmaSource;
use T3o\Karma\Domain\Repository\KarmaSourceRepository;
use T3o\Karma\Service\KarmaService;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
......@@ -40,6 +42,11 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll
*/
protected $frontendUserRepository;
/**
* @var KarmaSourceRepository
*/
protected $karmaSourceRepository;
/**
* @param KarmaService $karmaService
*/
......@@ -56,6 +63,12 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* @param KarmaSourceRepository $karmaSourceRepository
*/
public function injectKarmaSourceRepository(KarmaSourceRepository $karmaSourceRepository) {
$this->karmaSourceRepository = $karmaSourceRepository;
}
/**
* The test action
......@@ -64,7 +77,13 @@ class ManagerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControll
{
$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);
}
......
......@@ -32,14 +32,14 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
*
* @var int
*/
protected $karmaImmutableValue = 0;
protected $karmaImmutableValueCache = 0;
/**
* Cached mutable karma total
*
* @var int
*/
protected $karmaMutableValue = 0;
protected $karmaMutableValueCache = 0;
/**
* Karma total cache generation timestamp
......@@ -53,19 +53,19 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
*
* @return int
*/
public function getKarmaImmutableValue(): int
public function getKarmaImmutableValueCache(): int
{
return $this->karmaImmutableValue;
return $this->karmaImmutableValueCache;
}
/**
* 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
*
* @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
*
* @param int $karmaMutableValue
*/
public function setKarmaMutableValue(int $karmaMutableValue)
public function setKarmaMutableValueCache(int $karmaMutableValue)
{
$this->karmaMutableValue = $karmaMutableValue;
$this->karmaMutableValueCache = $karmaMutableValue;
}
/**
......
......@@ -2,7 +2,7 @@
namespace T3o\Karma\Domain\Model;
use In2code\Userprofile\Domain\Model\FrontendUser;
use T3o\Karma\Domain\Model\FrontendUser;
/***************************************************************
* Copyright notice
......
......@@ -2,9 +2,13 @@
namespace T3o\Karma\Service;
use T3o\Karma\Domain\Model\Campaign;
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\LedgerEntryRepository;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
/***************************************************************
* Copyright notice
......@@ -60,17 +64,17 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
/**
* 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 $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update immutable cache.
* @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);
if (!$ignoreCache) {
return $frontendUser->getKarmaImmutableValue();
return $frontendUser->getKarmaImmutableValueCache();
}
$immutableKarma = $this->ledgerEntryRepository->calculateImmutableKarmaTotalForUser($frontendUser);
......@@ -78,8 +82,8 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
if ($updateCacheEntry) {
$immutableKarma = $this->getMutableKarmaForUser($frontendUser, true, false);
$frontendUser->setKarmaImmutableValue($immutableKarma);
$frontendUser->setKarmaImmutableValue($immutableKarma);
$frontendUser->setKarmaImmutableValueCache($immutableKarma);
$frontendUser->setKarmaImmutableValueCache($immutableKarma);
$frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser);
......@@ -91,12 +95,12 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
/**
* 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 $updateCacheEntry Update the user's cache entry if we're ignoring cache. Will also update mutable cache.
* @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);
......@@ -109,8 +113,8 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
if ($updateCacheEntry) {
$mutableKarma = $this->getImmutableKarmaForUser($frontendUser, true, false);
$frontendUser->setKarmaMutableValue($mutableKarma);
$frontendUser->setKarmaMutableValue($mutableKarma);
$frontendUser->setKarmaMutableValueCache($mutableKarma);
$frontendUser->setKarmaMutableValueCache($mutableKarma);
$frontendUser->setKarmaCacheTimestamp(time());
$this->frontendUserRepository->update($frontendUser);
......@@ -119,14 +123,44 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
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
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser
* @param ExtbaseFrontendUser $frontendUser
* @return FrontendUser
* @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 (!($frontendUser instanceof FrontendUser)) {
......
<?php
declare(strict_types = 1);
$ll = 'LLL:EXT:karma/Resources/Private/Language/locallang_db.xlf:';
/*
* Table configuration fe_users
*/
$feUsersColumns = [
'tx_karma_immutable_value_cache' => [
'karma_immutable_value_cache' => [
'exclude' => 0,
'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,
'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,
'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_
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'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);
......@@ -18,7 +18,7 @@ $tx_karma_domain_model_ledgerentry = [
'iconfile' => 'EXT:karma/Resources/Public/Icons/tx_karma_domain_model_ledgerentry.svg',
],
'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' => [
'hidden' => [
......@@ -77,14 +77,14 @@ $tx_karma_domain_model_ledgerentry = [
'default' => 0,
]
],
'type' => [
'karma_source' => [
'exclude' => true,
'label' => $ll . 'tx_karma_domain_model_ledgerentry.type',
'label' => $ll . 'tx_karma_domain_model_ledgerentry.karma_source',
'config' => [
'type' => 'group',
'internal_type' => 'db',
'foreign_table' => 'tx_karma_domain_model_type',
'allowed' => 'tx_karma_domain_model_type',
'foreign_table' => 'tx_karma_domain_model_karmasource',
'allowed' => 'tx_karma_domain_model_karmasource',
'size' => 1,
'minitems' => 1,
'maxitems' => 1,
......@@ -119,7 +119,7 @@ $tx_karma_domain_model_ledgerentry = [
],
'types' => [
'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 {
view {
templateRootPath = {$plugin.tx_userprofile.view.templateRootPath}
partialRootPath = {$plugin.tx_userprofile.view.partialRootPath}
layoutRootPath = {$plugin.tx_userprofile.view.layoutRootPath}
templateRootPath = {$plugin.tx_karma.view.templateRootPath}
partialRootPath = {$plugin.tx_karma.view.partialRootPath}
layoutRootPath = {$plugin.tx_karma.view.layoutRootPath}
}
persistence {
storagePid = {$plugin.tx_userprofile.persistence.storagePid}
storagePid = {$plugin.tx_karma.persistence.storagePid}
}
features {
......
......@@ -27,8 +27,8 @@
<trans-unit id="tx_karma_domain_model_ledgerentry.mutable_value">
<source>Mutable Value</source>
</trans-unit>
<trans-unit id="tx_karma_domain_model_ledgerentry.type">
<source>Type</source>
<trans-unit id="tx_karma_domain_model_ledgerentry.karma_source">
<source>Karma Source</source>
</trans-unit>
<trans-unit id="tx_karma_domain_model_ledgerentry.campaign">
<source>Campaign</source>
......@@ -45,6 +45,15 @@
<trans-unit id="tx_karma_domain_model_karmasource.code">
<source>Unique Alphanumeric Code</source>
</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>
</file>
</xliff>
\ No newline at end of file
......@@ -61,7 +61,7 @@ CREATE TABLE tx_karma_domain_model_campaign (
# Table structure for table 'fe_users'
#
CREATE TABLE fe_users (
tx_karma_immutable_value_cache int(11) DEFAULT '0',
tx_karma_mutable_value_cache int(11) DEFAULT '0',
tx_karma_cache_timestamp int(11) DEFAULT '0' NOT NULL,
karma_immutable_value_cache int(11) DEFAULT '0',
karma_mutable_value_cache int(11) DEFAULT '0',
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