Passed
Pull Request — master (#724)
by John
12:30 queued 05:09
created

GroupHomework   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
eloc 61
dl 0
loc 109
rs 10
c 3
b 1
f 2
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A problems() 0 3 1
A serializeDate() 0 3 1
A getStatisticsAttribute() 0 15 3
A group() 0 3 1
F cacheStatistics() 0 73 10
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