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

AdminController::pdfView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 12
rs 9.9332
1
<?php
2
3
namespace App\Http\Controllers\Contest;
4
5
use App\Models\ContestModel;
6
use App\Models\AccountModel;
7
use App\Http\Controllers\Controller;
8
use App\Exports\AccountExport;
9
use Imtigger\LaravelJobStatus\JobStatus;
10
use Auth;
11
use Redirect;
12
use App\Models\Eloquent\Contest;
13
use Excel;
14
use Cache;
15
use DB;
16
use Storage;
17
18
class AdminController extends Controller
19
{
20
    /**
21
     * Show the Contest Admin Page.
22
     *
23
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Contest\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
24
     */
25
    public function admin($cid)
26
    {
27
        $contestModel=new ContestModel();
28
        $verified=$contestModel->isVerified($cid);
29
        $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...
30
        if ($clearance<=2) {
31
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Contest\Response.
Loading history...
32
        }
33
        $contest_name=$contestModel->contestName($cid);
34
        $customInfo=$contestModel->getCustomInfo($cid);
35
        $accountModel=new AccountModel();
36
        $basicInfo=$contestModel->basic($cid);
37
        $contest_accounts=$accountModel->getContestAccount($cid);
38
        $gcode=$contestModel->gcode($cid);
39
        $isEnd=$contestModel->remainingTime($cid)<0;
40
        $generatePDFStatus=JobStatus::find(Cache::tags(['contest', 'admin', 'PDFGenerate'])->get($cid, 0));
41
        $generatePDFStatus=is_null($generatePDFStatus) ? 'empty' : $generatePDFStatus->status;
42
        if (in_array($generatePDFStatus, ['finished', 'failed'])) {
43
            Cache::tags(['contest', 'admin', 'PDFGenerate'])->forget($cid);
44
        }
45
        $anticheatStatus=JobStatus::find(Cache::tags(['contest', 'admin', 'anticheat'])->get($cid, 0));
46
        $anticheatProgress=is_null($anticheatStatus) ? 0 : $anticheatStatus->progress_percentage;
47
        $anticheatStatus=is_null($anticheatStatus) ? 'empty' : $anticheatStatus->status;
48
        if (Storage::disk('local')->exists("contest/anticheat/$cid/report/report.zip")) {
49
            $anticheatStatus='finished';
50
            $anticheatProgress=100;
51
        }
52
        if (in_array($anticheatStatus, ['finished', 'failed'])) {
53
            Cache::tags(['contest', 'admin', 'anticheat'])->forget($cid);
54
        }
55
        return view('contest.board.admin', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...> $anticheatProgress))) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Contest\Response.
Loading history...
56
            'page_title'=>"Admin",
57
            'navigation' => "Contest",
58
            'site_title'=>$contest_name,
59
            'contest_name'=>$contest_name,
60
            'cid'=>$cid,
61
            'custom_info' => $customInfo,
62
            'clearance'=> $clearance,
63
            'contest_accounts'=>$contest_accounts,
64
            'verified'=>$verified,
65
            'gcode'=>$gcode,
66
            'basic'=>$basicInfo,
67
            'is_end'=>$isEnd,
68
            'generatePDFStatus'=>$generatePDFStatus,
69
            'anticheat'=>[
70
                'status'=>$anticheatStatus,
71
                'progress'=>$anticheatProgress
72
            ]
73
        ]);
74
    }
75
76
    public function downloadContestAccountXlsx($cid)
77
    {
78
        $contestModel=new ContestModel();
79
        $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...
80
        if ($clearance<=2) {
81
            return Redirect::route('contest.detail', ['cid' => $cid]);
82
        }
83
        $account=$contestModel->getContestAccount($cid);
84
        if ($account==null) {
85
            return;
86
        } else {
87
            $AccountExport=new AccountExport($account);
88
            $filename="ContestAccount$cid";
89
            return Excel::download($AccountExport, $filename.'.xlsx');
90
        }
91
    }
92
93
    public function refreshContestRank($cid) {
94
        $contestModel=new ContestModel();
95
        $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...
96
        if ($clearance<=2) {
97
            return Redirect::route('contest.detail', ['cid' => $cid]);
98
        }
99
        $contest_eloquent=Contest::find($cid);
100
        $contestRankRaw=$contest_eloquent->rankRefresh();
101
        Cache::tags(['contest', 'rank'])->put($cid, $contestRankRaw);
102
        Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $contestRankRaw);
103
        $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
104
        if (time()>strtotime($end_time)) {
105
            $contestModel->storeContestRankInMySQL($cid, $contestRankRaw);
106
        }
107
        $contestModel->deleteZip("contestCodeZip/$cid/");
108
        return Redirect::route('contest.board.rank', ['cid' => $cid]);
109
    }
110
111
    public function scrollBoard($cid) {
112
        $contestModel=new ContestModel();
113
        $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...
114
        if ($clearance<=2) {
115
            return Redirect::route('contest.detail', ['cid' => $cid]);
116
        }
117
        $basicInfo=$contestModel->basic($cid);
118
        if ($basicInfo['froze_length']==0) {
119
            return Redirect::route('contest.board.admin', ['cid' => $cid]);
120
        }
121
        if ($basicInfo['registration']==0) {
122
            return Redirect::route('contest.board.admin', ['cid' => $cid]);
123
        }
124
        if ($basicInfo['rule']!=1) {
125
            return Redirect::route('contest.board.admin', ['cid' => $cid]);
126
        }
127
        return view('contest.board.scrollBoard', [
128
            'page_title'=>"ScrollBoard",
129
            'navigation' => "Contest",
130
            'site_title' => config("app.name"),
131
            'basic_info' => $basicInfo,
132
        ]);
133
    }
134
135
    public function pdfView($cid) {
136
        $record = Contest::find($cid);
137
        $accessConfig = request()->accessConfig;
138
        return view('pdf.contest.main', [
139
            'conf' => $accessConfig,
140
            'contest' => [
141
                'cid' => $cid,
142
                'name' => $record->name,
143
                'shortName' => $record->name,
144
                'date' => date("F j, Y", strtotime($record->begin_time)),
145
            ],
146
            'problemset' => $record->getProblemSet(),
147
        ]);
148
    }
149
}
150