Commit d39b86ec authored by mabolek's avatar mabolek

[FEATURE] Fetch sum of user's karma on per-source basis

parent 9b1bc16b
Pipeline #6161 failed with stages
in 17 seconds
......@@ -48,14 +48,16 @@ class UserDisplayController extends AbstractController
$immutableValue = $this->karmaService->getImmutableKarmaForUser($frontendUser);
$mutableValue = $this->karmaService->getMutableKarmaForUser($frontendUser);
$sourceTotals = [];
foreach ($this->getKarmaSourceCodes() as $sourceCode) {
$value = $this->karmaService->getImmutableKarmaSourceTotalForUser($sourceCode, $frontendUser);
$sourceTotals[$sourceCode] = $value;
}
$this->view->assign('immutableValue', $immutableValue);
$this->view->assign('mutableValue', $mutableValue);
$this->view->assign('hasKarma', ($immutableValue > 0 || $mutableValue > 0));
$this->view->assign('sourceTotals', $sourceTotals);
}
}
}
......@@ -48,6 +48,18 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
*/
protected $karmaCacheTimestamp = 0;
/**
* JSON encoded associative array containing totals for the karma source
*
* @var string
*/
protected $karmaSourceTotalCache = '';
/**
* @var array|null
*/
private $karmaSourceTotalCacheDecoded;
/**
* Get the cached immutable karma total
*
......@@ -107,4 +119,51 @@ class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
{
$this->karmaCacheTimestamp = $karmaCacheTimestamp;
}
/**
* Get the JSON string containing the karma source total cache array
*
* @return string
*/
public function getKarmaSourceTotalCache(): string
{
return $this->karmaSourceTotalCache;
}
/**
* Set the JSON string containing the karma source total cache array
*
* @param string $karmaSourceTotalCache
*/
public function setKarmaSourceTotalCache(string $karmaSourceTotalCache)
{
$this->karmaSourceTotalCache = $karmaSourceTotalCache;
}
/**
* @return array|null
*/
public function getKarmaSourceTotalCacheDecoded(): ?array
{
if ($this->karmaSourceTotalCacheDecoded === null) {
$this->karmaSourceTotalCacheDecoded = json_decode($this->getKarmaSourceTotalCache());
if(!is_array($this->karmaSourceTotalCacheDecoded)) {
$this->karmaSourceTotalCacheDecoded = [];
$this->setKarmaSourceTotalCache(json_encode($this->karmaSourceTotalCacheDecoded));
}
}
return $this->karmaSourceTotalCacheDecoded;
}
/**
* @param array|null $karmaSourceTotalCacheDecoded
*/
public function setKarmaSourceTotalCacheDecoded(array $karmaSouceTotalDecoded) {
$this->karmaSourceTotalCacheDecoded = $karmaSouceTotalDecoded;
$this->setKarmaSourceTotalCache(json_encode($this->karmaSourceTotalCacheDecoded));
}
}
......@@ -116,7 +116,7 @@ class LedgerEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
->where(
$queryBuilder->expr()->eq('user', $frontendUser->getUid()),
$queryBuilder->expr()->eq('expired', 0),
$queryBuilder->expr()->eq('karma_source', $source)
$queryBuilder->expr()->eq('karma_source', $queryBuilder->quote($source))
)
->execute();
$sum = $result->fetchColumn(0);
......
......@@ -2,11 +2,9 @@
namespace T3o\Karma\Service;
use T3o\Karma\Domain\Model\Campaign;
use T3o\Karma\Domain\Model\FrontendUser;
use T3o\Karma\Domain\Model\LedgerEntry;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Domain\Repository\KarmaSourceRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
......@@ -85,6 +83,7 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$frontendUser->setKarmaImmutableValueCache($immutableKarma);
$frontendUser->setKarmaMutableValueCache($mutableKarma);
$frontendUser->setKarmaCacheTimestamp(time());
$frontendUser->setKarmaSourceTotalCacheDecoded([]);
$this->frontendUserRepository->update($frontendUser);
}
......@@ -116,6 +115,7 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$frontendUser->setKarmaMutableValueCache($mutableKarma);
$frontendUser->setKarmaImmutableValueCache($immutableKarma);
$frontendUser->setKarmaCacheTimestamp(time());
$frontendUser->setKarmaSourceTotalCacheDecoded([]);
$this->frontendUserRepository->update($frontendUser);
}
......@@ -123,6 +123,38 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
return $mutableKarma;
}
/**
* Get the immutable karma total for a specific karma source code from the user
*
* @param string $karmaSourceCode The source code to fetch the total for
* @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 getImmutableKarmaSourceTotalForUser($karmaSourceCode, ExtbaseFrontendUser $frontendUser, $ignoreCache = false, $updateCacheEntry = true)
{
$frontendUser = $this->ensureCorrectFrontendUserSubclass($frontendUser);
$totals = $frontendUser->getKarmaSourceTotalCacheDecoded();
if (!$ignoreCache) {
if (isset($totals[$karmaSourceCode])) {
return $totals[$karmaSourceCode];
}
}
$sourceTotal = $this->ledgerEntryRepository->calculateImmutableKarmaSourceTotalForUser($karmaSourceCode, $frontendUser);
if ($updateCacheEntry) {
$totals[$karmaSourceCode] = $sourceTotal;
$frontendUser->setKarmaSourceTotalCacheDecoded($totals);
$this->frontendUserRepository->update($frontendUser);
}
return $sourceTotal;
}
/**
* Add karma to a user
*
......
......@@ -34,6 +34,15 @@ $feUsersColumns = [
'readOnly' => true,
],
],
'karma_source_total_cache' => [
'exclude' => 0,
'config' => [
'label' => $ll . 'fe_users.karma_source_total_cache',
'type' => 'text',
'eval' => '',
'readOnly' => true,
],
],
];
$fields = 'tx_karma_immutable_value_cache,tx_karma_mutable_value_cache,tx_karma_cache_timestamp,';
......
......@@ -31,7 +31,7 @@
<source>Karma Source Code</source>
</trans-unit>
<trans-unit id="tx_karma_domain_model_ledgerentry.issuer">
<source>Issuer Code/source>
<source>Issuer Code</source>
</trans-unit>
<trans-unit id="tx_karma_domain_model_ledgerentry.issuer_action">
<source>Issuer Action Code</source>
......@@ -51,15 +51,18 @@
<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">
<trans-unit id="fe_users.karma_immutable_value_cache">
<source>Immutable Karma Value Cache</source>
</trans-unit>
<trans-unit id="fe_users.tx_karma_mutable_value_cache">
<trans-unit id="fe_users.karma_mutable_value_cache">
<source>Mutable Karma Value Cache</source>
</trans-unit>
<trans-unit id="fe_users.tx_karma_cache_timestamp">
<trans-unit id="fe_users.karma_cache_timestamp">
<source>Karma Cache Timestamp</source>
</trans-unit>
<trans-unit id="fe_users.karma_source_total_cache">
<source>Karma Source Total Cache</source>
</trans-unit>
</body>
</file>
</xliff>
......@@ -6,6 +6,12 @@
<strong>{immutableValue}</strong> karma points<br>
<strong>{mutableValue}</strong> karma cash
</p>
<ul>
<f:for each="{sourceTotals}" as="sourceTotal" key="sourceCode">
<li>{settings.sourceCodes.{sourceCode}.label}: {sourceTotal}</li>
</f:for>
</ul>
</f:then>
<f:else>
<p>You do not have any karma yet. Participate in the community to earn some.</p>
......
......@@ -50,4 +50,5 @@ CREATE TABLE fe_users (
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,
karma_source_total_cache text DEFAULT '' NOT NULL,
);
Markdown is supported
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