Passed
Push — master ( 3dac84...29c427 )
by John
13:35 queued 04:51
created

ContestAdminController::generatePDF()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 32
c 1
b 0
f 1
nc 9
nop 1
dl 0
loc 41
rs 8.1635
1
<?php
2
3
namespace App\Http\Controllers\Ajax;
4
5
use App\Models\ContestModel;
6
use App\Models\Eloquent\Contest;
7
use App\Models\GroupModel;
8
use App\Models\ResponseModel;
9
use App\Models\AccountModel;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Http\Request;
12
use App\Jobs\ProcessSubmission;
13
use Illuminate\Validation\Validator;
14
use Illuminate\Support\Facades\Storage;
15
use App\Jobs\GeneratePDF;
16
use App\Jobs\AntiCheat;
17
use Arr;
18
use Log;
19
use Auth;
20
use Cache;
21
use Response;
22
23
class ContestAdminController extends Controller
24
{
25
    public function assignMember(Request $request)
26
    {
27
        $request->validate([
28
            'cid' => 'required|integer',
29
            'uid' => 'required|integer'
30
        ]);
31
        $cid=$request->input('cid');
32
        $uid=$request->input('uid');
33
34
        $groupModel=new GroupModel();
35
        $contestModel=new ContestModel();
36
37
        $contest_info=$contestModel->basic($cid);
38
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
39
            return ResponseModel::err(2001);
40
        }
41
42
        if ($groupModel->judgeClearance($contest_info['gid'], $uid)<2) {
43
            return ResponseModel::err(7004);
44
        }
45
46
        $contestModel->assignMember($cid, $uid);
47
        return ResponseModel::success(200);
48
    }
49
50
    public function details(Request $request)
51
    {
52
        $request->validate([
53
            'cid' => 'required|integer',
54
        ]);
55
        $cid=$request->input('cid');
56
57
        $contestModel=new ContestModel();
58
        $groupModel=new GroupModel();
59
60
        $contest_problems=$contestModel->problems($cid);
61
        $contest_detail=$contestModel->basic($cid);
62
        $contest_detail['problems']=$contest_problems;
63
        $assign_uid=$contest_detail['assign_uid'];
64
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
65
        if ($clearance!=3) {
66
            return ResponseModel::err(2001);
67
        }
68
        if ($assign_uid!=0) {
69
            $assignee=$groupModel->userProfile($assign_uid, $contest_detail['gid']);
70
        } else {
71
            $assignee=null;
72
        }
73
        $ret=[
74
            'contest_info' => $contest_detail,
75
            'assignee' => $assignee,
76
            'is_admin' => $clearance==3,
77
        ];
78
        return ResponseModel::success(200, null, $ret);
79
    }
80
81
    public function rejudge(Request $request)
82
    {
83
        $request->validate([
84
            'cid' => 'required|integer',
85
            'filter' => 'required',
86
        ]);
87
88
        $all_data=$request->all();
89
        $filter=$all_data['filter'];
90
        $filter=Arr::where($filter, function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

90
        $filter=Arr::where($filter, function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
            return in_array($value, [
92
                "Judge Error",
93
                "System Error",
94
                'Submission Error',
95
                "Runtime Error",
96
                "Wrong Answer",
97
                "Presentation Error",
98
                "Compile Error",
99
                "Time Limit Exceed",
100
                "Real Time Limit Exceed",
101
                "Memory Limit Exceed",
102
                'Output Limit Exceeded',
103
                "Idleness Limit Exceed",
104
                "Partially Accepted",
105
                "Accepted",
106
            ]);
107
        });
108
109
        $contestModel=new ContestModel();
110
        if ($contestModel->judgeClearance($all_data['cid'], Auth::user()->id)<3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
111
            return ResponseModel::err(2001);
112
        }
113
114
        $rejudgeQueue=$contestModel->getRejudgeQueue($all_data["cid"], $filter);
115
116
        foreach ($rejudgeQueue as $r) {
117
            dispatch(new ProcessSubmission($r))->onQueue($r["oj"]);
118
        }
119
120
        return ResponseModel::success(200);
121
    }
122
123
    public function update(Request $request)
124
    {
125
        $request->validate([
126
            'cid' => 'required|integer',
127
            'name' => 'required|max:255',
128
            'problems' => 'required|max:2550',
129
            'status_visibility' => 'required|integer',
130
            'begin_time' => 'required|date',
131
            'end_time' => 'required|date|after:begin_time',
132
            'description' => 'string'
133
        ]);
134
        $all_data=$request->all();
135
        $cid=$all_data['cid'];
136
137
        $contestModel=new ContestModel();
138
        if ($contestModel->judgeClearance($all_data['cid'], Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
139
            return ResponseModel::err(2001);
140
        }
141
142
        if ($contestModel->remainingTime($cid)>0) {
143
            $problems=explode(",", $all_data["problems"]);
144
            if (count($problems)>26) {
145
                return ResponseModel::err(4002);
146
            }
147
            $i=0;
148
            $problemSet=[];
149
            foreach ($problems as $p) {
150
                if (!empty($p)) {
151
                    $i++;
152
                    $problemSet[]=[
153
                        "number"=>$i,
154
                        "pcode"=>$p,
155
                        "points"=>100
156
                    ];
157
                }
158
            }
159
            $allow_update=['name', 'description', 'begin_time', 'end_time', 'status_visibility'];
160
161
            foreach ($all_data as $key => $value) {
162
                if (!in_array($key, $allow_update)) {
163
                    unset($all_data[$key]);
164
                }
165
            }
166
            $contestModel->contestUpdate($cid, $all_data, $problemSet);
167
            return ResponseModel::success(200);
168
        } else {
169
            $allow_update=['name', 'description'];
170
171
            foreach ($all_data as $key => $value) {
172
                if (!in_array($key, $allow_update)) {
173
                    unset($all_data[$key]);
174
                }
175
            }
176
            $contestModel->contestUpdate($cid, $all_data, false);
177
            return ResponseModel::success(200, '
178
                Successful! However, only the name and description of the match can be changed for the match that has been finished.
179
            ');
180
        }
181
182
    }
183
184
    public function issueAnnouncement(Request $request) {
185
        $request->validate([
186
            'cid' => 'required|integer',
187
            'title' => 'required|string|max:250',
188
            'content' => 'required|string|max:65536',
189
        ]);
190
191
        $all_data=$request->all();
192
193
        $contestModel=new ContestModel();
194
        $clearance=$contestModel->judgeClearance($all_data["cid"], Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
195
        if ($clearance<3) {
196
            return ResponseModel::err(2001);
197
        } else {
198
            return ResponseModel::success(200, null, [
199
                "ccid" => $contestModel->issueAnnouncement($all_data["cid"], $all_data["title"], $all_data["content"], Auth::user()->id)
200
            ]);
201
        }
202
    }
203
204
    public function replyClarification(Request $request) {
205
        $request->validate([
206
            'cid' => 'required|integer',
207
            'ccid' => 'required|integer',
208
            'content' => 'required|string|max:65536',
209
        ]);
210
211
        $all_data=$request->all();
212
213
        $contestModel=new ContestModel();
214
        $clearance=$contestModel->judgeClearance($all_data["cid"], Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
215
        if ($clearance<3) {
216
            return ResponseModel::err(2001);
217
        } else {
218
            return ResponseModel::success(200, null, [
219
                "line" => $contestModel->replyClarification($all_data["ccid"], $all_data["content"])
220
            ]);
221
        }
222
    }
223
224
    public function setClarificationPublic(Request $request) {
225
        $request->validate([
226
            'cid' => 'required|integer',
227
            'ccid' => 'required|integer',
228
            'public' => 'required',
229
        ]);
230
231
        $all_data=$request->all();
232
233
        $contestModel=new ContestModel();
234
        $clearance=$contestModel->judgeClearance($all_data["cid"], Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
235
        if ($clearance<3) {
236
            return ResponseModel::err(2001);
237
        } else {
238
            return ResponseModel::success(200, null, [
239
                "line" => $contestModel->setClarificationPublic($all_data["ccid"], $all_data["public"])
240
            ]);
241
        }
242
    }
243
244
    public function generateContestAccount(Request $request)
245
    {
246
        $request->validate([
247
            'cid' => 'required|integer',
248
            'ccode' => 'required|min:3|max:10',
249
            'num' => 'required|integer|max:100'
250
        ]);
251
252
        $all_data=$request->all();
253
254
        $groupModel=new GroupModel();
255
        $contestModel=new ContestModel();
256
        $verified=$contestModel->isVerified($all_data["cid"]);
257
        if (!$verified) {
258
            return ResponseModel::err(2001);
259
        }
260
        $gid=$contestModel->gid($all_data["cid"]);
261
        $clearance=$groupModel->judgeClearance($gid, Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
262
        if ($clearance<3) {
263
            return ResponseModel::err(2001);
264
        }
265
        $accountModel=new AccountModel();
266
        $ret=$accountModel->generateContestAccount($all_data["cid"], $all_data["ccode"], $all_data["num"]);
267
        $cache_data=Cache::tags(['contest', 'account'])->get($all_data["cid"]);
268
        $cache_data[]=$ret;
269
        Cache::tags(['contest', 'account'])->put($all_data["cid"], $cache_data);
270
        return ResponseModel::success(200, null, $ret);
271
    }
272
273
    public function getScrollBoardData(Request $request)
274
    {
275
        $request->validate([
276
            'cid' => 'required|integer',
277
        ]);
278
        $cid=$request->input('cid');
279
        $contestModel=new ContestModel();
280
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
281
            return ResponseModel::err(2001);
282
        }
283
        if ($contestModel->remainingTime($cid)>=0) {
284
            return ResponseModel::err(4008);
285
        }
286
        if ($contestModel->basic($cid)['froze_length']==0) {
287
            return ResponseModel::err(4009);
288
        }
289
        $data=$contestModel->getScrollBoardData($cid);
290
        return ResponseModel::success(200, null, $data);
291
    }
292
293
    public function downloadCode(Request $request)
294
    {
295
        $request->validate([
296
            "cid"=>"required|integer",
297
        ]);
298
        $cid=$request->input('cid');
299
        $groupModel=new GroupModel();
0 ignored issues
show
Unused Code introduced by
The assignment to $groupModel is dead and can be removed.
Loading history...
300
        $contestModel=new ContestModel();
301
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
302
            return ResponseModel::err(2001);
303
        }
304
305
        $zip_name=$contestModel->zipName($cid);
306
        if (!(Storage::disk("private")->exists("contestCodeZip/$cid/".$cid.".zip"))) {
307
            $contestModel->GenerateZip("contestCodeZip/$cid/", $cid, "contestCode/$cid/", $zip_name);
308
        }
309
310
        $files=Storage::disk("private")->files("contestCodeZip/$cid/");
311
        response()->download(base_path("/storage/app/private/".$files[0]), $zip_name, [
312
            "Content-Transfer-Encoding" => "binary",
313
            "Content-Type"=>"application/octet-stream",
314
            "filename"=>$zip_name
315
        ])->send();
316
317
    }
318
319
    public function downloadPlagiarismReport(Request $request)
320
    {
321
        $request->validate([
322
            "cid"=>"required|integer",
323
        ]);
324
        $cid=$request->input('cid');
325
        $contestModel=new ContestModel();
326
327
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
328
            return ResponseModel::err(2001);
329
        }
330
        $name=$contestModel->basic($cid)["name"];
331
332
        return response()->download(storage_path("app/contest/anticheat/$cid/report/report.zip"), __("contest.inside.admin.anticheat.downloadFile", ["name" => $name]).".zip");
0 ignored issues
show
Bug introduced by
Are you sure __('contest.inside.admin...array('name' => $name)) of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

332
        return response()->download(storage_path("app/contest/anticheat/$cid/report/report.zip"), /** @scrutinizer ignore-type */ __("contest.inside.admin.anticheat.downloadFile", ["name" => $name]).".zip");
Loading history...
333
    }
334
335
    public function generatePDF(Request $request)
336
    {
337
        $request->validate([
338
            "cid"=>"required|integer",
339
            "config.cover"=>"required",
340
            "config.advice"=>"required",
341
            "config.renderer"=>"required|string",
342
            "config.formula"=>"required|string",
343
        ]);
344
        $cid=$request->input('cid');
345
        $renderer = $request->input('config.renderer');
346
        $formula = $request->input('config.formula');
347
        if($renderer == 'blink') {
348
            if($formula != 'tex') {
349
                return ResponseModel::err(4011, 'Illegal Formula Rendering Option.');
350
            }
351
        } else if ($renderer == 'cpdf') {
352
            if($formula != 'svg' && $formula != 'png') {
353
                return ResponseModel::err(4011, 'Illegal Formula Rendering Option.');
354
            }
355
        } else {
356
            return ResponseModel::err(4011, 'Unknown Render Engine.');
357
        }
358
        $config=[
359
            'cover'=>$request->input('config.cover')=='true',
360
            'advice'=>$request->input('config.advice')=='true',
361
            'renderer'=>$renderer,
362
            'formula'=>$formula,
363
        ];
364
        $contestModel=new ContestModel();
365
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
366
            return ResponseModel::err(2001);
367
        }
368
        if (!is_null(Cache::tags(['contest', 'admin', 'PDFGenerate'])->get($cid))) {
369
            return ResponseModel::err(8001);
370
        }
371
        $generateProcess=new GeneratePDF($cid, $config);
372
        dispatch($generateProcess)->onQueue('normal');
373
        Cache::tags(['contest', 'admin', 'PDFGenerate'])->put($cid, $generateProcess->getJobStatusId());
374
        return ResponseModel::success(200, null, [
375
            'JobStatusId'=>$generateProcess->getJobStatusId()
376
        ]);
377
    }
378
379
    public function removePDF(Request $request)
380
    {
381
        $request->validate([
382
            "cid"=>"required|integer",
383
        ]);
384
        $cid=$request->input('cid');
385
        $contestModel=new ContestModel();
386
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
387
            return ResponseModel::err(2001);
388
        }
389
        $contest=Contest::find($cid);
390
        $contest->pdf=0;
391
        $contest->save();
392
        return ResponseModel::success(200, null, []);
393
    }
394
395
    public function anticheat(Request $request)
396
    {
397
        $request->validate([
398
            "cid"=>"required|integer"
399
        ]);
400
        $cid=$request->input('cid');
401
        $contestModel=new ContestModel();
402
        if ($contestModel->judgeClearance($cid, Auth::user()->id)!=3) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
403
            return ResponseModel::err(2001);
404
        }
405
        if (!is_null(Cache::tags(['contest', 'admin', 'anticheat'])->get($cid))) {
406
            return ResponseModel::err(8001);
407
        }
408
        if (Contest::find($cid)->isJudgingComplete()) {
409
            $anticheatProcess=new AntiCheat($cid);
410
            dispatch($anticheatProcess)->onQueue('normal');
411
            Cache::tags(['contest', 'admin', 'anticheat'])->put($cid, $anticheatProcess->getJobStatusId());
412
            return ResponseModel::success(200, null, [
413
                'JobStatusId'=>$anticheatProcess->getJobStatusId()
414
            ]);
415
        }
416
        return ResponseModel::err(4010);
417
    }
418
}
419