Пример #1
0
 /**
  * @param $id
  * @param $rating
  * @return string
  */
 public function actionSet($id, $rating)
 {
     $recordId = intval($id);
     $ratingValue = intval($rating);
     \Yii::$app->response->format = Response::FORMAT_JSON;
     if (!$this->_validationRatingValue($rating)) {
         return $this->_sendErrorResponse("Hey-hey-hey, fella, take it easy… No need to abuse our confidence.");
     }
     if (!$this->_validateRecordId($recordId)) {
         return $this->_sendErrorResponse("Hey-hey-hey, fella, take it easy… No need to abuse our confidence.");
     }
     if (!$id || !$ratingValue || !\Yii::$app->request->isAjax) {
         return $this->_sendErrorResponse("Hey-hey-hey, fella, take it easy… No need to abuse our confidence.");
     }
     if ($this->_checkVote($recordId)) {
         return $this->_sendErrorResponse("C'mon, lad, ya've already voted");
     }
     $ratingModel = new Rating();
     $ratingModel->record_id = $id;
     $ratingModel->rating = $rating;
     $ratingModel->user_id = \Yii::$app->user->id;
     if ($ratingModel->save()) {
         $this->_createRatingAddEvent($id, $rating);
         return $this->_sendSuccessResponse("We have got your vote!", ['rating' => $rating]);
     }
     return $this->_sendErrorResponse("Arrr! Can't get yer vote. Try again later, lad!");
 }