randomizing questions via XML
i have application pulls in data (questions) via xml , randomizes them. right if there 50 questions in xml file, 50 questions displayed , randomized in application. trying figure out how limit randomizing pool 35 questions display or that. current code brings in file , randomizes below.
package exam {
import flash.net.* //to create urlrequest , load xml file
import flash.events.* //for load events
//import flash.display.* //for testing purposes - remove when testing complete
//this class downloads xml document, , inserts info arrays , variables.
//also provides text of questions , correct answers when requested.
internal class xamldiplomat extends eventdispatcher {
//variables:
//for loaded xml doc
private var myxml:xml;
//for loading in xml
private var examloader:urlloader;
private var numqstns:number; //count of total questions in section
private var sectstart:number;
//contains exam questions - arrays 0 relative -> actual question numbers = array index + 1
private var questions:array;
//contain associated answer choices
private var choicea:array;
private var choiceb:array;
private var choicec:array;
private var choiced:array;
private var choicee:array;
private var choicef:array;
//array of correct answers
private var answers:array;
//use custom mixer class randomize order
private var mymixer:mixer;
//constructor
public function xamldiplomat () { //need add arguments (doclocation)
//create urlrequest argument
var examsite:urlrequest = new urlrequest("protected/questions.xml");
//create loader xml
examloader = new urlloader();
//add listener load completion
examloader.addeventlistener(event.complete, fullyloaded);
examloader.load(examsite);
//var ugly:mixer = new mixer(25);
}
//load listener - creates xml object, , checks multiple sections. if multiple, asks users
//which section want - first needs check database completed sections.
//if single, goes ahead , starts array creation first (& only) section
private function fullyloaded (e:event):void {
myxml = new xml(examloader.data);
//myxml.prettyprinting = false;
//still need pull out system data , pass along...
var system:xmllist = myxml..system;
var sysobj:object = new object();
sysobj.examtitle = system.title.tostring();
sysobj.totalmin = system.minutes.tostring();
sysobj.retakepw = system.retakepw.tostring();
var numsections:number = myxml..section.length();
if (numsections == 1) {
generatearrays(1);
}
dispatchevent(new multisectevent(multisectevent.sections, numsections, sysobj));
}
//assigns arrays instance variables selected section
internal function generatearrays (sectn:number):void {
var whichsection:xmllist = myxml..section.(@id == string(sectn));
var problist:xmllist = whichsection.problem;
numqstns = problist.length();
sectstart = number(problist[0].@id);
questions = new array();
choicea = new array();
choiceb = new array();
choicec = new array();
choiced = new array();
choicee = new array();
choicef = new array();
answers = new array();
(var i:number=0; i<numqstns; i++) {
var curprob:xmllist = problist.(@id == string(i+1));
if (curprob.question.hassimplecontent()) {
questions[i] = curprob.question.tostring();
}else {
//trace(curprob.question.toxmlstring());
questions[i] = droptags(curprob.question[0]);
}
choicea[i] = curprob.choice.(@letter == "a").tostring();
choiceb[i] = curprob.choice.(@letter == "b").tostring();
choicec[i] = curprob.choice.(@letter == "c").tostring();
choiced[i] = curprob.choice.(@letter == "d").tostring();
choicee[i] = curprob.choice.(@letter == "e").tostring();
choicef[i] = curprob.choice.(@letter == "f").tostring();
answers[i] = curprob.choice.(hasownproperty("@correct") && @correct == "true").@letter.tostring();
}
mymixer = new mixer(numqstns);//mixer commented out
/*
trace("question: "+questions[3]);
trace("a: "+choicea[3]);
trace("b: "+choiceb[3]);
trace("c: "+choicec[3]);
trace("d: "+choiced[3]);
trace("\r\n answer: "+answers[3]); */
}
//method external classes acquire text of current exam question
internal function getquestion (qnum:number):object {
var returnobj:object = new object();
var randomq:number = mymixer.getrandomnumber(qnum-1);
returnobj.q = questions[randomq];
returnobj.ca = choicea[randomq];
returnobj.cb = choiceb[randomq];
returnobj.cc = choicec[randomq];
returnobj.cd = choiced[randomq];
returnobj.ce = choicee[randomq];
returnobj.cf = choicef[randomq];
returnobj.num = qnum;
//trace(randomq);
return returnobj;
}
private function droptags (txt:xml):string {
var txtstring:string = "";
each (var child:xml in txt.*) {
if (child.nodekind == "text") {
txtstring += child.tostring();
}else {
txtstring += " " + child.toxmlstring();
}
}
//trace(txtstring);
return txtstring;
}
/*
private function droptags (txt:string):string {
var slicestart:number = txt.indexof(">");
var slicestop:number = txt.lastindexof("<");
return txt.slice((slicestart+1), slicestop);
}*/
internal function getanswer (num:number):string {
return answers[num];
}
internal function getqcount ():number {
return numqstns;
}
internal function getsectstart():number {
return sectstart;
}
internal function getrealnum (num:number):number {
return mymixer.getrandomnumber(num-1);
}
}
}
call getquestion() 35 times.
More discussions in ActionScript 3
adobe
Comments
Post a Comment