| Total Complexity | 48 |
| Total Lines | 551 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 9 | class SubmissionModel extends Model |
||
| 10 | { |
||
| 11 | protected $tableName='submission'; |
||
| 12 | protected $table='submission'; |
||
| 13 | protected $primaryKey='sid'; |
||
| 14 | const DELETED_AT=null; |
||
| 15 | const UPDATED_AT=null; |
||
| 16 | const CREATED_AT=null; |
||
| 17 | |||
| 18 | public $colorScheme=[ |
||
| 19 | "Waiting" => "wemd-blue-text", |
||
| 20 | "Judge Error" => "wemd-black-text", |
||
| 21 | "System Error" => "wemd-black-text", |
||
| 22 | "Compile Error" => "wemd-orange-text", |
||
| 23 | "Runtime Error" => "wemd-red-text", |
||
| 24 | "Wrong Answer" => "wemd-red-text", |
||
| 25 | "Time Limit Exceed" => "wemd-deep-purple-text", |
||
| 26 | "Real Time Limit Exceed" => "wemd-deep-purple-text", |
||
| 27 | "Accepted" => "wemd-green-text", |
||
| 28 | "Memory Limit Exceed" => "wemd-deep-purple-text", |
||
| 29 | "Presentation Error" => "wemd-red-text", |
||
| 30 | "Submitted" => "wemd-blue-text", |
||
| 31 | "Pending" => "wemd-blue-text", |
||
| 32 | "Judging" => "wemd-blue-text", |
||
| 33 | "Partially Accepted" => "wemd-cyan-text", |
||
| 34 | 'Submission Error' => 'wemd-black-text', |
||
| 35 | 'Output Limit Exceeded' => 'wemd-deep-purple-text', |
||
| 36 | "Idleness Limit Exceed" => 'wemd-deep-purple-text' |
||
| 37 | ]; |
||
| 38 | public $langConfig=[]; |
||
| 39 | |||
| 40 | public function __construct() |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | public function insert($sub) |
||
| 304 | } |
||
| 305 | |||
| 306 | public function getJudgeStatus($sid, $uid) |
||
| 307 | { |
||
| 308 | $status=DB::table($this->tableName)->where(['sid'=>$sid])->first(); |
||
| 309 | if (empty($status)) { |
||
| 310 | return []; |
||
| 311 | } |
||
| 312 | if ($status["share"]==1 && $status["cid"]) { |
||
| 313 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$status["cid"]])->select("end_time")->first()["end_time"]); |
||
| 314 | if (time()<$end_time) { |
||
| 315 | $status["solution"]=null; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | if ($status["share"]==0 && $status["uid"]!=$uid) { |
||
| 319 | $status["solution"]=null; |
||
| 320 | } |
||
| 321 | $compilerModel=new CompilerModel(); |
||
| 322 | $status["lang"]=$compilerModel->detail($status["coid"])["lang"]; |
||
| 323 | $status["owner"]=$uid==$status["uid"]; |
||
| 324 | return $status; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function downloadCode($sid, $uid) |
||
| 328 | { |
||
| 329 | $status=DB::table($this->tableName)->where(['sid'=>$sid])->first(); |
||
| 330 | if (empty($status) || ($status["share"]==0 && $status["uid"]!=$uid)) { |
||
| 331 | return []; |
||
| 332 | } |
||
| 333 | $lang=DB::table("compiler")->where(['coid'=>$status["coid"]])->first()["lang"]; |
||
| 334 | $curLang=isset($this->langConfig[$lang]) ? $this->langConfig[$lang] : $this->langConfig["plaintext"]; |
||
| 335 | return [ |
||
| 336 | "content"=>$status["solution"], |
||
| 337 | "name"=>$status["submission_date"].$curLang["extensions"][0], |
||
| 338 | ]; |
||
| 339 | } |
||
| 340 | |||
| 341 | public function getProblemStatus($pid, $uid, $cid=null) |
||
| 342 | { |
||
| 343 | if ($cid) { |
||
| 344 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
| 345 | // Get the very first AC record |
||
| 346 | $ac=DB::table($this->tableName)->where([ |
||
| 347 | 'pid'=>$pid, |
||
| 348 | 'uid'=>$uid, |
||
| 349 | 'cid'=>$cid, |
||
| 350 | 'verdict'=>'Accepted' |
||
| 351 | ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first(); |
||
| 352 | if (empty($ac)) { |
||
| 353 | $pac=DB::table($this->tableName)->where([ |
||
| 354 | 'pid'=>$pid, |
||
| 355 | 'uid'=>$uid, |
||
| 356 | 'cid'=>$cid, |
||
| 357 | 'verdict'=>'Partially Accepted' |
||
| 358 | ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first(); |
||
| 359 | 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; |
||
| 360 | } else { |
||
| 361 | return $ac; |
||
| 362 | } |
||
| 363 | } else { |
||
| 364 | $ac=DB::table($this->tableName)->where([ |
||
| 365 | 'pid'=>$pid, |
||
| 366 | 'uid'=>$uid, |
||
| 367 | 'cid'=>$cid, |
||
| 368 | 'verdict'=>'Accepted' |
||
| 369 | ])->orderBy('submission_date', 'desc')->first(); |
||
| 370 | return empty($ac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->first() : $ac; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | public function getProblemSubmission($pid, $uid, $cid=null) |
||
| 375 | { |
||
| 376 | $statusList=DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->limit(10)->get()->all(); |
||
| 377 | return $statusList; |
||
| 378 | } |
||
| 379 | |||
| 380 | public function countSolution($s) |
||
| 381 | { |
||
| 382 | return DB::table($this->tableName)->where(['solution'=>$s])->count(); |
||
| 383 | } |
||
| 384 | |||
| 385 | public function getEarliestSubmission($oid) |
||
| 386 | { |
||
| 387 | return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 388 | ->select("sid", "OJ as oid", "remote_id", "cid", "jid") |
||
| 389 | ->where(['verdict'=>'Waiting', 'OJ'=>$oid]) |
||
| 390 | ->orderBy("sid", "asc") |
||
| 391 | ->first(); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function countEarliestWaitingSubmission($oid) |
||
| 405 | } |
||
| 406 | |||
| 407 | |||
| 408 | public function getWaitingSubmission() |
||
| 409 | { |
||
| 410 | return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 411 | ->select("sid", "OJ as oid", "remote_id", "cid", "jid") |
||
| 412 | ->where(['verdict'=>'Waiting']) |
||
| 413 | ->get(); |
||
| 414 | } |
||
| 415 | |||
| 416 | public function countWaitingSubmission($oid) |
||
| 417 | { |
||
| 418 | return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
||
| 419 | ->where(['verdict'=>'Waiting', 'OJ'=>$oid]) |
||
| 420 | ->count(); |
||
| 421 | } |
||
| 422 | |||
| 423 | public function updateSubmission($sid, $sub) |
||
| 424 | { |
||
| 425 | if (isset($sub['verdict'])) { |
||
| 426 | $sub["color"]=$this->colorScheme[$sub['verdict']]; |
||
| 427 | } |
||
| 428 | return DB::table($this->tableName)->where(['sid'=>$sid])->update($sub); |
||
| 429 | } |
||
| 430 | |||
| 431 | public function formatSubmitTime($date) |
||
| 462 | } |
||
| 463 | |||
| 464 | public function getRecord($filter) |
||
| 465 | { |
||
| 466 | $paginator=DB::table("submission")->where([ |
||
| 467 | 'cid'=>null |
||
| 468 | ])->join( |
||
| 469 | "users", |
||
| 470 | "users.id", |
||
| 471 | "=", |
||
| 472 | "submission.uid" |
||
| 473 | )->join( |
||
| 474 | "problem", |
||
| 475 | "problem.pid", |
||
| 476 | "=", |
||
| 477 | "submission.pid" |
||
| 478 | )->select( |
||
| 479 | "sid", |
||
| 480 | "uid", |
||
| 481 | "problem.pid as pid", |
||
| 482 | "pcode", |
||
| 483 | "name", |
||
| 484 | "color", |
||
| 485 | "verdict", |
||
| 486 | "time", |
||
| 487 | "memory", |
||
| 488 | "language", |
||
| 489 | "score", |
||
| 490 | "submission_date", |
||
| 491 | "share" |
||
| 492 | )->orderBy( |
||
| 493 | 'submission_date', |
||
| 494 | 'desc' |
||
| 495 | ); |
||
| 496 | |||
| 497 | if($filter["pcode"]){ |
||
| 498 | $paginator=$paginator->where(["pcode"=>$filter["pcode"]]); |
||
| 499 | } |
||
| 500 | |||
| 501 | if($filter["result"]){ |
||
| 502 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
| 503 | } |
||
| 504 | |||
| 505 | if($filter["account"]){ |
||
| 506 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
| 507 | } |
||
| 508 | |||
| 509 | $paginator=$paginator->paginate(50); |
||
| 510 | |||
| 511 | |||
| 512 | $records=$paginator->all(); |
||
| 513 | foreach ($records as &$r) { |
||
| 514 | $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"])); |
||
| 515 | $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]); |
||
| 516 | $r["nick_name"]=""; |
||
| 517 | } |
||
| 518 | return [ |
||
| 519 | "paginator"=>$paginator, |
||
| 520 | "records"=>$records |
||
| 521 | ]; |
||
| 522 | } |
||
| 523 | |||
| 524 | public function share($sid, $uid) |
||
| 535 | ]; |
||
| 536 | } |
||
| 537 | |||
| 538 | public function sharePB($sid, $uid) |
||
| 560 | ]; |
||
| 561 | } |
||
| 564 |