UserRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\User;
  4. use DB;
  5. class UserRepository extends ResourceRepository {
  6. //protected $user;
  7. public function __construct(User $user) {
  8. $this->user = $user;
  9. }
  10. private function save(User $user, Array $inputs) {
  11. $user->name = $inputs['name'];
  12. $user->email = $inputs['email'];
  13. $user->admin = isset($inputs['admin']);
  14. $user->save();
  15. }
  16. public function getPaginate($n) {
  17. return $this->user->paginate($n);
  18. }
  19. public function store(Array $inputs) {
  20. $user = new $this->user;
  21. $user->password = bcrypt($inputs['password']);
  22. $this->save($user, $inputs);
  23. return $user;
  24. }
  25. public function getById($id) {
  26. return $this->user->findOrFail($id);
  27. }
  28. public function update($id, Array $inputs) {
  29. $this->save($this->getById($id), $inputs);
  30. }
  31. public function update_confidence_score($user_id, $new_confidence_score) {
  32. User::where('id', $user_id)
  33. ->update(['score' => $new_confidence_score]);
  34. }
  35. public function get_best_users_by_real_score() {
  36. return User::join("annotations", function($join) {
  37. $join->on("annotations.user_id", "=", "users.id");
  38. })
  39. ->select(DB::raw('count(*)*score as real_score, users.*'))
  40. ->groupBy('users.id')
  41. ->orderBy('real_score', 'desc')
  42. ->where('is_admin', '=', '0')->take(5)->get();
  43. }
  44. public function get_best_users_by_quantity() {
  45. return User::join("annotations", function($join) {
  46. $join->on("annotations.user_id", "=", "users.id");
  47. })
  48. ->select(DB::raw('count(*) as quantity, users.*'))
  49. ->groupBy('users.id')
  50. ->orderBy('quantity', 'desc')
  51. ->where('is_admin', '=', '0')
  52. ->take(5)->get();
  53. }
  54. public function get_level_by_id($user_id) {
  55. return User::select('level')->where('id', $user_id)->first()->level;
  56. }
  57. public function get_user_count() {
  58. return User::select(DB::raw('count(*) as count'))->first();
  59. }
  60. public function destroy($id) {
  61. $this->getById($id)->delete();
  62. }
  63. }