public function run() { if (isset($this->model->rating)) { $rating = $this->model->rating; } else { $rating = Rating::getValue($this->model); $rating = Rating::getHtml($rating); } $model_id = get_class($this->model); $value = null; if (!Yii::app()->user->isGuest) { $value = Rating::model()->fetchScalarByAttributes(array('user_id' => Yii::app()->user->id, 'object_id' => $this->model->id, 'model_id' => $model_id), 'value'); } $this->render('RatingWidget', array('object_id' => $this->model->id, 'user_id' => $this->model->getUserId(), 'model_id' => $model_id, 'rating' => $rating, 'value' => $value)); }
public function actionCreate() { if (!isset($_POST['Rating'])) { $this->badRequest(); } $rating = Rating::model()->findByAttributes(array('user_id' => Yii::app()->user->id, 'object_id' => $_POST['Rating']['object_id'], 'model_id' => $_POST['Rating']['model_id'])); if (!$rating) { $rating = new Rating(); } $rating->attributes = $_POST['Rating']; if ($rating->save()) { $rating = Rating::getValue($rating->model_id, $rating->object_id); echo Rating::getHtml($rating); } else { echo CJSON::encode(array('errors' => $rating->errors_flat_array)); } }
/** * Return user mark for the good * @return string user mark for the good */ public function GetUserRating() { $value = Rating::model()->findByAttributes(array('good_id' => $this->id, 'user_id' => Yii::app()->user->id)); //echo $value; if (isset($value)) { return sprintf("%d", $value->value); } else { return '0'; } }
/** * Set mark for the good by user * @param $good_id * @param $rate mark (1-5) */ public function actionSetMark($good_id, $rate) { if (Yii::app()->user->isGuest) { echo 'you are not registered user'; return; } $user = Yii::app()->user->id; $rating = Rating::model()->with(array('good', 'good.marksCount'))->findByAttributes(array('good_id' => $good_id, 'user_id' => $user)); if (empty($rating)) { if ($rate == 'undefined') { return; } $rating = new Rating(); $rating->good_id = $good_id; $rating->user_id = $user; $rating->value = $rate; if ($rating->save()) { $good = $rating->good; $good->rating = round($good->rating + ($rate * 100 - $good->rating) / ($good->marksCount + 1)); $good->save(); echo 'success'; } else { echo 'fail saving'; } } else { if ($rating->value != $rate) { if ($rate == 'undefined') { echo 'rate deleted'; $rating->delete(); return; } $old_mark = $rating->value; $rating->value = $rate; if ($rating->save()) { $good = $rating->good; $good->rating = round($good->rating + ($rate * 100 - $old_mark * 100) / $good->marksCount); $good->save(); echo 'success'; } else { echo 'fail saving'; } } else { echo 'no need to change'; } } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Rating::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function afterSave() { $criteria = new CDbCriteria(); $criteria->select = new CDbExpression('AVG(rating_score) as avg_rating'); $criteria->condition = 'product_id=:product_id'; $criteria->params = array(':product_id' => $this->product_id); $model = Rating::model()->find($criteria); //echo $model->avg_rating ;//die();//var_dump($model); die(); //$modelProduct=Product::model()->with('rates')->findByPk($bid); //findByPk($bid); $values = array('rating_score' => $model->avg_rating); $modelProduct = Product::model()->updateByPk($this->product_id, $values); return parent::afterSave(); }
/** * Save rating if need * @return void */ public function afterSave() { $rating = Rating::model()->findByAttributes(array('good_id' => $this->good_id, 'user_id' => $this->user_id)); if ($rating == null) { $rating = new Rating(); $rating->value = $this->rating; $rating->good_id = $this->good_id; $rating->user_id = $this->user_id; $rating->Save(); } elseif ($rating->value != $this->rating) { $rating->value = $this->rating; $rating->Save(); } }