1/**
2 * Choose File Input Field
3 */
4
5function inputFileKey(problem_name) {
6    $realInputField = document.getElementById("code-" + problem_name);
7    $realInputField.click();
8}
9function changeFilePath(problem_name) {
10    $realInputField = document.getElementById("code-" + problem_name);
11    document.getElementById("code-file-" + problem_name).value = $realInputField.files[0].name;
12}
13
14/**
15 * Ajax Part
16 */
17
18function resultRefresh(problem_name, type, user) {
19    if (user === "null" && type === "test-case") {
20        $url = DOKU_BASE + "lib/exe/ajax.php";
21        jQuery.post(
22            $url,
23            {
24                call: 'plugin_judge',
25                name: 'scoreboardRefresh',
26                questions: problem_name
27            },
28            function (data) {
29                document.getElementById("scoreboard-row-" + problem_name.toString()).innerHTML = data;
30            },
31            'json'
32        );
33    } else {
34        $url = DOKU_BASE + "lib/exe/ajax.php";
35        jQuery.post(
36            $url,
37            {
38                call: 'plugin_judge',
39                name: 'resultRefresh',
40                user: user,
41                problem_name: problem_name,
42                type: type
43            },
44            function (data) {
45                document.getElementById("result-row-" + problem_name).innerHTML = data;
46            },
47            'json'
48        );
49    }
50}
51
52var problem_name;
53var user;
54var type;
55var language;
56var runtime;
57var path;
58var problem_name_id;
59
60function submitKey(problem_name, user, language, type, runtime, path) {
61
62    /**
63     * set variables form inputs
64     */
65    window["problem_name"] = problem_name;
66    window["user"] = user;
67    window["type"] = type;
68    window["language"] = language;
69    window["runtime"] = runtime;
70    window["path"] = path;
71    window["problem_name_id"] = problem_name.replace(/:/g, "\\:");
72
73    if (type === "output-only" && !document.getElementById("user-output-" + problem_name).value) {
74        document.getElementById("result-label-" + problem_name).innerHTML = LANG.plugins.judge['error'];
75        document.getElementById("result-" + problem_name).innerHTML = LANG.plugins.judge['answer_not_specified'];
76        document.getElementById("result-" + problem_name).className = "false";
77        return 0;
78    }
79    if (type === "test-case" && !document.getElementById("code-file-" + problem_name).value) {
80        document.getElementById("result-label-" + problem_name).innerHTML = LANG.plugins.judge['error'];
81        document.getElementById("result-" + problem_name).innerHTML = LANG.plugins.judge['file_not_specified'];
82        document.getElementById("result-" + problem_name).className = "false";
83        return 0;
84    }
85
86    if (type === "test-case") {
87        if (language == "all") {
88            window["language"] = document.getElementById("language-" + problem_name).value;
89        }
90        testCaseUpload();
91    } else {
92        outputAnswer();
93    }
94}
95
96function testCaseUpload() {
97
98    $url = DOKU_BASE + "lib/exe/ajax.php";
99    var r = new FileReader();
100    r.onload = function (e) {
101        jQuery.post(
102            $url,
103            {
104                call: 'plugin_judge',
105                name: 'upload',
106                code: e.target.result,
107                path: path,
108                file_name: document.getElementById("code-" + problem_name).files[0].name
109            },
110            function (data) {
111                if (data[0] != null) {
112                    document.getElementById("result-label-" + problem_name).innerHTML = LANG.plugins.judge['error'];
113                    document.getElementById("result-" + problem_name).innerHTML = data;
114                    document.getElementById("result-" + problem_name).className = "false";
115                } else {
116                    testCaseSubmit();
117                }
118            },
119            'json'
120        );
121    };
122    r.readAsText(document.getElementById("code-" + problem_name).files[0]);
123
124}
125
126function testCaseSubmit() {
127    open();
128
129    $url = DOKU_BASE + "lib/exe/ajax.php";
130    jQuery.post(
131        $url,
132        {
133            call: 'plugin_judge',
134            name: 'submit',
135            user: user,
136            problem_name: problem_name,
137            type: type,
138            language: language,
139            runtime: runtime,
140            status_code: 0
141        },
142        function (data) {
143            appendResult("running", data.row_number, data.date);
144            judge(data.id);
145        },
146        'json'
147    );
148}
149
150function outputAnswer() {
151    open();
152
153    $url = DOKU_BASE + "lib/exe/ajax.php";
154    jQuery.post(
155        $url,
156        {
157            call: 'plugin_judge',
158            name: 'outputonlyResult',
159            user_output: document.getElementById("user-output-" + problem_name).value,
160            problem_name: problem_name
161        },
162        function (data) {
163            /**
164             * Append to Result Table
165             **/
166            outputSubmit(data[0]);
167        },
168        'json'
169    );
170}
171
172function outputSubmit($status) {
173
174    if ($status) {
175        $name = "true";
176        $fa_name = LANG.plugins.judge['correct'];
177        $status_code = 1;
178    } else {
179        $name = "false";
180        $fa_name = LANG.plugins.judge['wrong'];
181        $status_code = 0;
182    }
183
184    /**
185     * Show Result
186     **/
187    document.getElementById("result-label-" + problem_name).innerHTML = LANG.plugins.judge['answer_status'];
188    document.getElementById("result-" + problem_name).innerHTML = $fa_name;
189    document.getElementById("result-" + problem_name).className = $name;
190
191    $url = DOKU_BASE + "lib/exe/ajax.php";
192    jQuery.post(
193        $url,
194        {
195            call: 'plugin_judge',
196            name: 'submit',
197            user: user,
198            problem_name: problem_name,
199            type: type,
200            status_code: $status_code
201        },
202        function (data) {
203            appendResult($fa_name, data.row_number, data.date, data.id);
204        },
205        'json'
206    );
207
208}
209
210function appendResult(status, number, date) {
211
212    if (type == "test-case") {
213        var new_row = '<tr class="row1"><td class="col0">' + number + '</td><td class="col1">' + date + '</td><td class="col2">' + language + '</td><td class="col3"><div class="loader"></div>' + '</td></tr>';
214    }
215    else {
216        var new_row = '<tr class="row1"><td class="col0">' + number + '</td><td class="col1">' + date + '</td><td class="col2">' + status + '</td></tr>';
217    }
218
219    document.getElementById("result-row-" + problem_name).innerHTML += new_row;
220}
221
222function judge(id) {
223    $url = DOKU_BASE + "lib/exe/ajax.php";
224    var r = new FileReader();
225    r.onload = function (e) {
226        jQuery.post(
227            $url,
228            {
229                call: 'plugin_judge',
230                name: 'judge',
231                id: id,
232                code: e.target.result
233            }
234        );
235    };
236    r.readAsText(document.getElementById("code-" + problem_name).files[0]);
237}
238
239function open() {
240    /**
241     * display previous submissions button after new submit
242     */
243    if (jQuery("#previous_submissions-" + problem_name_id).css('display') == "none") {
244        jQuery("#previous_submissions-" + problem_name_id).slideToggle();
245    }
246
247    /**
248     * display previous submissions box contents after new submit
249     */
250    if (jQuery("#previous_submissions-table-" + problem_name_id).css('display') == "none") {
251        jQuery("#previous_submissions-table-" + problem_name_id).slideToggle();
252    }
253}