1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Carbon; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use Exception; |
10
|
|
|
use Cache; |
11
|
|
|
use Log; |
12
|
|
|
|
13
|
|
|
class GroupHomework extends Model |
14
|
|
|
{ |
15
|
|
|
use HasFactory; |
16
|
|
|
|
17
|
|
|
public function group() |
18
|
|
|
{ |
19
|
|
|
return $this->belongsTo('App\Models\Eloquent\Group', 'group_id', 'gid'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function problems() |
23
|
|
|
{ |
24
|
|
|
return $this->hasMany('App\Models\Eloquent\GroupHomeworkProblem'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function serializeDate(DateTimeInterface $date) |
28
|
|
|
{ |
29
|
|
|
return $date->format('Y-m-d H:i:s'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getStatisticsAttribute() |
33
|
|
|
{ |
34
|
|
|
$cachedStatistics = Cache::tags(['homework', 'statistics'])->get($this->id); |
35
|
|
|
|
36
|
|
|
if (blank($cachedStatistics)) { |
37
|
|
|
$cachedStatistics = $this->cacheStatistics(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($cachedStatistics === false) { |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$cachedStatistics['timestamp'] = Carbon::createFromTimestamp($cachedStatistics['timestamp']); |
45
|
|
|
|
46
|
|
|
return $cachedStatistics; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function cacheStatistics() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$statistics = []; |
53
|
|
|
$homeworkProblems = $this->problems->sortBy('order_index'); |
54
|
|
|
$users = $this->group->members()->where('role', '>=', 1)->get(); |
55
|
|
|
$userIDArr = $users->pluck('uid'); |
56
|
|
|
$defaultVerdict = []; |
57
|
|
|
|
58
|
|
|
foreach ($homeworkProblems as $homeworkProblem) { |
59
|
|
|
$statistics['problems'][] = [ |
60
|
|
|
'pid' => $homeworkProblem->problem_id, |
61
|
|
|
'readable_name' => $homeworkProblem->problem->readable_name, |
62
|
|
|
]; |
63
|
|
|
$defaultVerdict[$homeworkProblem->problem_id] = [ |
64
|
|
|
"icon" => "checkbox-blank-circle-outline", |
65
|
|
|
"color" => "wemd-grey-text" |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($users as $user) { |
70
|
|
|
$statistics['data'][$user->uid] = [ |
71
|
|
|
'name' => $user->user->name, |
72
|
|
|
'nick_name' => blank($user->nick_name) ? null : $user->nick_name, |
73
|
|
|
'solved' => 0, |
74
|
|
|
'attempted' => 0, |
75
|
|
|
'verdict' => $defaultVerdict |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$endedAt = Carbon::parse($this->ended_at); |
80
|
|
|
|
81
|
|
|
foreach ($homeworkProblems as $homeworkProblem) { |
82
|
|
|
$userProbIDArr = []; |
83
|
|
|
|
84
|
|
|
foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt, ['Accepted'])->get() as $acceptedRecord) { |
85
|
|
|
$statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [ |
86
|
|
|
"icon" => "checkbox-blank-circle", |
87
|
|
|
"color" => $acceptedRecord['color'] |
88
|
|
|
]; |
89
|
|
|
$statistics['data'][$acceptedRecord['uid']]['solved']++; |
90
|
|
|
$userProbIDArr[] = $acceptedRecord['uid']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt, ['Partially Accepted'])->get() as $acceptedRecord) { |
94
|
|
|
$statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [ |
95
|
|
|
"icon" => "cisco-webex", |
96
|
|
|
"color" => $acceptedRecord['color'] |
97
|
|
|
]; |
98
|
|
|
$statistics['data'][$acceptedRecord['uid']]['attempted']++; |
99
|
|
|
$userProbIDArr[] = $acceptedRecord['uid']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt)->get() as $acceptedRecord) { |
103
|
|
|
$statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [ |
104
|
|
|
"icon" => "cisco-webex", |
105
|
|
|
"color" => $acceptedRecord['color'] |
106
|
|
|
]; |
107
|
|
|
$statistics['data'][$acceptedRecord['uid']]['attempted']++; |
108
|
|
|
$userProbIDArr[] = $acceptedRecord['uid']; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
usort($statistics['data'], function ($a, $b) { |
113
|
|
|
return $b["solved"] == $a["solved"] ? $b["attempted"] <=> $a["attempted"] : $b["solved"] <=> $a["solved"]; |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
$statistics['timestamp'] = time(); |
117
|
|
|
Cache::tags(['homework', 'statistics'])->put($this->id, $statistics, 360); |
118
|
|
|
return $statistics; |
119
|
|
|
} catch (Exception $e) { |
120
|
|
|
Log::alert($e); |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|