| Total Complexity | 112 |
| Total Lines | 896 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ContestModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContestModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ContestModel extends Model |
||
| 11 | { |
||
| 12 | protected $tableName='contest'; |
||
| 13 | public $rule=["Unknown", "ICPC", "OI", "Custom ICPC", "Custom OI"]; |
||
| 14 | |||
| 15 | public function calcLength($a, $b) |
||
| 16 | { |
||
| 17 | $s=strtotime($b)-strtotime($a); |
||
| 18 | $h=intval($s / 3600); |
||
| 19 | $m=round(($s-$h * 3600) / 60); |
||
| 20 | if ($m==60) { |
||
| 21 | $h++; |
||
| 22 | $m=0; |
||
| 23 | } |
||
| 24 | if ($m==0 && $h==0) { |
||
| 25 | $text="$s Seconds"; |
||
| 26 | } elseif ($m==0) { |
||
| 27 | $text="$h Hours"; |
||
| 28 | } elseif ($h==0) { |
||
| 29 | $text="$m Minutes"; |
||
| 30 | } else { |
||
| 31 | $text="$h Hours $m Minutes"; |
||
| 32 | } |
||
| 33 | return $text; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function canViewContest($cid, $uid) |
||
| 37 | { |
||
| 38 | $contest_detail=DB::table($this->tableName)->where([ |
||
| 39 | "cid"=>$cid |
||
| 40 | ])->first(); |
||
| 41 | |||
| 42 | if ($contest_detail["public"]==1) { |
||
| 43 | return $contest_detail; |
||
| 44 | } else { |
||
| 45 | // group contest |
||
| 46 | if ($uid==0) { |
||
| 47 | return []; |
||
| 48 | } |
||
| 49 | $group_info=DB::table("group_member")->where([ |
||
| 50 | "uid"=>$uid, |
||
| 51 | "gid"=>$contest_detail['gid'], |
||
| 52 | ["role", ">", 0] |
||
| 53 | ])->first(); |
||
| 54 | return empty($group_info) ? [] : $contest_detail; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | public function basic($cid) |
||
| 59 | { |
||
| 60 | return DB::table($this->tableName)->where([ |
||
| 61 | "cid"=>$cid |
||
| 62 | ])->first(); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function detail($cid, $uid=0) |
||
| 66 | { |
||
| 67 | $contest_clearance=$this->judgeOutSideClearance($cid, $uid); |
||
| 68 | $contest_detail=$this->basic($cid); |
||
| 69 | |||
| 70 | if ($contest_clearance==0) { |
||
| 71 | return [ |
||
| 72 | "ret"=>1000, |
||
| 73 | "desc"=>"You have no right to view this contest.", |
||
| 74 | "data"=>null |
||
| 75 | ]; |
||
| 76 | } else { |
||
| 77 | $contest_detail["rule_parsed"]=$this->rule[$contest_detail["rule"]]; |
||
| 78 | $contest_detail["date_parsed"]=[ |
||
| 79 | "date"=>date_format(date_create($contest_detail["begin_time"]), 'j'), |
||
| 80 | "month_year"=>date_format(date_create($contest_detail["begin_time"]), 'M, Y'), |
||
| 81 | ]; |
||
| 82 | $contest_detail["length"]=$this->calcLength($contest_detail["begin_time"], $contest_detail["end_time"]); |
||
| 83 | $contest_detail["description_parsed"]=clean(Markdown::convertToHtml($contest_detail["description"])); |
||
| 84 | $contest_detail["group_info"]=DB::table("group")->where(["gid"=>$contest_detail["gid"]])->first(); |
||
| 85 | $contest_detail["problem_count"]=DB::table("contest_problem")->where(["cid"=>$cid])->count(); |
||
| 86 | return [ |
||
| 87 | "ret"=>200, |
||
| 88 | "desc"=>"succeed", |
||
| 89 | "data"=>[ |
||
| 90 | "contest_detail"=>$contest_detail |
||
| 91 | ] |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | public function gid($cid) |
||
| 97 | { |
||
| 98 | return DB::table($this->tableName)->where([ |
||
| 99 | "cid"=>$cid |
||
| 100 | ])->first()["gid"]; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function grantAccess($uid, $cid, $audit=0) |
||
| 109 | ]); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function listByGroup($gid) |
||
| 127 | } |
||
| 128 | |||
| 129 | public function rule($cid) |
||
| 134 | } |
||
| 135 | |||
| 136 | public function list() |
||
| 137 | { |
||
| 138 | $contest_list=DB::table($this->tableName)->where([ |
||
| 139 | "public"=>1, |
||
| 140 | "audit_status"=>1 |
||
| 141 | ])->orderBy('begin_time', 'desc')->get()->all(); |
||
| 142 | |||
| 143 | foreach ($contest_list as &$c) { |
||
| 144 | $c["rule_parsed"]=$this->rule[$c["rule"]]; |
||
| 145 | $c["date_parsed"]=[ |
||
| 146 | "date"=>date_format(date_create($c["begin_time"]), 'j'), |
||
| 147 | "month_year"=>date_format(date_create($c["begin_time"]), 'M, Y'), |
||
| 148 | ]; |
||
| 149 | $c["length"]=$this->calcLength($c["begin_time"], $c["end_time"]); |
||
| 150 | } |
||
| 151 | return $contest_list; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function featured() |
||
| 155 | { |
||
| 156 | $featured=DB::table($this->tableName)->where([ |
||
| 157 | "public"=>1, |
||
| 158 | "audit_status"=>1, |
||
| 159 | "featured"=>1 |
||
| 160 | ])->orderBy('begin_time', 'desc')->first(); |
||
| 161 | |||
| 162 | $featured["rule_parsed"]=$this->rule[$featured["rule"]]; |
||
| 163 | $featured["date_parsed"]=[ |
||
| 164 | "date"=>date_format(date_create($featured["begin_time"]), 'j'), |
||
| 165 | "month_year"=>date_format(date_create($featured["begin_time"]), 'M, Y'), |
||
| 166 | ]; |
||
| 167 | $featured["length"]=$this->calcLength($featured["begin_time"], $featured["end_time"]); |
||
| 168 | return $featured; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function remainingTime($cid) |
||
| 172 | { |
||
| 173 | $end_time=DB::table($this->tableName)->where([ |
||
| 174 | "cid"=>$cid |
||
| 175 | ])->select("end_time")->first()["end_time"]; |
||
| 176 | $end_time=strtotime($end_time); |
||
| 177 | $cur_time=time(); |
||
| 178 | return $end_time-$cur_time; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function intToChr($index, $start=65) |
||
| 182 | { |
||
| 183 | $str=''; |
||
| 184 | if (floor($index / 26)>0) { |
||
| 185 | $str.=$this->intToChr(floor($index / 26)-1); |
||
| 186 | } |
||
| 187 | return $str.chr($index % 26+$start); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function contestProblems($cid, $uid) |
||
| 191 | { |
||
| 192 | $submissionModel=new SubmissionModel(); |
||
| 193 | |||
| 194 | $contest_rule=$this->contestRule($cid); |
||
| 195 | |||
| 196 | $problemSet=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
| 197 | "cid"=>$cid |
||
| 198 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all(); |
||
| 199 | |||
| 200 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
| 201 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
|
|
|||
| 202 | |||
| 203 | foreach ($problemSet as &$p) { |
||
| 204 | if ($contest_rule==1) { |
||
| 205 | $prob_stat=DB::table("submission")->select( |
||
| 206 | DB::raw("count(sid) as submission_count"), |
||
| 207 | DB::raw("sum(verdict='accepted') as passed_count"), |
||
| 208 | DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate") |
||
| 209 | )->where([ |
||
| 210 | "pid"=>$p["pid"], |
||
| 211 | "cid"=>$cid |
||
| 212 | ])->where("submission_date", "<", $frozen_time)->first(); |
||
| 213 | |||
| 214 | if ($prob_stat["submission_count"]==0) { |
||
| 215 | $p["submission_count"]=0; |
||
| 216 | $p["passed_count"]=0; |
||
| 217 | $p["ac_rate"]=0; |
||
| 218 | } else { |
||
| 219 | $p["submission_count"]=$prob_stat["submission_count"]; |
||
| 220 | $p["passed_count"]=$prob_stat["passed_count"]; |
||
| 221 | $p["ac_rate"]=round($prob_stat["ac_rate"], 2); |
||
| 222 | } |
||
| 223 | } else { |
||
| 224 | $prob_stat=$this->contestProblemInfoOI($cid, $p["pid"], $uid); |
||
| 225 | $p["points"]=$prob_stat["points"]; |
||
| 226 | $p["score"]=empty($prob_stat["score_parsed"]) ? 0 : $prob_stat["score_parsed"]; |
||
| 227 | } |
||
| 228 | $prob_status=$submissionModel->getProblemStatus($p["pid"], $uid, $cid); |
||
| 229 | if (empty($prob_status)) { |
||
| 230 | $p["prob_status"]=[ |
||
| 231 | "icon"=>"checkbox-blank-circle-outline", |
||
| 232 | "color"=>"wemd-grey-text" |
||
| 233 | ]; |
||
| 234 | } else { |
||
| 235 | $p["prob_status"]=[ |
||
| 236 | "icon"=>$prob_status["verdict"]=="Accepted" ? "checkbox-blank-circle" : "cisco-webex", |
||
| 237 | "color"=>$prob_status["color"] |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | return $problemSet; |
||
| 243 | } |
||
| 244 | |||
| 245 | public function getPid($cid, $ncode) |
||
| 251 | } |
||
| 252 | |||
| 253 | public function getPcode($cid, $ncode) |
||
| 254 | { |
||
| 255 | return DB::table("problem")->where([ |
||
| 258 | } |
||
| 259 | |||
| 260 | public function getCustomInfo($cid) |
||
| 261 | { |
||
| 262 | $basic_info=DB::table($this->tableName)->where([ |
||
| 263 | "cid"=>$cid |
||
| 264 | ])->select("verified", "custom_icon", "custom_title")->first(); |
||
| 265 | return $basic_info["verified"] ? ((is_null($basic_info["custom_icon"])&&is_null($basic_info["custom_title"]))?null:$basic_info) : null; |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | public function formatTime($seconds) |
||
| 270 | { |
||
| 271 | if ($seconds>3600) { |
||
| 272 | $hours=intval($seconds / 3600); |
||
| 273 | $minutes=$seconds % 3600; |
||
| 274 | $time=$hours.":".gmstrftime('%M:%S', $minutes); |
||
| 275 | } else { |
||
| 276 | $time=gmstrftime('%H:%M:%S', $seconds); |
||
| 277 | } |
||
| 278 | return $time; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function contestProblemInfoOI($cid, $pid, $uid) |
||
| 315 | } |
||
| 316 | |||
| 317 | public function isFrozen($cid) |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | public function contestProblemInfoACM($cid, $pid, $uid) |
||
| 395 | } |
||
| 396 | |||
| 397 | public function contestRank($cid, $uid) |
||
| 531 | } |
||
| 532 | |||
| 533 | public function getClarificationList($cid) |
||
| 534 | { |
||
| 535 | return DB::table("contest_clarification")->where([ |
||
| 536 | "cid"=>$cid, |
||
| 537 | "public"=>1 |
||
| 538 | ])->orWhere([ |
||
| 539 | "cid" => $cid, |
||
| 540 | 'uid' => Auth::user()->id |
||
| 541 | ])->orderBy('create_time', 'desc')->get()->all(); |
||
| 542 | } |
||
| 543 | |||
| 544 | public function fetchClarification($cid) |
||
| 545 | { |
||
| 546 | return DB::table("contest_clarification")->where([ |
||
| 547 | "cid"=>$cid, |
||
| 548 | "type"=>0, |
||
| 549 | "public"=>1 |
||
| 550 | ])->whereBetween( |
||
| 551 | 'create_time', [ |
||
| 552 | date("Y-m-d H:i:s",time()-59), |
||
| 553 | date("Y-m-d H:i:s") |
||
| 554 | ] |
||
| 555 | )->first(); |
||
| 556 | } |
||
| 557 | |||
| 558 | public function getlatestClarification($cid) |
||
| 565 | } |
||
| 566 | |||
| 567 | public function getClarificationDetail($ccid) |
||
| 568 | { |
||
| 569 | return DB::table("contest_clarification")->where([ |
||
| 570 | "ccid"=>$ccid, |
||
| 571 | "public"=>1 |
||
| 572 | ])->first(); |
||
| 573 | } |
||
| 574 | |||
| 575 | public function requestClarification($cid, $title, $content, $uid) |
||
| 576 | { |
||
| 577 | return DB::table("contest_clarification")->insertGetId([ |
||
| 578 | "cid"=>$cid, |
||
| 579 | "type"=>1, |
||
| 580 | "title"=>$title, |
||
| 581 | "content"=>$content, |
||
| 582 | "public"=>"0", |
||
| 583 | "uid"=>$uid, |
||
| 584 | "create_time"=>date("Y-m-d H:i:s") |
||
| 585 | ]); |
||
| 586 | } |
||
| 587 | |||
| 588 | public function isContestEnded($cid) |
||
| 591 | } |
||
| 592 | |||
| 593 | public function formatSubmitTime($date) |
||
| 594 | { |
||
| 595 | $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
||
| 596 | $lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
||
| 597 | |||
| 598 | $now=time(); |
||
| 599 | $unix_date=strtotime($date); |
||
| 600 | |||
| 601 | if (empty($unix_date)) { |
||
| 602 | return "Bad date"; |
||
| 603 | } |
||
| 604 | |||
| 605 | if ($now>$unix_date) { |
||
| 606 | $difference=$now-$unix_date; |
||
| 607 | $tense="ago"; |
||
| 608 | } else { |
||
| 609 | $difference=$unix_date-$now; |
||
| 610 | $tense="from now"; |
||
| 611 | } |
||
| 612 | |||
| 613 | for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) { |
||
| 614 | $difference/=$lengths[$j]; |
||
| 615 | } |
||
| 616 | |||
| 617 | $difference=round($difference); |
||
| 618 | |||
| 619 | if ($difference!=1) { |
||
| 620 | $periods[$j].="s"; |
||
| 621 | } |
||
| 622 | |||
| 623 | return "$difference $periods[$j] {$tense}"; |
||
| 624 | } |
||
| 625 | |||
| 626 | public function formatAbsTime($sec) |
||
| 645 | } |
||
| 646 | |||
| 647 | public function frozenTime($cid) |
||
| 651 | } |
||
| 652 | |||
| 653 | |||
| 654 | public function getContestRecord($cid) |
||
| 756 | ]; |
||
| 757 | } |
||
| 758 | |||
| 759 | public function registration($cid, $uid=0) |
||
| 760 | { |
||
| 761 | if ($uid==0) { |
||
| 762 | return []; |
||
| 763 | } |
||
| 764 | |||
| 765 | if ($contest_info["registration"]) { |
||
| 766 | // check if uid in registration, temp return 3 |
||
| 767 | return $isParticipant=DB::table("contest_participant")->where([ |
||
| 768 | "cid" => $cid, |
||
| 769 | "uid" => $uid, |
||
| 770 | "audit" => 1 |
||
| 771 | ])->first(); |
||
| 772 | } else { |
||
| 773 | return []; |
||
| 774 | } |
||
| 775 | } |
||
| 776 | |||
| 777 | public function judgeClearance($cid, $uid=0) |
||
| 778 | { |
||
| 779 | if ($uid==0) { |
||
| 780 | return 0; |
||
| 781 | } |
||
| 782 | |||
| 783 | $contest_started=DB::table("contest")->where("cid", $cid)->where("begin_time", "<", date("Y-m-d H:i:s"))->count(); |
||
| 784 | $contest_ended=DB::table("contest")->where("cid", $cid)->where("end_time", "<", date("Y-m-d H:i:s"))->count(); |
||
| 785 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
| 786 | |||
| 787 | if (!$contest_started) { |
||
| 788 | // not started or do not exist |
||
| 789 | return 0; |
||
| 790 | } |
||
| 791 | |||
| 792 | if ($contest_info["public"]) { |
||
| 793 | //public |
||
| 794 | if ($contest_ended) { |
||
| 795 | return 1; |
||
| 796 | } else { |
||
| 797 | if ($contest_info["registration"]) { |
||
| 798 | // check if uid in registration, temp return 3 |
||
| 799 | $isParticipant=DB::table("contest_participant")->where([ |
||
| 800 | "cid" => $cid, |
||
| 801 | "uid" => $uid, |
||
| 802 | "audit" => 1 |
||
| 803 | ])->count(); |
||
| 804 | if ($isParticipant) { |
||
| 805 | return 2; |
||
| 806 | } else { |
||
| 807 | return 0; |
||
| 808 | } |
||
| 809 | } else { |
||
| 810 | return 2; |
||
| 811 | } |
||
| 812 | } |
||
| 813 | } else { |
||
| 814 | //private |
||
| 815 | $isMember=DB::table("group_member")->where([ |
||
| 816 | "gid"=> $contest_info["gid"], |
||
| 817 | "uid"=> $uid |
||
| 818 | ])->where("role", ">", 0)->count(); |
||
| 819 | if (!$isMember) { |
||
| 820 | return 0; |
||
| 821 | } else { |
||
| 822 | if ($contest_info["registration"]) { |
||
| 823 | // check if uid in registration, temp return 3 |
||
| 824 | $isParticipant=DB::table("contest_participant")->where([ |
||
| 825 | "cid" => $cid, |
||
| 826 | "uid" => $uid, |
||
| 827 | "audit" => 1 |
||
| 828 | ])->count(); |
||
| 829 | if ($isParticipant) { |
||
| 830 | return 2; |
||
| 831 | } else { |
||
| 832 | return 0; |
||
| 833 | } |
||
| 834 | } else { |
||
| 835 | return 2; |
||
| 836 | } |
||
| 837 | } |
||
| 838 | } |
||
| 839 | } |
||
| 840 | |||
| 841 | public function judgeOutsideClearance($cid, $uid=0) |
||
| 842 | { |
||
| 843 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
| 844 | if (empty($contest_info)) { |
||
| 845 | return 0; |
||
| 846 | } |
||
| 847 | if ($contest_info["public"]) { |
||
| 848 | return 1; |
||
| 849 | } else { |
||
| 850 | if ($uid==0) { |
||
| 851 | return 0; |
||
| 852 | } |
||
| 853 | return DB::table("group_member")->where([ |
||
| 854 | "gid"=> $contest_info["gid"], |
||
| 855 | "uid"=> $uid |
||
| 856 | ])->where("role", ">", 0)->count() ? 1 : 0; |
||
| 857 | } |
||
| 858 | } |
||
| 859 | |||
| 860 | public function contestName($cid) |
||
| 863 | } |
||
| 864 | |||
| 865 | public function contestRule($cid) |
||
| 868 | } |
||
| 869 | |||
| 870 | public function arrangeContest($gid, $config, $problems) |
||
| 906 | } |
||
| 907 | } |
||
| 908 |