Passed
Pull Request — master (#788)
by John
06:12
created

Problem::getReadableNameAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 solutions()
43
    {
44
        return $this->hasMany('App\Models\Eloquent\ProblemSolution', 'pid', 'pid');
45
    }
46
47
    public function homework_problems()
48
    {
49
        return $this->hasMany('App\Models\Eloquent\GroupHomeworkProblem', 'problem_id', 'pid');
50
    }
51
52
    public function onlinejudge()
53
    {
54
        return $this->belongsTo('App\Models\Eloquent\OJ', 'OJ', 'oid');
55
    }
56
57
    public function getProblemStatusAttribute()
58
    {
59
        return $this->getProblemStatus();
60
    }
61
62
    public function getProblemStatus($userID = null, $contestID = null, Carbon $till = null)
63
    {
64
        if (blank($userID)) {
65
            if (Auth::guard('web')->check()) {
66
                $userID = Auth::guard('web')->user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
67
            }
68
        }
69
70
        if (filled($userID)) {
71
            $probStatus = $this->getProblemStatusFromDB($userID, $contestID, $till);
72
            if (blank($probStatus)) {
73
                return [
74
                    "icon" => "checkbox-blank-circle-outline",
75
                    "color" => "wemd-grey-text"
76
                ];
77
            } else {
78
                return [
79
                    "icon" => $probStatus->verdict == "Accepted" ? "checkbox-blank-circle" : "cisco-webex",
80
                    "color" => $probStatus->color
81
                ];
82
            }
83
        } else {
84
            return [
85
                "icon" => "checkbox-blank-circle-outline",
86
                "color" => "wemd-grey-text"
87
            ];
88
        }
89
    }
90
91
    private function getProblemStatusFromDB($userID, $contestID = null, Carbon $till = null)
92
    {
93
        $endedAt = Carbon::now();
94
95
        if (filled($contestID)) {
96
            try {
97
                $endedAt = Carbon::parse(Contest::findOrFail($contestID)->endedAt);
0 ignored issues
show
Bug introduced by
The property endedAt does not seem to exist on App\Models\Eloquent\Contest. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
98
            } catch (Exception $e) {
99
                return null;
100
            }
101
        }
102
103
        // Get the very first AC record
104
105
        $acRecords = $this->submissions()->where([
106
            'uid' => $userID,
107
            'cid' => $contestID,
108
            'verdict' => 'Accepted'
109
        ]);
110
        if (filled($contestID)) {
111
            $acRecords = $acRecords->where("submission_date", "<", $endedAt->timestamp);
112
        }
113
        if (filled($till)) {
114
            $acRecords = $acRecords->where("submission_date", "<", $till->timestamp);
115
        }
116
        $acRecords = $acRecords->orderBy('submission_date', 'desc')->first();
117
        if (blank($acRecords)) {
118
            $pacRecords = $this->submissions()->where([
119
                'uid' => $userID,
120
                'cid' => $contestID,
121
                'verdict' => 'Partially Accepted'
122
            ]);
123
            if (filled($contestID)) {
124
                $pacRecords = $pacRecords->where("submission_date", "<", $endedAt->timestamp);
125
            }
126
            if (filled($till)) {
127
                $pacRecords = $pacRecords->where("submission_date", "<", $till->timestamp);
128
            }
129
            $pacRecords = $pacRecords->orderBy('submission_date', 'desc')->first();
130
            if (blank($pacRecords)) {
131
                $otherRecords = $this->submissions()->where([
132
                    'uid' => $userID,
133
                    'cid' => $contestID
134
                ]);
135
                if (filled($contestID)) {
136
                    $otherRecords = $otherRecords->where("submission_date", "<", $endedAt->timestamp);
137
                }
138
                if (filled($till)) {
139
                    $otherRecords = $otherRecords->where("submission_date", "<", $till->timestamp);
140
                }
141
                return $otherRecords->orderBy('submission_date', 'desc')->first();
142
            }
143
            return $pacRecords;
144
        } else {
145
            return $acRecords;
146
        }
147
    }
148
149
    public function users_latest_submission($users, $contestID = null, Carbon $till = null, $verdictFilter = [])
150
    {
151
        if (filled($contestID)) {
152
            $endedAt = Carbon::parse(Contest::findOrFail($contestID)->endedAt);
0 ignored issues
show
Bug introduced by
The property endedAt does not seem to exist on App\Models\Eloquent\Contest. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
153
        }
154
155
        $lastRecordSubQuery = $this->submissions()->select('uid', DB::raw('MAX(submission_date) as submission_date'))->whereIntegerInRaw('uid', $users)->where('cid', $contestID)->groupBy('uid');
156
157
        if (filled($contestID)) {
158
            $lastRecordSubQuery = $lastRecordSubQuery->where("submission_date", "<", $endedAt->timestamp);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $endedAt does not seem to be defined for all execution paths leading up to this point.
Loading history...
159
        }
160
161
        if (filled($till)) {
162
            $lastRecordSubQuery = $lastRecordSubQuery->where("submission_date", "<", $till->timestamp);
163
        }
164
165
        if(filled($verdictFilter)) {
166
            $lastRecordSubQuery = $lastRecordSubQuery->whereIn('verdict', $verdictFilter);
167
        }
168
169
        $query = DB::table(DB::raw("({$lastRecordSubQuery->toSql()}) last_sub"))->leftJoinSub(Submission::toBase(), 'submissions', function ($join) {
170
            $join->on('last_sub.submission_date', '=', 'submissions.submission_date')->on('last_sub.uid', '=', 'submissions.uid');
171
        })->select('sid', 'last_sub.submission_date as submission_date', 'last_sub.uid as uid', 'verdict', 'color')->orderBy('uid', 'ASC');
172
173
        return $query->mergeBindings($lastRecordSubQuery->toBase());
174
    }
175
176
    /*     public function getSamplesAttribute()
177
    {
178
        return array_map(function($sample) {
179
            return [
180
                'sample_input' => $sample->sample_input,
181
                'sample_output' => $sample->sample_output,
182
                'sample_note' => $sample->sample_note,
183
            ];
184
        }, $this->problemSamples()->select('sample_input', 'sample_output', 'sample_note')->get()->all());
185
    }
186
187
    public function setSamplesAttribute($value)
188
    {
189
        return;
190
    } */
191
}
192