Passed
Pull Request — master (#724)
by John
10:53
created

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