/** * Update Rating * * @Rest\View */ public function updateRatingAction() { // update rating can only auth user $user = $this->get('security.context')->getToken()->getUser(); // user can be anon. if ($user == "anon.") { return array('success' => false, 'errorStr' => $this->get('translator')->trans("validation.errors.user.You should auth at first")); } // Collect Data $restaurantId = $this->getRequest()->get('restaurantId'); $rating = $this->getRequest()->get('rating'); $restaurant = $this->getRestaurantManager()->findOneById($restaurantId); if (!$restaurant instanceof Restaurant) { return array("success" => false, "errorStr" => $this->get('translator')->trans('validation.errors.restaurant.Restaurant not found')); } if (is_null($rating)) { return array("success" => false, "errorStr" => $this->get('translator')->trans('validation.errors.restaurant.tableOrder.Rating not found')); } // check if user can change rating $userRating = $this->getRatingStatManager()->getUserRestaurantRating($user->getId()); // Also this restaurant shouldn't have rating today foreach ($userRating as $userRate) { if ($restaurantId == $userRate->getRestaurant()->getId()) { return array('success' => false, 'errorStr' => $this->get('translator')->trans("validation.errors.restaurant.tableOrder.Rating is has already set")); } } // only 3 rates if (count($userRating) > 2) { return array('success' => false, 'errorStr' => $this->get('translator')->trans("validation.errors.restaurant.tableOrder.Rating is disabled")); } // Update Restaurant Rating $em = $this->getDoctrine()->getManager(); $newRating = round(($restaurant->getRating() + $rating) / 2); $restaurant->setRating($newRating); $em->persist($restaurant); $em->flush(); // Update Rating Stat $ratingStat = $this->getRatingStatManager()->getUser2RestaurantRating($user->getId(), $restaurant->getId()); if (empty($ratingStat)) { $ratingStat = new RatingStat(); $ratingStat->setUser($user); $ratingStat->setRestaurant($restaurant); } $ratingStat->setLastUpdateTime(new \DateTime()); $ratingStat->setRating($rating); $em->persist($ratingStat); $em->flush(); return array('success' => true, 'rating' => $newRating); }
/** * Update rating * * @param Request $request * */ public function updateRestaurantRatingAction(Request $request) { // Collect Data $restaurantId = $request->request->get('restaurantId'); $rating = $request->request->get('value'); $objId = $request->request->get('objId'); // get Current user $user = $this->container->get('security.context')->getToken()->getUser(); $restaurant = $this->getRestaurantManager()->findOneById($restaurantId); // Update Restaurant Rating $em = $this->getDoctrine()->getManager(); $newRating = round(($restaurant->getRating() + $rating) / 2); $restaurant->setRating($newRating); $em->persist($restaurant); $em->flush(); // Update Rating Stat $ratingStat = $this->getRatingStatManager()->getUser2RestaurantRating($user->getId(), $restaurant->getId()); if (empty($ratingStat)) { $ratingStat = new RatingStat(); $ratingStat->setUser($user); $ratingStat->setRestaurant($restaurant); } $ratingStat->setLastUpdateTime(new \DateTime()); $ratingStat->setRating($rating); $em->persist($ratingStat); $em->flush(); // check if user can change rating $isRatingDisabled = false; $userRating = $this->getRatingStatManager()->getUserRestaurantRating($user->getId()); // only 3 state if (count($userRating) > 2) { $isRatingDisabled = true; } // Also we should get restaurants array , who has already have rating today $restaurantsWhoHadHasAlreadyRating = array(); foreach ($userRating as $restRating) { $restaurantsWhoHadHasAlreadyRating[] = $restRating->getRestaurant()->getId(); } /* THIS INFORMATION SHOULD BE IN EACH CONTROLLER BECAUSE WE USE IT IN HEADER */ // get city list $cityList = $this->getCityManager()->findAll(); /// get all category list $categoryList = $this->getRestaurantCategoryManager()->getCategories(); // get all kitchen list $kitchenList = $this->getRestaurantKitchenManager()->getKitchens(); // get current city $searchCity = $this->getRequest()->query->get('searchCity'); // if null set default -> krasnodar if (is_null($searchCity)) { $searchCity = 1; } /* * ** */ // registration form (header) $regForm = $this->container->get('fos_user.registration.form'); return $this->render('TableRestaurantBundle:Default:rating.html.twig', array('restaurant' => $restaurant, 'isRatingDisabled' => $isRatingDisabled, 'restaurantsWhoHadHasAlreadyRating' => $restaurantsWhoHadHasAlreadyRating, 'id' => $objId, 'cityList' => $cityList, 'categoryList' => $categoryList, 'kitchenList' => $kitchenList, 'searchCity' => $searchCity, 'formReg' => $regForm->createView())); }