利用ASP + XML 架设在线考试系统
<-------------此程序非本人原创-------->
使用这个在线的考试系统,我们能处理任何类型在线测试。 尽管我们一般是用传统方式实现,读者非常希望将。
如果从总体上考虑。 所有问题都储存在服务器( 它可能在数据库里) 里面的的xml 文件里。 用户准备花费考试,然后用户测试的通体将通过微软的XML HTTP 组件传送到浏览器。 使用同一个XML HTTP 组件,每当用户请求一个问题的时候,那些问题内容被从服务器解释并且显示在页上。 对用户的任何问题,你所选择的答案会被储存在客户端。
一次考试的持续时间是5 分钟。 没有回答不了,你可以使用NEXT回答下洋问题。 一旦用户启动考试,所有问题目录将来自服务器。 所给问题的Id 每请求到服务器以来在内目录在客户拿给并且给服务器派内储存。 服务器将返回问题内容,符合问题Id,从xml 文件。 当用户选择任何一个答案时,体制将在那些应答表里储存和在在客户边里的选择表里。 用户最后已经选择的正确的答案,应答表用来并不地检查。 选择表在那里是以便系统将自动选择用户已经选择了的选择 ( 例如用户点击以前的按钮) 考试将结束或者用户点击终结按钮或者首先来,时间( 例如5 分钟) 结束。 关于终结,系统将计算并不右边答案的并且展示它。 那些以下的文件被在在线的考试系统里使用:
OLExam.html
1<html>
2<script>
3var objXmlHTTP,objXmlDOM;
4var aQuest; //to store question ids
5var aAnswer = new Array(); // to track the result
6var aSelected = new Array(); // to store user's response
7var count = 0; //to store the current question no
8var ansSel = 0; //to store user's selection
9var ExamDuration = 5 * 60 ; // 5 minutes
10var timerID; //to store the setInterval fun's id
11var radIndex = -1; //to store the selected radio's index
12
13//constructor like function
14//here XML objects are created and
15//No of questions as well as question ids list
16//are fetched from the server.
17function init(){
18objXmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
19objXmlDOM = new ActiveXObject("Microsoft.XMLDOM");
20objXmlHTTP.open("POST","OLExam.asp?Action=Start",false);
21objXmlHTTP.send("");
22temp =objXmlHTTP.ResponseText;
23aQuest = temp.split(",");
24
25//initialize the user's answers list
26for(i=0;i<aQuest.length; i++){
27aAnswer[i] = 0; // 0 for wrong; 1 for right answer
28aSelected[i] = -1; // to store the radio's index
29}
30
31if(count < aQuest.length) {
32url = "OLExam.asp?Action=NextQ&QNo=" + aQuest[count];
33objXmlHTTP.open("POST", url ,false);
34objXmlHTTP.send("");
35objXmlDOM.loadXML(objXmlHTTP.ResponseText);
36
37//parse the response content fetched from the server
38//and display the question
39parseQ();
40}
41
42//change the Start button's caption and its click event
43document.frm.btnFinish.value = "Finish the Exam";
44document.frm.btnFinish.onclick = showResult; //function
45
46//start the timer
47timerID = setInterval("timer()",1000);
48}
49
50function getPreQ() {
51//update the user's answers list
52checkAnswer();
53
54//decrement the question no - i.e. to previous Question
55count--;
56
57//stop the timer
58clearInterval(timerID);
59
60//fetch the question for the aQuest[count] id
61url = "OLExam.asp?Action=NextQ&QNo=" + aQuest[count];
62objXmlHTTP.open("POST",url ,false);
63objXmlHTTP.send("");
64objXmlDOM.loadXML(objXmlHTTP.ResponseText);
65
66//parse the response content fetched from the server
67//and display the question
68parseQ();
69
70//start the timer
71timerID = setInterval("timer()",1000);
72}
73
74function getNextQ() {
75//update the user's answers list
76checkAnswer();
77
78//increment the question no - i.e. to next Question
79count++;
80
81//stop the timer
82clearInterval(timerID);
83
84url = "OLExam.asp?Action=NextQ&QNo=" + aQuest[count];
85objXmlHTTP.open("POST", url ,false);
86objXmlHTTP.send("");
87objXmlDOM.loadXML(objXmlHTTP.ResponseText);
88
89//parse the response content fetched from the server
90//and display the question
91parseQ();
92
93//start the timer
94timerID = setInterval("timer()",1000);
95}
96
97function parseQ(){
98//fetch the question from theXML Object
99//format the display
100strOut = "<table border=0 align=center width=80%>";
101strOut += "<tr><td colspan=2><b>";
102strOut += "Question No: " + (count+1) + " of ";
103strOut += aQuest.length + "</b></td></tr>";
104strOut += "<tr><td colspan=2> </td></tr>";
105
106temp = objXmlDOM.selectSingleNode("data/qtext").text;
107
108strOut += "<tr><td colspan=2><b>"+temp+"</b></td></tr>";
109strOut += "<tr><td colspan=2> </td></tr>";
110
111Nodes = objXmlDOM.selectNodes("data/choice");
112
113for(i=0;i<Nodes.length;i++){
114strOut += "<tr><td align=center width=10%>";
115strOut += "<input type=radio name=ansUsr ";
116strOut += " onClick='ansSel=" + (i+1);
117strOut += ";radIndex=" + i + "' ";
118strOut += "value=" + (i+1) + "></td><td>";
119strOut += Nodes.item(i).text + "</td></tr>";
120}
121
122//set ansNo (hidden field) to the actual answer
123temp = objXmlDOM.selectSingleNode("data/answer").text;
124document.frm.ansNo.value = temp;
125
126strOut += "<tr><td colspan=2> </td></tr>";
127strOut += "<tr><td colspan=2>";
128
129if(count != 0 ){
130strOut += "<input type=button value=Previous ";
131strOut += " onClick='getPreQ()'> ";
132}
133
134if(count < aQuest.length-1){
135strOut += " <input type=button value=Next";
136strOut += " onClick='getNextQ()'>";
137}
138
139strOut += "</td></tr></table>";
140
141//set the strOut content to <P> tag named QArea
142QArea.innerHTML = strOut;
143
144//set the default value to ansSel
145ansSel = 0;
146radIndex = -1;
147
148//check the radio if user has selected previously
149if (aSelected[count] != -1) {
150radIndex = aSelected[count];
151ansSel = radIndex + 1;
152document.frm.ansUsr[radIndex].checked = true;
153}
154}
155
156function checkAnswer(){
157//store the selected radio's index
158aSelected[count] = radIndex;
159
160//if the user selection matches the actual answer
161if (ansSel == document.frm.ansNo.value)
162aAnswer[count] = 1;
163else
164aAnswer[count] = 0;
165}
166
167function showResult() {
168rights = 0;
169
170//stop the timer
171clearInterval(timerID);
172
173//update the user's answers list
174checkAnswer();
175
176//count no of answers
177for(i=0;i<aAnswer.length;i++){
178if(aAnswer[i] == 1)
179rights++;
180}
181strRes = "<h2 align=center><br>";
182
183//if all the answers are correct then greet
184if(rights == aAnswer.length)
185strRes += "<br><br>Congratulations...!";
186
187strRes += "<br><br>your score is " + rights;
188strRes += " out of " + aAnswer.length + "</h2>";
189
190document.write(strRes);
191}
192
193var timeCount = 0;
194function timer(){
195timeCount++; //increment the time by one second
196
197//to display the time in the status bar,
198// uncomment the next line
199//window.status = "..." + timeCount + " secs" ;
200
201//to display the time
202temp = "Time: " + parseInt(timeCount/60);
203temp += " min : " + (timeCount%60) + " sec ";
204TBlock.innerText = temp;
205
206//if the time is up
207if (timeCount == ExamDuration) {
208alert("Sorry, time is up");
209showResult();
210}
211}
212</script>
213<body>
214<h2 align="center"><font color="green">OnLine Exam</font></h2>
215<form name="frm">
216<table align="center" bgcolor="DarkSeaGreen" border="1" width="95%">
217<tr><td align="right"><b id="TBlock"></b></td></tr>
218<tr><td>
219<p id="QArea">
220<center>
221<br/>
222Relax...! The duration of this exam is 5 minutes.
223<br/>
224There is no order to answer a question. You may use Next as
225well as Previous button to get a question to answer.
226<br/>
227<br/>
228<input name="btnFinish" onclick="init()" type="button" value="Start the Exam"/>
229</center>
230</p>
231<input name="ansNo" type="hidden"/>
232</td></tr></table>
233</form>
234</body>
235</html>
OLExam.asp
1
2Response.expires = 0
3'create an instance of MS XMLDOM Object
4'and load the QBank.xml file where all the questions are.
5
6set obj = server.createobject("Microsoft.XMLDOM")
7obj.async = false
8obj.load(Server.MapPath("QBank.xml"))
9
10'very first request from the client
11if trim(request("Action")) = "Start" then
12'set no of questions per exam
13Dim NoQ,TotalQ
14
15NoQ = 5 'set no less than the totalquestions
16
17'count no of questions in the xml file
18'( or from database)
19TotalQ = obj.selectNodes("data/question").length
20
21Dim aQuest(),temp,isExist, strQ
22ReDim aQuest(0) 'to store the question ids
23
24'generate (=NoQ) question ids randomly
25while ubound(aQuest) < NoQ
26isExist = false
27temp = Int((TotalQ * Rnd) + 1)
28for i = 1 to ubound(aQuest)
29if aQuest(i) = temp then
30isExist = true
31exit for
32end if
33next
34if Not isExist then
35Redim Preserve aQuest(ubound(aQuest)+1)
36aQuest(ubound(aQuest)) = temp
37strQ = aQuest(i) & "," & strQ
38end if
39wend
40
41'remove the last comma ',' from strQ
42strQ = left(strQ,len(strQ)-1)
43
44'send the question in the strQ to the client
45response.write strQ
46
47'all further requests - after the first request
48elseif trim(request("Action")) = "NextQ" then
49'fetch the question from the XML Object
50'and form the output string
51temp = "data/question[@id=" & trim(request("QNo")) & "]"
52
53set Node = obj.selectSingleNode(temp)
54
55strXML = "
<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>
1"
2
3'send the output to the client
4Response.Write (strXML)
5end if
QBank.xml
1<data>
2<question id="1">
3<qtext>What does KB stand for?</qtext>
4<choices>
5<choice>Kilo Bits</choice>
6<choice>Key Board</choice>
7<choice>Kilo Bytes</choice>
8<choice>None</choice>
9</choices>
10<answer>3</answer>
11</question>
12<question id="2">
13<qtext>CPU stands for</qtext>
14<choices>
15<choice>Central Processing Unit</choice>
16<choice>Central Power Unit</choice>
17<choice>Core Processing Unit</choice>
18<choice>Core Power Unit</choice>
19</choices>
20<answer>1</answer>
21</question>
22<question id="3">
23<qtext>1 KB equals</qtext>
24<choices>
25<choice>1000 Bytes</choice>
26<choice>1024 Bytes</choice>
27<choice>1000 Bits</choice>
28<choice>1024 Bits</choice>
29<choice>Nothing</choice>
30</choices>
31<answer>2</answer>
32</question>
33<question id="4">
34<qtext>RAM is </qtext>
35<choices>
36<choice>Random Access Modifier</choice>
37<choice>Primary Memory</choice>
38<choice>Secondary Memory</choice>
39<choice>Read And Modify</choice>
40</choices>
41<answer>2</answer>
42</question>
43<question id="5">
44<qtext>Hard Disk is </qtext>
45<choices>
46<choice>Hard to break</choice>
47<choice>Primary Memory Storage</choice>
48<choice>Temporary Memory Storage</choice>
49<choice>Secondary Memory Storage</choice>
50</choices>
51<answer>4</answer>
52</question>
53<question id="6">
54<qtext>Computer Monitor is used </qtext>
55<choices>
56<choice>To monitor activities</choice>
57<choice>To control Computer</choice>
58<choice>As display unit</choice>
59<choice>None</choice>
60</choices>
61<answer>3</answer>
62</question>
63<question id="7">
64<qtext>XML stands for</qtext>
65<choices>
66<choice>Extended Markup Language</choice>
67<choice>Extended Model Language</choice>
68<choice>Extensible Markup Language</choice>
69<choice>Extensible Model Language</choice>
70</choices>
71<answer>3</answer>
72</question>
73<question id="8">
74<qtext>ASP stands for</qtext>
75<choices>
76<choice>Active Server Page</choice>
77<choice>Application Service Provision</choice>
78<choice>As Soon as Possible</choice>
79<choice>All</choice>
80</choices>
81<answer>1</answer>
82</question>
83</data>