modules/api/v1/controllers/ProductRatingController.php
/**
     * Displays a single ProductRating model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
Displays a single ProductRating model.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
Displays a single ProductRating model . @param integer $id @return mixed @throws NotFoundHttpException if the model cannot be found
public function actionView($id)
    {
        $model = ProductRating::find()->where(['product_id' => $id])->andWhere(['IN', 'status', [ProductRating::STATUS_PENDING, ProductRating::STATUS_APPROVE]])->all();
        $modelProduct = Product::findOne($id);
        if (!$modelProduct instanceof Product) {
            throw new NotFoundHttpException('Product doesn\'t exist.');
        }
        if (empty($model)) {
            throw new NotFoundHttpException('Product rating doesn\'t exist.');
        }
        $totalRatings = count($model);
        $ratings = [];
        $i = $j = $k = $l = $m = $sumRatings = $rating = 0;
        foreach ($model as $dataRatings) {
            if ($dataRatings->rating == 5) {
                $i++;
            }
            if ($dataRatings->rating == 4) {
                $j++;
            }
            if ($dataRatings->rating == 3) {
                $k++;
            }
            if ($dataRatings->rating == 2) {
                $l++;
            }
            if ($dataRatings->rating == 1) {
                $m++;
            }
            $sumRatings += $dataRatings->rating;
        }
        $ratings['5'] = $i;
        $ratings['4'] = $j;
        $ratings['3'] = $k;
        $ratings['2'] = $l;
        $ratings['1'] = $m;
        if ($totalRatings != 0) {
            $rating = $sumRatings / $totalRatings;
        }
        $ratings['averageRatings'] = number_format((float)$rating, 1, '.', '');
        return $ratings;
    }
public function actionView ( $ id ) { $ model = ProductRating :: find ( ) -> where ( [ 'product_id' => $ id ] ) -> andWhere ( [ 'IN' , 'status' , [ ProductRating :: STATUS_PENDING , ProductRating :: STATUS_APPROVE ] ] ) -> all ( ) ; $ modelProduct = Product :: findOne ( $ id ) ; if ( ! $ modelProduct instanceof Product ) { throw new NotFoundHttpException ( 'Product doesn\'t exist.' ) ; } if ( empty ( $ model ) ) { throw new NotFoundHttpException ( 'Product rating doesn\'t exist.' ) ; } $ totalRatings = count ( $ model ) ; $ ratings = [ ] ; $ i = $ j = $ k = $ l = $ m = $ sumRatings = $ rating = 0 ; foreach ( $ model as $ dataRatings ) { if ( $ dataRatings -> rating == 5 ) { $ i ++ ; } if ( $ dataRatings -> rating == 4 ) { $ j ++ ; } if ( $ dataRatings -> rating == 3 ) { $ k ++ ; } if ( $ dataRatings -> rating == 2 ) { $ l ++ ; } if ( $ dataRatings -> rating == 1 ) { $ m ++ ; } $ sumRatings += $ dataRatings -> rating ; } $ ratings [ '5' ] = $ i ; $ ratings [ '4' ] = $ j ; $ ratings [ '3' ] = $ k ; $ ratings [ '2' ] = $ l ; $ ratings [ '1' ] = $ m ; if ( $ totalRatings != 0 ) { $ rating = $ sumRatings / $ totalRatings ; } $ ratings [ 'averageRatings' ] = number_format ( ( float ) $ rating , 1 , '.' , '' ) ; return $ ratings ; }
Displays a single ProductRating model.
Displays a single ProductRating model .
// $ratings['averageRatings'] = (int)$rating;