/**
  * Get user  information
  * @param RainLab\User\Models\User $user
  * @return array
  */
 protected function getUserInfo($user)
 {
     // Get common user points format via UserProfileTransformer
     $userTransformer = new UserProfileTransformer();
     $points = $userTransformer->getUserPoints($user);
     return ['id' => $user->getKey(), 'points' => $points];
 }
 /**
  * @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);
 }
 /**
  * @SWG\Definition(
  *      definition="response.rate",
  *      required={"data"},
  *      @SWG\Property(
  *          property="data",
  *          type="object",
  *          ref="#/definitions/rate.payload"
  *      )
  * )
  * 
  * @SWG\Definition(
  *      definition="rate.payload",
  *      required={"success", "message", "user", "rating"},
  *      @SWG\Property(
  *          property="success",
  *          type="boolean"
  *      ),
  *      @SWG\Property(
  *          property="message",
  *          type="string"
  *      ),
  *      @SWG\Property(
  *          property="user",
  *          type="object",
  *          ref="#/definitions/user.info.points"
  *      ),
  *      @SWG\Property(
  *          property="rating",
  *          type="object",
  *          ref="#/definitions/rating.stats"
  *      )   
  * )
  * 
  * @SWG\Definition(
  *      definition="rating.stats",
  *      required={"total", "average", "object_type", "object_id"},
  *      @SWG\Property(
  *          property="total",
  *          type="number",
  *          format="float"
  *      ),
  *      @SWG\Property(
  *          property="average",
  *          type="number",
  *          format="float"
  *      ),
  *      @SWG\Property(
  *          property="object_type",
  *          type="string"
  *      ),
  *      @SWG\Property(
  *          property="object_id",
  *          type="integer",
  *          format="int32"
  *      )  
  * )
  * 
  * 
  * @SWG\GET(
  *     path="ratings/rate/{object}/{objectId}/user/{user}/{rate}",
  *     description="Rate an object",
  *     summary="Rate an object",
  *     tags={ "ratings"},
  *
  *     @SWG\Parameter(
  *         description="Object to rate",
  *         in="path",
  *         name="object",
  *         required=true,
  *         type="string",
  *         enum={"activity", "badge"}
  *     ),
  *     @SWG\Parameter(
  *         description="ID of object to rate",
  *         format="int64",
  *         in="path",
  *         name="objectId",
  *         required=true,
  *         type="integer"
  *     ),
  *     @SWG\Parameter(
  *         description="ID of user",
  *         format="int64",
  *         in="path",
  *         name="user",
  *         required=true,
  *         type="integer"
  *     ),     
  *     @SWG\Parameter(
  *         description="Rate value",
  *         format="float",
  *         in="path",
  *         name="rate",
  *         required=true,
  *         type="number",
  *         minimum=1,
  *         maximum=5
  *     ),  
  *     @SWG\Response(
  *         response=200,
  *         description="Successful response",
  *         @SWG\Schema(ref="#/definitions/response.rate")
  *     ),
  *     @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 addObjectRating($objectType, $objectId, $user, $rateValue, $comment = null)
 {
     if ($user = User::find($user)) {
         if ($instance = $this->getObject($objectType, $objectId)) {
             list($success, $rating) = $instance->addRating($user, $rateValue, $comment);
             // Get common user points format via UserProfileTransformer
             $userTransformer = new UserProfileTransformer();
             $points = $userTransformer->getUserPoints($user);
             $payload = ['data' => ['success' => $success, 'message' => "{$objectType} has been rate succesfully.", 'user' => ['id' => $user->getKey(), 'points' => $points], 'rating' => array_merge($instance->getRatingStats(), ['object_type' => $objectType, 'object_id' => intVal($objectId)])]];
             $httpCode = 201;
             if (!$success) {
                 $httpCode = 200;
                 $payload['data']['message'] = "User has already rate this {$objectType}";
             }
             return Response::api()->setStatusCode($httpCode)->withArray($payload);
         } else {
             return Response::api()->errorNotFound("{$object} not found");
         }
     } else {
         return Response::api()->errorNotFound('User not found');
     }
 }