Example #1
0
 public function onRemove()
 {
     $points = post('points');
     if (!is_numeric($points)) {
         return;
     }
     $userExtend = new UserExtend($this->model);
     $userExtend->removePoints($points, false);
     return ['span.points' => $userExtend->user->points, 'span.points-today' => $userExtend->user->points_today, 'span.points-week' => $userExtend->user->points_this_week];
 }
 public function testCanHaveAndRemovePoints()
 {
     $incrementVal = 10;
     $user = FactoryMuffin::create('RainLab\\User\\Models\\User');
     $points = $user->points;
     $points_this_week = $user->points_this_week;
     $userExtend = new UserExtend($user);
     // Add points
     $userExtend->addPoints($incrementVal);
     $points += $incrementVal;
     $points_this_week += $incrementVal;
     $this->assertEquals($user->points, $points);
     $this->assertEquals($user->points_this_week, $points_this_week);
     // Remove Points
     $userExtend->removePoints($incrementVal);
     $points -= $incrementVal;
     $points_this_week -= $incrementVal;
     $this->assertEquals($user->points, $points);
     $this->assertEquals($user->points_this_week, $points_this_week);
 }
 /**
  * Redeem a reward for a user
  * @param int $id
  * The id of the reward to redeem
  * @param User $user
  * The user model to redeem the reward for
  */
 public static function redeem($id, $user)
 {
     $reward = Reward::find($id);
     if (!$reward) {
         throw SystemException(Lang::get('dma.friends.exceptions.missingReward', ['id' => $id]));
     }
     try {
         // Check overall inventory
         if ($reward->inventory !== null && $reward->inventory == 0) {
             Session::put('rewardError', Lang::get('dma.friends::lang.rewards.noInventory'));
             return;
         }
         // Check a users individual inventory
         $count = $user->rewards()->where('reward_id', $reward->id)->count();
         if (!empty($reward->user_redeem_limit) && $count >= $reward->user_redeem_limit) {
             Session::put('rewardError', Lang::get('dma.friends::lang.rewards.alreadyRedeemed'));
             return;
         }
         $userExtend = new UserExtend($user);
         if ($userExtend->removePoints($reward->points, false)) {
             if ($reward->inventory > 0) {
                 $reward->inventory--;
                 $reward->save();
             }
             $user->rewards()->save($reward);
             Event::fire('dma.friends.reward.redeemed', [$reward, $user]);
             $params = ['user' => $user, 'object' => $reward];
             FriendsLog::reward($params);
             // TODO handle printing of reward coupon
             Session::put('rewardMessage', Lang::get('dma.friends::lang.rewards.redeemed', ['title' => $reward->title]));
         } else {
             Session::put('rewardError', Lang::get('dma.friends::lang.rewards.noPoints'));
         }
     } catch (Exception $e) {
         throw SystemException(Lang::get('dma.friends.exceptions.rewardFailed'));
     }
 }