Commit ffad70af authored by Victor Emanouilov's avatar Victor Emanouilov
Browse files

[ENH] add goalId to credits when rewarding credits to users via reached goals

parent b8e1cfa2
Pipeline #757341390 passed with stages
in 8 minutes and 41 seconds
......@@ -3748,6 +3748,7 @@ CREATE TABLE `tiki_credits` (
`total_amount` FLOAT NOT NULL DEFAULT 0,
`used_amount` FLOAT NOT NULL DEFAULT 0,
`product_id` INT( 8 ) NULL ,
`goalId` INT NULL ,
PRIMARY KEY ( `creditId` ) ,
INDEX ( `userId` , `credit_type` )
) ENGINE=MyISAM;
......
ALTER TABLE `tiki_credits` ADD `goalId` INT NULL;
......@@ -33,7 +33,7 @@ class CreditSource implements Search_ContentSource_Interface
$credit_types = TikiLib::lib('credits')->getCreditTypes();
$credit = $this->table->fetchRow(['userId', 'credit_type', 'creation_date', 'expiration_date', 'total_amount', 'used_amount', 'product_id'], [
$credit = $this->table->fetchRow(['userId', 'credit_type', 'creation_date', 'expiration_date', 'total_amount', 'used_amount', 'product_id', 'goalId'], [
'creditId' => $objectId,
]);
......@@ -59,6 +59,7 @@ class CreditSource implements Search_ContentSource_Interface
'total_amount' => $typeFactory->numeric($credit['total_amount']),
'used_amount' => $typeFactory->numeric($credit['used_amount']),
'product_id' => $typeFactory->numeric($credit['product_id']),
'goal_id' => $typeFactory->numeric($credit['goalId']),
'allowed_groups' => $typeFactory->multivalue(['Anonymous', 'Registered']),
];
} else {
......@@ -71,7 +72,7 @@ class CreditSource implements Search_ContentSource_Interface
return [
'title', 'creation_date', 'expiration_date', 'contributors', 'user', 'credit_type',
'credit_type_text', 'credit_type_unit', 'credit_type_static_level', 'credit_type_scaling_divisor',
'total_amonut', 'used_amount', 'product_id'
'total_amonut', 'used_amount', 'product_id', 'goal_id'
];
}
......@@ -91,6 +92,7 @@ class CreditSource implements Search_ContentSource_Interface
'total_amount' => 'numeric',
'used_amount' => 'numeric',
'product_id' => 'numeric',
'goal_id' => 'numeric',
];
}
......
......@@ -199,7 +199,7 @@ class CreditsLib extends TikiLib
/**
* Adds a new credits entry for the user.
*/
public function addCredits($userId, $creditType, $amount, $expirationDate = null, $validFrom = null)
public function addCredits($userId, $creditType, $amount, $expirationDate = null, $validFrom = null, $goalId = null)
{
if (! $amount) {
return false;
......@@ -225,9 +225,9 @@ class CreditsLib extends TikiLib
$this->query(
"INSERT INTO `tiki_credits`
(`userId`, `credit_type`, `total_amount`, `expiration_date`, `creation_date`)
VALUES(?,?,?,if (? = '', NULL, ?),if (?='', NULL, ?))",
[$userId, $creditType, $amount, $expirationDate, $expirationDate, $validFrom, $validFrom]
(`userId`, `credit_type`, `total_amount`, `expiration_date`, `creation_date`, `goalId`)
VALUES(?,?,?,if (? = '', NULL, ?),if (?='', NULL, ?),?)",
[$userId, $creditType, $amount, $expirationDate, $expirationDate, $validFrom, $validFrom, $goalId]
);
return true;
......
......@@ -168,6 +168,12 @@ class GoalLib
'group' => $context['group'],
]);
if (! empty($goal['rewards'])) {
foreach ($goal['rewards'] as &$reward) {
$reward['goalId'] = $goal['goalId'];
}
}
$rewardlib = TikiLib::lib('goalreward');
if ($goal['type'] == 'group') {
$rewardlib->giveRewardsToMembers($context['group'], $goal['rewards']);
......
......@@ -63,7 +63,7 @@ class GoalRewardLib
if (! empty($creditTypes[$reward['creditType']])) {
$userId = TikiLib::lib('tiki')->get_user_id($user);
$lib = TikiLib::lib('credits');
$lib->addCredits($userId, $reward['creditType'], $reward['creditQuantity']);
$lib->addCredits($userId, $reward['creditType'], $reward['creditQuantity'], null, null, $reward['goalId'] ?? null);
}
},
'applyGroup' => function ($group, $reward) {
......
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