Completed
Push — master ( 3d3760...b0d42a )
by John
30s queued 13s
created

IndexController::homeworkStatistics()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 16
nc 3
nop 2
dl 0
loc 20
rs 9.7333
c 1
b 0
f 1
1
<?php
2
3
namespace App\Http\Controllers\Group;
4
5
use App\Models\GroupModel;
6
use App\Models\ContestModel;
7
use App\Models\Eloquent\GroupHomework;
8
use App\Models\Eloquent\Group;
9
use App\Exports\GroupAnalysisExport;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Http\Request;
12
use Auth;
13
use Carbon;
14
use Excel;
15
use Redirect;
16
17
class IndexController extends Controller
18
{
19
    /**
20
     * Show the Group Page.
21
     *
22
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Group\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
23
     */
24
    public function index()
25
    {
26
        $groupModel=new GroupModel();
27
        $trending_groups=$groupModel->trendingGroups();
28
        $user_groups=Auth::check() ? $groupModel->userGroups(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...
29
        return view('group.index', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('group.index...mine' => $user_groups)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Group\Response.
Loading history...
30
            'page_title' => "Group",
31
            'site_title' => config("app.name"),
32
            'navigation' => "Group",
33
            'trending' => $trending_groups,
34
            'mine' => $user_groups
35
        ]);
36
    }
37
38
    /**
39
     * Show the Group Detail Page.
40
     *
41
     * @return Response
42
     */
43
    public function detail($gcode)
44
    {
45
        $groupModel=new GroupModel();
46
        $contestModel=new ContestModel();
47
        $basic_info=$groupModel->details($gcode);
48
        $my_profile=$groupModel->userProfile(Auth::user()->id, $basic_info["gid"]);
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...
49
        $clearance=$groupModel->judgeClearance($basic_info["gid"], Auth::user()->id);
50
        $member_list=$groupModel->userList($basic_info["gid"]);
51
        $group_notice=$groupModel->groupNotice($basic_info["gid"]);
52
        // PHP 7.4 Fix
53
        $groupContest=$contestModel->listByGroup($basic_info["gid"]);
54
        if (is_null($groupContest)) {
55
            $contest_list=null;
56
            $paginator=null;
57
        } else {
58
            $contest_list=$contestModel->listByGroup($basic_info["gid"])['contest_list'];
59
            $paginator=$contestModel->listByGroup($basic_info["gid"])['paginator'];
60
        }
61
        return view('group.detail', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('group.detai...d_at', 'desc')->get())) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Group\Response.
Loading history...
62
            'page_title'=>"Group Detail",
63
            'site_title'=>config("app.name"),
64
            'navigation'=>"Group",
65
            "basic_info"=>$basic_info,
66
            'my_profile'=>$my_profile,
67
            'member_list'=>$member_list,
68
            'group_notice'=>$group_notice,
69
            'contest_list'=>$contest_list,
70
            'paginator'=>$paginator,
71
            'group_clearance'=>$clearance,
72
            'runningHomework'=>Group::find($basic_info["gid"])->homework()->where('ended_at', '>=', Carbon::now())->orderBy('ended_at', 'desc')->get()
73
        ]);
74
    }
75
76
    /**
77
     * Show the Group Create Page.
78
     *
79
     * @return Response
80
     */
81
    public function create()
82
    {
83
        //$groupModel=new GroupModel();
84
        //$basic_info=$groupModel->details($gcode);
85
        return view('group.create', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('group.creat...avigation' => 'Group')) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Group\Response.
Loading history...
86
            'page_title'=>"Group Create",
87
            'site_title'=>config("app.name"),
88
            'navigation'=>"Group",
89
            //"basic_info"=>$basic_info,
90
        ]);
91
    }
92
93
    /*
94
     * Show the Contest Analysis Tab.
95
     *
96
     * @return Response
97
     */
98
    public function analysis($gcode)
99
    {
100
        $groupModel=new GroupModel();
101
        $basic_info=$groupModel->details($gcode);
102
        $clearance=$groupModel->judgeClearance($basic_info["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...
103
        if ($clearance<1) {
104
            return Redirect::route('group.detail', ['gcode' => $gcode]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('...ray('gcode' => $gcode)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Group\Response.
Loading history...
105
        }
106
        return view('group.analysis', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('group.analy...arance' => $clearance)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Group\Response.
Loading history...
107
            'page_title'=>"Group Analysis",
108
            'site_title'=>config("app.name"),
109
            'navigation'=>"Group",
110
            'basic_info'=>$basic_info,
111
            'group_clearance'=>$clearance
112
        ]);
113
    }
114
115
    public function analysisDownload($gcode, Request $request)
116
    {
117
        $all_data=$request->all();
118
        $groupModel=new GroupModel();
119
        $group_info=$groupModel->details($gcode);
120
        $mode=$all_data['mode'] ?? 'contest';
121
        if ($mode=='contest') {
122
            $data=$groupModel->groupMemberPracticeContestStat($group_info['gid']);
123
            return Excel::download(
124
                new GroupAnalysisExport(
125
                    [
126
                        'contest_data' => $data['contest_list'],
127
                        'member_data' => $data['member_data'],
128
                    ],
129
                    [
130
                        'mode' => $all_data['mode'] ?? 'contest',
131
                        'maxium' => $all_data['maxium'] ?? true,
132
                        'percent' => $all_data['percent'] ?? false,
133
                    ]
134
                ),
135
                $gcode.'_Group_Contest_Analysis.xlsx'
136
            );
137
        } else {
138
            $data=$groupModel->groupMemberPracticeTagStat($group_info['gid']);
139
            return Excel::download(
140
                new GroupAnalysisExport(
141
                    [
142
                        'tag_problems' => $data['tag_problems'],
143
                        'member_data' => $data['member_data'],
144
                    ],
145
                    [
146
                        'mode' => $all_data['mode'] ?? 'tag',
147
                        'maxium' => $all_data['maxium'] ?? true,
148
                        'percent' => $all_data['percent'] ?? false,
149
                    ]
150
                ),
151
                $gcode.'_Group_Tag_Analysis.xlsx'
152
            );
153
        }
154
    }
155
156
    public function allHomework($gcode)
157
    {
158
        $groupModel=new GroupModel();
159
        $basic_info=$groupModel->details($gcode);
160
        $clearance=$groupModel->judgeClearance($basic_info["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...
161
        if ($clearance<1) {
162
            return Redirect::route('group.detail', ['gcode' => $gcode]);
163
        }
164
        return view('group.homeworkList', [
165
            'page_title'=>"Group Homework",
166
            'site_title'=>config("app.name"),
167
            'navigation'=>"Group",
168
            'basic_info'=>$basic_info,
169
            'homework_list'=>Group::find($basic_info["gid"])->homework()->orderBy('created_at', 'desc')->orderBy('id', 'desc')->get(),
170
            'group_clearance'=>$clearance
171
        ]);
172
    }
173
174
    public function homework($gcode, $homework_id)
175
    {
176
        $groupModel=new GroupModel();
177
        $basic_info=$groupModel->details($gcode);
178
        $clearance=$groupModel->judgeClearance($basic_info["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...
179
        if ($clearance<1) {
180
            return Redirect::route('group.detail', ['gcode' => $gcode]);
181
        }
182
        $homeworkInfo = GroupHomework::where(['id' => $homework_id, 'group_id' => $basic_info['gid']])->first();
183
        if (blank($homeworkInfo)) {
184
            return Redirect::route('group.detail', ['gcode' => $gcode]);
185
        }
186
        return view('group.homework', [
187
            'page_title'=>"Homework Details",
188
            'site_title'=>config("app.name"),
189
            'navigation'=>"Group",
190
            'basic_info'=>$basic_info,
191
            'homework_info'=>$homeworkInfo,
192
            'group_clearance'=>$clearance
193
        ]);
194
    }
195
196
    public function homeworkStatistics($gcode, $homework_id)
197
    {
198
        $groupModel = new GroupModel();
199
        $basic_info = $groupModel->details($gcode);
200
        $clearance = $groupModel->judgeClearance($basic_info["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...
201
        if ($clearance < 2) {
202
            return Redirect::route('group.detail', ['gcode' => $gcode]);
203
        }
204
        $homeworkInfo = GroupHomework::where(['id' => $homework_id, 'group_id' => $basic_info['gid']])->first();
205
        if (blank($homeworkInfo)) {
206
            return Redirect::route('group.detail', ['gcode' => $gcode]);
207
        }
208
        return view('group.homeworkStatistics', [
209
            'page_title' => "Homework Statistics",
210
            'site_title' => config("app.name"),
211
            'navigation' => "Group",
212
            'basic_info' => $basic_info,
213
            'homework_info' => $homeworkInfo,
214
            'statistics' => $homeworkInfo->statistics,
215
            'group_clearance' => $clearance
216
        ]);
217
    }
218
}
219