| Total Complexity | 53 |
| Total Lines | 571 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 4 | Features | 0 |
Complex classes like SubmissionModel 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 SubmissionModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class SubmissionModel extends Model |
||
| 11 | { |
||
| 12 | protected $tableName='submission'; |
||
| 13 | protected $table='submission'; |
||
| 14 | protected $primaryKey='sid'; |
||
| 15 | const DELETED_AT=null; |
||
| 16 | const UPDATED_AT=null; |
||
| 17 | const CREATED_AT=null; |
||
| 18 | |||
| 19 | public $colorScheme=[ |
||
| 20 | "Waiting" => "wemd-blue-text", |
||
| 21 | "Judge Error" => "wemd-black-text", |
||
| 22 | "System Error" => "wemd-black-text", |
||
| 23 | "Compile Error" => "wemd-orange-text", |
||
| 24 | "Runtime Error" => "wemd-red-text", |
||
| 25 | "Wrong Answer" => "wemd-red-text", |
||
| 26 | "Time Limit Exceed" => "wemd-deep-purple-text", |
||
| 27 | "Real Time Limit Exceed" => "wemd-deep-purple-text", |
||
| 28 | "Accepted" => "wemd-green-text", |
||
| 29 | "Memory Limit Exceed" => "wemd-deep-purple-text", |
||
| 30 | "Presentation Error" => "wemd-red-text", |
||
| 31 | "Submitted" => "wemd-blue-text", |
||
| 32 | "Pending" => "wemd-blue-text", |
||
| 33 | "Judging" => "wemd-blue-text", |
||
| 34 | "Partially Accepted" => "wemd-cyan-text", |
||
| 35 | 'Submission Error' => 'wemd-black-text', |
||
| 36 | 'Output Limit Exceeded' => 'wemd-deep-purple-text', |
||
| 37 | "Idleness Limit Exceed" => 'wemd-deep-purple-text' |
||
| 38 | ]; |
||
| 39 | public $langConfig=[]; |
||
| 40 | |||
| 41 | public function __construct() |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | public function insert($sub) |
||
| 305 | } |
||
| 306 | |||
| 307 | public function basic($sid) |
||
| 308 | { |
||
| 309 | return DB::table($this->tableName)->where(['sid'=>$sid])->first(); |
||
| 310 | } |
||
| 311 | |||
| 312 | public function getJudgeStatus($sid, $uid) |
||
| 313 | { |
||
| 314 | $status=$this->basic($sid); |
||
| 315 | if (empty($status)) { |
||
| 316 | return []; |
||
| 317 | } |
||
| 318 | if ($status["share"]==1 && $status["cid"]) { |
||
| 319 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$status["cid"]])->select("end_time")->first()["end_time"]); |
||
| 320 | if (time()<$end_time) { |
||
| 321 | $status["solution"]=null; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | if ($status["share"]==0 && $status["uid"]!=$uid) { |
||
| 325 | $status["solution"]=null; |
||
| 326 | } |
||
| 327 | $compilerModel=new CompilerModel(); |
||
| 328 | $status["lang"]=$compilerModel->detail($status["coid"])["lang"]; |
||
| 329 | $status["owner"]=$uid==$status["uid"]; |
||
| 330 | return $status; |
||
| 331 | } |
||
| 332 | |||
| 333 | public function downloadCode($sid, $uid) |
||
| 334 | { |
||
| 335 | $status=DB::table($this->tableName)->where(['sid'=>$sid])->first(); |
||
| 336 | if (empty($status) || ($status["share"]==0 && $status["uid"]!=$uid)) { |
||
| 337 | return []; |
||
| 338 | } |
||
| 339 | $lang=DB::table("compiler")->where(['coid'=>$status["coid"]])->first()["lang"]; |
||
| 340 | $curLang=isset($this->langConfig[$lang]) ? $this->langConfig[$lang] : $this->langConfig["plaintext"]; |
||
| 341 | return [ |
||
| 342 | "content"=>$status["solution"], |
||
| 343 | "name"=>$status["submission_date"].$curLang["extensions"][0], |
||
| 344 | ]; |
||
| 345 | } |
||
| 346 | |||
| 347 | public function getProblemStatus($pid, $uid, $cid=null) |
||
| 348 | { |
||
| 349 | if ($cid) { |
||
| 350 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
| 351 | // Get the very first AC record |
||
| 352 | $ac=DB::table($this->tableName)->where([ |
||
| 353 | 'pid'=>$pid, |
||
| 354 | 'uid'=>$uid, |
||
| 355 | 'cid'=>$cid, |
||
| 356 | 'verdict'=>'Accepted' |
||
| 357 | ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first(); |
||
| 358 | if (empty($ac)) { |
||
| 359 | $pac=DB::table($this->tableName)->where([ |
||
| 360 | 'pid'=>$pid, |
||
| 361 | 'uid'=>$uid, |
||
| 362 | 'cid'=>$cid, |
||
| 363 | 'verdict'=>'Partially Accepted' |
||
| 364 | ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first(); |
||
| 365 | return empty($pac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first() : $pac; |
||
| 366 | } else { |
||
| 367 | return $ac; |
||
| 368 | } |
||
| 369 | } else { |
||
| 370 | $ac=DB::table($this->tableName)->where([ |
||
| 371 | 'pid'=>$pid, |
||
| 372 | 'uid'=>$uid, |
||
| 373 | 'cid'=>$cid, |
||
| 374 | 'verdict'=>'Accepted' |
||
| 375 | ])->orderBy('submission_date', 'desc')->first(); |
||
| 376 | return empty($ac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->first() : $ac; |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | public function getProblemSubmission($pid, $uid, $cid=null) |
||
| 381 | { |
||
| 382 | $statusList=DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->limit(10)->get()->all(); |
||
| 383 | return $statusList; |
||
| 384 | } |
||
| 385 | |||
| 386 | public function countSolution($s) |
||
| 387 | { |
||
| 388 | return DB::table($this->tableName)->where(['solution'=>$s])->count(); |
||
| 389 | } |
||
| 390 | |||
| 391 | public function getEarliestSubmission($oid) |
||
| 392 | { |
||
| 393 | return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 394 | ->select("sid", "OJ as oid", "remote_id", "cid", "jid") |
||
| 395 | ->where(['verdict'=>'Waiting', 'OJ'=>$oid]) |
||
| 396 | ->orderBy("sid", "asc") |
||
| 397 | ->first(); |
||
| 398 | } |
||
| 399 | |||
| 400 | public function countEarliestWaitingSubmission($oid) |
||
| 401 | { |
||
| 402 | $early_sid=$this->getEarliestSubmission($oid); |
||
| 403 | if ($early_sid==null) { |
||
| 404 | return 0; |
||
| 405 | } |
||
| 406 | $early_sid=$early_sid["sid"]; |
||
| 407 | return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 408 | ->where(['OJ'=>$oid]) |
||
| 409 | ->where("sid", ">=", $early_sid) |
||
| 410 | ->count(); |
||
| 411 | } |
||
| 412 | |||
| 413 | |||
| 414 | public function getWaitingSubmission() |
||
| 415 | { |
||
| 416 | $ret=DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 417 | ->select("sid", "OJ as oid", "remote_id", "cid", "jid", "vcid", "problem.pid as pid") |
||
| 418 | ->where(['verdict'=>'Waiting']) |
||
| 419 | ->get() |
||
| 420 | ->all(); |
||
| 421 | foreach($ret as &$r){ |
||
| 422 | $r["ocode"]=DB::table("oj")->where(["oid"=>$r["oid"]])->first()["ocode"]; |
||
| 423 | } |
||
| 424 | return $ret; |
||
| 425 | } |
||
| 426 | |||
| 427 | public function countWaitingSubmission($oid) |
||
| 428 | { |
||
| 429 | return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 430 | ->where(['verdict'=>'Waiting', 'OJ'=>$oid]) |
||
| 431 | ->count(); |
||
| 432 | } |
||
| 433 | |||
| 434 | public function updateSubmission($sid, $sub) |
||
| 435 | { |
||
| 436 | if (isset($sub['verdict'])) { |
||
| 437 | $sub["color"]=$this->colorScheme[$sub['verdict']]; |
||
| 438 | } |
||
| 439 | $result = DB::table($this->tableName)->where(['sid'=>$sid])->update($sub); |
||
| 440 | $contestModel = new ContestModel(); |
||
| 441 | $submission_info = DB::table($this->tableName) -> where(['sid'=>$sid]) -> get() -> first(); |
||
| 442 | if ($result==1 && $submission_info['cid'] && $contestModel->isContestRunning($submission_info['cid'])){ |
||
| 443 | $sub['pid'] = $submission_info['pid']; |
||
| 444 | $sub['uid'] = $submission_info['uid']; |
||
| 445 | $sub['cid'] = $submission_info['cid']; |
||
| 446 | $sub['sid'] = $sid; |
||
| 447 | $contestModel->updateContestRankTable($submission_info['cid'],$sub); |
||
| 448 | } |
||
| 449 | return $result; |
||
| 450 | } |
||
| 451 | |||
| 452 | public function formatSubmitTime($date) |
||
| 483 | } |
||
| 484 | |||
| 485 | public function getRecord($filter) |
||
| 486 | { |
||
| 487 | $paginator=DB::table("submission")->where([ |
||
| 488 | 'cid'=>null |
||
| 489 | ])->join( |
||
| 490 | "users", |
||
| 491 | "users.id", |
||
| 492 | "=", |
||
| 493 | "submission.uid" |
||
| 494 | )->join( |
||
| 495 | "problem", |
||
| 496 | "problem.pid", |
||
| 497 | "=", |
||
| 498 | "submission.pid" |
||
| 499 | )->select( |
||
| 500 | "sid", |
||
| 501 | "uid", |
||
| 502 | "problem.pid as pid", |
||
| 503 | "pcode", |
||
| 504 | "name", |
||
| 505 | "color", |
||
| 506 | "verdict", |
||
| 507 | "time", |
||
| 508 | "memory", |
||
| 509 | "language", |
||
| 510 | "score", |
||
| 511 | "submission_date", |
||
| 512 | "share" |
||
| 513 | )->orderBy( |
||
| 514 | 'submission_date', |
||
| 515 | 'desc' |
||
| 516 | ); |
||
| 517 | |||
| 518 | if($filter["pcode"]){ |
||
| 519 | $paginator=$paginator->where(["pcode"=>$filter["pcode"]]); |
||
| 520 | } |
||
| 521 | |||
| 522 | if($filter["result"]){ |
||
| 523 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
| 524 | } |
||
| 525 | |||
| 526 | if($filter["account"]){ |
||
| 527 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
| 528 | } |
||
| 529 | |||
| 530 | $paginator=$paginator->paginate(50); |
||
| 531 | |||
| 532 | |||
| 533 | $records=$paginator->all(); |
||
| 534 | foreach ($records as &$r) { |
||
| 535 | $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"])); |
||
| 536 | $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]); |
||
| 537 | $r["nick_name"]=""; |
||
| 538 | } |
||
| 539 | return [ |
||
| 540 | "paginator"=>$paginator, |
||
| 541 | "records"=>$records |
||
| 542 | ]; |
||
| 543 | } |
||
| 544 | |||
| 545 | public function share($sid, $uid) |
||
| 556 | ]; |
||
| 557 | } |
||
| 558 | |||
| 559 | public function sharePB($sid, $uid) |
||
| 581 | ]; |
||
| 582 | } |
||
| 583 | } |
||
| 584 | } |
||
| 585 |