Passed
Pull Request — master (#610)
by John
22:48
created

AccountModel::getSocialiteInfo()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 21
rs 9.5222
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Hash;
8
use App\Models\Eloquent\User;
9
use grubersjoe\BingPhoto;
10
use Cache;
11
use Storage;
12
13
class AccountModel extends Model
14
{
15
    public function generatePassword($length=8)
16
    {
17
        $chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
18
19
        $password='';
20
        for ($i=0; $i<$length; $i++) {
21
            $password.=$chars[mt_rand(0, strlen($chars)-1)];
22
        }
23
        return $password;
24
    }
25
26
    public function feed($uid=null)
27
    {
28
        $ret=[];
29
        $solution=DB::table("problem_solution")->join("problem", "problem.pid", "=", "problem_solution.pid")->where(["uid"=>$uid, "audit"=>1])->select("problem.pid as pid", "pcode", "title", "problem_solution.created_at as created_at")->orderBy("problem_solution.created_at", "DESC")->get()->all();
30
        foreach ($solution as &$s) {
31
            $s["type"]="event";
32
            $s["color"]="wemd-orange";
33
            $s["icon"]="comment-check-outline";
34
            $ret[]=$s;
35
        }
36
        return $ret;
37
    }
38
39
    public function generateContestAccount($cid, $ccode, $num)
40
    {
41
        $ret=[];
42
        $starting=DB::table("users")->where('prefix', '=', $ccode)->count();
43
        $contestModel=new ContestModel();
44
        for ($i=1; $i<=$num; $i++) {
45
            $pass=$this->generatePassword();
46
            $name=strtoupper($ccode).str_pad($starting+$i, 3, "0", STR_PAD_LEFT);
47
            $uid=$this->add([
48
                'name' => $name,
49
                'email' => "[email protected]",
50
                'email_verified_at' => date("Y-m-d H:i:s"),
51
                'password' => $pass,
52
                'avatar' => "/static/img/avatar/default.png",
53
                'contest_account' => $cid,
54
                'prefix' => $ccode,
55
            ]);
56
            $contestModel->grantAccess($uid, $cid, 1);
57
            $ret[]=[
58
                "uid"=>$uid,
59
                "name"=>$name,
60
                "email"=>"[email protected]",
61
                "password"=>$pass
62
            ];
63
        }
64
        return $ret;
65
    }
66
67
    public function getContestAccount($cid)
68
    {
69
        return DB::table('users')->where(["contest_account"=>$cid])->get()->all();
70
    }
71
72
    public function add($data)
73
    {
74
        return DB::table("users")->insertGetId([
75
            'name' => $data['name'],
76
            'email' => $data['email'],
77
            'email_verified_at' => $data["email_verified_at"],
78
            'password' => Hash::make($data['password']),
79
            'avatar' => $data["avatar"],
80
            'contest_account' => $data["contest_account"],
81
            'remember_token'=>null,
82
            'prefix' => $data["prefix"],
83
            'created_at'=>date("Y-m-d H:i:s"),
84
            'updated_at'=>date("Y-m-d H:i:s")
85
        ]);
86
    }
87
88
    public function detail($uid)
89
    {
90
        if (filter_var($uid, FILTER_VALIDATE_INT)===false) {
91
            return null;
92
        }
93
        $ret=DB::table("users")->where(["id"=>$uid])->first();
94
        if (empty($ret)) {
95
            return null;
96
        }
97
        $ret["uid"]=$uid;
98
        $ret["submissionCount"]=DB::table("submission")->where([
99
            "uid"=>$uid,
100
        ])->whereNotIn('verdict', [
101
            'System Error',
102
            'Submission Error'
103
        ])->count();
104
        $ret["solved"]=DB::table("submission")->where([
105
            "uid"=>$uid,
106
            "verdict"=>"Accepted"
107
        ])->join("problem", "problem.pid", "=", "submission.pid")->select('pcode')->distinct()->get()->all();
108
        $ret["solvedCount"]=count($ret["solved"]);
109
        // Casual
110
        $ret["rank"]=Cache::tags(['rank', $ret["id"]])->get("rank", "N/A");
111
        $ret["rankTitle"]=Cache::tags(['rank', $ret["id"]])->get("title", "Recruit");
112
        $ret["rankTitleColor"]=RankModel::getColor($ret["rankTitle"]);
113
        // Professional
114
        $ret["professionalTitle"]=RankModel::getProfessionalTitle($ret["professional_rate"]);
115
        $ret["professionalTitleColor"]=RankModel::getProfessionalColor($ret["professionalTitle"]);
116
        // Administration Group
117
        $ret["admin"]=User::find($uid)->hasPermission(1);
118
        if (Cache::tags(['bing', 'pic'])->get(date("Y-m-d"))==null) {
119
            $bing=new BingPhoto([
120
                'locale' => 'zh-CN',
121
            ]);
122
            Storage::disk('NOJPublic')->put("static/img/bing/".date("Y-m-d").".jpg", file_get_contents($bing->getImage()['url']), 86400);
123
            Cache::tags(['bing', 'pic'])->put(date("Y-m-d"), "/static/img/bing/".date("Y-m-d").".jpg");
124
        }
125
        $ret["image"]=Cache::tags(['bing', 'pic'])->get(date("Y-m-d"));
126
        return $ret;
127
    }
128
}
129