#!/usr/bin/perl
use DBI;
use CGI qw/:standard/;

$sid=param(session_id);
$sid=~s/\D/\./g;
$cols=param(c);
$cols=~s/\D/\./g;
$rows=param(r);
$rows=~s/\D/\./g;
#$path="http://mmaketchup.hopto.org/students";
$path="http://mmaketchup.homeunix.org/students";

#HTML
print"Content-type: text/html;charset=utf-8;\n\n";
print"<HTML><HEAD><TITLE>AskMe 3.0</TITLE></HEAD>\n";

print"<style>
table.slot {border-collapse: collapse;}
table.slot td
{border: 1px solid;
padding: 2px;
font-family: monospace;
font-size: 12px;}
</style>\n";

print"<script type=\"text/javascript\">\n";
print"var session_id = $sid;\n";
print"var q = new Array();\n";
print"var qid = new Array();\n";
print"var a = new Array();\n";
print"var b = new Array();\n";
print"var c = new Array();\n";
print"var d = new Array();\n";
print"var e = new Array();\n";
print"var f = new Array();\n";
print"var g = new Array();\n";
print"var h = new Array();\n";
print"var i = new Array();\n";
print"var answer_str = new Array();\n";
print"var cq = -1;\n";
print"var cl=2;\n";
print"var correct=0;\n";
print"var rows=$rows;\n";
print"var cols=$cols;\n";

$dbh=DBI->connect("DBI:mysql:database=mc;host=localhost","bill","passw0rd");
$table_name=$dbh->selectrow_array("SELECT table_name from open_sessions where id=$sid AND turn=0;");
$select_string="SELECT qt,id,a,b,c,d,e,f,g,h,i,answer from $table_name;";
$sth=$dbh->prepare("$select_string");
$sth->execute();
$i=0;
while(@data=$sth->fetchrow_array())
{
$temp=$dbh->selectrow_array("SELECT answer from log_student where session_id=$sid and question_id=$data[1] order by turn desc limit 1;");
if($temp ne "correct")
{
    print"q[$i]=\'@data[0]\';\n";
    print"qid[$i]=\'@data[1]\';\n";
    print"a[$i]=\'@data[2]\';\n";
    print"b[$i]=\'@data[3]\';\n";
    print"c[$i]=\'@data[4]\';\n";
    print"d[$i]=\'@data[5]\';\n";
    print"e[$i]=\'@data[6]\';\n";
    print"f[$i]=\'@data[7]\';\n";
    print"g[$i]=\'@data[8]\';\n";
    print"h[$i]=\'@data[9]\';\n";
    print"i[$i]=\'@data[10]\';\n";
    print"answer_str[$i]=\'@data[11]\';\n";
    $i++;
}
}
$turn=$dbh->selectrow_array("SELECT max(turn) from open_sessions where id=$sid;");
$turn++;
$dbh->do("INSERT INTO open_sessions SET id=$sid,turn=$turn,table_name=\'$table_name\',qtotal=$i;");
$last=$i;
$dbh->disconnect;

print"var last = $last;
var turn=$turn;
var slot=-1;\n";

print"function ThisQuestion()
{
var answer = new Array();
answer[0]=0;
answer[1]=0;
answer[2]=0;
answer[3]=0;
answer[4]=0;
answer[5]=0;
answer[6]=0;
answer[7]=0;
answer[8]=0;
answer[9]=0;
var id;
var j=0;
var sa=\"\";

id=\"s\"+slot+\"_\"+j;
document.getElementById(id).innerHTML = \"<i><b>\"+q[cq]+\"</b></i>\";
tid=\"s\"+slot;
document.getElementById(tid).bgColor = \"#f0f0f0\";

for(j=1;j<=cl;j++)
    {
    chk=\"chks\"+slot+\"_\"+j;
    id=\"s\"+slot+\"_\"+j;
    if(document.getElementById(chk).checked==true){answer[j-1]=1;} else {answer[j-1]=0;}
    document.getElementById(chk).disabled=true;
    switch (j)
    {
    case 1:
	if(answer_str[cq].match(\"a\")==\"a\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+a[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+a[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+a[cq]+\"</i>\";}
        break;
    case 2:
	if(answer_str[cq].match(\"b\")==\"b\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+b[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+b[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+b[cq]+\"</i>\";}
        break;
    case 3:
	if(answer_str[cq].match(\"c\")==\"c\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+c[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+c[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+c[cq]+\"</i>\";}
        break;
    case 4:
	if(answer_str[cq].match(\"d\")==\"d\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+d[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+d[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+d[cq]+\"</i>\";}
        break;
    case 5:
	if(answer_str[cq].match(\"e\")==\"e\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+e[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+e[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+e[cq]+\"</i>\";}
        break;
    case 6:
	if(answer_str[cq].match(\"f\")==\"f\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+f[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+f[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+f[cq]+\"</i>\";}
        break;
    case 7:
	if(answer_str[cq].match(\"g\")==\"g\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+g[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+g[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+g[cq]+\"</i>\";}
        break;
    case 8:
	if(answer_str[cq].match(\"h\")==\"h\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+h[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+h[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+h[cq]+\"</i>\";}
        break;
    case 9:
	if(answer_str[cq].match(\"i\")==\"i\"){document.getElementById(id).innerHTML = \"<u><i><b>\"+i[cq]+\"&nbsp;</b></i></u>\";}
	else if(answer[j-1]==1){document.getElementById(id).innerHTML = \"<i><del>\"+i[cq]+\"</del></i>\";}
	else {document.getElementById(id).innerHTML = \"<i>\"+i[cq]+\"</i>\";}
        break;
    default:
        break;
    }
    }
if(answer[0]==1){sa+=\"a\";}
if(answer[1]==1){sa+=\"b\";}
if(answer[2]==1){sa+=\"c\";}
if(answer[3]==1){sa+=\"d\";}
if(answer[4]==1){sa+=\"e\";}
if(answer[5]==1){sa+=\"f\";}
if(answer[6]==1){sa+=\"g\";}
if(answer[7]==1){sa+=\"h\";}
if(answer[8]==1){sa+=\"i\";}
if(sa==answer_str[cq]){sa=\"correct\";correct++;}
//var src=\"$path/answer.pl?session_id=\"+session_id+\"&question_id=\"+qid[cq]+\"&answer=\"+sa+\"&turn=\"+turn;
var url=\"answer.pl?session_id=\"+session_id+\"&question_id=\"+qid[cq]+\"&answer=\"+sa+\"&turn=\"+turn;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");
}
xmlhttp.open(\"GET\",url,true);
xmlhttp.send(null);
//document.getElementById('test').innerHTML=xmlhttp.responseText;
//alert(url);

//document.getElementById(\"a_frame\").src = src;
}\n";

print"function NextQuestion()
{
var id;
var j;

cq++;
if(cq==last){
alert(\"Приехали. Нажмите ОК чтобы вернуть неправильно решенные вопросы.\");
window.location=\"$path/mc.pl?session_id=$sid&r=$rows&c=$cols\";
}
else
{
slot++;
if(slot==rows*cols){slot=0;}
DrawSlot(slot);
tid=\"s\"+slot;
document.getElementById(tid).bgColor = \"#aac0c0\";

for(j=0;j<=10;j++)
    {
    id=\"s\"+slot+\"_\"+j;
    switch (j)
    {
    case 0:
        document.getElementById(id).innerHTML = \"<b>\"+q[cq]+\"<blink>_</blink></b>\";
        break;
    case 1:
        document.getElementById(id).innerHTML = a[cq]+\"&nbsp;\";
        break;
    case 2:
        document.getElementById(id).innerHTML = b[cq]+\"&nbsp;\";cl=2;
        break;
    case 3:
        if(c[cq]!=\"\"){document.getElementById(id).innerHTML = c[cq]+\"&nbsp;\";cl=3;}
        break;
    case 4:
        if(d[cq]!=\"\"){document.getElementById(id).innerHTML = d[cq]+\"&nbsp;\";cl=4;}
        break;
    case 5:
        if(e[cq]!=\"\"){document.getElementById(id).innerHTML = e[cq]+\"&nbsp;\";cl=5;}
        break;
    case 6:
        if(f[cq]!=\"\"){document.getElementById(id).innerHTML = f[cq]+\"&nbsp;\";cl=6;}
        break;
    case 7:
        if(g[cq]!=\"\"){document.getElementById(id).innerHTML = g[cq]+\"&nbsp;\";cl=7;}
        break;
    case 8:
        if(h[cq]!=\"\"){document.getElementById(id).innerHTML = h[cq]+\"&nbsp;\";cl=8;}
        break;
    case 9:
        if(i[cq]!=\"\"){document.getElementById(id).innerHTML = i[cq]+\"&nbsp;\";cl=9;}
        break;
    default:
        break;
    }
    }
}
var total = cq;
var percent = 0;
if (total!=0) {percent=100*correct/total;}
document.getElementById(\"result\").innerHTML = percent+\" % (\"+correct+\"; \"+total+\"; \"+last+\")\";
}\n";

print"function DrawSlot(n)
{
var string=\"\";
var j;
string=\"<table border=0 width=100% id=s\"+n+\" class=slot>\"+
\"<tr><td width=10><td id=s\"+n+\"_0>&nbsp;</td></tr>\"+
\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_1></td><td id=s\"+n+\"_1>&nbsp;</td></tr>\"+
\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_2></td><td id=s\"+n+\"_2>&nbsp;</td></tr>\";
for(j=0;j<10;j++)
    {
    switch (j)
    {
    case 3:
	if(c[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_3></td><td id=s\"+n+\"_3>&nbsp;</td></tr>\";}
        break;
    case 4:
	if(d[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_4></td><td id=s\"+n+\"_4>&nbsp;</td></tr>\";}
        break;
    case 5:
	if(e[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_5></td><td id=s\"+n+\"_5>&nbsp;</td></tr>\"};
        break;
    case 6:
	if(f[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_6></td><td id=s\"+n+\"_6>&nbsp;</td></tr>\"};
        break;
    case 7:
	if(g[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_7></td><td id=s\"+n+\"_7>&nbsp;</td></tr>\"};
        break;
    case 8:
	if(h[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_8></td><td id=s\"+n+\"_8>&nbsp;</td></tr>\"};
        break;
    case 9:
	if(h[cq]!=\"\"){string+=\"<tr><td width=10><input type=checkbox id=chks\"+n+\"_9></td><td id=s\"+n+\"_9>&nbsp;</td></tr>\"};
        break;
    default:
        break;
    }
    }
    string=string+\"</table>&nbsp;\";
    cid=\"cs_\"+n;
    document.getElementById(cid).innerHTML = string;\n}\n";
print"</script>";
print"</head>\n";

if($last!=0) {print"<body onLoad=\"NextQuestion();\" style=\"margin: 0px;\">\n";}
else {print"<body onLoad=alert(\"Приплыли\.\"); style=\"margin: 0px;\">\n";}

print"<table border=0 width=100% height=100%><tr height=1%><td align=center colspan=$cols style=\"border-bottom: 1px solid;\">\n";

print"<table border=0 width=100%>
<tr>
<td id=\"result\" width=48% align=right>0&nbsp;%&nbsp;</td>
<td>&nbsp;
<iframe src=\"$path/answer.pl?session_id=$sid&question_id=-1&answer=-1&turn=$turn\" id=\"a_frame\" width=0 height=0 style=\"display: none;\"></iframe>
</td>
<td width=48%align=left><input type=\"button\" onclick=\"ThisQuestion();NextQuestion();\" value=\"Next\" style=\"width: 50px;\">&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"$path/mc.pl?session_id=$sid&r=$rows&c=$cols\">reload</a> (нажимать если долго виснет)</td>
</tr>
</table>\n";

print"</td></tr><tr>";
$width=100/$cols."%";
for($j=0;$j<$cols;$j++)
    {
    print"<td align=center valign=center width=$width >\n";
    print"<table border=0 width=100% cellspacing=0 cellpadding=0>\n";
    for($k=0;$k<$rows;$k++)
        {
        if(($j==0)||($j==2)||($j==4)){$temp=($j+1)*$rows-1-$k;}
        else{$temp=$k+$rows*$j;}
        print"<tr>";
        print"<td width=507 id=cs\_$temp valign=top align=center>&nbsp;</td>";
        print"</tr>\n";
        }
    print"</table>\n";
    print"</td>\n";
    }

print"</tr><table>\n";
print"</body></html>\n";
#//window.location.reload();
