1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use App\Models\Eloquent\Contest; |
7
|
|
|
use Auth; |
8
|
|
|
use Carbon; |
9
|
|
|
use DB; |
10
|
|
|
use Exception; |
11
|
|
|
use App\Models\Traits\LikeScope; |
12
|
|
|
use DateTimeInterface; |
13
|
|
|
|
14
|
|
|
class Problem extends Model |
15
|
|
|
{ |
16
|
|
|
use LikeScope; |
17
|
|
|
|
18
|
|
|
protected $table = 'problem'; |
19
|
|
|
protected $primaryKey = 'pid'; |
20
|
|
|
const UPDATED_AT = "update_date"; |
21
|
|
|
|
22
|
|
|
protected function serializeDate(DateTimeInterface $date) |
23
|
|
|
{ |
24
|
|
|
return $date->format('Y-m-d H:i:s'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getReadableNameAttribute() |
28
|
|
|
{ |
29
|
|
|
return $this->pcode . '. ' . $this->title; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function submissions() |
33
|
|
|
{ |
34
|
|
|
return $this->hasMany('App\Models\Eloquent\Submission', 'pid', 'pid'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function problemSamples() |
38
|
|
|
{ |
39
|
|
|
return $this->hasMany('App\Models\Eloquent\ProblemSample', 'pid', 'pid'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function homework_problems() |
43
|
|
|
{ |
44
|
|
|
return $this->hasMany('App\Models\Eloquent\GroupHomeworkProblem', 'problem_id', 'pid'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function onlinejudge() |
48
|
|
|
{ |
49
|
|
|
return $this->belongsTo('App\Models\Eloquent\OJ', 'OJ', 'oid'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getProblemStatusAttribute() |
53
|
|
|
{ |
54
|
|
|
return $this->getProblemStatus(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getProblemStatus($userID = null, $contestID = null, Carbon $till = null) |
58
|
|
|
{ |
59
|
|
|
if (blank($userID)) { |
60
|
|
|
if (Auth::guard('web')->check()) { |
61
|
|
|
$userID = Auth::guard('web')->user()->id; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (filled($userID)) { |
66
|
|
|
$probStatus = $this->getProblemStatusFromDB($userID, $contestID, $till); |
67
|
|
|
if (blank($probStatus)) { |
68
|
|
|
return [ |
69
|
|
|
"icon" => "checkbox-blank-circle-outline", |
70
|
|
|
"color" => "wemd-grey-text" |
71
|
|
|
]; |
72
|
|
|
} else { |
73
|
|
|
return [ |
74
|
|
|
"icon" => $probStatus->verdict == "Accepted" ? "checkbox-blank-circle" : "cisco-webex", |
75
|
|
|
"color" => $probStatus->color |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
return [ |
80
|
|
|
"icon" => "checkbox-blank-circle-outline", |
81
|
|
|
"color" => "wemd-grey-text" |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getProblemStatusFromDB($userID, $contestID = null, Carbon $till = null) |
87
|
|
|
{ |
88
|
|
|
$endedAt = Carbon::now(); |
89
|
|
|
|
90
|
|
|
if (filled($contestID)) { |
91
|
|
|
try { |
92
|
|
|
$endedAt = Carbon::parse(Contest::findOrFail($contestID)->endedAt); |
93
|
|
|
} catch (Exception $e) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Get the very first AC record |
99
|
|
|
|
100
|
|
|
$acRecords = $this->submissions()->where([ |
101
|
|
|
'uid' => $userID, |
102
|
|
|
'cid' => $contestID, |
103
|
|
|
'verdict' => 'Accepted' |
104
|
|
|
]); |
105
|
|
|
if (filled($contestID)) { |
106
|
|
|
$acRecords = $acRecords->where("submission_date", "<", $endedAt->timestamp); |
107
|
|
|
} |
108
|
|
|
if (filled($till)) { |
109
|
|
|
$acRecords = $acRecords->where("submission_date", "<", $till->timestamp); |
110
|
|
|
} |
111
|
|
|
$acRecords = $acRecords->orderBy('submission_date', 'desc')->first(); |
112
|
|
|
if (blank($acRecords)) { |
113
|
|
|
$pacRecords = $this->submissions()->where([ |
114
|
|
|
'uid' => $userID, |
115
|
|
|
'cid' => $contestID, |
116
|
|
|
'verdict' => 'Partially Accepted' |
117
|
|
|
]); |
118
|
|
|
if (filled($contestID)) { |
119
|
|
|
$pacRecords = $pacRecords->where("submission_date", "<", $endedAt->timestamp); |
120
|
|
|
} |
121
|
|
|
if (filled($till)) { |
122
|
|
|
$pacRecords = $pacRecords->where("submission_date", "<", $till->timestamp); |
123
|
|
|
} |
124
|
|
|
$pacRecords = $pacRecords->orderBy('submission_date', 'desc')->first(); |
125
|
|
|
if (blank($pacRecords)) { |
126
|
|
|
$otherRecords = $this->submissions()->where([ |
127
|
|
|
'uid' => $userID, |
128
|
|
|
'cid' => $contestID |
129
|
|
|
]); |
130
|
|
|
if (filled($contestID)) { |
131
|
|
|
$otherRecords = $otherRecords->where("submission_date", "<", $endedAt->timestamp); |
132
|
|
|
} |
133
|
|
|
if (filled($till)) { |
134
|
|
|
$otherRecords = $otherRecords->where("submission_date", "<", $till->timestamp); |
135
|
|
|
} |
136
|
|
|
return $otherRecords->orderBy('submission_date', 'desc')->first(); |
137
|
|
|
} |
138
|
|
|
return $pacRecords; |
139
|
|
|
} else { |
140
|
|
|
return $acRecords; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function users_latest_submission($users, $contestID = null, Carbon $till = null, $verdictFilter = []) |
145
|
|
|
{ |
146
|
|
|
if (filled($contestID)) { |
147
|
|
|
$endedAt = Carbon::parse(Contest::findOrFail($contestID)->endedAt); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$lastRecordSubQuery = $this->submissions()->select('uid', DB::raw('MAX(submission_date) as submission_date'))->whereIntegerInRaw('uid', $users)->where('cid', $contestID)->groupBy('uid'); |
151
|
|
|
|
152
|
|
|
if (filled($contestID)) { |
153
|
|
|
$lastRecordSubQuery = $lastRecordSubQuery->where("submission_date", "<", $endedAt->timestamp); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (filled($till)) { |
157
|
|
|
$lastRecordSubQuery = $lastRecordSubQuery->where("submission_date", "<", $till->timestamp); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if(filled($verdictFilter)) { |
161
|
|
|
$lastRecordSubQuery = $lastRecordSubQuery->whereIn('verdict', $verdictFilter); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$query = DB::table(DB::raw("({$lastRecordSubQuery->toSql()}) last_sub"))->leftJoinSub(Submission::toBase(), 'submissions', function ($join) { |
165
|
|
|
$join->on('last_sub.submission_date', '=', 'submissions.submission_date')->on('last_sub.uid', '=', 'submissions.uid'); |
166
|
|
|
})->select('sid', 'last_sub.submission_date as submission_date', 'last_sub.uid as uid', 'verdict', 'color')->orderBy('uid', 'ASC'); |
167
|
|
|
|
168
|
|
|
return $query->mergeBindings($lastRecordSubQuery->toBase()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/* public function getSamplesAttribute() |
172
|
|
|
{ |
173
|
|
|
return array_map(function($sample) { |
174
|
|
|
return [ |
175
|
|
|
'sample_input' => $sample->sample_input, |
176
|
|
|
'sample_output' => $sample->sample_output, |
177
|
|
|
'sample_note' => $sample->sample_note, |
178
|
|
|
]; |
179
|
|
|
}, $this->problemSamples()->select('sample_input', 'sample_output', 'sample_note')->get()->all()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function setSamplesAttribute($value) |
183
|
|
|
{ |
184
|
|
|
return; |
185
|
|
|
} */ |
186
|
|
|
} |
187
|
|
|
|