Passed
Pull Request — master (#51)
by
unknown
03:33
created

ProblemModel::basic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 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_list = $paginator->all();
118
119
        if (empty($prob_list)) {
120
            return null;
121
        }
122
        foreach ($prob_list as &$p) {
123
            $prob_stat=DB::table("submission")->select(
124
                DB::raw("count(sid) as submission_count"),
125
                DB::raw("sum(verdict='accepted') as passed_count"),
126
                DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
127
            )->where(["pid"=>$p["pid"]])->first();
128
            if ($prob_stat["submission_count"]==0) {
129
                $p["submission_count"]=0;
130
                $p["passed_count"]=0;
131
                $p["ac_rate"]=0;
132
            } else {
133
                $p["submission_count"]=$prob_stat["submission_count"];
134
                $p["passed_count"]=$prob_stat["passed_count"];
135
                $p["ac_rate"]=round($prob_stat["ac_rate"], 2);
136
            }
137
        }
138
        return [
139
            'paginator' => $paginator,
140
            'problems' => $prob_list,
141
        ];
142
    }
143
144
    public function existPCode($pcode)
145
    {
146
        $temp=DB::table($this->tableName)->where(["pcode"=>$pcode])->select("pcode")->first();
147
        return empty($temp) ? null : $temp["pcode"];
148
    }
149
150
    public function pid($pcode)
151
    {
152
        $temp=DB::table($this->tableName)->where(["pcode"=>$pcode])->select("pid")->first();
153
        return empty($temp) ? 0 : $temp["pid"];
154
    }
155
156
    public function pcode($pid)
157
    {
158
        $temp=DB::table($this->tableName)->where(["pid"=>$pid])->select("pcode")->first();
159
        return empty($temp) ? 0 : $temp["pcode"];
160
    }
161
162
    public function ocode($pid)
163
    {
164
        $temp=DB::table($this->tableName)->where(["pid"=>$pid])->select("OJ as oid")->first();
165
        return empty($temp) ? null : DB::table("oj")->where(["oid"=>$temp["oid"]])->select("ocode")->first()["ocode"];
166
    }
167
168
    public function clearTags($pid)
169
    {
170
        DB::table("problem_tag")->where(["pid"=>$pid])->delete();
171
        return true;
172
    }
173
174
    public function addTags($pid, $tag)
175
    {
176
        DB::table("problem_tag")->insert(["pid"=>$pid, "tag"=>$tag]);
177
        return true;
178
    }
179
180
    public function getSolvedCount($oid)
181
    {
182
        return DB::table($this->tableName)->select("pid", "solved_count")->where(["OJ"=>$oid])->get()->all();
183
    }
184
185
    public function updateDifficulty($pid, $diff_level)
186
    {
187
        DB::table("problem_tag")->where(["pid"=>$pid])->update(["difficulty"=>$diff_level]);
188
        return true;
189
    }
190
191
    public function insertProblem($data)
192
    {
193
        $pid=DB::table($this->tableName)->insertGetId([
194
            'difficulty'=>-1,
195
            'file'=>$data['file'],
196
            'title'=>$data['title'],
197
            'time_limit'=>$data['time_limit'],
198
            'memory_limit'=>$data['memory_limit'],
199
            'OJ'=>$data['OJ'],
200
            'description'=>$data['description'],
201
            'input'=>$data['input'],
202
            'output'=>$data['output'],
203
            'note'=>$data['note'],
204
            'input_type'=>$data['input_type'],
205
            'output_type'=>$data['output_type'],
206
            'pcode'=>$data['pcode'],
207
            'contest_id'=>$data['contest_id'],
208
            'index_id'=>$data['index_id'],
209
            'origin'=>$data['origin'],
210
            'source'=>$data['source'],
211
            'solved_count'=>$data['solved_count'],
212
            'update_date'=>date("Y-m-d H:i:s"),
213
            'tot_score'=>$data['tot_score'],
214
            'partial'=>$data['partial'],
215
            'markdown'=>$data['markdown'],
216
            'special_compiler'=>$data['special_compiler'],
217
        ]);
218
219
        if (!empty($data["sample"])) {
220
            foreach ($data["sample"] as $d) {
221
                DB::table("problem_sample")->insert([
222
                    'pid'=>$pid,
223
                    'sample_input'=>$d['sample_input'],
224
                    'sample_output'=>$d['sample_output'],
225
                ]);
226
            }
227
        }
228
229
        return $pid;
230
    }
231
232
    public function updateProblem($data)
233
    {
234
        DB::table($this->tableName)->where(["pcode"=>$data['pcode']])->update([
235
            'difficulty'=>-1,
236
            'file'=>$data['file'],
237
            'title'=>$data['title'],
238
            'time_limit'=>$data['time_limit'],
239
            'memory_limit'=>$data['memory_limit'],
240
            'OJ'=>$data['OJ'],
241
            'description'=>$data['description'],
242
            'input'=>$data['input'],
243
            'output'=>$data['output'],
244
            'note'=>$data['note'],
245
            'input_type'=>$data['input_type'],
246
            'output_type'=>$data['output_type'],
247
            'contest_id'=>$data['contest_id'],
248
            'index_id'=>$data['index_id'],
249
            'origin'=>$data['origin'],
250
            'source'=>$data['source'],
251
            'solved_count'=>$data['solved_count'],
252
            'update_date'=>date("Y-m-d H:i:s"),
253
            'tot_score'=>$data['tot_score'],
254
            'partial'=>$data['partial'],
255
            'markdown'=>$data['markdown'],
256
            'special_compiler'=>$data['special_compiler'],
257
        ]);
258
259
        $pid=$this->pid($data['pcode']);
260
261
        DB::table("problem_sample")->where(["pid"=>$pid])->delete();
262
263
        if (!empty($data["sample"])) {
264
            foreach ($data["sample"] as $d) {
265
                DB::table("problem_sample")->insert([
266
                    'pid'=>$pid,
267
                    'sample_input'=>$d['sample_input'],
268
                    'sample_output'=>$d['sample_output'],
269
                ]);
270
            }
271
        }
272
273
        return $pid;
274
    }
275
}
276