PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/f_resp1_bk.php

https://bitbucket.org/krishna2793/aces
PHP | 260 lines | 172 code | 69 blank | 19 comment | 19 complexity | 916299191a6e524933fbfbc881ecce7c MD5 | raw file
  1. <?php session_start();
  2. /* $User_id = $_SESSION['user_id']; //stu_id in this case..
  3. */
  4. //-- Getting the session-id
  5. $Session_id = session_id();
  6. //-- Assigning temporary file names
  7. $user_op = $Session_id."op.txt";
  8. $user_err = $Session_id."err.txt";
  9. $user_runerr = $Session_id."runerr.txt";
  10. //-- Assignment of variables that are posted from previous page
  11. $p_id_temp =$_SESSION['ARR'][$_SESSION['iter']];
  12. $ip_name=$p_id_temp."ip.txt";
  13. $try = $_POST[id];
  14. $op = $_POST[drop];
  15. $up = $_POST[filename];
  16. $main_class_name = $_POST[j_m];
  17. $input_file = $_POST[pno];
  18. //-- Fetching the input file name given during the problem creation
  19. $std_ip_name=mysql_result(mysql_query("select input_name from code_problem_pool where prob_id='$p_id_temp'"),0);
  20. //-- Copying the input file from the stored location to the current location (In the given input file name)
  21. exec("chmod 0777 /var/www/questionsetter -R");
  22. exec("chmod 0777 /var/www/upload -R");
  23. shell_exec("cp questionsetter/$ip_name $std_ip_name") ;
  24. //-- Uploading the User Submitted code
  25. if ($_FILES["file"]["error"] > 0)
  26. {
  27. echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  28. }
  29. else
  30. {
  31. $target_path = "upload/";
  32. $target_path = $target_path . basename( $_FILES['file']['name']);
  33. chmod($_FILES["file"]["tmp_name"],0777);
  34. move_uploaded_file($_FILES["file"]["tmp_name"],
  35. $target_path);
  36. }
  37. $path ="upload/" . $_FILES["file"]["name"];
  38. $fil = $_FILES["file"]["name"];
  39. exec("chmod 0777 /var/www/questionsetter -R");
  40. exec("chmod 0777 /var/www/upload -R");
  41. //-- Checking which language has been selected. Then the file uploaded is compiled and executed
  42. printf("\n");
  43. //-- Option 1 is selected - C-Language
  44. if($op == 1)
  45. {
  46. shell_exec("gcc $path 2>$user_err") ;
  47. shell_exec("./a.out >$user_op") ;
  48. shell_exec("./a.out 2>$user_runerr");
  49. }
  50. //-- Option 2 is selected - C++ Language
  51. else if($op == 2)
  52. {
  53. shell_exec("g++ $path 2>$user_err");
  54. shell_exec("./a.out >$user_op");
  55. shell_exec("./a.out 2>$user_runerr");
  56. }
  57. //-- Option 3 is selected - Java Language //-- All java files are zipped into a file and uploaded
  58. else if($op == 3)
  59. {
  60. //-- Using ZipArchive class to extract the file
  61. $zip = new ZipArchive;
  62. $res = $zip->open($path);
  63. if ($res === TRUE) {
  64. //-- extract it to the path we determined above
  65. $zip->extractTo('/var/www/myzipfiles') or die ('extraction failed');
  66. $zip->close();
  67. }
  68. else {
  69. echo "couldn't open $path";
  70. }
  71. //-- Moving the input file into the class path of java
  72. shell_exec("mv $std_ip_name /var/www/myzipfiles/$std_ip_name");
  73. $theclasspath="/var/www/myzipfiles";
  74. shell_exec("javac /var/www/myzipfiles/$main_class_name 2>$user_err");
  75. $main_class_name = substr($main_class_name,0,-5);
  76. shell_exec("java -cp ./myzipfiles $main_class_name >$user_op");
  77. shell_exec("java -cp ./myzipfiles $main_class_name 2>$user_runerr");
  78. }
  79. //-- Opening the required files to read
  80. $f=fopen($path,'r') or die('cant openf') ; //..user code
  81. $fp = fopen($user_op,'r') or die('cant openfp') ; //..user code output
  82. $fp1=fopen($user_err,'r'); //..user code error
  83. $fp2=fopen($user_runerr,'r') or die('no.. am here!'); //..user code run time error
  84. //-- Displaying the submitted code
  85. $code = file_get_contents($path);
  86. echo '<br>'."Your code:".'<br>'.$code.'<br>';
  87. //-- If there are compilation errors they are displayed and the program will not execute other cases.
  88. if( filesize($user_err)!=0)
  89. {
  90. echo '<br>'."Error generated by your code:".'<br><br>';
  91. $data_err=fread($fp1,filesize($user_err));
  92. echo $data_err;
  93. }
  94. //-- If there are any run-time errors they are displayed.
  95. else if(filesize($user_runerr)!=0)
  96. {
  97. echo '<br>'."RunTime Error generated by your code:".'<br><br>';
  98. $data_rerr=fread($fp2,filesize($user_runerr)) or die('here is it');
  99. echo $data_rerr;
  100. }
  101. //-- Otherwise output is printed
  102. else
  103. {
  104. echo '<br>'."Output generated by your code:".'<br><br>';
  105. $data=fread($fp,filesize($user_op));
  106. echo $data;
  107. //-- Retrieving the correct output file and the user's output file
  108. $out_test = mysql_result(mysql_query("select output_url from code_problem_pool where prob_id=$p_id_temp") ,0);
  109. $sample_op = $user_op;
  110. $ot = fopen($out_test,'r') ;
  111. $op = fopen($user_op,'r');
  112. echo fread($ot,filesize($out_test));
  113. //-- Getting the number of lines in the test case
  114. $lines_tc = mysql_result(mysql_query("select num_of_lines_per_tc from code_problem_pool where prob_id=$p_id_temp") ,0);
  115. $f_ot = file_get_contents($out_test);
  116. $f_op = file_get_contents($user_op);
  117. // ot is the file given by Question setter
  118. $lines_ot = preg_split('/[\n]+/', $f_ot, -1, PREG_SPLIT_NO_EMPTY);
  119. $i = 0;
  120. $limit = 10;
  121. $count1 = count($lines_ot);
  122. while ($i < $count1) {
  123. $words_ot[$i] = preg_split('/[\s]+/',$lines_ot[$i], -1, PREG_SPLIT_NO_EMPTY);
  124. ++$i;
  125. }
  126. // op is the user program output
  127. $lines_op = preg_split('/[\n]+/', $f_op, -1, PREG_SPLIT_NO_EMPTY);
  128. $i = 0;
  129. $limit = 10;
  130. $count2 = count($lines_op);
  131. while ($i < $count2) {
  132. $words_op[$i] = preg_split('/[\s]+/',$lines_op[$i], -1, PREG_SPLIT_NO_EMPTY);
  133. ++$i;
  134. }
  135. //compare...
  136. $i=0;
  137. $lines_match=0;
  138. $temp=0;
  139. while(($i<$count1)&&($i<$count2))
  140. {
  141. $flag = 1;
  142. $j=0;
  143. if(count($words_ot[$i])!=count($words_op[$i]))
  144. $flag=0;
  145. else
  146. {
  147. while($j<count($words_ot[$i]))
  148. {
  149. if($words_ot[$i][$j] != $words_op[$i][$j])
  150. {
  151. $flag=0;
  152. break;
  153. }
  154. ++$j;
  155. }
  156. }
  157. if($flag==1)
  158. {
  159. ++$temp;
  160. if($temp==$lines_tc)
  161. {
  162. ++$lines_match;
  163. $temp=0;
  164. }
  165. }
  166. if(($flag==0)&&($temp<$lines_tc))
  167. {
  168. break;
  169. }
  170. ++$i;
  171. }
  172. $num_of_tc = $count1/$lines_tc;
  173. $result=($lines_match/$num_of_tc)*100;
  174. $temp12 = $_SESSION['ARR'][$_SESSION['iter']];
  175. echo 'Prob:id'.$temp12;
  176. echo "<br><br> Score ".$result;
  177. mysql_query("insert into each_prob_score values('$_SESSION[user_id]','$_SESSION[test_id]',$temp12,'$result')") or die("sadadakjdsakldhajksdhaksjdhaklsjdhjaksjdljakdk!!!!!!!!!!!!!!!".mysql_error());
  178. //store score in user_gradebook_coding
  179. }
  180. ?>