UserContentRewardTrait
Credits for published content: an advert (Bazaar), a company profile (Comcat) and a discussion post (Discussion); and for a complete user profile ({@see self::rewardCompletedProfile()}).
The content module decides when its record is published (approved, confirmed, paid, not banned)
and calls self::rewardPublishedContent() with the author; this trait pays the credits of the
action (reward.<action>.credits, module 1) at most once per record. Each decision is recorded in
cms_system_user_credit_rewards, so a record unpublished and published again never pays twice.
Not rewarded:
- content inserted before the reward was switched on (
reward.<action>.since), otherwise the next edit of an old advert would pay for it, - an author who cannot sign in (inactive, banned, deleted) — recorded as
user_inactive, - over the monthly cap of the action (
reward.<action>.monthlyCap) — recorded asmonthly_cap.
Fail-open: a failure here is logged (creditReward) and never breaks the save of the content.
Requires on the host: $userService, getSettingContext(), bumpCacheTagsForClass().
Table of Contents
Constants
- CONTENT_REWARD_ACTIONS : array<string|int, mixed> = [self::CONTENT_REWARD_BAZAAR_ADVERT, self::CONT...
- Actions rewarded for content, in the order of the admin screen.
- CONTENT_REWARD_BAZAAR_ADVERT : string = 'bazaarAdvert'
- CONTENT_REWARD_COMCAT_ADVERT : string = 'comcatAdvert'
- CONTENT_REWARD_DISCUSSION_MESSAGE : string = 'discussionMessage'
- CONTENT_REWARD_SKIP_MONTHLY_CAP : string = 'monthly_cap'
- CONTENT_REWARD_SKIP_USER_INACTIVE : string = 'user_inactive'
- PROFILE_REWARD_ACTION : string = 'profileComplete'
- Complete profile: once per account, `subject_id` = the user's own id.
Methods
- getContentRewardCredits() : int
- Credits of the action; 0 = the action is not rewarded (also for an unknown action).
- rewardCompletedProfile() : void
- Marks the first complete profile of the account (one-way flag `profile_completed_at`) and pays `reward.profileComplete.credits` at that moment. Called after every user save and after the account activation.
- rewardPublishedContent() : void
- Pays the credits of the action to the author of a record that has just been published. Safe to call repeatedly for the same record (each save, moderation toggle or payment): only the first call records a decision.
- contentRewardSince() : DateTimeImmutable|null
- Moment the reward of the action was switched on; null = never (nothing is rewarded).
- contentRewardSkipReason() : string|null
Constants
CONTENT_REWARD_ACTIONS
Actions rewarded for content, in the order of the admin screen.
public
array<string|int, mixed>
CONTENT_REWARD_ACTIONS
= [self::CONTENT_REWARD_BAZAAR_ADVERT, self::CONTENT_REWARD_COMCAT_ADVERT, self::CONTENT_REWARD_DISCUSSION_MESSAGE]
CONTENT_REWARD_BAZAAR_ADVERT
public
string
CONTENT_REWARD_BAZAAR_ADVERT
= 'bazaarAdvert'
CONTENT_REWARD_COMCAT_ADVERT
public
string
CONTENT_REWARD_COMCAT_ADVERT
= 'comcatAdvert'
CONTENT_REWARD_DISCUSSION_MESSAGE
public
string
CONTENT_REWARD_DISCUSSION_MESSAGE
= 'discussionMessage'
CONTENT_REWARD_SKIP_MONTHLY_CAP
public
string
CONTENT_REWARD_SKIP_MONTHLY_CAP
= 'monthly_cap'
CONTENT_REWARD_SKIP_USER_INACTIVE
public
string
CONTENT_REWARD_SKIP_USER_INACTIVE
= 'user_inactive'
PROFILE_REWARD_ACTION
Complete profile: once per account, `subject_id` = the user's own id.
public
string
PROFILE_REWARD_ACTION
= 'profileComplete'
Methods
getContentRewardCredits()
Credits of the action; 0 = the action is not rewarded (also for an unknown action).
public
getContentRewardCredits(string $action) : int
Parameters
- $action : string
Return values
intrewardCompletedProfile()
Marks the first complete profile of the account (one-way flag `profile_completed_at`) and pays `reward.profileComplete.credits` at that moment. Called after every user save and after the account activation.
public
rewardCompletedProfile(int $userId) : void
IMPORTANT: the flag is set at the first complete save even while the reward is off (0 credits), and it is never cleared: a profile completed once never earns credits, not after a later switch-on, nor after it is emptied and filled in again. Profiles complete before the feature got the flag by the migration. An account that cannot sign in yet is left alone, so a profile filled in the registration form is flagged and paid after the activation.
Parameters
- $userId : int
rewardPublishedContent()
Pays the credits of the action to the author of a record that has just been published. Safe to call repeatedly for the same record (each save, moderation toggle or payment): only the first call records a decision.
public
rewardPublishedContent(string $action, int $subjectId, int $userId, DateTimeInterface $insertedAt) : void
Parameters
- $action : string
- $subjectId : int
- $userId : int
- $insertedAt : DateTimeInterface
contentRewardSince()
Moment the reward of the action was switched on; null = never (nothing is rewarded).
private
contentRewardSince(string $action) : DateTimeImmutable|null
Parameters
- $action : string
Return values
DateTimeImmutable|nullcontentRewardSkipReason()
private
contentRewardSkipReason(string $action, int $userId) : string|null
Parameters
- $action : string
- $userId : int