SubmissionController::edit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Models\Eloquent\Submission;
6
use App\Models\Eloquent\Contest;
7
use App\Models\Eloquent\Judger;
8
use App\Models\Eloquent\Compiler;
9
use App\Models\Eloquent\User;
10
use App\Models\Eloquent\Problem;
11
use App\Http\Controllers\Controller;
12
use Encore\Admin\Controllers\HasResourceActions;
13
use Encore\Admin\Form;
14
use Encore\Admin\Grid;
15
use Encore\Admin\Layout\Content;
16
use Encore\Admin\Show;
17
18
class SubmissionController extends Controller
19
{
20
    use HasResourceActions;
21
22
    /**
23
     * Index interface.
24
     *
25
     * @param Content $content
26
     * @return Content
27
     */
28
    public function index(Content $content)
29
    {
30
        return $content
31
            ->header(__('admin.submissions.index.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.index.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
            ->header(/** @scrutinizer ignore-type */ __('admin.submissions.index.header'))
Loading history...
32
            ->description(__('admin.submissions.index.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.index.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            ->description(/** @scrutinizer ignore-type */ __('admin.submissions.index.description'))
Loading history...
33
            ->body($this->grid()->render());
34
    }
35
36
    /**
37
     * Show interface.
38
     *
39
     * @param mixed $id
40
     * @param Content $content
41
     * @return Content
42
     */
43
    public function show($id, Content $content)
44
    {
45
        return $content
46
            ->header(__('admin.submissions.show.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.show.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            ->header(/** @scrutinizer ignore-type */ __('admin.submissions.show.header'))
Loading history...
47
            ->description(__('admin.submissions.show.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.show.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
            ->description(/** @scrutinizer ignore-type */ __('admin.submissions.show.description'))
Loading history...
48
            ->body($this->detail($id));
49
    }
50
51
    /**
52
     * Edit interface.
53
     *
54
     * @param mixed $id
55
     * @param Content $content
56
     * @return Content
57
     */
58
    public function edit($id, Content $content)
59
    {
60
        return $content
61
            ->header(__('admin.submissions.edit.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.edit.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            ->header(/** @scrutinizer ignore-type */ __('admin.submissions.edit.header'))
Loading history...
62
            ->description(__('admin.submissions.edit.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.edit.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            ->description(/** @scrutinizer ignore-type */ __('admin.submissions.edit.description'))
Loading history...
63
            ->body($this->form()->edit($id));
64
    }
65
66
    /**
67
     * Create interface.
68
     *
69
     * @param Content $content
70
     * @return Content
71
     */
72
    public function create(Content $content)
73
    {
74
        return $content
75
            ->header(__('admin.submissions.create.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.create.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            ->header(/** @scrutinizer ignore-type */ __('admin.submissions.create.header'))
Loading history...
76
            ->description(__('admin.submissions.create.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.create.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            ->description(/** @scrutinizer ignore-type */ __('admin.submissions.create.description'))
Loading history...
77
            ->body($this->form());
78
    }
79
80
    /**
81
     * Make a grid builder.
82
     *
83
     * @return Grid
84
     */
85
    protected function grid()
86
    {
87
        $grid=new Grid(new Submission);
88
        $grid->column('sid', "ID")->sortable();
89
        $grid->column("time", __('admin.submissions.time'))->display(function($time) {
0 ignored issues
show
Bug introduced by
It seems like __('admin.submissions.time') can also be of type array and array; however, parameter $label of Encore\Admin\Grid::column() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
        $grid->column("time", /** @scrutinizer ignore-type */ __('admin.submissions.time'))->display(function($time) {
Loading history...
90
            return __('admin.submissions.timeFormat', ['time'=>$time]);
91
        });
92
        $grid->column("memory", __('admin.submissions.memory'))->display(function($memory) {
93
            return __('admin.submissions.memoryFormat', ['memory'=>$memory]);
94
        });
95
        $grid->column('verdict', __('admin.submissions.verdict'))->display(function($verdict) {
96
            return '<i class="fa fa-circle '.$this->color.'"></i> '.$verdict;
0 ignored issues
show
Bug Best Practice introduced by
The property color does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
97
        });
98
        $grid->column("language", __('admin.submissions.language'));
99
        $grid->column("submission_date", __('admin.submissions.submission_date'))->display(function($submission_date) {
100
            return date("Y-m-d H:i:s", $submission_date);
101
        });
102
        $grid->column("user_name", __('admin.submissions.user_name'))->display(function() {
103
            return $this->user->readable_name;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
104
        });
105
        $grid->column("contest_name", __('admin.submissions.contest_name'))->display(function() {
106
            if (!is_null($this->contest)) {
0 ignored issues
show
Bug Best Practice introduced by
The property contest does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
107
                return $this->contest->name;
108
            }
109
        });
110
        $grid->column("readable_name", __('admin.submissions.readable_name'))->display(function() {
111
            return $this->problem->readable_name;
0 ignored issues
show
Bug Best Practice introduced by
The property problem does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
112
        });
113
        $grid->column("judger_name", __('admin.submissions.judger_name'))->display(function() {
114
            return $this->judger_name;
0 ignored issues
show
Bug Best Practice introduced by
The property judger_name does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
115
        });
116
        $grid->column("share", __('admin.submissions.share'))->switch();
117
        $grid->column("parsed_score", __('admin.submissions.parsed_score'))->display(function() {
118
            return $this->parsed_score;
0 ignored issues
show
Bug Best Practice introduced by
The property parsed_score does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
119
        });
120
        $grid->filter(function(Grid\Filter $filter) {
121
            $filter->column(6, function($filter) {
122
                $filter->like('verdict', __('admin.submissions.verdict'));
123
            });
124
            $filter->column(6, function($filter) {
125
                $filter->equal('cid', __('admin.submissions.cid'))->select(Contest::all()->pluck('name', 'cid'));
126
                $filter->equal('uid', __('admin.submissions.uid'))->select(function($id) {
127
                    $user=User::find($id);
128
                    if ($user) {
129
                        return [$user->id => $user->readable_name];
130
                    }
131
                })->config('minimumInputLength', 4)->ajax(route('admin.api.users'));
132
                $filter->equal('pid', __('admin.submissions.pid'))->select(function($pid) {
133
                    $problem=Problem::find($pid);
134
                    if ($problem) {
135
                        return [$problem->pid => $problem->readable_name];
136
                    }
137
                })->config('minimumInputLength', 4)->ajax(route('admin.api.problems'));
138
                $filter->equal('share', __('admin.submissions.share'))->select([
139
                    0 => __('admin.submissions.disableshare'),
140
                    1 => __('admin.submissions.enableshare')
141
                ]);
142
            });
143
        });
144
        return $grid;
145
    }
146
147
    /**
148
     * Make a show builder.
149
     *
150
     * @param mixed $id
151
     * @return Show
152
     */
153
    protected function detail($id)
154
    {
155
        $show=new Show(Submission::findOrFail($id));
156
        $show->sid('SID');
157
        $show->time(__('admin.submissions.time'));
158
        $show->memory(__('admin.submissions.memory'));
159
        $show->verdict(__('admin.submissions.verdict'));
160
        $show->language(__('admin.submissions.language'));
161
        $show->submission_date(__('admin.submissions.submission_date'));
162
        $show->remote_id(__('admin.submissions.remote_id'));
163
        $this->codify($show->solution(__('admin.submissions.solution')), $show->getModel()->compiler->lang);
0 ignored issues
show
Bug introduced by
The property lang does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
164
        if (!blank($show->getModel()->compile_info)) {
165
            $this->codify($show->compile_info());
166
        }
167
        $show->uid(__('admin.submissions.uid'));
168
        $show->pid(__('admin.submissions.pid'));
169
        $show->cid(__('admin.submissions.cid'));
170
        $show->jid(__('admin.submissions.jid'));
171
        $show->coid(__('admin.submissions.coid'));
172
        $show->vcid(__('admin.submissions.vcid'));
173
        $show->score(__('admin.submissions.parsed_score'));
174
        $show->share(__('admin.submissions.share'))->using([__('admin.submissions.disableshare'), __('admin.submissions.enableshare')]);
175
        return $show;
176
    }
177
178
    private function codify($field, $lang=null)
179
    {
180
        $field->unescape()->as(function($value) use ($field, $lang) {
181
            $field->border=false;
182
            $hash=md5($value);
183
            if (blank($value)) {
184
                $value=" ";
185
            }
186
            return "
187
                <style>
188
                #x$hash {
189
                    background: #ffffff;
190
                    border-top-left-radius: 0;
191
                    border-top-right-radius: 0;
192
                    border-bottom-right-radius: 3px;
193
                    border-bottom-left-radius: 3px;
194
                    padding: 10px;
195
                    border: 1px solid #d2d6de;
196
                }
197
                #x$hash code {
198
                    background: #ffffff;
199
                }
200
                </style>
201
                <pre id='x$hash'><code class='$lang'>".htmlspecialchars($value)."</code></pre>
202
                <script>
203
                    try{
204
                        hljs.highlightElement(document.querySelector('#x$hash code'));
205
                    }catch(err){
206
                        window.addEventListener('load', function(){hljs.highlightElement(document.querySelector('#x$hash code'));});
207
                    }
208
                </script>
209
            ";
210
        });
211
    }
212
213
    /**
214
     * Make a form builder.
215
     *
216
     * @return Form
217
     */
218
    protected function form()
219
    {
220
        $form=new Form(new Submission);
221
        $form->tab('Basic', function(Form $form) {
222
            $form->display('sid', 'SID');
223
            $form->text('time', __('admin.submissions.time'))->rules('required');
224
            $form->text('memory', __('admin.submissions.memory'))->rules('required');
225
            $form->text('verdict', __('admin.submissions.verdict'))->rules('required');
226
            $form->text('color', __('admin.submissions.color'))->rules('required');
227
            $form->textarea('language', __('admin.submissions.language'))->rules('required');
228
            $form->display('submission_date', __('admin.submissions.submission_date'));
229
            $form->select('uid', __('admin.submissions.uid'))->options(function($id) {
230
                $user=User::find($id);
231
                if ($user) {
232
                    return [$user->id => $user->readable_name];
233
                }
234
            })->config('minimumInputLength', 4)->ajax(route('admin.api.users'))->required();
235
            $form->select('cid', __('admin.submissions.cid'))->options(Contest::all()->pluck('name', 'cid'));
236
            $form->select('pid', __('admin.submissions.pid'))->options(function($pid) {
237
                $problem=Problem::find($pid);
238
                if ($problem) {
239
                    return [$problem->pid => $problem->readable_name];
240
                }
241
            })->config('minimumInputLength', 4)->ajax(route('admin.api.problems'))->required();
242
            $form->select('jid', __('admin.submissions.jid'))->options(Judger::all()->pluck('readable_name', 'jid'));
243
            $form->select('coid', __('admin.submissions.coid'))->options(Compiler::all()->pluck('readable_name', 'coid'))->rules('required');
244
            $form->number('score', __('admin.submissions.rawscore'))->rules('required');
245
            $form->select('share', __('admin.submissions.share'))->options([
246
                0 => __('admin.submissions.disableshare'),
247
                1 => __('admin.submissions.enableshare')
248
            ])->default(0)->rules('required');
249
        });
250
        return $form;
251
    }
252
}
253