/** * 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')); } }
/** * Execute the console command. * @return void */ public function fire() { // Taxonomy terms $termRelations = $this->db->table('wp_term_relationships')->get(); foreach ($termRelations as $relation) { $activity = Activity::findWordpress($relation->object_id)->first(); if ($activity) { if (!$activity->categories->contains($relation->term_taxonomy_id)) { $category = Category::find($relation->term_taxonomy_id); if ($category) { $activity->categories()->save($category); } } } } // p2p connections $p2ps = $this->db->table('wp_p2p')->get(); foreach ($p2ps as $p2p) { list($from, $t, $to) = explode('-', $p2p->p2p_type); switch ($from) { case 'activity': $from = Activity::findWordpress($p2p->p2p_from)->first(); $from_table = 'activity'; break; case 'badge': $from = Badge::findWordpress($p2p->p2p_from)->first(); $from_table = 'badge'; break; case 'dma-location': $from = Location::findWordpress($p2p->p2p_from)->first(); $from_table = 'location'; break; case 'step': $from = Step::findWordpress($p2p->p2p_from)->first(); $from_table = 'step'; break; default: $from = false; } switch ($to) { case 'activity': $to = Activity::findWordpress($p2p->p2p_to)->first(); $to_table = 'activity'; break; case 'badge': $to = Badge::findWordpress($p2p->p2p_to)->first(); $to_table = 'badge'; break; case 'dma-location': $to = Location::findWordpress($p2p->p2p_to)->first(); $to_table = 'location'; break; case 'step': $to = Step::findWordpress($p2p->p2p_to)->first(); $to_table = 'step'; break; default: $to = false; } if ($from && $to) { $table = 'dma_friends_' . $from_table . '_' . $to_table; switch ($table) { case 'dma_friends_activity_step': $from->steps()->save($to); $this->info('activity: ' . $from->title . ' --> step: ' . $to->title); break; case 'dma_friends_step_badge': $to->steps()->save($from); $this->info('step: ' . $from->title . ' --> badge: ' . $to->title); break; default: $values = [$from_table . '_id' => $from->id, $to_table . '_id' => $to->id]; if (Schema::hasTable($table)) { DB::table($table)->insert($values); $this->info('from: ' . $from->title . ' ----- ' . $to->title); } else { $this->error('table doesnt exist: ' . $table); } } } } // User achievements $achievements = $this->db->table('wp_usermeta')->where('meta_key', '_badgeos_achievements')->get(); $post = new Post(); $this->info('Sync Achievements'); foreach ($achievements as $achievement) { $user = User::find($achievement->user_id); if (empty($user)) { continue; } // Flush existing records DB::table($this->userStepTable)->where('user_id', $user->id)->delete(); DB::table($this->userBadgeTable)->where('user_id', $user->id)->delete(); $data = unserialize($achievement->meta_value); // wtf we don't need arrays in our arrays if we want to array $data = array_pop($data); foreach ($data as $d) { $link = ['user_id' => $user->id, 'created_at' => $post->epochToTimestamp($d->date_earned)]; // About half way thru the data for the location key changes. // so lets deal with that if (isset($d->location)) { $location_id = $d->location; } elseif (isset($d->location_earned)) { $location_id = $d->location_earned; } else { $location_id = null; } $location = Location::findWordpress($location_id)->first(); if (isset($location->id)) { $link['location_id'] = $location->id; } if ($d->post_type == 'step') { $step = Step::findWordpress($d->ID)->first(); if ($step) { $link['step_id'] = $step->id; DB::table($this->userStepTable)->insert($link); } } elseif ($d->post_type == 'badge') { $badge = Badge::findWordpress($d->ID)->first(); if ($badge) { $link['badge_id'] = $badge->id; DB::table($this->userBadgeTable)->insert($link); } } } } // Syncronize activities and users $this->info('Importing Activity/User relations'); $table = 'dma_friends_activity_user'; DB::table($table)->delete(); ActivityLog::where('action', '=', 'activity')->chunk(100, function ($activityLogs) use($table) { foreach ($activityLogs as $activityLog) { if ($activityLog->object_id) { echo '.'; $pivotTable = ['user_id' => $activityLog->user_id, 'activity_id' => $activityLog->object_id, 'created_at' => $activityLog->timestamp, 'updated_at' => $activityLog->timestamp]; DB::table($table)->insert($pivotTable); } } }); // Syncronize rewards and users $this->info('Importing Reward/User relations'); $table = 'dma_friends_reward_user'; //DB::table($table)->delete(); ActivityLog::where('action', '=', 'reward')->where('timestamp', '<', '2015-02-02 12:10:35')->chunk(100, function ($activityLogs) use($table) { foreach ($activityLogs as $activityLog) { if ($activityLog->object_id) { echo '.'; if (Reward::find($activityLog->object_id) && User::find($activityLog->user_id)) { $pivotTable = ['user_id' => $activityLog->user_id, 'reward_id' => $activityLog->object_id, 'created_at' => $activityLog->timestamp, 'updated_at' => $activityLog->timestamp]; DB::table($table)->insert($pivotTable); } } } }); $this->info('Sync complete'); }
public function loadReward() { $id = post('id'); return Reward::find($id); }
/** * @SWG\Definition( * definition="response.redeem", * required={"data"}, * @SWG\Property( * property="data", * type="object", * ref="#/definitions/redeem.payload" * ) * ) * * @SWG\Definition( * definition="redeem.payload", * required={"success", "message", "user"}, * @SWG\Property( * property="success", * type="boolean" * ), * @SWG\Property( * property="message", * type="string" * ), * @SWG\Property( * property="user", * type="object", * ref="#/definitions/user.info.points" * ) * ) * * * @SWG\GET( * path="rewards/redeem/{reward}/user/{user}", * description="Redeem user points for rewards", * summary="Redeem a reward", * tags={ "rewards"}, * * @SWG\Parameter( * description="ID of reward to redeem", * format="int64", * in="path", * name="reward", * required=true, * type="integer" * ), * @SWG\Parameter( * description="ID of user", * format="int64", * in="path", * name="user", * required=true, * type="integer" * ), * @SWG\Response( * response=200, * description="Unsuccessful response", * @SWG\Schema(ref="#/definitions/response.redeem") * ), * @SWG\Response( * response=201, * description="Successful response", * @SWG\Schema(ref="#/definitions/response.redeem") * ), * @SWG\Response( * response=500, * description="Unexpected error", * @SWG\Schema(ref="#/definitions/error500") * ), * @SWG\Response( * response=404, * description="User not found", * @SWG\Schema(ref="#/definitions/UserError404") * ) * ) */ public function redeemByGet($rewardId, $userId) { if (is_null($user = User::find($userId))) { return Response::api()->errorNotFound('User not found'); } if (is_null($reward = Reward::find($rewardId))) { return Response::api()->errorNotFound('Reward not found'); } RewardManager::redeem($rewardId, $user); // Check if redeem was successful $message = Session::pull('rewardMessage'); $type = $message ? 'info' : 'error'; $success = true; $httpCode = 201; if ($type == 'error') { $success = false; $httpCode = 200; $message = Session::pull('rewardError'); } // Get common user points format via UserProfileTransformer $userTransformer = new UserProfileTransformer(); $points = $userTransformer->getUserPoints($user); $payload = ['data' => ['success' => $success, 'message' => $message, 'user' => ['id' => $user->getKey(), 'points' => $points]]]; return Response::api()->setStatusCode($httpCode)->withArray($payload); }