利用asp + xml 架设在线考试系统
<————-此程序非本人原创——–>
使用这个在线的考试系统,我们能处理任何类型在线测试。 尽管我们一般是用传统方式实现,读者非常希望将。
如果从总体上考虑。 所有问题都储存在服务器( 它可能在数据库里) 里面的的xml 文件里。 用户准备花费考试,然后用户测试的通体将通过微软的xml http 组件传送到浏览器。 使用同一个xml http 组件,每当用户请求一个问题的时候,那些问题内容被从服务器解释并且显示在页上。 对用户的任何问题,你所选择的答案会被储存在客户端。
一次考试的持续时间是5 分钟。 没有回答不了,你可以使用next回答下洋问题。 一旦用户启动考试,所有问题目录将来自服务器。 所给问题的id 每请求到服务器以来在内目录在客户拿给并且给服务器派内储存。 服务器将返回问题内容,符合问题id,从xml 文件。 当用户选择任何一个答案时,体制将在那些应答表里储存和在在客户边里的选择表里。 用户最后已经选择的正确的答案,应答表用来并不地检查。 选择表在那里是以便系统将自动选择用户已经选择了的选择 ( 例如用户点击以前的按钮) 考试将结束或者用户点击终结按钮或者首先来,时间( 例如5 分钟) 结束。 关于终结,系统将计算并不右边答案的并且展示它。 那些以下的文件被在在线的考试系统里使用:
olexam.html
<html>
<script>
var objxmlhttp,objxmldom;
var aquest; //to store question ids
var aanswer = new array(); // to track the result
var aselected = new array(); // to store users response
var count = 0; //to store the current question no
var anssel = 0; //to store users selection
var examduration = 5 * 60 ; // 5 minutes
var timerid; //to store the setinterval funs id
var radindex = -1; //to store the selected radios index
//constructor like function
//here xml objects are created and
//no of questions as well as question ids list
//are fetched from the server.
function init(){
objxmlhttp = new activexobject("microsoft.xmlhttp");
objxmldom = new activexobject("microsoft.xmldom");
objxmlhttp.open("post","olexam.asp?action=start",false);
objxmlhttp.send("");
temp =objxmlhttp.responsetext;
aquest = temp.split(",");
//initialize the users answers list
for(i=0;i<aquest.length; i++){
aanswer[i] = 0; // 0 for wrong; 1 for right answer
aselected[i] = -1; // to store the radios index
}
if(count < aquest.length) {
url = "olexam.asp?action=nextq&qno=" + aquest[count];
objxmlhttp.open("post", url ,false);
objxmlhttp.send("");
objxmldom.loadxml(objxmlhttp.responsetext);
//parse the response content fetched from the server
//and display the question
parseq();
}
//change the start buttons caption and its click event
document.frm.btnfinish.value = "finish the exam";
document.frm.btnfinish.onclick = showresult; //function
//start the timer
timerid = setinterval("timer()",1000);
}
function getpreq() {
//update the users answers list
checkanswer();
//decrement the question no – i.e. to previous question
count–;
//stop the timer
clearinterval(timerid);
//fetch the question for the aquest[count] id
url = "olexam.asp?action=nextq&qno=" + aquest[count];
objxmlhttp.open("post",url ,false);
objxmlhttp.send("");
objxmldom.loadxml(objxmlhttp.responsetext);
//parse the response content fetched from the server
//and display the question
parseq();
//start the timer
timerid = setinterval("timer()",1000);
}
function getnextq() {
//update the users answers list
checkanswer();
//increment the question no – i.e. to next question
count++;
//stop the timer
clearinterval(timerid);
url = "olexam.asp?action=nextq&qno=" + aquest[count];
objxmlhttp.open("post", url ,false);
objxmlhttp.send("");
objxmldom.loadxml(objxmlhttp.responsetext);
//parse the response content fetched from the server
//and display the question
parseq();
//start the timer
timerid = setinterval("timer()",1000);
}
function parseq(){
//fetch the question from thexml object
//format the display
strout = "<table border=0 align=center width=80%>";
strout += "<tr><td colspan=2><b>";
strout += "question no: " + (count+1) + " of ";
strout += aquest.length + "</b></td></tr>";
strout += "<tr><td colspan=2> </td></tr>";
temp = objxmldom.selectsinglenode("data/qtext").text;
strout += "<tr><td colspan=2><b>"+temp+"</b></td></tr>";
strout += "<tr><td colspan=2> </td></tr>";
nodes = objxmldom.selectnodes("data/choice");
for(i=0;i<nodes.length;i++){
strout += "<tr><td align=center width=10%>";
strout += "<input type=radio name=ansusr ";
strout += " onclick=anssel=" + (i+1);
strout += ";radindex=" + i + " ";
strout += "value=" + (i+1) + "></td><td>";
strout += nodes.item(i).text + "</td></tr>";
}
//set ansno (hidden field) to the actual answer
temp = objxmldom.selectsinglenode("data/answer").text;
document.frm.ansno.value = temp;
strout += "<tr><td colspan=2> </td></tr>";
strout += "<tr><td colspan=2>";
if(count != 0 ){
strout += "<input type=button value=previous ";
strout += " onclick=getpreq()> ";
}
if(count < aquest.length-1){
strout += " <input type=button value=next";
strout += " onclick=getnextq()>";
}
strout += "</td></tr></table>";
//set the strout content to <p> tag named qarea
qarea.innerhtml = strout;
//set the default value to anssel
anssel = 0;
radindex = -1;
//check the radio if user has selected previously
if (aselected[count] != -1) {
radindex = aselected[count];
anssel = radindex + 1;
document.frm.ansusr[radindex].checked = true;
}
}
function checkanswer(){
//store the selected radios index
aselected[count] = radindex;
//if the user selection matches the actual answer
if (anssel == document.frm.ansno.value)
aanswer[count] = 1;
else
aanswer[count] = 0;
}
function showresult() {
rights = 0;
//stop the timer
clearinterval(timerid);
//update the users answers list
checkanswer();
//count no of answers
for(i=0;i<aanswer.length;i++){
if(aanswer[i] == 1)
rights++;
}
strres = "<h2 align=center><br>";
//if all the answers are correct then greet
if(rights == aanswer.length)
strres += "<br><br>congratulations…!";
strres += "<br><br>your score is " + rights;
strres += " out of " + aanswer.length + "</h2>";
document.write(strres);
}
var timecount = 0;
function timer(){
timecount++; //increment the time by one second
//to display the time in the status bar,
// uncomment the next line
//window.status = "…" + timecount + " secs" ;
//to display the time
temp = "time: " + parseint(timecount/60);
temp += " min : " + (timecount%60) + " sec ";
tblock.innertext = temp;
//if the time is up
if (timecount == examduration) {
alert("sorry, time is up");
showresult();
}
}
</script>
<body>
<h2 align=center><font color=green>online exam</font></h2>
<form name=frm >
<table border=1 width=95% bgcolor=darkseagreen align=center>
<tr><td align=right><b id=tblock></b></td></tr>
<tr><td>
<p id="qarea">
<center>
<br>
relax…! the duration of this exam is 5 minutes.
<br>
there is no order to answer a question. you may use next as
well as previous button to get a question to answer.
<br>
<br>
<input type=button name=btnfinish value="start the exam"
onclick="init()">
</center>
</p>
<input type=hidden name=ansno>
</td></tr></table>
</form>
</body>
</html>
olexam.asp
<%
response.expires = 0
create an instance of ms xmldom object
and load the qbank.xml file where all the questions are.
set obj = server.createobject("microsoft.xmldom")
obj.async = false
obj.load(server.mappath("qbank.xml"))
very first request from the client
if trim(request("action")) = "start" then
set no of questions per exam
dim noq,totalq
noq = 5 set no less than the totalquestions
count no of questions in the xml file
( or from database)
totalq = obj.selectnodes("data/question").length
dim aquest(),temp,isexist, strq
redim aquest(0) to store the question ids
generate (=noq) question ids randomly
while ubound(aquest) < noq
isexist = false
temp = int((totalq * rnd) + 1)
for i = 1 to ubound(aquest)
if aquest(i) = temp then
isexist = true
exit for
end if
next
if not isexist then
redim preserve aquest(ubound(aquest)+1)
aquest(ubound(aquest)) = temp
strq = aquest(i) & "," & strq
end if
wend
remove the last comma , from strq
strq = left(strq,len(strq)-1)
send the question in the strq to the client
response.write strq
all further requests – after the first request
elseif trim(request("action")) = "nextq" then
fetch the question from the xml object
and form the output string
temp = "data/question[@id=" & trim(request("qno")) & "]"
set node = obj.selectsinglenode(temp)
strxml = "<data>"
strxml = strxml & "<qtext>"
strxml = strxml & node.selectsinglenode("qtext").text
strxml = strxml & "</qtext>"
strxml = strxml & "<answer>"
strxml = strxml & node.selectsinglenode("answer").text
strxml = strxml & "</answer>"
set node = node.selectnodes("choices/choice")
for i = 0 to node.length-1
strxml = strxml & "<choice>"
strxml = strxml & node.item(i).text
strxml = strxml & "</choice>"
next
strxml = strxml & "</data>"
send the output to the client
response.write (strxml)
end if
%>
qbank.xml
<?xml version="1.0"?>
<data>
<question id="1">
<qtext>what does kb stand for?</qtext>
<choices>
<choice>kilo bits</choice>
<choice>key board</choice>
<choice>kilo bytes</choice>
<choice>none</choice>
</choices>
<answer>3</answer>
</question>
<question id="2">
<qtext>cpu stands for</qtext>
<choices>
<choice>central processing unit</choice>
<choice>central power unit</choice>
<choice>core processing unit</choice>
<choice>core power unit</choice>
</choices>
<answer>1</answer>
</question>
<question id="3">
<qtext>1 kb equals</qtext>
<choices>
<choice>1000 bytes</choice>
<choice>1024 bytes</choice>
<choice>1000 bits</choice>
<choice>1024 bits</choice>
<choice>nothing</choice>
</choices>
<answer>2</answer>
</question>
<question id="4">
<qtext>ram is </qtext>
<choices>
<choice>random access modifier</choice>
<choice>primary memory</choice>
<choice>secondary memory</choice>
<choice>read and modify</choice>
</choices>
<answer>2</answer>
</question>
<question id="5">
<qtext>hard disk is </qtext>
<choices>
<choice>hard to break</choice>
<choice>primary memory storage</choice>
<choice>temporary memory storage</choice>
<choice>secondary memory storage</choice>
</choices>
<answer>4</answer>
</question>
<question id="6">
<qtext>computer monitor is used </qtext>
<choices>
<choice>to monitor activities</choice>
<choice>to control computer</choice>
<choice>as display unit</choice>
<choice>none</choice>
</choices>
<answer>3</answer>
</question>
<question id="7">
<qtext>xml stands for</qtext>
<choices>
<choice>extended markup language</choice>
<choice>extended model language</choice>
<choice>extensible markup language</choice>
<choice>extensible model language</choice>
</choices>
<answer>3</answer>
</question>
<question id="8">
<qtext>asp stands for</qtext>
<choices>
<choice>active server page</choice>
<choice>application service provision</choice>
<choice>as soon as possible</choice>
<choice>all</choice>
</choices>
<answer>1</answer>
</question>
</data>
