Passed
Push — master ( cacb65...2a19cd )
by John
03:23
created

SubmissionModel::formatSubmitTime()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 9
nop 1
dl 0
loc 31
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
8
class SubmissionModel extends Model
9
{
10
    protected $tableName='submission';
11
    public $colorScheme=[
12
        "Waiting"                => "wemd-blue-text",
13
        "Judge Error"            => "wemd-black-text",
14
        "System Error"           => "wemd-black-text",
15
        "Compile Error"          => "wemd-orange-text",
16
        "Runtime Error"          => "wemd-red-text",
17
        "Wrong Answer"           => "wemd-red-text",
18
        "Time Limit Exceed"      => "wemd-deep-purple-text",
19
        "Real Time Limit Exceed" => "wemd-deep-purple-text",
20
        "Accepted"               => "wemd-green-text",
21
        "Memory Limit Exceed"    => "wemd-deep-purple-text",
22
        "Presentation Error"     => "wemd-red-text",
23
        "Submitted"              => "wemd-blue-text",
24
        "Pending"                => "wemd-blue-text",
25
        "Judging"                => "wemd-blue-text",
26
        "Partially Accepted"     => "wemd-cyan-text",
27
        'Submission Error'       => 'wemd-black-text',
28
        'Output Limit Exceeded'  => 'wemd-deep-purple-text',
29
        "Idleness Limit Exceed"  => 'wemd-deep-purple-text'
30
    ];
31
32
    public function insert($sub)
33
    {
34
        if (strlen($sub['verdict'])==0) {
35
            $sub['verdict']="Judge Error";
36
        }
37
38
        $sid=DB::table($this->tableName)->insertGetId([
39
            'time' => $sub['time'],
40
            'verdict' => $sub['verdict'],
41
            'solution' => $sub['solution'],
42
            'language' => $sub['language'],
43
            'submission_date' => $sub['submission_date'],
44
            'memory' => $sub['memory'],
45
            'uid' => $sub['uid'],
46
            'pid' => $sub['pid'],
47
            'cid' => $sub['cid'],
48
            'color' => $this->colorScheme[$sub['verdict']],
49
            'remote_id'=>$sub['remote_id'],
50
            'compile_info'=>"",
51
            'coid'=>$sub['coid'],
52
            'score'=>$sub['score']
53
        ]);
54
55
        return $sid;
56
    }
57
58
    public function getJudgeStatus($sid)
59
    {
60
        $status=DB::table($this->tableName)->where(['sid'=>$sid])->first();
61
        return $status;
62
    }
63
64
    public function getProblemStatus($pid, $uid, $cid=null)
65
    {
66
        if ($cid) {
67
            $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
68
            // Get the very first AC record
69
            $ac=DB::table($this->tableName)->where([
70
                'pid'=>$pid,
71
                'uid'=>$uid,
72
                'cid'=>$cid,
73
                'verdict'=>'Accepted'
74
            ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first();
75
            if (empty($ac)) {
76
                $pac=DB::table($this->tableName)->where([
77
                    'pid'=>$pid,
78
                    'uid'=>$uid,
79
                    'cid'=>$cid,
80
                    'verdict'=>'Partially Accepted'
81
                ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first();
82
                return empty($pac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first() : $pac;
83
            } else {
84
                return $ac;
85
            }
86
        } else {
87
            $ac=DB::table($this->tableName)->where([
88
                'pid'=>$pid,
89
                'uid'=>$uid,
90
                'cid'=>$cid,
91
                'verdict'=>'Accepted'
92
            ])->orderBy('submission_date', 'desc')->first();
93
            return empty($ac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->first() : $ac;
94
        }
95
    }
96
97
    public function getProblemSubmission($pid, $uid, $cid=null)
98
    {
99
        $statusList=DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->limit(10)->get()->all();
100
        return $statusList;
101
    }
102
103
    public function countSolution($s)
104
    {
105
        return DB::table($this->tableName)->where(['solution'=>$s])->count();
106
    }
107
108
    public function getWaitingSubmission()
109
    {
110
        return DB::table($this->tableName)  ->join('problem', 'problem.pid', '=', 'submission.pid')
111
                                            ->select("sid", "OJ as oid", "remote_id", "cid")
112
                                            ->where(['verdict'=>'Waiting'])
113
                                            ->get();
114
    }
115
116
    public function countWaitingSubmission($oid)
117
    {
118
        return DB::table($this->tableName)  ->join('problem', 'problem.pid', '=', 'submission.pid')
119
                                            ->where(['verdict'=>'Waiting', 'OJ'=>$oid])
120
                                            ->count();
121
    }
122
123
    public function updateSubmission($sid, $sub)
124
    {
125
        if (isset($sub['verdict'])) $sub["color"]=$this->colorScheme[$sub['verdict']];
126
        return DB::table($this->tableName)->where(['sid'=>$sid])->update($sub);
127
    }
128
129
    public function formatSubmitTime($date)
130
    {
131
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
132
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
133
134
        $now=time();
135
        $unix_date=strtotime($date);
136
137
        if (empty($unix_date)) {
138
            return "Bad date";
139
        }
140
141
        if ($now>$unix_date) {
142
            $difference=$now-$unix_date;
143
            $tense="ago";
144
        } else {
145
            $difference=$unix_date-$now;
146
            $tense="from now";
147
        }
148
149
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
150
            $difference/=$lengths[$j];
151
        }
152
153
        $difference=round($difference);
154
155
        if ($difference!=1) {
156
            $periods[$j].="s";
157
        }
158
159
        return "$difference $periods[$j] {$tense}";
160
    }
161
162
    public function getRecord()
163
    {
164
        $paginator=DB::table("submission")->where([
165
            'cid'=>null
166
        ])->join(
167
            "users",
168
            "users.id",
169
            "=",
170
            "submission.uid"
171
        )->select(
172
            "sid",
173
            "uid",
174
            "pid",
175
            "name",
176
            "color",
177
            "verdict",
178
            "time",
179
            "memory",
180
            "language",
181
            "score",
182
            "submission_date"
183
        )->orderBy(
184
            'submission_date',
185
            'desc'
186
        )->paginate(50);
187
188
189
        $records= $paginator->all();
190
        foreach ($records as &$r) {
191
            $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"]));
192
            $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]);
193
            $r["nick_name"]="";
194
        }
195
        return [
196
            "paginator"=>$paginator,
197
            "records"=>$records
198
        ];
199
    }
200
}
201