CompilerModel::list()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
// use League\Flysystem\Exception;
8
use Exception;
9
10
class CompilerModel extends Model
11
{
12
    protected $tableName='compiler';
13
14
    public function list($oid=1, $pid=null)
15
    {
16
        $special=null;
17
        if ($pid) {
18
            $special=DB::table("problem")->where(['pid'=>$pid])->select(['special_compiler'])->first();
19
        }
20
        $t=DB::table($this->tableName)->where(["oid"=>$oid, "available"=>1, "deleted"=>0]);
21
        if ($special && $special['special_compiler']) {
22
            $t=$t->whereIn('coid', explode(',', $special['special_compiler']));
23
        }
24
        $compiler_list=$t->orderBy('display_name')->get()->all();
25
        return $compiler_list;
26
    }
27
28
    public function pref($compiler_list, $pid, $uid, $cid=null)
29
    {
30
        $countCompilerList=count($compiler_list);
31
        $pref=-1;
32
        $precise=true;
33
        // get user pref of this problem for compilers
34
        $temp_last_submission=DB::table("submission")->where(["pid"=>$pid, "uid"=>$uid, "cid"=>$cid])->orderBy('submission_date', 'desc')->first();
35
        if (empty($temp_last_submission)) {
36
            // get user pref of this OJ for compilers
37
            $problemModel=new ProblemModel();
38
            $oid=$problemModel->oid($pid);
39
            $temp_last_submission=DB::table("submission")->join("problem", "submission.pid", "=", "problem.pid")->where(["OJ"=>$oid, "uid"=>$uid])->orderBy('submission_date', 'desc')->first();
40
            if (empty($temp_last_submission)) {
41
                // get user pref for compilers
42
                $temp_last_submission=DB::table("submission")->where(["uid"=>$uid])->orderBy('submission_date', 'desc')->first();
43
                if (empty($temp_last_submission)) {
44
                    return [
45
                        "pref"=>$pref,
46
                        "code"=>""
47
                    ];
48
                }
49
            }
50
            $precise=false;
51
        }
52
        $last_submission=$temp_last_submission;
53
        if ($precise) {
54
            $ret["code"]=$last_submission["solution"];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.
Loading history...
55
            $ret['code']=str_replace('\\', '\\\\', $ret['code']);
56
            $ret['code']=str_replace("\r\n", "\\n", $ret['code']);
57
            $ret['code']=str_replace("\n", "\\n", $ret['code']);
58
            $ret['code']=str_replace("\"", "\\\"", $ret['code']);
59
            $ret['code']=str_replace("<", "\<", $ret['code']);
60
            $ret['code']=str_replace(">", "\>", $ret['code']);
61
        } else {
62
            $ret["code"]="";
63
        }
64
        $ret["coid"]=$last_submission["coid"];
65
        $ret["detail"]=$this->detail($last_submission["coid"]);
66
        // match precise compiler
67
        for ($i=0; $i<$countCompilerList; $i++) {
68
            if ($compiler_list[$i]["coid"]==$ret["coid"]) {
69
                $pref=$i;
70
                break;
71
            }
72
        }
73
        if ($pref==-1) {
74
            // precise compiler is dead, use  other compiler with same lang
75
            for ($i=0; $i<$countCompilerList; $i++) {
76
                if ($compiler_list[$i]["lang"]==$ret["detail"]["lang"]) {
77
                    $pref=$i;
78
                    break;
79
                }
80
            }
81
        }
82
        if ($pref==-1) {
83
            // same lang compilers are all dead, use other compiler within the same group
84
            for ($i=0; $i<$countCompilerList; $i++) {
85
                if ($compiler_list[$i]["comp"]==$ret["detail"]["comp"]) {
86
                    $pref=$i;
87
                    break;
88
                }
89
            }
90
        }
91
        // the entire comp group dead
92
        $ret["pref"]=$pref;
93
        return $ret;
94
    }
95
96
    public function detail($coid)
97
    {
98
        return DB::table($this->tableName)->where(["coid"=>$coid])->first();
99
    }
100
101
    public static function add($row)
102
    {
103
        if (self::checkExist([
104
            "oid"=>$row["oid"],
105
            "lcode"=>$row["lcode"],
106
            "deleted"=>0
107
        ])) {
108
            throw new Exception("Duplicate Language Code");
109
        }
110
        return DB::table('compiler')->insert($row);
111
    }
112
113
    public static function remove($filter)
114
    {
115
        return DB::table('compiler')->where($filter)->update([
116
            "deleted"=>1
117
        ]);
118
    }
119
120
    public static function modify($filter, $row)
121
    {
122
        $filter["deleted"]=0;
123
        return DB::table('compiler')->where($filter)->update($row);
124
    }
125
126
    public static function checkExist($filter)
127
    {
128
        return boolval(DB::table('compiler')->where($filter)->count());
129
    }
130
}
131