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'); }
public function search($input) { $query = Rate::query(); $columns = Schema::getColumnListing('rates'); $attributes = array(); foreach ($columns as $attribute) { if (isset($input[$attribute]) and !empty($input[$attribute])) { $query->where($attribute, $input[$attribute]); $attributes[$attribute] = $input[$attribute]; } else { $attributes[$attribute] = null; } } return [$query->get(), $attributes]; }
public function actionGetRate() { $from_currency_id = Yii::$app->request->post('from_currency_id'); $to_currency_id = Yii::$app->request->post('to_currency_id'); if ($from_currency_id !== null and $to_currency_id !== null) { $rate = Rate::find()->where("from_currency_id = :from_currency_id", [':from_currency_id' => $from_currency_id])->andWhere("to_currency_id = :to_currency_id", [':to_currency_id' => $to_currency_id])->asArray()->one(); if ((bool) $rate) { $result = 'success'; } else { $result = 'Для выбранной пары валют нет возможности конвертации'; } } else { $result = ['result' => 'Методу не передан обязательный набор параметров']; $rate = ''; } \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ['rate' => $rate, 'result' => $result, 'from_currency_id' => $from_currency_id, 'to_currency_id' => $to_currency_id]; }
/** * @return \yii\db\ActiveQuery */ public function getRates0() { return $this->hasMany(Rate::className(), ['from_currency_id' => 'id']); }
/** * Finds the Rate model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Rate the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Rate::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
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; }