-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubmit-code.php
129 lines (122 loc) · 4.92 KB
/
submit-code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
session_start();
if(!isset($_SESSION["user"]))
{
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thor · Submit Code</title>
<link rel="stylesheet" href="css/nav.css">
<link rel="stylesheet" href="css/submitCode.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="ace/src-min/ace.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<nav>
<h1 class="title">
Thor
</h1>
<div class="links">
<a href="index.php">Rank List</a>
<a href="problems.php">Problems</a>
<?php
if(isset($_SESSION["user"])){
echo '<a href="submit-code.php"><span>Submit Code</span></a>';
echo '<a href="javascript:void(0);"><span>'.$_SESSION["user"].'</span></a>';
echo '<a href="logout.php"><span>Logout</span></a>';
}
else{
echo '<a href="login.php"><span>Login</span></a>';
echo '<a href="register.php"><span>Register</span></a>';
}
?>
</div>
</nav>
<section class="section1">
<h1>Submit Code</h1>
<form action="#" class="inputs-container">
<div class="problemID">
<label>Problem ID - </label><input type="text" name="problemID" autocomplete="off">
</div>
<div class="language">
<label>Language - </label>
<select name="language">
<option value="cpp">C++</option>
<option value="py">Python</option>
<option value="c">C</option>
<option value="java">Java</option>
</select>
</div>
<div class="ace-code">
<label>Your code</label><br>
<div id="editor">#include</div>
<textarea id="code" name="submittedCode"></textarea><br>
</div>
<div class="submitButton">
<input type="submit" name="submit" required>
</div>
</form>
<p class="result"></p>
</section>
<script>
//Configure the Code Editor
$("#code").css("display","none");//hide textarea
var editor = ace.edit("editor");
editor.setTheme("ace/theme/xcode");
editor.getSession().setMode("ace/mode/c_cpp");
//Handle default code in code editor
var content = "#include <iostream>\nusing namespace std;\n\nint main() {\n //your code goes here\n return 0;\n}";
editor.setValue(content,1);
//Handle language change in code editor
$('form select').on('change', function(){
var lang = $(this).val();
if(lang==="cpp"){
editor.getSession().setMode("ace/mode/c_cpp");
content = "#include <iostream>\nusing namespace std;\n\nint main() {\n //your code goes here\n return 0;\n}";
editor.setValue(content,1);
}
else if(lang==="c"){
editor.getSession().setMode("ace/mode/c_cpp");
content = "#include <stdio.h>\n\nint main(void) {\n //your code goes here\n return 0;\n}";
editor.setValue(content,1);
}
else if(lang==="py"){
editor.getSession().setMode("ace/mode/python");
content = "# your code goes here";
editor.setValue(content,1);
}
else if(lang==="java"){
editor.getSession().setMode("ace/mode/java");
content = "/* package whatever; //don't place package name! */\n\nimport java.util.*;\nimport java.lang.*;\nimport java.io.*;\n\n/* Name of the class has to be \"Main\" only. */\n\nclass Main\n{ public static void main (String[] args) throws java.lang.Exception\n {\n // your code goes here\n }\n}";
editor.setValue(content,1);
}
});
//Handle AJAX Call
$("form").on("submit",function(e){
e.preventDefault();
$("p.result").html("Checking...");
$("#code").val(editor.getSession().getValue());//enter code editor code to text area to POST to API
var data = $(this).serialize();
data += "&submit=yes";
//console.log(data);
$.post("api/result.php",data,function(response){
//console.log(response);
if(response){
$("p.result").html("Verdict: "+response);
}
else{
$("p.result").html("Some error, try again in some time.");
}
});
});
</script>
</body>
<?php
mysqli_close($conn);
?>
</html>