Test Setup Failed
Push — master ( 60f940...0da644 )
by he
04:47
created

contest_permission()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
use App\Entities\Contest;
4
use App\Entities\Option;
5
use App\Entities\Solution;
6
use App\Entities\User;
7
use App\Services\ContestService;
8
use Carbon\Carbon;
9
use Illuminate\Database\Eloquent\Collection;
10
use Ramsey\Uuid\Uuid;
11
12
if (! function_exists('new_judge_code')) {
13
    function new_judge_code()
14
    {
15
        return Uuid::getFactory()->uuid4();
16
    }
17
}
18
19
if (! function_exists('get_option')) {
20
    function get_option($name, $default = null)
21
    {
22
        /** @var Collection|Option[] $options */
23
        $options = Option::all();
24
        foreach ($options as $option) {
25
            if ($option->key === $name && $option->value) {
26
                return $option->value;
27
            }
28
        }
29
30
        return $default;
31
    }
32
}
33
34
if (! function_exists('can_attend')) {
35
    /**
36
     * @param Contest $contest
37
     *
38
     * @return bool
39
     */
40
    function can_attend($contest)
41
    {
42
        /** @var User $user */
43
        $user = auth()->user();
44
        if (! $user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
45
            return false;
46
        }
47
        if ($user->hasRole('admin')) {
48
            return true;
49
        }
50
51
        return $contest->users()->wherePivot('user_id', '=', $user->id)->get()->count();
52
    }
53
}
54
55
if (! function_exists('contest_permission')) {
56
    /**
57
     * @param Contest $contest
58
     *
59
     * @return string
60
     */
61
    function contest_permission($contest) {
62
        if ($contest instanceof Contest) {
0 ignored issues
show
introduced by
$contest is always a sub-type of App\Entities\Contest.
Loading history...
63
            $contest = $contest->id;
64
        }
65
66
        return 'contest.'. $contest;
67
    }
68
}
69
70
if (! function_exists('can_view_code')) {
71
    function can_view_code($solution)
72
    {
73
        /** @var User $user */
74
        $user = auth()->user();
75
        if (! $user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
76
            return false;
77
        }
78
        if ($user->hasRole('admin')) {
79
            return true;
80
        }
81
82
        return $solution->user_id == $user->id;
83
    }
84
}
85
86
if (! function_exists('show_ratio')) {
87
    function show_ratio($number, $total)
88
    {
89
        $result = 0;
90
        if ($total > 0) {
91
            $result = 100.0 * $number / $total;
92
        }
93
94
        return sprintf('%2.2f%%', $result);
95
    }
96
}
97
98
if (! function_exists('display_penalize_time')) {
99
    function display_penalize_time($seconds)
100
    {
101
        $hour = (int) ($seconds / (Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR));
102
        $seconds -= $hour * Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR;
103
        $minute = (int) ($seconds / Carbon::SECONDS_PER_MINUTE);
104
        $leftSeconds = $seconds % Carbon::SECONDS_PER_MINUTE;
105
106
        return sprintf('%d:%02d:%02d', $hour, $minute, $leftSeconds);
107
    }
108
}
109
110
if (! function_exists('show_order')) {
111
    function show_order($order)
112
    {
113
        return chr($order + ord('A'));
114
    }
115
}
116
117
if (! function_exists('show_problem_id')) {
118
    /**
119
     * @param Solution $solution
120
     *
121
     * @return int|string
122
     */
123
    function show_problem_id($solution)
124
    {
125
        if ($solution->contest_id) {
126
            return show_order($solution->order);
127
        }
128
129
        return $solution->problem_id;
130
    }
131
}
132
133
if (! function_exists('opening_contest')) {
134
    function opening_contest()
135
    {
136
        return (new ContestService())->openingContest();
137
    }
138
}
139
140
if (! function_exists('original_order')) {
141
    function original_order($order)
142
    {
143
        return ord($order) - ord('A');
144
    }
145
}
146
147
if (! function_exists('!show_status')) {
148
    function show_status($status)
149
    {
150
        if (array_key_exists($status, Solution::$status)) {
151
            return Solution::$status[$status];
152
        }
153
154
        return 'not found!';
155
    }
156
}
157