Test Setup Failed
Push — master ( 92b796...78abf9 )
by John
05:25
created

ContestModel::isFrozen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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