Passed
Push — master ( 7753b7...16b395 )
by
unknown
04:57 queued 10s
created

ProblemModel::detail()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 61
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 51
nc 13
nop 2
dl 0
loc 61
rs 8.4468
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 ProblemModel extends Model
10
{
11
    protected $table='problem';
12
    protected $primaryKey = 'pid';
13
    const UPDATED_AT = "update_date";
14
15
    public function detail($pcode, $cid=null)
16
    {
17
        $prob_detail=DB::table($this->table)->where("pcode", $pcode)->first();
18
        // [Depreciated] Joint Query was depreciated here for code maintenance reasons
19
        if (!is_null($prob_detail)) {
20
            if($prob_detail["force_raw"]) {
21
                $prob_detail["parsed"]=[
22
                    "description"=>$prob_detail["description"],
23
                    "input"=>$prob_detail["input"],
24
                    "output"=>$prob_detail["output"],
25
                    "note"=>$prob_detail["note"]
26
                ];
27
            } elseif ($prob_detail["markdown"]) {
28
                $prob_detail["parsed"]=[
29
                    "description"=>clean(Markdown::convertToHtml($prob_detail["description"])),
30
                    "input"=>clean(Markdown::convertToHtml($prob_detail["input"])),
31
                    "output"=>clean(Markdown::convertToHtml($prob_detail["output"])),
32
                    "note"=>clean(Markdown::convertToHtml($prob_detail["note"]))
33
                ];
34
            } else {
35
                $prob_detail["parsed"]=[
36
                    "description"=>$prob_detail["description"],
37
                    "input"=>$prob_detail["input"],
38
                    "output"=>$prob_detail["output"],
39
                    "note"=>$prob_detail["note"]
40
                ];
41
            }
42
            $prob_detail["update_date"]=date_format(date_create($prob_detail["update_date"]), 'm/d/Y H:i:s');
43
            $prob_detail["oj_detail"]=DB::table("oj")->where("oid", $prob_detail["OJ"])->first();
44
            $prob_detail["samples"]=DB::table("problem_sample")->where("pid", $prob_detail["pid"])->get()->all();
45
            $prob_detail["tags"]=DB::table("problem_tag")->where("pid", $prob_detail["pid"])->get()->all();
46
            if ($cid) {
47
                $frozen_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
48
                $prob_stat=DB::table("submission")->select(
49
                    DB::raw("count(sid) as submission_count"),
50
                    DB::raw("sum(verdict='accepted') as passed_count"),
51
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
52
                )->where([
53
                    "pid"=>$prob_detail["pid"],
54
                    "cid"=>$cid,
55
                ])->where("submission_date", "<", $frozen_time)->first();
56
                $prob_detail["points"]=DB::table("contest_problem")->where(["cid"=>$cid, "pid"=>$prob_detail["pid"]])->select("points")->first()["points"];
57
            } else {
58
                $prob_stat=DB::table("submission")->select(
59
                    DB::raw("count(sid) as submission_count"),
60
                    DB::raw("sum(verdict='accepted') as passed_count"),
61
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
62
                )->where(["pid"=>$prob_detail["pid"]])->first();
63
                $prob_detail["points"]=0;
64
            }
65
            if ($prob_stat["submission_count"]==0) {
66
                $prob_detail["submission_count"]=0;
67
                $prob_detail["passed_count"]=0;
68
                $prob_detail["ac_rate"]=0;
69
            } else {
70
                $prob_detail["submission_count"]=$prob_stat["submission_count"];
71
                $prob_detail["passed_count"]=$prob_stat["passed_count"];
72
                $prob_detail["ac_rate"]=round($prob_stat["ac_rate"], 2);
73
            }
74
        }
75
        return $prob_detail;
76
    }
77
78
    public function basic($pid)
79
    {
80
        return DB::table($this->table)->where("pid", $pid)->first();
81
    }
82
83
    public function tags()
84
    {
85
        return DB::table("problem_tag")->groupBy('tag')->select("tag", DB::raw('count(*) as tag_count'))->orderBy('tag_count', 'desc')->limit(12)->get()->all();
86
    }
87
88
    public function ojs()
89
    {
90
        return DB::table("oj")->orderBy('oid', 'asc')->limit(12)->get()->all();
91
    }
92
93
    public function ojdetail($oid)
94
    {
95
        return DB::table("oj")->where('oid', $oid)->first();
96
    }
97
98
    public function isBlocked($pid, $cid=null)
99
    {
100
        $conflictContests=DB::table("contest")
101
                            ->join("contest_problem", "contest.cid", "=", "contest_problem.cid")
102
                            ->where("end_time", ">", date("Y-m-d H:i:s"))
103
                            ->where(["verified"=>1, "pid"=>$pid])
104
                            ->select(["contest_problem.cid as cid"])
105
                            ->get()
106
                            ->all();
107
        if (empty($conflictContests)) {
108
            return false;
109
        }
110
        foreach ($conflictContests as $c) {
111
            if ($cid==$c["cid"]) {
112
                return false;
113
            }
114
        }
115
        // header("HTTP/1.1 403 Forbidden");
116
        // exit();
117
        return true;
118
    }
119
120
    public function list($filter)
121
    {
122
        // $prob_list = DB::table($this->table)->select("pid","pcode","title")->get()->all(); // return a array
123
        $preQuery=DB::table($this->table);
124
        if ($filter['oj']) {
125
            $preQuery=$preQuery->where(["OJ"=>$filter['oj']]);
126
        }
127
        if ($filter['tag']) {
128
            $preQuery=$preQuery->join("problem_tag", "problem.pid", "=", "problem_tag.pid")->where(["tag"=>$filter['tag']]);
129
        }
130
        $paginator=$preQuery->select("problem.pid as pid", "pcode", "title")->paginate(20);
131
        $prob_list=$paginator->all();
132
133
        if (empty($prob_list)) {
134
            return null;
135
        }
136
        foreach ($prob_list as &$p) {
137
            $prob_stat=DB::table("submission")->select(
138
                DB::raw("count(sid) as submission_count"),
139
                DB::raw("sum(verdict='accepted') as passed_count"),
140
                DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
141
            )->where(["pid"=>$p["pid"]])->first();
142
            if ($prob_stat["submission_count"]==0) {
143
                $p["submission_count"]=0;
144
                $p["passed_count"]=0;
145
                $p["ac_rate"]=0;
146
            } else {
147
                $p["submission_count"]=$prob_stat["submission_count"];
148
                $p["passed_count"]=$prob_stat["passed_count"];
149
                $p["ac_rate"]=round($prob_stat["ac_rate"], 2);
150
            }
151
        }
152
        return [
153
            'paginator' => $paginator,
154
            'problems' => $prob_list,
155
        ];
156
    }
157
158
    public function existPCode($pcode)
159
    {
160
        $temp=DB::table($this->table)->where(["pcode"=>$pcode])->select("pcode")->first();
161
        return empty($temp) ? null : $temp["pcode"];
162
    }
163
164
    public function pid($pcode)
165
    {
166
        $temp=DB::table($this->table)->where(["pcode"=>$pcode])->select("pid")->first();
167
        return empty($temp) ? 0 : $temp["pid"];
168
    }
169
170
    public function pcode($pid)
171
    {
172
        $temp=DB::table($this->table)->where(["pid"=>$pid])->select("pcode")->first();
173
        return empty($temp) ? 0 : $temp["pcode"];
174
    }
175
176
    public function ocode($pid)
177
    {
178
        $temp=DB::table($this->table)->where(["pid"=>$pid])->select("OJ as oid")->first();
179
        return empty($temp) ? null : DB::table("oj")->where(["oid"=>$temp["oid"]])->select("ocode")->first()["ocode"];
180
    }
181
182
    public function oid($pid)
183
    {
184
        return DB::table($this->table)->where(["pid"=>$pid])->select("OJ as oid")->first()["oid"];
185
    }
186
187
    public function clearTags($pid)
188
    {
189
        DB::table("problem_tag")->where(["pid"=>$pid])->delete();
190
        return true;
191
    }
192
193
    public function addTags($pid, $tag)
194
    {
195
        DB::table("problem_tag")->insert(["pid"=>$pid, "tag"=>$tag]);
196
        return true;
197
    }
198
199
    public function getSolvedCount($oid)
200
    {
201
        return DB::table($this->table)->select("pid", "solved_count")->where(["OJ"=>$oid])->get()->all();
202
    }
203
204
    public function updateDifficulty($pid, $diff_level)
205
    {
206
        DB::table("problem_tag")->where(["pid"=>$pid])->update(["difficulty"=>$diff_level]);
207
        return true;
208
    }
209
210
    public function insertProblem($data)
211
    {
212
        $pid=DB::table($this->table)->insertGetId([
213
            'difficulty'=>-1,
214
            'file'=>$data['file'],
215
            'title'=>$data['title'],
216
            'time_limit'=>$data['time_limit'],
217
            'memory_limit'=>$data['memory_limit'],
218
            'OJ'=>$data['OJ'],
219
            'description'=>$data['description'],
220
            'input'=>$data['input'],
221
            'output'=>$data['output'],
222
            'note'=>$data['note'],
223
            'input_type'=>$data['input_type'],
224
            'output_type'=>$data['output_type'],
225
            'pcode'=>$data['pcode'],
226
            'contest_id'=>$data['contest_id'],
227
            'index_id'=>$data['index_id'],
228
            'origin'=>$data['origin'],
229
            'source'=>$data['source'],
230
            'solved_count'=>$data['solved_count'],
231
            'update_date'=>date("Y-m-d H:i:s"),
232
            'tot_score'=>$data['tot_score'],
233
            'partial'=>$data['partial'],
234
            'markdown'=>$data['markdown'],
235
            'special_compiler'=>$data['special_compiler'],
236
        ]);
237
238
        if (!empty($data["sample"])) {
239
            foreach ($data["sample"] as $d) {
240
                DB::table("problem_sample")->insert([
241
                    'pid'=>$pid,
242
                    'sample_input'=>$d['sample_input'],
243
                    'sample_output'=>$d['sample_output'],
244
                ]);
245
            }
246
        }
247
248
        return $pid;
249
    }
250
251
    public function updateProblem($data)
252
    {
253
        DB::table($this->table)->where(["pcode"=>$data['pcode']])->update([
254
            'difficulty'=>-1,
255
            'file'=>$data['file'],
256
            'title'=>$data['title'],
257
            'time_limit'=>$data['time_limit'],
258
            'memory_limit'=>$data['memory_limit'],
259
            'OJ'=>$data['OJ'],
260
            'description'=>$data['description'],
261
            'input'=>$data['input'],
262
            'output'=>$data['output'],
263
            'note'=>$data['note'],
264
            'input_type'=>$data['input_type'],
265
            'output_type'=>$data['output_type'],
266
            'contest_id'=>$data['contest_id'],
267
            'index_id'=>$data['index_id'],
268
            'origin'=>$data['origin'],
269
            'source'=>$data['source'],
270
            'solved_count'=>$data['solved_count'],
271
            'update_date'=>date("Y-m-d H:i:s"),
272
            'tot_score'=>$data['tot_score'],
273
            'partial'=>$data['partial'],
274
            'markdown'=>$data['markdown'],
275
            'special_compiler'=>$data['special_compiler'],
276
        ]);
277
278
        $pid=$this->pid($data['pcode']);
279
280
        DB::table("problem_sample")->where(["pid"=>$pid])->delete();
281
282
        if (!empty($data["sample"])) {
283
            foreach ($data["sample"] as $d) {
284
                DB::table("problem_sample")->insert([
285
                    'pid'=>$pid,
286
                    'sample_input'=>$d['sample_input'],
287
                    'sample_output'=>$d['sample_output'],
288
                ]);
289
            }
290
        }
291
292
        return $pid;
293
    }
294
}
295