/f_resp1_bk.php
PHP | 260 lines | 172 code | 69 blank | 19 comment | 19 complexity | 916299191a6e524933fbfbc881ecce7c MD5 | raw file
- <?php session_start();
-
- /* $User_id = $_SESSION['user_id']; //stu_id in this case..
- */
- //-- Getting the session-id
- $Session_id = session_id();
- //-- Assigning temporary file names
- $user_op = $Session_id."op.txt";
- $user_err = $Session_id."err.txt";
- $user_runerr = $Session_id."runerr.txt";
- //-- Assignment of variables that are posted from previous page
- $p_id_temp =$_SESSION['ARR'][$_SESSION['iter']];
-
- $ip_name=$p_id_temp."ip.txt";
-
- $try = $_POST[id];
- $op = $_POST[drop];
- $up = $_POST[filename];
- $main_class_name = $_POST[j_m];
- $input_file = $_POST[pno];
- //-- Fetching the input file name given during the problem creation
- $std_ip_name=mysql_result(mysql_query("select input_name from code_problem_pool where prob_id='$p_id_temp'"),0);
-
- //-- Copying the input file from the stored location to the current location (In the given input file name)
- exec("chmod 0777 /var/www/questionsetter -R");
- exec("chmod 0777 /var/www/upload -R");
-
- shell_exec("cp questionsetter/$ip_name $std_ip_name") ;
- //-- Uploading the User Submitted code
- if ($_FILES["file"]["error"] > 0)
- {
- echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
- }
- else
- {
-
- $target_path = "upload/";
- $target_path = $target_path . basename( $_FILES['file']['name']);
- chmod($_FILES["file"]["tmp_name"],0777);
-
- move_uploaded_file($_FILES["file"]["tmp_name"],
- $target_path);
-
- }
- $path ="upload/" . $_FILES["file"]["name"];
- $fil = $_FILES["file"]["name"];
- exec("chmod 0777 /var/www/questionsetter -R");
- exec("chmod 0777 /var/www/upload -R");
- //-- Checking which language has been selected. Then the file uploaded is compiled and executed
- printf("\n");
-
- //-- Option 1 is selected - C-Language
- if($op == 1)
- {
- shell_exec("gcc $path 2>$user_err") ;
- shell_exec("./a.out >$user_op") ;
- shell_exec("./a.out 2>$user_runerr");
- }
- //-- Option 2 is selected - C++ Language
- else if($op == 2)
- {
- shell_exec("g++ $path 2>$user_err");
- shell_exec("./a.out >$user_op");
- shell_exec("./a.out 2>$user_runerr");
- }
- //-- Option 3 is selected - Java Language //-- All java files are zipped into a file and uploaded
- else if($op == 3)
- {
- //-- Using ZipArchive class to extract the file
- $zip = new ZipArchive;
- $res = $zip->open($path);
- if ($res === TRUE) {
- //-- extract it to the path we determined above
- $zip->extractTo('/var/www/myzipfiles') or die ('extraction failed');
- $zip->close();
- }
- else {
- echo "couldn't open $path";
-
- }
- //-- Moving the input file into the class path of java
- shell_exec("mv $std_ip_name /var/www/myzipfiles/$std_ip_name");
- $theclasspath="/var/www/myzipfiles";
- shell_exec("javac /var/www/myzipfiles/$main_class_name 2>$user_err");
- $main_class_name = substr($main_class_name,0,-5);
- shell_exec("java -cp ./myzipfiles $main_class_name >$user_op");
- shell_exec("java -cp ./myzipfiles $main_class_name 2>$user_runerr");
- }
- //-- Opening the required files to read
- $f=fopen($path,'r') or die('cant openf') ; //..user code
- $fp = fopen($user_op,'r') or die('cant openfp') ; //..user code output
- $fp1=fopen($user_err,'r'); //..user code error
- $fp2=fopen($user_runerr,'r') or die('no.. am here!'); //..user code run time error
- //-- Displaying the submitted code
-
- $code = file_get_contents($path);
- echo '<br>'."Your code:".'<br>'.$code.'<br>';
- //-- If there are compilation errors they are displayed and the program will not execute other cases.
- if( filesize($user_err)!=0)
- {
- echo '<br>'."Error generated by your code:".'<br><br>';
- $data_err=fread($fp1,filesize($user_err));
- echo $data_err;
- }
- //-- If there are any run-time errors they are displayed.
- else if(filesize($user_runerr)!=0)
- {
- echo '<br>'."RunTime Error generated by your code:".'<br><br>';
- $data_rerr=fread($fp2,filesize($user_runerr)) or die('here is it');
- echo $data_rerr;
- }
- //-- Otherwise output is printed
- else
- {
- echo '<br>'."Output generated by your code:".'<br><br>';
- $data=fread($fp,filesize($user_op));
- echo $data;
- //-- Retrieving the correct output file and the user's output file
- $out_test = mysql_result(mysql_query("select output_url from code_problem_pool where prob_id=$p_id_temp") ,0);
-
- $sample_op = $user_op;
- $ot = fopen($out_test,'r') ;
- $op = fopen($user_op,'r');
- echo fread($ot,filesize($out_test));
- //-- Getting the number of lines in the test case
- $lines_tc = mysql_result(mysql_query("select num_of_lines_per_tc from code_problem_pool where prob_id=$p_id_temp") ,0);
-
- $f_ot = file_get_contents($out_test);
- $f_op = file_get_contents($user_op);
- // ot is the file given by Question setter
- $lines_ot = preg_split('/[\n]+/', $f_ot, -1, PREG_SPLIT_NO_EMPTY);
- $i = 0;
- $limit = 10;
- $count1 = count($lines_ot);
- while ($i < $count1) {
- $words_ot[$i] = preg_split('/[\s]+/',$lines_ot[$i], -1, PREG_SPLIT_NO_EMPTY);
-
- ++$i;
-
- }
- // op is the user program output
-
- $lines_op = preg_split('/[\n]+/', $f_op, -1, PREG_SPLIT_NO_EMPTY);
-
- $i = 0;
- $limit = 10;
- $count2 = count($lines_op);
-
- while ($i < $count2) {
- $words_op[$i] = preg_split('/[\s]+/',$lines_op[$i], -1, PREG_SPLIT_NO_EMPTY);
-
- ++$i;
-
- }
- //compare...
- $i=0;
- $lines_match=0;
- $temp=0;
- while(($i<$count1)&&($i<$count2))
- {
- $flag = 1;
- $j=0;
- if(count($words_ot[$i])!=count($words_op[$i]))
- $flag=0;
- else
- {
- while($j<count($words_ot[$i]))
- {
- if($words_ot[$i][$j] != $words_op[$i][$j])
- {
- $flag=0;
- break;
- }
- ++$j;
- }
- }
- if($flag==1)
- {
- ++$temp;
- if($temp==$lines_tc)
- {
- ++$lines_match;
- $temp=0;
- }
- }
- if(($flag==0)&&($temp<$lines_tc))
- {
- break;
- }
- ++$i;
- }
-
- $num_of_tc = $count1/$lines_tc;
- $result=($lines_match/$num_of_tc)*100;
- $temp12 = $_SESSION['ARR'][$_SESSION['iter']];
- echo 'Prob:id'.$temp12;
- echo "<br><br> Score ".$result;
- mysql_query("insert into each_prob_score values('$_SESSION[user_id]','$_SESSION[test_id]',$temp12,'$result')") or die("sadadakjdsakldhajksdhaksjdhaklsjdhjaksjdljakdk!!!!!!!!!!!!!!!".mysql_error());
- //store score in user_gradebook_coding
- }
- ?>