Exemple #1
0
 /**
  * Creates a new Items model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionSetrating($id, $value)
 {
     if ($value <= 5) {
         $uid = Yii::$app->user->identity->getId();
         $itemsRating = ItemsRating::getItemRating($id);
         $is_find = 0;
         for ($i = 0; $i < count($itemsRating->getModels()); $i++) {
             //            echo $itemsRating->getModels()[$i]['user_id']."---". $uid;
             echo $itemsRating->getModels()[$i]['id'];
             if ($itemsRating->getModels()[$i]['user_id'] == $uid) {
                 $is_find = 1;
                 break;
             } else {
                 $is_find = 0;
             }
         }
         if ($is_find == 0) {
             //                echo $itemsRating->getModels()[$i]['user_id'] . "---" . $uid;
             Yii::$app->db->createCommand('INSERT INTO `itemsRating` (`id`, `item_id`, `user_id`, `rating`)   VALUES (NULL,' . $id . ',' . $uid . ', 1)')->execute();
             $allRatings = Yii::$app->db->createCommand('SELECT avg(rating) FROM `itemsRating` WHERE item_id=' . $id)->queryAll();
             $itemRating = $allRatings[0]['avg(rating)'];
             Yii::$app->db->createCommand('UPDATE `items` SET `items`.rating=' . $itemRating . ' WHERE items.id =' . $id)->execute();
         } else {
             Yii::$app->db->createCommand('UPDATE `itemsRating` SET
               itemsRating.id=' . $itemsRating->getModels()[$i]['id'] . ',
               itemsRating.user_id=' . $uid . ',
               itemsRating.item_id=' . $id . ',
               itemsRating.rating=' . $value . '     WHERE itemsRating.id =' . $itemsRating->getModels()[$i]['id'])->execute();
             $allRatings = Yii::$app->db->createCommand('SELECT avg(rating) FROM `itemsRating` WHERE item_id=' . $id)->queryAll();
             $itemRating = $allRatings[0]['avg(rating)'];
             Yii::$app->db->createCommand('UPDATE `items` SET `items`.rating=' . $itemRating . ' WHERE items.id =' . $id)->execute();
         }
         return $this->redirect(['view', 'id' => $id]);
     } else {
         return $this->redirect(['view', 'id' => $id]);
     }
 }
Exemple #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getItemsRatings()
 {
     return $this->hasMany(ItemsRating::className(), ['item_id' => 'id']);
 }