public function actionFollowedprograms($id) { $followings = FollowerProgram::find()->where(['user_id' => $id])->all(); $following_programs_ids = ArrayHelper::getColumn($followings, 'program_id'); $query = ProgramRecord::find()->where(['id' => $following_programs_ids]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count()]); $models = $query->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('showFollowedPrograms', ['models' => $models, 'caller' => $id]); }
public function suggestionsForPrograms() { $current_user = \Yii::$app->user->identity->getId(); $programsFollowedByCurrentUser = FollowerProgram::find()->where(['user_id' => $current_user])->all(); if (empty($programsFollowedByCurrentUser)) { $programs = ProgramRecord::find()->all(); $scoresOfEachProgram = []; $models = []; foreach ($programs as $program) { $follows = $program->noOfFollowers; $likes = $program->noOLikes; $dislikes = $program->noOfDislikes; $score = 2 * $follows + $likes - 0.5 * $dislikes; $scoresOfEachProgram[$program->id] = $score; } arsort($scoresOfEachProgram); foreach (array_keys($scoresOfEachProgram) as $id) { $program = ProgramRecord::findOne($id); array_push($models, $program); } return $models; } else { $targetPrograms = []; foreach ($programsFollowedByCurrentUser as $followedProgram) { $allFollowersOfFollowedProgram = FollowerProgram::find()->where(['program_id' => $followedProgram->program_id])->all(); foreach ($allFollowersOfFollowedProgram as $singleFollowerOfFollowee) { $followeesOfFollowerOfFolloweesOfCurrentUser = FollowerProgram::find()->where(['user_id' => $singleFollowerOfFollowee->user_id])->all(); foreach ($followeesOfFollowerOfFolloweesOfCurrentUser as $targetProgram) { array_push($targetPrograms, $targetProgram); } } } $followingProgramsIds = ArrayHelper::getColumn($programsFollowedByCurrentUser, 'program_id'); $targetProgramsIds = ArrayHelper::getColumn($targetPrograms, 'program_id'); $resultProgramsIds = array_diff($targetProgramsIds, $followingProgramsIds); $countOfPrograms = array_count_values($resultProgramsIds); $scoresOfEachProgram = []; foreach (array_unique($resultProgramsIds) as $programId) { $followersOfProgram = FollowerProgram::find()->where(['program_id' => $programId])->all(); $firstWeight = count($followersOfProgram); $secondWeight = $countOfPrograms[$programId]; $scoresOfEachProgram[$programId] = ($firstWeight + $secondWeight) / 2; } $models = []; arsort($scoresOfEachProgram); foreach (array_keys($scoresOfEachProgram) as $id) { $program = ProgramRecord::findOne($id); array_push($models, $program); } return $models; } }
<?php $facultyRecord = \common\models\faculty\FacultyRecord::findOne($model->faculty_id); $faculty = $facultyRecord->name; $university = \common\models\university\UniversityRecord::findOne($facultyRecord->university_id)->name; $link_program = '<h5 class="title" style="font-size: 20px; margin-left: -37%; margin-top: 10%;">' . $model->name . '</h5>'; echo Html::a($link_program, ['/course/index', 'program_id' => $model->id, 'faculty' => $faculty, 'university' => $university]); ?> </div> <div class="col-md-4"> <?php $follow_status = 'Unfollow'; $btn_follow = '<i class="fa fa-minus-circle"></i>'; $follow_btn_class = ''; if (count(\common\models\FollowerProgram::find()->where(['user_id' => \Yii::$app->user->identity->getId(), 'program_id' => $model->id])->all()) == 0) { $follow_status = 'Follow'; $btn_follow = '<i class="fa fa-user-plus"></i>'; } ?> <button type="button" style="" class="btn btn-border-success btn-block btn-follow-program <?php echo $follow_btn_class; ?> " id="program-<?php echo $model->id; ?> " data-id="<?php echo $model->id; ?> " data-follow-status="<?php echo $follow_status;