コード例 #1
0
 public function actionUpdate()
 {
     if (Yii::$app->request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $data = Yii::$app->request->post();
         $like_status = $data['like_status'];
         $present_status = $data['present_status'];
         $id = $data['id'];
         //echo $data;
         //for finding program of content
         $topic_id = ContentRecord::findOne($id)->topic_id;
         $course_id = TopicRecord::findOne($topic_id)->course_id;
         $program_id = CourseRecord::findOne($course_id)->program_id;
         $program = ProgramRecord::findOne($program_id);
         //end of finding program of content
         if ($like_status != $present_status) {
             if ($like_status == -1) {
                 $likeOrDislike = new LikeDislikeContent();
                 $likeOrDislike->content = $id;
                 $likeOrDislike->likedOrDislikedBy = \Yii::$app->user->identity->getId();
                 $likeOrDislike->likeOrDislike = $present_status;
                 $likeOrDislike->save();
                 //for program like counter
                 if ($present_status == 0) {
                     $program->noOfDislikes += 1;
                 } else {
                     $program->noOLikes += 1;
                 }
                 $program->save();
             } else {
                 $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
                 $likeOrDislike->likeOrDislike = $present_status;
                 $likeOrDislike->save();
                 //for program counter
                 if ($present_status == 0) {
                     $program->noOfDislikes += 1;
                     $program->noOLikes -= 1;
                 } else {
                     $program->noOfDislikes -= 1;
                     $program->noOLikes += 1;
                 }
                 $program->save();
             }
         } else {
             $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
             $likeOrDislike->delete();
             //for program like counter
             if ($present_status == 0) {
                 $program->noOfDislikes -= 1;
             } else {
                 $program->noOLikes -= 1;
             }
             $program->save();
         }
         $likes = LikeDislikeContent::find()->where(['content' => $id, 'likeOrDislike' => 1]);
         $dislikes = LikeDislikeContent::find()->where(['content' => $id, 'likeOrDislike' => 0]);
         $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
         if ($likeOrDislike == null) {
             $new_like_status = -1;
         } else {
             $new_like_status = $likeOrDislike->likeOrDislike;
         }
         return ['new_like_status' => $new_like_status, 'new_id' => $id, 'likes' => $likes->count(), 'dislikes' => $dislikes->count()];
     }
 }
コード例 #2
0
 public function actionUpdate()
 {
     if (Yii::$app->request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $data = Yii::$app->request->post();
         $like_status = $data['like_status'];
         $present_status = $data['present_status'];
         $id = $data['id'];
         //echo $data;
         if ($like_status != $present_status) {
             if ($like_status == -1) {
                 $likeOrDislike = new LikeDislikeContent();
                 $likeOrDislike->content = $id;
                 $likeOrDislike->likedOrDislikedBy = \Yii::$app->user->identity->getId();
                 $likeOrDislike->likeOrDislike = $present_status;
                 $likeOrDislike->save();
             } else {
                 $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
                 $likeOrDislike->likeOrDislike = $present_status;
                 $likeOrDislike->save();
             }
         } else {
             $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
             $likeOrDislike->delete();
         }
         $likes = LikeDislikeContent::find()->where(['content' => $id, 'likeOrDislike' => 1]);
         $dislikes = LikeDislikeContent::find()->where(['content' => $id, 'likeOrDislike' => 0]);
         $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
         if ($likeOrDislike == null) {
             $new_like_status = -1;
         } else {
             $new_like_status = $likeOrDislike->likeOrDislike;
         }
         return ['new_like_status' => $new_like_status, 'new_id' => $id, 'likes' => $likes->count(), 'dislikes' => $dislikes->count()];
     }
 }