public function rate(Request $request, $id) { $Quote = Quote::withCount(['ratesPlus', 'ratesMinus'])->find($id); if (null == $Quote) { return response()->json(['status' => 'error', 'message' => trans('app.not_found')]); } $count = $Quote->rates_plus_count - $Quote->rates_minus_count; $voted = $request->cookie(sha1('voted')); $voted = null == $voted ? [] : json_decode($voted); if (!empty($voted)) { if (in_array($id, $voted)) { return response()->json(['status' => 'error', 'message' => trans('app.twice_vote'), 'rate' => $count])->cookie(sha1('voted'), json_encode($voted), 60 * 60 * 24 * 120); } } $voted[] = $id; $Rate = Rate::where('quote_id', $id)->where('ip', $request->ip())->first(); if (null != $Rate) { return response()->json(['status' => 'error', 'message' => trans('app.twice_vote'), 'rate' => $count])->cookie(sha1('voted'), json_encode($voted), 60 * 60 * 24 * 120); } $rate = 1 == strpos($request->getPathInfo(), 'omg') ? 1 : (1 == strpos($request->getPathInfo(), 'wtf') ? -1 : 0); $Rate = new Rate(); $Rate->quote_id = $id; $Rate->ip = $request->ip(); $Rate->rate = $rate; $Rate->save(); $Quote->count += $rate; $Quote->save(); return response()->json(['status' => 'ok', 'message' => trans('app.voted'), 'rate' => $count + $rate])->cookie(sha1('voted'), json_encode($voted), 60 * 60 * 24 * 120); }
public function newrating() { $my_id = Auth::id(); $score = Input::get('score'); $date = Input::get('date'); $directing = Input::get('directing'); $lead_actors = Input::get('lead_actors'); $supporting_cast = Input::get('supporting_cast'); $music = Input::get('music'); $experience = Input::get('experience'); $mood = Input::get('mood'); $with = Input::get('with'); $new_rating = new Rate(); $new_rating->user_id = $my_id; $new_rating->movie_id = 42; $new_rating->rating = $score; $new_rating->date_watched = $date; $new_rating->directing = $directing; $new_rating->leading_actors = $lead_actors; $new_rating->supporting_cast = $supporting_cast; $new_rating->music = $music; $new_rating->experience = $experience; $new_rating->mood = $mood; $new_rating->watched_with = $with; $new_rating->save(); Alert::add("You rated a move successfully!"); return redirect()->route('rate'); }
/** * Creates a new Rate model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Rate(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Rate model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Rate(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { $currency_list = Currency::getCurrencyList(); return $this->render('create', ['model' => $model, 'currency' => $currency_list]); } }
public function rateByUser($user_id, $rate_score) { $rater = User::findOne(['id' => $user_id]); if ($rater) { $rate = Rate::findOne(['rated_id' => $this->id, 'rater_id' => intval($user_id)]); $rate_score = floatval($rate_score); if (!$rate && !$this->isNewRecord && 0 < $rate_score && 5 >= $rate_score) { $rate = new Rate(); $rate->rated_id = $this->id; $rate->rater_id = $rater->id; $rate->rate = $rate_score; $rate->save(); } } return false; }