Passed
Pull Request — master (#264)
by
unknown
05:25
created

ProblemModel::detail()   C

Complexity

Conditions 11
Paths 61

Size

Total Lines 78
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 11
eloc 67
c 2
b 1
f 1
nc 61
nop 2
dl 0
loc 78
rs 6.5733

How to fix   Long Method    Complexity   

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
use App\Models\Submission\SubmissionModel;
9
use Illuminate\Support\Str;
10
use Cache;
11
12
class ProblemModel extends Model
13
{
14
    protected $table='problem';
15
    protected $primaryKey='pid';
16
    const UPDATED_AT="update_date";
17
18
    public function detail($pcode, $cid=null)
19
    {
20
        $prob_detail=DB::table($this->table)->where("pcode", $pcode)->first();
21
        // [Depreciated] Joint Query was depreciated here for code maintenance reasons
22
        if (!is_null($prob_detail)) {
23
            if ($prob_detail["force_raw"]) {
24
                $prob_detail["parsed"]=[
25
                    "description"=>$prob_detail["description"],
26
                    "input"=>$prob_detail["input"],
27
                    "output"=>$prob_detail["output"],
28
                    "note"=>$prob_detail["note"],
29
                    "file"=>$prob_detail["file"]
30
                ];
31
            } elseif ($prob_detail["markdown"]) {
32
                $prob_detail["parsed"]=[
33
                    "description"=>clean(convertMarkdownToHtml($prob_detail["description"])),
34
                    "input"=>clean(convertMarkdownToHtml($prob_detail["input"])),
35
                    "output"=>clean(convertMarkdownToHtml($prob_detail["output"])),
36
                    "note"=>clean(convertMarkdownToHtml($prob_detail["note"])),
37
                    "file"=>clean(convertMarkdownToHtml($prob_detail["file"]))
38
                ];
39
            } else {
40
                $prob_detail["parsed"]=[
41
                    "description"=>$prob_detail["description"],
42
                    "input"=>$prob_detail["input"],
43
                    "output"=>$prob_detail["output"],
44
                    "note"=>$prob_detail["note"],
45
                    "file"=>$prob_detail["file"]
46
                ];
47
            }
48
            $prob_detail["pdf"]=false;
49
            $prob_detail["viewerShow"]=false;
50
            $prob_detail["file_ext"]=null;
51
            if($prob_detail['file'] && !blank($prob_detail['file_url'])){
52
                $prob_detail["file_ext"]=explode('.',basename($prob_detail['file_url']));
53
                $prob_detail["file_ext"]=end($prob_detail["file_ext"]);
54
                $prob_detail["pdf"]=Str::is("*.pdf", basename($prob_detail['file_url']));
55
                $prob_detail["viewerShow"]= blank($prob_detail["parsed"]["description"]) &&
56
                                            blank($prob_detail["parsed"]["input"]) &&
57
                                            blank($prob_detail["parsed"]["output"]) &&
58
                                            blank($prob_detail["parsed"]["note"]);
59
            }
60
            $prob_detail["update_date"]=date_format(date_create($prob_detail["update_date"]), 'm/d/Y H:i:s');
61
            $prob_detail["oj_detail"]=DB::table("oj")->where("oid", $prob_detail["OJ"])->first();
62
            $prob_detail["samples"]=DB::table("problem_sample")->where("pid", $prob_detail["pid"])->get()->all();
63
            $prob_detail["tags"]=DB::table("problem_tag")->where("pid", $prob_detail["pid"])->get()->all();
64
            if ($cid) {
65
                $frozen_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
66
                $prob_stat=DB::table("submission")->select(
67
                    DB::raw("count(sid) as submission_count"),
68
                    DB::raw("sum(verdict='accepted') as passed_count"),
69
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
70
                )->where([
71
                    "pid"=>$prob_detail["pid"],
72
                    "cid"=>$cid,
73
                ])->where("submission_date", "<", $frozen_time)->first();
74
                $prob_detail['vcid']=DB::table("contest")->where(["cid"=>$cid])->select("vcid")->first()['vcid'];
75
                $prob_detail["points"]=DB::table("contest_problem")->where(["cid"=>$cid, "pid"=>$prob_detail["pid"]])->select("points")->first()["points"];
76
            } else {
77
                $prob_stat=DB::table("submission")->select(
78
                    DB::raw("count(sid) as submission_count"),
79
                    DB::raw("sum(verdict='accepted') as passed_count"),
80
                    DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
81
                )->where(["pid"=>$prob_detail["pid"]])->first();
82
                $prob_detail['vcid']=null;
83
                $prob_detail["points"]=0;
84
            }
85
            if ($prob_stat["submission_count"]==0) {
86
                $prob_detail["submission_count"]=0;
87
                $prob_detail["passed_count"]=0;
88
                $prob_detail["ac_rate"]=0;
89
            } else {
90
                $prob_detail["submission_count"]=$prob_stat["submission_count"];
91
                $prob_detail["passed_count"]=$prob_stat["passed_count"];
92
                $prob_detail["ac_rate"]=round($prob_stat["ac_rate"], 2);
93
            }
94
        }
95
        return $prob_detail;
96
    }
97
98
    public function basic($pid)
99
    {
100
        return DB::table($this->table)->where("pid", $pid)->first();
101
    }
102
103
    public function tags()
104
    {
105
        return DB::table("problem_tag")->groupBy('tag')->select("tag", DB::raw('count(*) as tag_count'))->orderBy('tag_count', 'desc')->limit(12)->get()->all();
106
    }
107
108
    public function ojs()
109
    {
110
        return DB::table("oj")->orderBy('oid', 'asc')->limit(12)->get()->all();
111
    }
112
113
    public function ojdetail($oid)
114
    {
115
        return DB::table("oj")->where('oid', $oid)->first();
116
    }
117
118
    public function solutionList($pid, $uid=null)
119
    {
120
        if (is_null($uid)) {
121
            $details=DB::table("problem_solution")->join(
122
                "users",
123
                "id",
124
                "=",
125
                "problem_solution.uid"
126
            )->where([
127
                'problem_solution.pid'=>$pid,
128
                'problem_solution.audit'=>1
129
            ])->orderBy(
130
                "problem_solution.votes",
131
                "desc"
132
            )->get()->all();
133
        } else {
134
            $votes=DB::table("problem_solution_vote")->where([
135
                "uid"=>$uid
136
            ])->get()->all();
137
            foreach ($votes as $v) {
138
                $userVotes[$v["psoid"]]=$v["type"];
139
            }
140
            $details=DB::table("problem_solution")->join(
141
                "users",
142
                "id",
143
                "=",
144
                "problem_solution.uid"
145
            )->where([
146
                'problem_solution.pid'=>$pid,
147
                'problem_solution.audit'=>1
148
            ])->select([
149
                "problem_solution.psoid as psoid",
150
                "problem_solution.uid as uid",
151
                "problem_solution.pid as pid",
152
                "problem_solution.content as content",
153
                "problem_solution.audit as audit",
154
                "problem_solution.votes as votes",
155
                "problem_solution.created_at as created_at",
156
                "problem_solution.updated_at as updated_at",
157
                "avatar",
158
                "name"
159
            ])->orderBy("problem_solution.votes", "desc")->get()->all();
160
            foreach ($details as &$d) {
161
                $d["type"]=isset($userVotes[$d["psoid"]]) ? $userVotes[$d["psoid"]] : null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userVotes seems to be defined by a foreach iteration on line 137. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
162
            }
163
            unset($d);
164
        }
165
        foreach ($details as &$d) {
166
            $d["content_parsed"]=clean(convertMarkdownToHtml($d["content"]));
167
        }
168
        return $details;
169
    }
170
171
    public function solution($pid, $uid)
172
    {
173
        $details=DB::table("problem_solution")->join("users", "id", "=", "uid")->where(['pid'=>$pid, 'uid'=>$uid])->first();
174
        return $details;
175
    }
176
177
    public function addSolution($pid, $uid, $content)
178
    {
179
        $details=DB::table("problem_solution")->where(['pid'=>$pid, 'uid'=>$uid])->first();
180
        if (empty($details)) {
181
            DB::table("problem_solution")->insert([
182
                "uid"=>$uid,
183
                "pid"=>$pid,
184
                "content"=>$content,
185
                "votes"=>0,
186
                "audit"=>$this->inteliAudit($uid, $content),
187
                "created_at"=>date("Y-m-d H:i:s"),
188
                "updated_at"=>date("Y-m-d H:i:s"),
189
            ]);
190
            return true;
191
        }
192
        return false;
193
    }
194
195
    private function inteliAudit($uid, $content)
196
    {
197
        if (strpos($content, '```')!==false){
198
            $userSolutionHistory=DB::table("problem_solution")->where(['uid'=>$uid])->orderByDesc('updated_at')->first();
199
            if (!empty($userSolutionHistory) && $userSolutionHistory["audit"]==1){
200
                return 1;
201
            }
202
        }
203
        return 0;
204
    }
205
206
    public function voteSolution($psoid, $uid, $type)
207
    {
208
        $val=$type ? 1 : -1;
209
        $details=DB::table("problem_solution")->where(['psoid'=>$psoid])->first();
210
        if (empty($details)) {
211
            return ["ret"=>false];
212
        }
213
214
        $userVote=DB::table("problem_solution_vote")->where(['uid'=>$uid, "psoid"=>$psoid])->first();
215
216
        if (!empty($userVote)) {
217
            DB::table("problem_solution_vote")->where(['uid'=>$uid, "psoid"=>$psoid])->delete();
218
            if ($userVote["type"]==$type) {
219
                DB::table("problem_solution")->where([
220
                    'psoid'=>$psoid
221
                ])->update([
222
                    "votes"=>$details["votes"]+($userVote["type"]==1 ?-1 : 1),
223
                ]);
224
                return ["ret"=>true, "votes"=>$details["votes"]+($userVote["type"]==1 ?-1 : 1), "select"=>-1]; //disvote
225
            } elseif ($userVote["type"]==1) {
226
                $val--;
227
            } else {
228
                $val++;
229
            }
230
        }
231
232
        DB::table("problem_solution")->where([
233
            'psoid'=>$psoid
234
        ])->update([
235
            "votes"=>$details["votes"]+$val,
236
        ]);
237
238
        DB::table("problem_solution_vote")->insert([
239
            "uid"=>$uid,
240
            "psoid"=>$psoid,
241
            "type"=>$type,
242
        ]);
243
244
        return ["ret"=>true, "votes"=>$details["votes"]+$val, "select"=>$type];
245
    }
246
247
    public function removeSolution($psoid, $uid)
248
    {
249
        if (empty(DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->first())) {
250
            return false;
251
        }
252
        DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->delete();
253
        return true;
254
    }
255
256
    public function updateSolution($psoid, $uid, $content)
257
    {
258
        if (empty(DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->first())) {
259
            return false;
260
        }
261
        DB::table("problem_solution")->where(['psoid'=>$psoid, 'uid'=>$uid])->update([
262
            "content"=>$content,
263
            "audit"=>$this->inteliAudit($uid, $content),
264
            "updated_at"=>date("Y-m-d H:i:s"),
265
        ]);
266
        return true;
267
    }
268
269
    public function isBlocked($pid, $cid=null)
270
    {
271
        $conflictContests=DB::table("contest")
272
                            ->join("contest_problem", "contest.cid", "=", "contest_problem.cid")
273
                            ->where("end_time", ">", date("Y-m-d H:i:s"))
274
                            ->where(["verified"=>1, "pid"=>$pid])
275
                            ->select(["contest_problem.cid as cid"])
276
                            ->get()
277
                            ->all();
278
        if (empty($conflictContests)) {
279
            return false;
280
        }
281
        foreach ($conflictContests as $c) {
282
            if ($cid==$c["cid"]) {
283
                return false;
284
            }
285
        }
286
        // header("HTTP/1.1 403 Forbidden");
287
        // exit();
288
        return true;
289
    }
290
291
    public function list($filter, $uid=null)
292
    {
293
        // $prob_list = DB::table($this->table)->select("pid","pcode","title")->get()->all(); // return a array
294
        $submissionModel=new SubmissionModel();
295
        $preQuery=DB::table($this->table)->where('hide','=',0);
296
        if ($filter['oj']) {
297
            $preQuery=$preQuery->where(["OJ"=>$filter['oj']]);
298
        }
299
        if ($filter['tag']) {
300
            $preQuery=$preQuery->join("problem_tag", "problem.pid", "=", "problem_tag.pid")->where(["tag"=>$filter['tag']]);
301
        }
302
        $paginator=$preQuery->select("problem.pid as pid", "pcode", "title")->orderBy(
303
            "OJ",
304
            "ASC"
305
        )->orderBy(
306
            DB::raw("length(contest_id)"),
307
            "ASC"
308
        )->orderBy(
309
            "contest_id",
310
            "ASC"
311
        )->orderBy(
312
            DB::raw("length(index_id)"),
313
            "ASC"
314
        )->orderBy(
315
            "index_id",
316
            "ASC"
317
        )->orderBy(
318
            "pcode",
319
            "ASC"
320
        )->paginate(20);
321
        $prob_list=$paginator->all();
322
323
        if (empty($prob_list)) {
324
            return null;
325
        }
326
        foreach ($prob_list as &$p) {
327
            $prob_stat=DB::table("submission")->select(
328
                DB::raw("count(sid) as submission_count"),
329
                DB::raw("sum(verdict='accepted') as passed_count"),
330
                DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate")
331
            )->where(["pid"=>$p["pid"]])->first();
332
            if ($prob_stat["submission_count"]==0) {
333
                $p["submission_count"]=0;
334
                $p["passed_count"]=0;
335
                $p["ac_rate"]=0;
336
            } else {
337
                $p["submission_count"]=$prob_stat["submission_count"];
338
                $p["passed_count"]=$prob_stat["passed_count"];
339
                $p["ac_rate"]=round($prob_stat["ac_rate"], 2);
340
            }
341
            if (!is_null($uid)) {
342
                $prob_status=$submissionModel->getProblemStatus($p["pid"], $uid);
343
                if (empty($prob_status)) {
344
                    $p["prob_status"]=[
345
                        "icon"=>"checkbox-blank-circle-outline",
346
                        "color"=>"wemd-grey-text"
347
                    ];
348
                } else {
349
                    $p["prob_status"]=[
350
                        "icon"=>$prob_status["verdict"]=="Accepted" ? "checkbox-blank-circle" : "cisco-webex",
351
                        "color"=>$prob_status["color"]
352
                    ];
353
                }
354
            } else {
355
                $p["prob_status"]=[
356
                    "icon"=>"checkbox-blank-circle-outline",
357
                    "color"=>"wemd-grey-text"
358
                ];
359
            }
360
        }
361
        return [
362
            'paginator' => $paginator,
363
            'problems' => $prob_list,
364
        ];
365
    }
366
367
    public function existPCode($pcode)
368
    {
369
        $temp=DB::table($this->table)->where(["pcode"=>$pcode])->select("pcode")->first();
370
        return empty($temp) ? null : $temp["pcode"];
371
    }
372
373
    public function pid($pcode)
374
    {
375
        $temp=DB::table($this->table)->where(["pcode"=>$pcode])->select("pid")->first();
376
        return empty($temp) ? 0 : $temp["pid"];
377
    }
378
379
    public function pcode($pid)
380
    {
381
        $temp=DB::table($this->table)->where(["pid"=>$pid])->select("pcode")->first();
382
        return empty($temp) ? 0 : $temp["pcode"];
383
    }
384
385
    public function ocode($pid)
386
    {
387
        $temp=DB::table($this->table)->where(["pid"=>$pid])->select("OJ as oid")->first();
388
        return empty($temp) ? null : DB::table("oj")->where(["oid"=>$temp["oid"]])->select("ocode")->first()["ocode"];
389
    }
390
391
    public function oid($pid)
392
    {
393
        return DB::table($this->table)->where(["pid"=>$pid])->select("OJ as oid")->first()["oid"];
394
    }
395
396
    public function clearTags($pid)
397
    {
398
        DB::table("problem_tag")->where(["pid"=>$pid])->delete();
399
        return true;
400
    }
401
402
    public function addTags($pid, $tag)
403
    {
404
        DB::table("problem_tag")->insert(["pid"=>$pid, "tag"=>$tag]);
405
        return true;
406
    }
407
408
    public function getSolvedCount($oid)
409
    {
410
        return DB::table($this->table)->select("pid", "solved_count")->where(["OJ"=>$oid])->get()->all();
411
    }
412
413
    public function updateDifficulty($pid, $diff_level)
414
    {
415
        DB::table("problem_tag")->where(["pid"=>$pid])->update(["difficulty"=>$diff_level]);
416
        return true;
417
    }
418
419
    public function insertProblem($data)
420
    {
421
        $pid=DB::table($this->table)->insertGetId([
422
            'difficulty'=>-1,
423
            'file'=>$data['file'],
424
            'title'=>$data['title'],
425
            'time_limit'=>$data['time_limit'],
426
            'memory_limit'=>$data['memory_limit'],
427
            'OJ'=>$data['OJ'],
428
            'description'=>$data['description'],
429
            'input'=>$data['input'],
430
            'output'=>$data['output'],
431
            'note'=>$data['note'],
432
            'input_type'=>$data['input_type'],
433
            'output_type'=>$data['output_type'],
434
            'pcode'=>$data['pcode'],
435
            'contest_id'=>$data['contest_id'],
436
            'index_id'=>$data['index_id'],
437
            'origin'=>$data['origin'],
438
            'source'=>$data['source'],
439
            'solved_count'=>$data['solved_count'],
440
            'update_date'=>date("Y-m-d H:i:s"),
441
            'tot_score'=>$data['tot_score'],
442
            'partial'=>$data['partial'],
443
            'markdown'=>$data['markdown'],
444
            'special_compiler'=>$data['special_compiler'],
445
        ]);
446
447
        if (!empty($data["sample"])) {
448
            foreach ($data["sample"] as $d) {
449
                if(!isset($d['sample_note'])) $d['sample_note']=null;
450
                DB::table("problem_sample")->insert([
451
                    'pid'=>$pid,
452
                    'sample_input'=>$d['sample_input'],
453
                    'sample_output'=>$d['sample_output'],
454
                    'sample_note'=>$d['sample_note'],
455
                ]);
456
            }
457
        }
458
459
        return $pid;
460
    }
461
462
    public function updateProblem($data)
463
    {
464
        DB::table($this->table)->where(["pcode"=>$data['pcode']])->update([
465
            'difficulty'=>-1,
466
            'file'=>$data['file'],
467
            'title'=>$data['title'],
468
            'time_limit'=>$data['time_limit'],
469
            'memory_limit'=>$data['memory_limit'],
470
            'OJ'=>$data['OJ'],
471
            'description'=>$data['description'],
472
            'input'=>$data['input'],
473
            'output'=>$data['output'],
474
            'note'=>$data['note'],
475
            'input_type'=>$data['input_type'],
476
            'output_type'=>$data['output_type'],
477
            'contest_id'=>$data['contest_id'],
478
            'index_id'=>$data['index_id'],
479
            'origin'=>$data['origin'],
480
            'source'=>$data['source'],
481
            'solved_count'=>$data['solved_count'],
482
            'update_date'=>date("Y-m-d H:i:s"),
483
            'tot_score'=>$data['tot_score'],
484
            'partial'=>$data['partial'],
485
            'markdown'=>$data['markdown'],
486
            'special_compiler'=>$data['special_compiler'],
487
        ]);
488
489
        $pid=$this->pid($data['pcode']);
490
491
        DB::table("problem_sample")->where(["pid"=>$pid])->delete();
492
493
        if (!empty($data["sample"])) {
494
            foreach ($data["sample"] as $d) {
495
                if(!isset($d['sample_note'])) $d['sample_note']=null;
496
                DB::table("problem_sample")->insert([
497
                    'pid'=>$pid,
498
                    'sample_input'=>$d['sample_input'],
499
                    'sample_output'=>$d['sample_output'],
500
                    'sample_note'=>$d['sample_note'],
501
                ]);
502
            }
503
        }
504
505
        return $pid;
506
    }
507
508
    public function discussionList($pid)
509
    {
510
        $paginator = DB::table('problem_discussion')->join(
511
            "users",
512
            "id",
513
            "=",
514
            "problem_discussion.uid"
515
        )->where([
516
            'problem_discussion.pid'=>$pid,
517
            'problem_discussion.audit'=>1
518
        ])->orderBy(
519
            'problem_discussion.created_at',
520
            'desc'
521
        )->select([
522
            'problem_discussion.pdid',
523
            'problem_discussion.title',
524
            'problem_discussion.updated_at',
525
            'users.avatar',
526
            'users.name',
527
            'users.id as uid'
528
        ])->paginate(15);
529
        $list = $paginator->all();
530
        foreach($list as &$l){
531
            $l['updated_at'] = $this->formatTime($l['updated_at']);
532
            $l['comment_count'] = DB::table('problem_discussion_comment')->where('pdid','=',$l['pdid'])->count();
533
        }
534
        return [
535
            'paginator' => $paginator,
536
            'list' => $list,
537
        ];
538
    }
539
540
    public function formatTime($date)
541
    {
542
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
543
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
544
545
        $now=time();
546
        $unix_date=strtotime($date);
547
548
        if (empty($unix_date)) {
549
            return "Bad date";
550
        }
551
552
        if ($now>$unix_date) {
553
            $difference=$now-$unix_date;
554
            $tense="ago";
555
        } else {
556
            $difference=$unix_date-$now;
557
            $tense="from now";
558
        }
559
560
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
561
            $difference/=$lengths[$j];
562
        }
563
564
        $difference=round($difference);
565
566
        if ($difference!=1) {
567
            $periods[$j].="s";
568
        }
569
570
        return "$difference $periods[$j] {$tense}";
571
    }
572
573
    public function discussionDetail($pdid)
574
    {
575
        $main = DB::table('problem_discussion')->join(
576
            "users",
577
            "id",
578
            "=",
579
            "problem_discussion.uid"
580
        )->where(
581
            'problem_discussion.pdid',
582
            '=',
583
            $pdid
584
        )->select([
585
            'problem_discussion.pdid',
586
            'problem_discussion.title',
587
            'problem_discussion.content',
588
            'problem_discussion.votes',
589
            'problem_discussion.created_at',
590
            'users.avatar',
591
            'users.name',
592
            'users.id as uid'
593
        ])->get()->first();
594
        $main['created_at'] = $this->formatTime($main['created_at']);
595
        $main['content']=clean(Markdown::convertToHtml($main["content"]));
596
597
        $comment_count = DB::table('problem_discussion_comment')->where('pdid','=',$pdid)->count();
598
599
        $paginator = DB::table('problem_discussion_comment')->join(
600
            "users",
601
            "id",
602
            "=",
603
            "problem_discussion_comment.uid"
604
        )->where([
605
            'problem_discussion_comment.pdid'=>$pdid,
606
            'problem_discussion_comment.reply_id'=>null,
607
            'problem_discussion_comment.audit'=>1
608
        ])->select([
609
            'problem_discussion_comment.pdcid',
610
            'problem_discussion_comment.pdid',
611
            'problem_discussion_comment.content',
612
            'problem_discussion_comment.votes',
613
            'problem_discussion_comment.created_at',
614
            'users.avatar',
615
            'users.name',
616
            'users.id as uid'
617
        ])->paginate(10);
618
        $comment = $paginator->all();
619
        foreach($comment as &$c){
620
            $c['content']=clean(Markdown::convertToHtml($c["content"]));
621
            $c['created_at'] = $this->formatTime($c['created_at']);
622
            $c['reply'] = DB::table('problem_discussion_comment')->join(
623
                "users",
624
                "id",
625
                "=",
626
                "problem_discussion_comment.uid"
627
            )->where(
628
                'problem_discussion_comment.pdid',
629
                '=',
630
                $pdid
631
            )->where(
632
                'problem_discussion_comment.reply_id',
633
                '!=',
634
                null
635
            )->where(
636
                'problem_discussion_comment.audit',
637
                '=',
638
                1
639
            )->select([
640
                'problem_discussion_comment.pdcid',
641
                'problem_discussion_comment.pdid',
642
                'problem_discussion_comment.content',
643
                'problem_discussion_comment.reply_id',
644
                'problem_discussion_comment.votes',
645
                'problem_discussion_comment.created_at',
646
                'users.avatar',
647
                'users.name',
648
                'users.id as uid'
649
            ])->get()->all();
650
            foreach($c['reply'] as $k=>&$cr){
651
                $cr['content']=clean(Markdown::convertToHtml($cr["content"]));
652
                $cr['reply_uid'] = DB::table('problem_discussion_comment')->where(
653
                    'pdcid',
654
                    '=',
655
                    $cr['reply_id']
656
                )->get()->first()['uid'];
657
                $cr['reply_name'] = DB::table('users')->where(
658
                    'id',
659
                    '=',
660
                    $cr['reply_uid']
661
                )->get()->first()['name'];
662
                $cr['created_at'] = $this->formatTime($cr['created_at']);
663
                if($this->replyParent($cr['pdcid'])!=$c['pdcid']){
664
                    unset($c['reply'][$k]);
665
                }
666
            }
667
        }
668
        return [
669
            'main' => $main,
670
            'comment_count' => $comment_count,
671
            'paginator' => $paginator,
672
            'comment' => $comment
673
        ];
674
    }
675
676
    public function replyParent($pdcid)
677
    {
678
        $reply_id=DB::table('problem_discussion_comment')->where('pdcid','=',$pdcid)->get()->first()['reply_id'];
679
        $top=DB::table('problem_discussion_comment')->where('pdcid','=',$reply_id)->get()->first()['reply_id'];
680
        if(isset($top)){
681
            return $this->replyParent($reply_id);
682
        }else{
683
            return $reply_id;
684
        }
685
    }
686
687
    public function pcodeByPdid($dcode)
688
    {
689
        $pid = DB::table('problem_discussion')->where('pdid','=',$dcode)->get()->first()['pid'];
690
        $pcode = $this->pcode($pid);
691
        return $pcode;
692
    }
693
694
    public function addDiscussion($uid, $pid, $title, $content)
695
    {
696
        $pdid=DB::table("problem_discussion")->insertGetId([
697
            "uid"=>$uid,
698
            "pid"=>$pid,
699
            "title"=>$title,
700
            "content"=>$content,
701
            "votes"=>0,
702
            "audit"=>1,
703
            "created_at"=>date("Y-m-d H:i:s"),
704
            "updated_at"=>date("Y-m-d H:i:s"),
705
        ]);
706
        return $pdid;
707
    }
708
709
    public function pidByPdid($pdid)
710
    {
711
        $pid = DB::table('problem_discussion')->where('pdid','=',$pdid)->get()->first()['pid'];
712
        return $pid;
713
    }
714
715
    public function addComment($uid,$pdid,$content,$reply_id)
716
    {
717
        $pid=$this->pidByPdid($pdid);
718
        $pdcid=DB::table('problem_discussion_comment')->insertGetId([
719
            'pdid'=>$pdid,
720
            'uid'=>$uid,
721
            'pid'=>$pid,
722
            'content'=>$content,
723
            'reply_id'=>$reply_id,
724
            'votes'=>0,
725
            'audit'=>1,
726
            'created_at'=>date("Y-m-d H:i:s"),
727
            'updated_at'=>date("Y-m-d H:i:s"),
728
        ]);
729
        return $pdcid;
730
    }
731
  
732
    public function isHidden($pid)
733
    {
734
        return DB::table('problem')->where('pid','=',$pid)->get()->first()['hide'];
735
    }
736
}
737