function user_rating($source) { $value = ArticleRating::current($source->id); if ($value) { return $value; } return $this->default_rating(); }
function amp_badge_rating($options) { $queryd_item = isset($_GET['article_id']) && $_GET['article_id'] ? intval($_GET['article_id']) : 0; if ($queryd_item) { $article = new Article(AMP_Registry::getDbcon(), $queryd_item); $display = new Rating_Public_Display($article); return $display->execute(); } $rating = isset($_POST['rating']) && $_POST['rating'] ? intval($_POST['rating']) : 1; $posted_item = isset($_POST['article_id']) && $_POST['article_id'] ? intval($_POST['article_id']) : 0; $article = new Article(AMP_Registry::getDbcon(), $posted_item); $display = new Rating_Public_Display($article); if ($rating && $posted_item && AMP_SYSTEM_UNIQUE_VISITOR_ID && is_numeric($rating) && is_numeric($posted_item)) { //actually record the rating somewhere.... ArticleRating::create($posted_item, $rating); } return $display->execute(); }
function update($item_id, $rating_value) { if (!AMP_SYSTEM_UNIQUE_VISITOR_ID) { return false; } $session = AMP_SYSTEM_UNIQUE_VISITOR_ID; $articles_rated = AMP_lookup('article_ids_rated_by_session', $session); if (!isset($articles_rated[$item_id])) { return ArticleRating::create($item_id, $rating_value); } $rating_id = $articles_rated[$item_id]; $rating = new Rating(AMP_Registry::getDbcon(), $rating_id); if (!$rating->hasData()) { return false; } $rating->mergeData(array('rating' => $rating_value, 'updated_at' => date('Y-m-d h:i:s'))); $result = $rating->save(); AMP_lookup_clear_cached('article_ids_rated_by_session', $session); AMP_lookup_clear_cached('article_ratings_by_session', $session); AMP_lookup_clear_cached('article_ratings'); AMP_lookup_clear_cached('article_ratings_last_week'); AMP_lookup_clear_cached('article_ratings_last_month'); return $result; }
public function starRating($object_pk, $ratingAjax) { $object_pk = (int)$object_pk; $model = Article::model()->findByPk($object_pk); if($model){ $rating = ArticleRating::model()->findByAttributes(array( 'object'=>$object_pk )); // если не было рейтинга - делаем if(!$rating) { $rating = new ArticleRating; $rating->object = $object_pk; $rating->vote_count = 1; $rating->vote_sum = $ratingAjax; $rating->vote_average = round($rating->vote_sum / $rating->vote_count,2); $rating->save(false); $model->rating_id = $rating->id; $model->save(false,array('rating_id')); } else { $rating->vote_count = $rating->vote_count + 1; $rating->vote_sum = $rating->vote_sum + $ratingAjax; $rating->vote_average = round($rating->vote_sum / $rating->vote_count,2); if(!$rating->save()){ // VarDumper::dump($rating->errors); die(); // Ctrl + X Delete line } } } return true; }