Commit 4d0f0153 authored by Thomas Löffler's avatar Thomas Löffler
Browse files

Merge branch '226-add-hook-earning-karma-when-user-updates-profile-with-new-data' into 'develop'

Resolve "Add hook earning karma when new user is created"

Closes #226

See merge request t3o/my!115
parents 5525bd62 251f5b33
Pipeline #6135 passed with stages
in 2 minutes and 20 seconds
......@@ -2,6 +2,14 @@
namespace T3o\Karma\Domain\Repository;
use T3o\Karma\Domain\Model\LedgerEntry;
use T3o\Karma\Service\KarmaService;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
/***************************************************************
* Copyright notice
*
......@@ -27,4 +35,88 @@ namespace T3o\Karma\Domain\Repository;
class CampaignRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
/**
* @var KarmaService
*/
public $karmaService;
public function injectKarmaService(KarmaService $karmaService)
{
$this->karmaService = $karmaService;
}
/**
* Calculates the immutable karma total for a user
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser
* @return int
*/
public function calculateImmutableKarmaTotalForUser(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser)
{
$frontendUser = $this->karmaService->ensureCorrectFrontendUserSubclass($frontendUser);
$queryBuilder = $this->getQueryBuilder();
$result = $queryBuilder
->addSelectLiteral($queryBuilder->expr()->sum('immutable_value'))
->from($this->getTableName())
->where(
$queryBuilder->expr()->eq('user', $frontendUser->getUid()),
$queryBuilder->expr()->eq('expired', 0)
)
->execute();
$sum = $result->fetchColumn(0);
return $sum;
}
/**
* Calculates the mutable karma total for a user
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser
*/
public function calculateMutableKarmaTotalForUser(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $frontendUser)
{
$frontendUser = $this->karmaService->ensureCorrectFrontendUserSubclass($frontendUser);
$queryBuilder = $this->getQueryBuilder();
$result = $queryBuilder
->addSelectLiteral($queryBuilder->expr()->sum('mutable_value'))
->from($this->getTableName())
->where(
$queryBuilder->expr()->eq('user', $frontendUser->getUid()),
$queryBuilder->expr()->eq('expired', 0)
)
->execute();
$sum = $result->fetchColumn(0);
return $sum;
}
/**
* Get the table name for this class
*
* @return string
* @internal
*/
protected function getTableName()
{
/** @var DataMapper $dataMapper */
$dataMapper = GeneralUtility::makeInstance(DataMapper::class);
/** @var DataMap $dataMap */
$dataMap = $dataMapper->getDataMap(LedgerEntry::class);
return $dataMap->getTableName();
}
/**
* Get a QueryBuilder object
*
* @return QueryBuilder
*/
protected function getQueryBuilder()
{
return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->getTableName());
}
}
......@@ -7,6 +7,7 @@ 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\KarmaSourceRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
......@@ -45,6 +46,11 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
*/
protected $frontendUserRepository;
/**
* @var \T3o\Karma\Domain\Repository\KarmaSourceRepository
*/
protected $karmaSourceRepository;
/**
* @param LedgerEntryRepository $ledgerEntryRepository
*/
......@@ -61,6 +67,14 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* @param KarmaSourceRepository $karmaSourceRepository
*/
public function injectKarmaSourceRepository(KarmaSourceRepository $karmaSourceRepository)
{
$this->karmaSourceRepository = $karmaSourceRepository;
}
/**
* Get the imimmutable karma total from the user
*
......@@ -153,6 +167,34 @@ class KarmaService implements \TYPO3\CMS\Core\SingletonInterface
$this->frontendUserRepository->update($frontendUser);
}
/**
* Get a Karma Source object. Will generate a new object with code and label if none exists.
*
* @param string $sourceCode
* @param string $defaultLabel
* @return KarmaSource
* @throws \Exception when $sourceCode is an empty string
*/
public function getKarmaSourceByCode(string $sourceCode, $defaultLabel='[No Label Set]')
{
if ($sourceCode == '') {
throw new \Exception('Empty karma source code supplied.', 1541508560);
}
$karmaSource = $this->karmaSourceRepository->findByCode($sourceCode)->getFirst();
if ($karmaSource === null) {
/** @var KarmaSource $karmaSource */
$karmaSource = new KarmaSource();
$karmaSource->setCode($karmaSourceCode);
$karmaSource->setTitle($defaultLabel);
$this->karmaSourceRepository->add($karmaSource);
}
return $karmaSource;
}
/**
* Will take any FrontendUser object and return a karma extension FrontendUser subclass with the data we need
*
......
......@@ -12,6 +12,16 @@ plugin.tx_karma {
# uncomment the following line to enable the new Property Mapper.
# rewrittenPropertyMapper = 1
}
settings {
userProfileChangeKarmaIssuer {
newUserWasCreated {
sourceCode = usercreated
defaultSourceLabel = Created my.typo3.org User
valueEarned = 10
}
}
}
}
config.tx_extbase {
......
<section class="container">
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th>Karma Points</th>
<th>Karma Cash</th>
</tr>
</thead>
<tbody>
<f:for each="{entries}" as="entry">
<tr id="entry-{entry.uid}">
<td>{entry.crdate -> f:format.date(format:'j F, Y')}</td>
<td>{entry.karmaSource.title}</td>
<td>{entry.immutableValue}</td>
<td>{entry.mutableValue}</td>
</tr>
</f:for>
</tbody>
</table>
</section>
\ No newline at end of file
<section class="container">
<h3>Karma Status</h3>
<f:if condition="{hasKarma}">
<f:then>
<p>
<strong>{immutableValue}</strong> karma points<br>
<strong>{mutableValue}</strong> karma cash
</p>
</f:then>
<f:else>
<p>You do not have any karma yet. Participate in the community to earn some.</p>
</f:else>
</f:if>
</section>
\ No newline at end of file
......@@ -33,3 +33,13 @@ defined('TYPO3_MODE') or die('Access denied.');
'UserDisplay' => '',
]
);
//Add karma to users when they are created
/** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\SignalSlot\Dispatcher');
$signalSlotDispatcher->connect(
\In2code\Femanager\Controller\AbstractController::class,
'finalCreateAfterPersist',
\T3o\Karma\Utility\UserProfileChangeKarmaIssuer::class,
'newUserWasCreated'
);
<?php
namespace T3o\Karma\Controller;
use T3o\Karma\Domain\Repository\FrontendUserRepository;
use T3o\Karma\Domain\Repository\KarmaSourceRepository;
use T3o\Karma\Domain\Repository\LedgerEntryRepository;
use T3o\Karma\Service\KarmaService;
/***************************************************************
* Copyright notice
*
* (c) 2018
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
/**
* @var \T3o\Karma\Service\KarmaService
*/
protected $karmaService;
/**
* @var \T3o\Karma\Domain\Repository\FrontendUserRepository
*/
protected $frontendUserRepository;
/**
* @var \T3o\Karma\Domain\Repository\KarmaSourceRepository
*/
protected $karmaSourceRepository;
/**
* @var \T3o\Karma\Domain\Repository\LedgerEntryRepository
*/
protected $ledgerEntryRepository;
/**
* @param \T3o\Karma\Service\KarmaService $karmaService
*/
public function injectKarmaService(KarmaService $karmaService)
{
$this->karmaService = $karmaService;
}
/**
* @param \T3o\Karma\Domain\Repository\FrontendUserRepository $frontendUserRepository
*/
public function injectFrontendUserRepository(FrontendUserRepository $frontendUserRepository)
{
$this->frontendUserRepository = $frontendUserRepository;
}
/**
* @param KarmaSourceRepository $karmaSourceRepository
*/
public function injectKarmaSourceRepository(KarmaSourceRepository $karmaSourceRepository)
{
$this->karmaSourceRepository = $karmaSourceRepository;
}
/**
* @param LedgerEntryRepository $ledgerEntryRepository
*/
public function injectLedgerEntryRepository(LedgerEntryRepository $ledgerEntryRepository)
{
$this->ledgerEntryRepository = $ledgerEntryRepository;
}
}
<?php
namespace T3o\Karma\Controller;
use T3o\Karma\Utility\UserProfileChangeKarmaIssuer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/***************************************************************
* Copyright notice
*
* (c) 2018
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class ManagerController extends AbstractController
{
/**
* The test action
*/
public function testAction()
{
$frontendUser = $this->frontendUserRepository->findByUid(1);
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var UserProfileChangeKarmaIssuer $testUserProfileChangeKarmaIssuerSlot */
$testUserProfileChangeKarmaIssuerSlot = $objectManager->get(UserProfileChangeKarmaIssuer::class);
$testUserProfileChangeKarmaIssuerSlot->newUserWasCreated($frontendUser);
}
}
<?php
namespace T3o\Karma\Controller;
/***************************************************************
* Copyright notice
*
* (c) 2018
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class UserDisplayController extends AbstractController
{
/**
* Lists karma ledger entries for the currently logged-in user
*/
public function listAction()
{
$entries = $this->ledgerEntryRepository->findByUser($GLOBALS['TSFE']->fe_user->user['uid']);
$this->view->assign('entries', $entries);
}
/*
* Shows a small karma earned widget for the currently logged-in user
*/
public function statusWidgetAction()
{
if ($GLOBALS['TSFE']->fe_user->user['uid']) {
$frontendUser = $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
$immutableValue = $this->karmaService->getImmutableKarmaForUser($frontendUser);
$mutableValue = $this->karmaService->getMutableKarmaForUser($frontendUser);
$this->view->assign('immutableValue', $immutableValue);
$this->view->assign('mutableValue', $mutableValue);
$this->view->assign('hasKarma', ($immutableValue > 0 || $mutableValue > 0));
}
}
}
<?php
namespace T3o\Karma\Utility;
use In2code\Femanager\Controller\AbstractController as In2CodeAbstractController;
use T3o\Karma\Service\KarmaService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser as ExtbaseFrontendUser;
/***************************************************************
* Copyright notice
*
* (c) 2018
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class UserProfileChangeKarmaIssuer
{
/**
* @var \T3o\Karma\Service\KarmaService
*/
protected $karmaService;
/**
* @var array
*/
protected $settings;
/**
* UserProfileChangeKarmaIssuer constructor.
*/
public function __construct()
{
$objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
/** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
$this->settings = $configuration = $configurationManager->getConfiguration(
\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'karma'
);
}
/**
* @param \T3o\Karma\Service\KarmaService $karmaService
*/
public function injectKarmaService(KarmaService $karmaService)
{
$this->karmaService = $karmaService;
}
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
*/
public function injectObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* @param ExtbaseFrontendUser $frontendUser
* @param string $action
* @param In2CodeAbstractController $object
*/
public function newUserWasCreated(ExtbaseFrontendUser $frontendUser)
{
$karmaSource = $this->karmaService->getKarmaSourceByCode(
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['sourceCode'],
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['defaultSourceLabel']
);
$this->karmaService->addKarmaToUser(
$this->settings['userProfileChangeKarmaIssuer']['newUserWasCreated']['valueEarned'],
$frontendUser,
$karmaSource
);
}
}
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