Test Setup Failed
Push — master ( 9512c9...92b796 )
by John
05:22
created

ContestModel::contestProblemInfoACM()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 68
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 55
nc 3
nop 3
dl 0
loc 68
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Models;
4
5
use GrahamCampbell\Markdown\Facades\Markdown;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
9
class ContestModel extends Model
10
{
11
    protected $tableName='contest';
12
    public $rule=["Unknown", "ICPC", "OI", "Custom ICPC", "Custom OI"];
13
14
    public function calcLength($a, $b)
15
    {
16
        $s=strtotime($b)-strtotime($a);
17
        $h=intval($s / 3600);
18
        $m=round(($s-$h * 3600) / 60);
19
        if ($m==60) {
20
            $h++;
21
            $m=0;
22
        }
23
        if ($m==0 && $h==0) {
24
            $text="$s Seconds";
25
        } elseif ($m==0) {
26
            $text="$h Hours";
27
        } elseif ($h==0) {
28
            $text="$m Minutes";
29
        } else {
30
            $text="$h Hours $m Minutes";
31
        }
32
        return $text;
33
    }
34
35
    public function canViewContest($cid, $uid)
36
    {
37
        $contest_detail=DB::table($this->tableName)->where([
38
            "cid"=>$cid
39
        ])->first();
40
41
        if ($contest_detail["public"]==1) {
42
            return $contest_detail;
43
        } else {
44
            // group contest
45
            if ($uid==0) {
46
                return [];
47
            }
48
            $group_info=DB::table("group_member")->where([
49
                "uid"=>$uid,
50
                "gid"=>$contest_detail['gid'],
51
                ["role", ">", 0]
52
            ])->first();
53
            return empty($group_info) ? [] : $contest_detail;
54
        }
55
    }
56
57
    public function basic($cid)
58
    {
59
        return DB::table($this->tableName)->where([
60
            "cid"=>$cid
61
        ])->first();
62
    }
63
64
    public function detail($cid, $uid=0)
65
    {
66
        $contest_clearance=$this->judgeOutSideClearance($cid, $uid);
67
        $contest_detail=$this->basic($cid);
68
69
        if ($contest_clearance==0) {
70
            return [
71
                "ret"=>1000,
72
                "desc"=>"You have no right to view this contest.",
73
                "data"=>null
74
            ];
75
        } else {
76
            $contest_detail["rule_parsed"]=$this->rule[$contest_detail["rule"]];
77
            $contest_detail["date_parsed"]=[
78
                "date"=>date_format(date_create($contest_detail["begin_time"]), 'j'),
79
                "month_year"=>date_format(date_create($contest_detail["begin_time"]), 'M, Y'),
80
            ];
81
            $contest_detail["length"]=$this->calcLength($contest_detail["begin_time"], $contest_detail["end_time"]);
82
            $contest_detail["description_parsed"]=clean(Markdown::convertToHtml($contest_detail["description"]));
83
            $contest_detail["group_info"]=DB::table("group")->where(["gid"=>$contest_detail["gid"]])->first();
84
            $contest_detail["problem_count"]=DB::table("contest_problem")->where(["cid"=>$cid])->count();
85
            return [
86
                "ret"=>200,
87
                "desc"=>"succeed",
88
                "data"=>[
89
                    "contest_detail"=>$contest_detail
90
                ]
91
            ];
92
        }
93
    }
94
95
    public function gid($cid)
96
    {
97
        return DB::table($this->tableName)->where([
98
            "cid"=>$cid
99
        ])->first()["gid"];
100
    }
101
102
    public function grantAccess($uid, $cid, $audit=0)
103
    {
104
        return DB::table('contest_participant')->insert([
105
            "cid"=>$cid,
106
            "uid"=>$uid,
107
            "audit"=>$audit
108
        ]);
109
    }
110
111
    public function listByGroup($gid)
112
    {
113
        $contest_list=DB::table($this->tableName)->where([
114
            "gid"=>$gid
115
        ])->orderBy('begin_time', 'desc')->get()->all();
116
117
        foreach ($contest_list as &$c) {
118
            $c["rule_parsed"]=$this->rule[$c["rule"]];
119
            $c["date_parsed"]=[
120
                "date"=>date_format(date_create($c["begin_time"]), 'j'),
121
                "month_year"=>date_format(date_create($c["begin_time"]), 'M, Y'),
122
            ];
123
            $c["length"]=$this->calcLength($c["begin_time"], $c["end_time"]);
124
        }
125
        return $contest_list;
126
    }
127
128
    public function rule($cid)
129
    {
130
        return DB::table($this->tableName)->where([
131
            "cid"=>$cid
132
        ])->first()["rule"];
133
    }
134
135
    public function list()
136
    {
137
        $contest_list=DB::table($this->tableName)->where([
138
            "public"=>1,
139
            "audit_status"=>1
140
        ])->orderBy('begin_time', 'desc')->get()->all();
141
142
        foreach ($contest_list as &$c) {
143
            $c["rule_parsed"]=$this->rule[$c["rule"]];
144
            $c["date_parsed"]=[
145
                "date"=>date_format(date_create($c["begin_time"]), 'j'),
146
                "month_year"=>date_format(date_create($c["begin_time"]), 'M, Y'),
147
            ];
148
            $c["length"]=$this->calcLength($c["begin_time"], $c["end_time"]);
149
        }
150
        return $contest_list;
151
    }
152
153
    public function featured()
154
    {
155
        $featured=DB::table($this->tableName)->where([
156
            "public"=>1,
157
            "audit_status"=>1,
158
            "featured"=>1
159
        ])->orderBy('begin_time', 'desc')->first();
160
161
        $featured["rule_parsed"]=$this->rule[$featured["rule"]];
162
        $featured["date_parsed"]=[
163
            "date"=>date_format(date_create($featured["begin_time"]), 'j'),
164
            "month_year"=>date_format(date_create($featured["begin_time"]), 'M, Y'),
165
        ];
166
        $featured["length"]=$this->calcLength($featured["begin_time"], $featured["end_time"]);
167
        return $featured;
168
    }
169
170
    public function remainingTime($cid)
171
    {
172
        $end_time=DB::table($this->tableName)->where([
173
            "cid"=>$cid
174
        ])->select("end_time")->first()["end_time"];
175
        $end_time=strtotime($end_time);
176
        $cur_time=time();
177
        return $end_time-$cur_time;
178
    }
179
180
    public function intToChr($index, $start=65)
181
    {
182
        $str='';
183
        if (floor($index / 26)>0) {
184
            $str.=$this->intToChr(floor($index / 26)-1);
185
        }
186
        return $str.chr($index % 26+$start);
187
    }
188
189
    public function contestProblems($cid, $uid)
190
    {
191
        $submissionModel=new SubmissionModel();
192
193
        $contest_rule=$this->contestRule($cid);
194
195
        $problemSet=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([
196
            "cid"=>$cid
197
        ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all();
198
199
        $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"];
200
        $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
0 ignored issues
show
Unused Code introduced by
The assignment to $end_time is dead and can be removed.
Loading history...
201
202
        foreach ($problemSet as &$p) {
203
            if ($contest_rule==1) {
204
                $prob_stat=DB::table("submission")->select(
205
                    DB::raw("count(sid) as submission_count"),
206
                    DB::raw("sum(verdict='accepted') as passed_count"),
207
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
208
                )->where([
209
                    "pid"=>$p["pid"],
210
                    "cid"=>$cid
211
                ])->where("submission_date", "<", $frozen_time)->first();
212
213
                if ($prob_stat["submission_count"]==0) {
214
                    $p["submission_count"]=0;
215
                    $p["passed_count"]=0;
216
                    $p["ac_rate"]=0;
217
                } else {
218
                    $p["submission_count"]=$prob_stat["submission_count"];
219
                    $p["passed_count"]=$prob_stat["passed_count"];
220
                    $p["ac_rate"]=round($prob_stat["ac_rate"], 2);
221
                }
222
            } else {
223
                $prob_stat=$this->contestProblemInfoOI($cid, $p["pid"], $uid);
224
                $p["points"]=$prob_stat["points"];
225
                $p["score"]=empty($prob_stat["score_parsed"]) ? 0 : $prob_stat["score_parsed"];
226
            }
227
            $prob_status=$submissionModel->getProblemStatus($p["pid"], $uid, $cid);
228
            if (empty($prob_status)) {
229
                $p["prob_status"]=[
230
                    "icon"=>"checkbox-blank-circle-outline",
231
                    "color"=>"wemd-grey-text"
232
                ];
233
            } else {
234
                $p["prob_status"]=[
235
                    "icon"=>$prob_status["verdict"]=="Accepted" ? "checkbox-blank-circle" : "cisco-webex",
236
                    "color"=>$prob_status["color"]
237
                ];
238
            }
239
        }
240
241
        return $problemSet;
242
    }
243
244
    public function getPid($cid, $ncode)
245
    {
246
        return DB::table("contest_problem")->where([
247
            "cid"=>$cid,
248
            "ncode"=>$ncode
249
        ])->select("contest_problem.pid")->first()["pid"];
250
    }
251
252
    public function getPcode($cid, $ncode)
0 ignored issues
show
Unused Code introduced by
The parameter $ncode 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

252
    public function getPcode($cid, /** @scrutinizer ignore-unused */ $ncode)

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...
253
    {
254
        return DB::table("problem")->where([
255
            "cid"=>$cid
256
        ])->select("contest_problem.pid")->first()["pcode"];
257
    }
258
259
    public function getCustomInfo($cid)
260
    {
261
        $basic_info=DB::table($this->tableName)->where([
262
            "cid"=>$cid
263
        ])->select("verified", "gid")->first();
264
        return $basic_info["verified"] ? DB::table("group")->where([
265
            "gid"=>$basic_info["gid"]
266
        ])->select("custom_icon", "custom_title", "gcode")->first() : null;
267
    }
268
269
270
    public function formatTime($seconds)
271
    {
272
        if ($seconds>3600) {
273
            $hours=intval($seconds / 3600);
274
            $minutes=$seconds % 3600;
275
            $time=$hours.":".gmstrftime('%M:%S', $minutes);
276
        } else {
277
            $time=gmstrftime('%H:%M:%S', $seconds);
278
        }
279
        return $time;
280
    }
281
282
    public function contestProblemInfoOI($cid, $pid, $uid)
283
    {
284
        $ret=[
285
            "color"=>"",
286
            "score"=>null,
287
            "score_parsed"=>"",
288
            "solved"=>0,
289
            "points"=>DB::table("contest_problem")->where([
290
                "pid"=>$pid,
291
                "cid"=>$cid
292
            ])->first()["points"]
293
        ];
294
295
        $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"];
296
        $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
0 ignored issues
show
Unused Code introduced by
The assignment to $end_time is dead and can be removed.
Loading history...
297
298
        $highest_record=DB::table("submission")->where([
299
            "cid"=>$cid,
300
            "pid"=>$pid,
301
            "uid"=>$uid
302
        ])->where("submission_date", "<", $frozen_time)->orderBy('score', 'desc')->first();
303
304
        if (!empty($highest_record)) {
305
            $ret["score"]=$highest_record["score"];
306
307
            $tot_score=DB::table("problem")->where([
308
                "pid"=>$pid
309
            ])->first()["tot_score"];
310
311
            $ret["color"]=($ret["score"]==$tot_score) ? "wemd-teal-text" : "wemd-green-text";
312
            $ret["solved"]=($ret["score"]==$tot_score) ? 1 : 0;
313
            $ret["score_parsed"]=$ret["score"] / $tot_score * ($ret["points"]);
314
        }
315
        return $ret;
316
    }
317
318
    public function contestProblemInfoACM($cid, $pid, $uid)
319
    {
320
        $ret=[
321
            "color"=>"",
322
            "solved"=>0,
323
            "solved_time"=>"",
324
            "solved_time_parsed"=>"",
325
            "wrong_doings"=>0,
326
            "color"=>"",
327
        ];
328
329
        $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"];
330
        $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
0 ignored issues
show
Unused Code introduced by
The assignment to $end_time is dead and can be removed.
Loading history...
331
332
        $ac_record=DB::table("submission")->where([
333
            "cid"=>$cid,
334
            "pid"=>$pid,
335
            "uid"=>$uid,
336
            "verdict"=>"Accepted"
337
        ])->where("submission_date", "<", $frozen_time)->orderBy('submission_date', 'asc')->first();
338
339
        if (!empty($ac_record)) {
340
            $ret["solved"]=1;
341
342
            $ret["solved_time"]=$ac_record["submission_date"]-strtotime(DB::table($this->tableName)->where([
343
                "cid"=>$cid
344
            ])->first()["begin_time"]);
345
346
            $ret["solved_time_parsed"]=$this->formatTime($ret["solved_time"]);
347
348
            $ret["wrong_doings"]=DB::table("submission")->where([
349
                "cid"=>$cid,
350
                "pid"=>$pid,
351
                "uid"=>$uid
352
            ])->whereIn('verdict', [
353
                'Runtime Error',
354
                'Wrong Answer',
355
                'Time Limit Exceed',
356
                'Real Time Limit Exceed',
357
                'Memory Limit Exceed',
358
                'Presentation Error',
359
                'Output Limit Exceeded'
360
            ])->where("submission_date", "<", $ac_record["submission_date"])->count();
361
362
            $others_first=DB::table("submission")->where([
363
                "cid"=>$cid,
364
                "pid"=>$pid,
365
                "verdict"=>"Accepted"
366
            ])->where("submission_date", "<", $ac_record["submission_date"])->count();
367
368
            $ret["color"]=$others_first ? "wemd-green-text" : "wemd-teal-text";
369
        } else {
370
            $ret["wrong_doings"]=DB::table("submission")->where([
371
                "cid"=>$cid,
372
                "pid"=>$pid,
373
                "uid"=>$uid
374
            ])->whereIn('verdict', [
375
                'Runtime Error',
376
                'Wrong Answer',
377
                'Time Limit Exceed',
378
                'Real Time Limit Exceed',
379
                'Memory Limit Exceed',
380
                'Presentation Error',
381
                'Output Limit Exceeded'
382
            ])->where("submission_date", "<", $frozen_time)->count();
383
        }
384
385
        return $ret;
386
    }
387
388
    public function contestRank($cid, $uid)
389
    {
390
        // [ToDo] If the current user's in the organizer group show nick name
391
        // [ToDo] The participants determination
392
        // [ToDo] Frozen Time
393
        // [ToDo] Performance Opt
394
        // [Todo] Ajaxization - Should have done in controller
395
        // [Todo] Authorization ( Public / Private ) - Should have done in controller
396
397
        $ret=[];
398
399
        $contest_info=DB::table("contest")->where("cid", $cid)->first();
400
401
        $user_in_group=!empty(DB::table("group_member")->where([
402
            "uid" => $uid,
403
            "gid" => $contest_info["gid"]
404
        ])->where("role", ">", 0)->first());
405
406
        if ($contest_info["registration"]) {
407
            $submissionUsers=DB::table("contest_participant")->where([
408
                "cid"=>$cid,
409
                "audit"=>1
410
            ])->select('uid')->get()->all();
411
        } else {
412
            // Those who submitted are participants
413
            $submissionUsers=DB::table("submission")->where([
414
                "cid"=>$cid
415
            ])->select('uid')->groupBy('uid')->get()->all();
416
        }
417
418
        $problemSet=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([
419
            "cid"=>$cid
420
        ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all();
421
422
        if ($contest_info["rule"]==1) {
423
            // ACM/ICPC Mode
424
            foreach ($submissionUsers as $s) {
425
                $prob_detail=[];
426
                $totPen=0;
427
                $totScore=0;
428
                foreach ($problemSet as $p) {
429
                    $prob_stat=$this->contestProblemInfoACM($cid, $p["pid"], $s["uid"]);
430
                    $prob_detail[]=[
431
                        "ncode"=>$p["ncode"],
432
                        "pid"=>$p["pid"],
433
                        "color"=>$prob_stat["color"],
434
                        "wrong_doings"=>$prob_stat["wrong_doings"],
435
                        "solved_time_parsed"=>$prob_stat["solved_time_parsed"]
436
                    ];
437
                    if ($prob_stat["solved"]) {
438
                        $totPen+=$prob_stat["wrong_doings"] * 20;
439
                        $totPen+=$prob_stat["solved_time"] / 60;
440
                        $totScore+=$prob_stat["solved"];
441
                    }
442
                }
443
                $ret[]=[
444
                    "uid" => $s["uid"],
445
                    "name" => DB::table("users")->where([
446
                        "id"=>$s["uid"]
447
                    ])->first()["name"],
448
                    "nick_name" => $user_in_group ? DB::table("group_member")->where([
449
                        "uid" => $s["uid"],
450
                        "gid" => $contest_info["gid"]
451
                    ])->where("role", ">", 0)->first()["nick_name"] : "",
452
                    "score" => $totScore,
453
                    "penalty" => $totPen,
454
                    "problem_detail" => $prob_detail
455
                ];
456
            }
457
            usort($ret, function ($a, $b) {
458
                if ($a["score"]==$b["score"]) {
459
                    if ($a["penalty"]==$b["penalty"]) {
460
                        return 0;
461
                    } elseif (($a["penalty"]>$b["penalty"])) {
462
                        return 1;
463
                    } else {
464
                        return -1;
465
                    }
466
                } elseif ($a["score"]>$b["score"]) {
467
                    return -1;
468
                } else {
469
                    return 1;
470
                }
471
            });
472
        } elseif ($contest_info["rule"]==2) {
473
            // OI Mode
474
            foreach ($submissionUsers as $s) {
475
                $prob_detail=[];
476
                $totScore=0;
477
                $totSolved=0;
478
                foreach ($problemSet as $p) {
479
                    $prob_stat=$this->contestProblemInfoOI($cid, $p["pid"], $s["uid"]);
480
                    $prob_detail[]=[
481
                        "ncode"=>$p["ncode"],
482
                        "pid"=>$p["pid"],
483
                        "color"=>$prob_stat["color"],
484
                        "score"=>$prob_stat["score"],
485
                        "score_parsed"=>$prob_stat["score_parsed"]
486
                    ];
487
                    $totSolved+=$prob_stat["solved"];
488
                    $totScore+=intval($prob_stat["score_parsed"]);
489
                }
490
                $ret[]=[
491
                    "uid" => $s["uid"],
492
                    "name" => DB::table("users")->where([
493
                        "id"=>$s["uid"]
494
                    ])->first()["name"],
495
                    "nick_name" => $user_in_group ? DB::table("group_member")->where([
496
                        "uid" => $s["uid"],
497
                        "gid" => $contest_info["gid"]
498
                    ])->where("role", ">", 0)->first()["nick_name"] : "",
499
                    "score" => $totScore,
500
                    "solved" => $totSolved,
501
                    "problem_detail" => $prob_detail
502
                ];
503
            }
504
            usort($ret, function ($a, $b) {
505
                if ($a["score"]==$b["score"]) {
506
                    if ($a["solved"]==$b["solved"]) {
507
                        return 0;
508
                    } elseif (($a["solved"]<$b["solved"])) {
509
                        return 1;
510
                    } else {
511
                        return -1;
512
                    }
513
                } elseif ($a["score"]>$b["score"]) {
514
                    return -1;
515
                } else {
516
                    return 1;
517
                }
518
            });
519
        }
520
521
        return $ret;
522
    }
523
524
    public function getClarificationList($cid)
525
    {
526
        return DB::table("contest_clarification")->where([
527
            "cid"=>$cid,
528
            "public"=>1
529
        ])->orderBy('create_time', 'desc')->get()->all();
530
    }
531
532
    public function getlatestClarification($cid)
533
    {
534
        return DB::table("contest_clarification")->where([
535
            "cid"=>$cid,
536
            "type"=>0,
537
            "public"=>1
538
        ])->orderBy('create_time', 'desc')->first();
539
    }
540
541
    public function getClarificationDetail($ccid)
542
    {
543
        return DB::table("contest_clarification")->where([
544
            "ccid"=>$ccid,
545
            "public"=>1
546
        ])->first();
547
    }
548
549
    public function isContestEnded($cid)
550
    {
551
        return DB::table("contest")->where("cid", $cid)->where("end_time", "<", date("Y-m-d H:i:s"))->count();
552
    }
553
554
    public function formatSubmitTime($date)
555
    {
556
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
557
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
558
559
        $now=time();
560
        $unix_date=strtotime($date);
561
562
        if (empty($unix_date)) {
563
            return "Bad date";
564
        }
565
566
        if ($now>$unix_date) {
567
            $difference=$now-$unix_date;
568
            $tense="ago";
569
        } else {
570
            $difference=$unix_date-$now;
571
            $tense="from now";
572
        }
573
574
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
575
            $difference/=$lengths[$j];
576
        }
577
578
        $difference=round($difference);
579
580
        if ($difference!=1) {
581
            $periods[$j].="s";
582
        }
583
584
        return "$difference $periods[$j] {$tense}";
585
    }
586
587
588
    public function getContestRecord($cid)
589
    {
590
        $basicInfo = $this->basic($cid);
591
        $problemSet_temp = DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([
592
            "cid"=>$cid
593
        ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title", "points", "tot_score")->get()->all();
594
        $problemSet=[];
595
        foreach ($problemSet_temp as $p) {
596
            $problemSet[(string)$p["pid"]]=["ncode"=>$p["ncode"],"points"=>$p["points"],"tot_score"=>$p["tot_score"]];
597
        }
598
        if($basicInfo["status_visibility"]==2){
599
            // View all
600
            $records = DB::table("submission")->where([
601
                'cid'=>$cid
602
            ])->join("users","users.id","=","submission.uid")->select("sid","uid","pid","name","color","verdict","time","memory","language","score","submission_date")->orderBy('submission_date', 'desc')->get()->all();
603
        } elseif ($basicInfo["status_visibility"]==1) {
604
            $records = DB::table("submission")->where([
605
                'cid'=>$cid,
606
                'uid'=>Auth::user()->id
0 ignored issues
show
Bug introduced by
The type App\Models\Auth was not found. Did you mean Auth? If so, make sure to prefix the type with \.
Loading history...
607
            ])->join("users","users.id","=","submission.uid")->select("sid","uid","pid","name","color","verdict","time","memory","language","score","submission_date")->orderBy('submission_date', 'desc')->get()->all();
608
        } else {
609
            return [];
610
        }
611
        foreach($records as &$r){
612
            $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"]));
613
            $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]);
614
            $r["nick_name"]="";
615
            $r["ncode"]=$problemSet[(string)$r["pid"]]["ncode"];
616
            if($r["verdict"]=="Partially Accepted") {
617
                $score_parsed = round($r["score"] / $problemSet[(string)$r["pid"]]["tot_score"] * $problemSet[(string)$r["pid"]]["points"], 1);
618
                $r["verdict"].=" ($score_parsed)";
619
            }
620
        }
621
        return $records;
622
    }
623
624
    public function judgeClearance($cid, $uid=0)
625
    {
626
        if ($uid==0) {
627
            return 0;
628
        }
629
630
        $contest_started=DB::table("contest")->where("cid", $cid)->where("begin_time", "<", date("Y-m-d H:i:s"))->count();
631
        $contest_ended=DB::table("contest")->where("cid", $cid)->where("end_time", "<", date("Y-m-d H:i:s"))->count();
632
        $contest_info=DB::table("contest")->where("cid", $cid)->first();
633
634
        if (!$contest_started) {
635
            // not started or do not exist
636
            return 0;
637
        }
638
639
        if ($contest_info["public"]) {
640
            //public
641
            if ($contest_ended) {
642
                return 1;
643
            } else {
644
                if ($contest_info["registration"]) {
645
                    // check if uid in registration, temp return 3
646
                    $isParticipant=DB::table("contest_participant")->where([
647
                        "cid" => $cid,
648
                        "uid" => $uid,
649
                        "audit" => 1
650
                    ])->count();
651
                    if ($isParticipant) {
652
                        return 2;
653
                    } else {
654
                        return 0;
655
                    }
656
                } else {
657
                    return 2;
658
                }
659
            }
660
        } else {
661
            //private
662
            $isMember = DB::table("group_member")->where([
663
                "gid"=> $contest_info["gid"],
664
                "uid"=> $uid
665
            ])->where("role", ">", 0)->count();
666
            if (!$isMember) {
667
                return 0;
668
            } else {
669
                if ($contest_info["registration"]) {
670
                    // check if uid in registration, temp return 3
671
                    $isParticipant=DB::table("contest_participant")->where([
672
                        "cid" => $cid,
673
                        "uid" => $uid,
674
                        "audit" => 1
675
                    ])->count();
676
                    if ($isParticipant) {
677
                        return 2;
678
                    } else {
679
                        return 0;
680
                    }
681
                } else {
682
                    return 2;
683
                }
684
            }
685
        }
686
    }
687
688
    public function judgeOutsideClearance($cid, $uid=0)
689
    {
690
        $contest_info=DB::table("contest")->where("cid", $cid)->first();
691
        if (empty($contest_info)) {
692
            return 0;
693
        }
694
        if ($contest_info["public"]) {
695
            return 1;
696
        } else {
697
            if ($uid==0) {
698
                return 0;
699
            }
700
            return DB::table("group_member")->where([
701
                "gid"=> $contest_info["gid"],
702
                "uid"=> $uid
703
            ])->where("role", ">", 0)->count() ? 1 : 0;
704
        }
705
    }
706
707
    public function contestName($cid)
708
    {
709
        return DB::table("contest")->where("cid", $cid)->select("name")->first()["name"];
710
    }
711
712
    public function contestRule($cid)
713
    {
714
        return DB::table("contest")->where("cid", $cid)->select("rule")->first()["rule"];
715
    }
716
717
    public function arrangeContest($gid, $config, $problems)
718
    {
719
        DB::transaction(function () use ($gid, $config, $problems) {
720
            $cid=DB::table($this->tableName)->insertGetId([
721
                "gid"=>$gid,
722
                "name"=>$config["name"],
723
                "verified"=>0, //todo
724
                "rated"=>0,
725
                "anticheated"=>0,
726
                "featured"=>0,
727
                "description"=>$config["description"],
728
                "rule"=>1, //todo
729
                "begin_time"=>$config["begin_time"],
730
                "end_time"=>$config["end_time"],
731
                "public"=>0, //todo
732
                "registration"=>0, //todo
733
                "registration_due"=>null, //todo
734
                "registant_type"=>0, //todo
735
                "froze_length"=>0, //todo
736
                "status_visibility"=>2, //todo
737
                "create_time"=>date("Y-m-d H:i:s"),
738
                "audit_status"=>1                       //todo
739
            ]);
740
741
            foreach ($problems as $p) {
742
                $pid=DB::table("problem")->where(["pcode"=>$p["pcode"]])->select("pid")->first()["pid"];
743
                DB::table("contest_problem")->insert([
744
                    "cid"=>$cid,
745
                    "number"=>$p["number"],
746
                    "ncode"=>$this->intToChr($p["number"]-1),
747
                    "pid"=>$pid,
748
                    "alias"=>"",
749
                    "points"=>0
750
                ]);
751
            }
752
        }, 5);
753
    }
754
}
755