public function actionAddfollower()
 {
     if (Yii::$app->request->isAjax) {
         $return_message = -1;
         $data = Yii::$app->request->post();
         $followed_id = $data['followed_id'];
         $follow_status = $data['follow_status'];
         if ($follow_status == 'Follow') {
             $follow = new FollowerUsertoUser();
             $follow->followed_user_id = $followed_id;
             $follow->follower_user_id = \Yii::$app->user->identity->getId();
             if ($follow->save()) {
                 $return_message = 0;
             }
         } else {
             $follow_record = FollowerUsertoUser::findOne(['followed_user_id' => $followed_id, 'follower_user_id' => \Yii::$app->user->identity->getId()]);
             if ($follow_record->delete()) {
                 $return_message = 1;
             }
         }
         return $return_message;
     }
 }
 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;
     }
 }
Example #3
0
                    </div>
                    <div class="col-sm-6">
                        <p>
                            <strong class="col-sm-6">
                                <?php 
echo Html::a($followed_programs->count(), ['/follow/followedprograms', 'id' => $id]);
?>
                                <small> Programs</small>
                            </strong>
                        </p>

                        <?php 
$follow_status = 'Unfollow';
$btn_follow = '<i class="fa fa-minus-circle"></i>  Unfollow';
$follow_btn_class = '';
if (count(\common\models\FollowerUsertoUser::find()->where(['followed_user_id' => $id, 'follower_user_id' => \Yii::$app->user->identity->getId()])->all()) == 0) {
    $follow_status = 'Follow';
    $btn_follow = '<i class="fa fa-user-plus"></i>  Follow';
}
if ($id == \Yii::$app->user->identity->getId()) {
    $follow_btn_class = 'disabled';
}
?>
                        <p class="col-sm-6">
                            <button type="button" style="margin-top: -11%;margin-left:-22px;" class="btn btn-border-success btn-follow <?php 
echo $follow_btn_class;
?>
" id="<?php 
echo $id;
?>
" data-id="<?php 
    ?>
"><?php 
    echo $btn_follow;
    ?>
</span>
                                                </button>
                                            </div>
                                            <ul class="list-inline item-details">
                                                <li><i class="fa fa-clock-o"><?php 
    echo Yii::t('user', '{0, date}', $model->created_at);
    ?>
</i></li>
                                                <li>
                                                    <a href="#"><i class="fa fa-users">
                                                            <?php 
    echo \common\models\FollowerUsertoUser::find()->where(['followed_user_id' => $model->id])->count();
    ?>
</i>
                                                    </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>