Passed
Pull Request — master (#44)
by
unknown
03:17
created

ProblemModel   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 168
dl 0
loc 264
rs 9.52
c 0
b 0
f 0
wmc 36

16 Methods

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