1. Go to this page and download the library: Download bymayo/craft-points library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bymayo / craft-points example snippets
use bymayo\points\Points;
Points::getInstance()->awards->addAward($userId, 'profileCompleted');
Points::getInstance()->awards->removeAward($userId, 'profileCompleted');
return [
'*' => [
// Any of the settings keys above can go here
'pluginName' => 'Points',
'currencyName' => 'Point',
'currencyNamePlural' => 'Points',
'birthdayFieldHandle' => '',
'conversionPointsCount' => 100,
'conversionCurrencyUnits' => 1,
'redemptionMinPoints' => 1,
'redemptionMaxOrderPercent' => 100,
'redemptionRefundBehaviour' => 'restoreProportional',
],
// Per-environment overrides work the same as any Craft config file
'staging' => [
'redemptionMaxOrderPercent' => 100,
],
];
namespace mysite\points;
use bymayo\points\conditions\BaseConditionRule;
use bymayo\points\conditions\RuleEvaluationContext;
use bymayo\points\triggers\BaseTrigger;
use bymayo\points\triggers\TriggerContext;
use craft\base\Element;
use craft\events\ModelEvent;
use yii\base\Event;
use yourvendor\comments\elements\Comment; // wherever your Comment element lives
class CommentPostedTrigger extends BaseTrigger
{
public function handle(): string { return 'mysite.commentPosted'; }
public function label(): string { return 'Comment posted'; }
public function group(): string { return 'Comments'; }
public function subject(): string { return 'comment'; }
// One or more [Class, EVENT_CONST] pairs to subscribe to.
public function events(): array
{
return [[Comment::class, Element::EVENT_AFTER_SAVE]];
}
// Companion conditions, auto-registered when this trigger registers.
public function conditions(): array
{
return [new CommentLengthCondition()];
}
// Return a TriggerContext to award points, or null to skip this event.
public function handleEvent(Event $event): ?TriggerContext
{
/** @var ModelEvent $event */
if (!($event->isNew ?? false)) {
return null; // edits don't earn points, only new comments
}
/** @var Comment $comment */
$comment = $event->sender;
if (!$comment->authorId) {
return null; // anonymous comments can't earn points
}
return new TriggerContext(userId: (int) $comment->authorId);
}
}
class CommentLengthCondition extends BaseConditionRule
{
public function handle(): string { return 'mysite.commentLength'; }
public function label(): string { return 'Comment length'; }
public function group(): string { return 'Comments'; }
// Only show this condition when the rule's trigger has subject 'comment'.
public function appliesToSubjects(): ?array { return ['comment']; }
// Render the "is at least N characters" input in the rule builder.
// Points wraps the result in <div class="cnd-variant" data-variant="…" hidden>.
public function isInline(): bool { return true; }
public function renderConfigUi(int $index, array $config): string
{
return \craft\helpers\Cp::textHtml([
'name' => "conditions[{$index}][min]",
'type' => 'number',
'value' => (string) ($config['min'] ?? ''),
]) . ' <span class="cnd-suffix">characters or more</span>';
}
public function evaluate(array $config, RuleEvaluationContext $ctx): bool
{
$min = (int) ($config['min'] ?? 0);
$comment = $ctx->triggerEvent?->sender ?? null;
$length = $comment ? mb_strlen((string) $comment->body) : 0;
return $length >= $min;
}
}
use bymayo\points\Points;
use mysite\points\CommentPostedTrigger;
public function init(): void
{
parent::init();
Points::getInstance()->triggers->register(new CommentPostedTrigger());
// CommentLengthCondition is pulled in via conditions() — no extra call needed.
}
use bymayo\points\events\AwardEvent;
use bymayo\points\services\Awards;
use yii\base\Event;
// Block an award (e.g. fraud check)
Event::on(Awards::class, Awards::EVENT_BEFORE_ADD_AWARD, function(AwardEvent $e) {
if (suspiciousActivity($e->userId)) {
$e->isValid = false;
}
});
// Send a "you earned points!" email
Event::on(Awards::class, Awards::EVENT_AFTER_ADD_AWARD, function(AwardEvent $e) {
sendThankYouEmail($e->userId, $e->rule, $e->award);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.