/**
  * Lists all ContentRecord models.
  * @return mixed
  */
 public function actionUpdate($like_status, $present_status, $id)
 {
     if ($like_status == $present_status) {
         return;
     }
     $likeOrDislike = LikeDislikeContent::findOne(['content' => $id, 'likedOrDislikedBy' => \Yii::$app->user->identity->getId()]);
     $likeOrDislike->likeOrDislike = $like_status;
     $likeOrDislike->save();
 }
 public function suggestionsForUsers()
 {
     $current_user = \Yii::$app->user->identity->getId();
     $followees = FollowerUsertoUser::find()->where(['follower_user_id' => $current_user])->all();
     if (empty($followees)) {
         $users = User::find()->all();
         $scoresOfEachUser = [];
         $models = [];
         foreach ($users as $user) {
             $followRecords = FollowerUsertoUser::find()->where(['followed_user_id' => $user->id]);
             $follows = $followRecords->count();
             $contents = ContentRecord::find()->where(['uploadedBy' => $user->id])->all();
             $contentsIds = ArrayHelper::getColumn($contents, 'id');
             $likeRecords = LikeDislikeContent::find()->where(['content' => $contentsIds, 'likeOrDislike' => 1]);
             $dislikeRecords = LikeDislikeContent::find()->where(['content' => $contentsIds, 'likeOrDislike' => 0]);
             $score = 2 * $follows + $likeRecords->count() - 0.5 * $dislikeRecords->count();
             $scoresOfEachUser[$user->id] = $score;
         }
         arsort($scoresOfEachUser);
         foreach (array_keys($scoresOfEachUser) as $id) {
             $user = User::findOne($id);
             array_push($models, $user);
         }
         return $models;
     } else {
         $targetUsers = [];
         foreach ($followees as $followee) {
             $followersOfFollowee = FollowerUsertoUser::find()->where(['followed_user_id' => $followee->followed_user_id])->all();
             foreach ($followersOfFollowee as $singleFollowerOfFollowee) {
                 $followeesOfFollowerOfFollowees = FollowerUsertoUser::find()->where(['follower_user_id' => $singleFollowerOfFollowee->follower_user_id])->all();
                 foreach ($followeesOfFollowerOfFollowees as $targetUser) {
                     array_push($targetUsers, $targetUser);
                 }
             }
         }
         $followeesIds = ArrayHelper::getColumn($followees, 'followed_user_id');
         $targetUsersIds = ArrayHelper::getColumn($targetUsers, 'followed_user_id');
         $resultUsersIds = array_diff($targetUsersIds, $followeesIds);
         $countOfUsers = array_count_values($resultUsersIds);
         $scoresOfEachUser = [];
         foreach (array_unique($resultUsersIds) as $userId) {
             $followersOfUser = FollowerUsertoUser::find()->where(['followed_user_id' => $userId])->all();
             $followeesOfUser = FollowerUsertoUser::find()->where(['follower_user_id' => $userId])->all();
             $followeesIdsOfUser = ArrayHelper::getColumn($followeesOfUser, 'followed_user_id');
             if (count($followeesOfUser) == 0) {
                 $firstWeight = count($followersOfUser);
             } else {
                 $firstWeight = count($followersOfUser) / count($followeesOfUser);
             }
             $secondWeight = $countOfUsers[$userId];
             $thirdWeight = array_intersect($followeesIdsOfUser, $followeesIds);
             $scoresOfEachUser[$userId] = ($firstWeight + $secondWeight + count($thirdWeight)) / 3;
         }
         $models = [];
         arsort($scoresOfEachUser);
         foreach (array_keys($scoresOfEachUser) as $id) {
             $user = User::findOne($id);
             array_push($models, $user);
         }
         return $models;
     }
 }
Пример #3
0
                                                    </a>
                                                </li>
                                                <li>
                                                    <a href="#"><i class="fa fa-thumbs-o-up"> </i>
                                                        <?php 
    $contents = \common\models\content\ContentRecord::find()->where(['uploadedBy' => $model->id])->all();
    $contentsIds = \yii\helpers\ArrayHelper::getColumn($contents, 'id');
    $likeRecords = \common\models\LikeDislikeContent::find()->where(['content' => $contentsIds, 'likeOrDislike' => 1]);
    echo $likeRecords->count();
    ?>
                                                    </a>
                                                </li>
                                                <li>
                                                    <a href="#"><i class="fa fa-thumbs-o-down"> </i>
                                                        <?php 
    $dislikeRecords = \common\models\LikeDislikeContent::find()->where(['content' => $contentsIds, 'likeOrDislike' => 0]);
    echo $dislikeRecords->count();
    ?>
                                                    </a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                <?php 
}
?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
 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()];
     }
 }
Пример #5
0
 public function getLikesDislikes()
 {
     return $this->hasMany(LikeDislikeContent::className(), ['content' => 'id']);
 }