Newer
Older
CEE_VE50 / com / digitec / cee / QuizMain.as
@cwolsen cwolsen on 23 Aug 2019 19 KB Ckeaning up commented out code.
/******************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/23/2010
 * 
 * This is the QuizMain class for the CEE application
 * CS5 version of com.digitec.cee.QuizMain
 *  
*******************************************/
package com.digitec.cee
{
	import flash.display.MovieClip;	
	import flash.display.Sprite;		
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.events.Event;
	import flash.events.MouseEvent;	
    import flash.text.TextField;	
	import flash.printing.PrintJob;   
	import flash.text.TextField;
	import flash.text.*;
	import flash.net.NetConnection;
     import flash.net.NetStream;
	//import fl.controls.TextArea;
	import flash.events.*;				 	
	//import fl.controls.Button;			
 	//import flash.events.Event;
	
	public class QuizMain extends MovieClip 
	{								
		private var quiz:Quiz;
		private var arrayOfQuestions:Array;
		private var numberOfQuestions:int;
		private var correctQuestions:int;		
		private var currQuestionNum:int;		
		private var currCorrectAnswer:int;
		private var currCorrectAnswerText:String;
		private var currSelectedAnswer:int;
		private var emailAddress:String;
		private var xmlQuiz:String =  MainConstants.XMLPATH + MainConstants.XMLQUIZ;
		private var emailButtonOn:Boolean;
		private var printButtonOn:Boolean;
		
		
		private var $nc:NetConnection;
        private var $ns:NetStream;
        private var $streampath:String;
        private var $streamurl:String = MainConstants.CEE_SERVER+MainConstants.XMLQUIZ;
		private var quizidSET;
		//private var quizidSET=3;
		//HEADER STRINGS
		private var conceptTitle_str:String;
		
		//MAX ANSWERS THAT FIX IN DISPLAY
		private var maxAnswers:int;
		
		//FOR FEEDBACK - SCREEN 3
		private var CORRECT_STR:String;
		private var INCORRECT_STR:String;
		private var correct_feedback:String;
		private var incorrect_feedback:String;
		private var dwthisispos:Number;
		//FOR QUIZCOMPLETED - SCREEN 4
		private var ANSW1_SCREEN4:String;
		private var ANSW2_SCREEN4:String;
		private var ANSW3_SCREEN4:String;
		private var COMPLETE_SCREEN4:String;				
		private var myFont:Font = new futura_bold();
		private var questionField:TextField;	
		private var lettersArr:Array = new Array("A", "B", "C", "D", "E");
		private var questionIdObj_arr:Array = new Array();
		private var questionAnsObj_arr:Array = new Array();
		private var currentQuestionId:String;
		private var currentQuestionTxt:String;
		private var currentCorrectTxt:String;
		private var btnAnswerId_arr:Array = new Array();
		private var btnAnswerTxt_arr:Array = new Array();
		private var dpasspath;
		private var previousAnswerTextFields_arr:Array = new Array();
		//TO SET ANSWER BUTTONS
		//private var lettersArr:Array = new Array("A", "B", "C", "D", "E");
		
		public function PassID(dwtempID, qpath){
			 quizidSET=dwtempID;
			 dpasspath=qpath;
			init();
		 }
		 
		 public function QuizMain()		
		 {								
		 	trace("A "+this.textBckg_A);
			trace("b "+this.textBckg_B);
			trace("c "+this.textBckg_C);
			trace("d "+this.textBckg_D);
			trace("e "+this.textBckg_E);
			//dpasspath=Object(parent).getDQpath();
			trace("dpasspath        "+dpasspath);
			
			//xmlQuiz = MainConstants.XMLPATH + MainConstants.XMLQUIZ;
			//dw_xml.quizconcept.quizid=contentItem.conceptID;
			//trace("dw_xml.quizconcept.quizid------------------------------------------------------"+dw_xml.quizconcept.quizid);
			emailAddress = MainConstants.EMAIL;
			start_btn.visible=false;
			correctQuestions = 0;
			maxAnswers = 5;						
			setupListeners();									
			//init();
			// loadXML();			
		}
		
		/*******************************************************************
	 	* FUNCTION:  TO INITIALIZE APPLICATION
		*******************************************************************/		
		//private function init(e:Event):void		
		private function init():void		
		{
			loadXML(dpasspath+ "_VE50DATA/"+xmlQuiz);		
		}				
		private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
		private function httpStatusHandlerQUIZ(event:HTTPStatusEvent):void {
            trace("[QuizMain.as:140]httpStatusHandler: -----------------------------------" + event);
            trace("status: " + event.status);
			if (event.status == 0){
				loadXML(dpasspath+ "_VE50DATA/"+xmlQuiz);	
			}else {
				loadXML($streamurl);
			}
			//loadLibraryFinal(MainConstants.XMLPATH + dwpasspub);
        }
 		
		
		/*******************************************************************
		 * FUNCTION:  SET UP ALL LISTENERS
		  *******************************************************************/
		private function setupListeners():void
		{
			this.start_btn.addEventListener(MouseEvent.CLICK, startQuiz);
			//addEventListener(Event.ADDED_TO_STAGE, init);																								
		}
		
		/*******************************************************************
	 	* FUNCTION:  TO LOAD TEXT FROM A XML FILE
		*******************************************************************/		
		private function loadXML(stream:String):void
		{			
			//TRYI FIRST TO CONNECT TO CEE SERVER 
			//trace("here first");
			var myurlloader:URLLoader = new URLLoader();			
			var myurlreq:URLRequest = new URLRequest(stream);						
			myurlloader.load(myurlreq);
			myurlloader.addEventListener(Event.COMPLETE, loadData);
			//myurlloader.addEventListener(Event.CONNECT, connectServer);
			
			function loadData(evt:Event)
			{				
				var _xml:XML = new XML(myurlloader.data);
				currQuestionNum = 0;
				
				//CREATE THE QUIZ OBJECT WITH 5 ANSWERS PER QUESTION														
				
				//SET HEADER STRINGS
				//myXML.IMAGE.(@TITLE=="school")
				for (var i:int = 0; i<_xml.quizconcept.quizid.length(); i++){
					var dwquiztemp2 = _xml.quizconcept.quizid[i];
					if(dwquiztemp2==quizidSET){
						dwthisispos = i;
						i=_xml.quizconcept.quizid.length();
					}
				};
				//trace(dwthisispos);
				
				quiz = new Quiz(_xml, maxAnswers, dwthisispos);	
				//quiz.dwkeeppos(dwthisispos);
				//var dwreallytemp = _xml.quizconcept.quizid;
				conceptTitle_str = _xml.quizconcept.concept_title[dwthisispos].toString();
				//SET HOME - SCREEN 1
				
				 setQuizInstructions();					
				 
				//SET STRING FOR FEEDBACK - SCREEN 3
				CORRECT_STR = _xml.quizconcept.feedback_movie.correct[dwthisispos].toString();
				INCORRECT_STR = _xml.quizconcept.feedback_movie.incorrect[dwthisispos].toString();
				correct_feedback =  "";
				incorrect_feedback = "";
				
				//SET STRINGS FOR QUIZCOMPLETED - SCREEN 4
				ANSW1_SCREEN4 = _xml.quiz_complete_screen.answered_num1.toString();
				ANSW2_SCREEN4 = _xml.quiz_complete_screen.answered_num2.toString();
				ANSW3_SCREEN4 = _xml.quiz_complete_screen.answered_num3.toString();
				COMPLETE_SCREEN4 = _xml.quiz_complete_screen.completed.toString();				
				
				//SET EMAIL AND PRINT BUTTON ON OR OFF
				if(  _xml.quizconcept.emailResultButton[dwthisispos].toString() == "on" ||  _xml.quizconcept.emailResultButton[dwthisispos].toString() == "yes" ||  _xml.quizconcept.emailResultButton[dwthisispos].toString() == "true" ) { emailButtonOn = true; } else  { emailButtonOn = false; }
				if(  _xml.quizconcept.printResultButton[dwthisispos].toString() == "on" || _xml.quizconcept.printResultButton[dwthisispos].toString() == "yes"  ||  _xml.quizconcept.printResultButton[dwthisispos].toString() == "true" ) { printButtonOn = true; } else  { printButtonOn = false; }
				
			}
		}
		
		/*******************************************************************
		 * FUNCTION:  SET QUIZ INSTRUCTIONS - SCREEN 1
		  *******************************************************************/
		private function setQuizInstructions():void
		{
			instructions_txt.text = quiz.quizInstructions.toString();
			
			//SET HEADER TEXTS
			questioncounter_txt.text = " " ;
			conceptTitle_txt.text =conceptTitle_str;
			start_btn.visible=true;
		}
		
		//private var questionId:String;
		//private var answerId:String;
		
		/*******************************************************************
		 * FUNCTION:  SET A QUESTION - SCREEN 2
		  *******************************************************************/					
		private function setQuestion(questionNum:int ):void
		{									
			//REMOVE PREVIOUS ANSWERS TEXT FIELDS/////////////////////////////////////////////////////////////
			
			if(previousAnswerTextFields_arr.length > 0){
				for(var i:int = 0; i<previousAnswerTextFields_arr.length; i++){
					this["textBckg_" + lettersArr[i] ].removeChild(previousAnswerTextFields_arr[i]);
				}
				previousAnswerTextFields_arr = new Array();
			}
				
			//MAKE ALL ANSWER BUTTONS INVISIBLE
			for(var ind:int=0; ind < lettersArr.length; ind++){ this["textBckg_" + lettersArr[ind] ].visible = false;	}
			
			//SET HEADER TEXT
			questioncounter_txt.text = "Question "+ currQuestionNum + " of " + numberOfQuestions;
			
			var _questionObj:Question = arrayOfQuestions[ questionNum - 1 ]
			currentQuestionTxt =  _questionObj.question.toString();		
			question_txt.text =  _questionObj.question.toString();						
			
			currentQuestionId = _questionObj.thequestionId.toString(); /////////////////////////////////////////////////////////////////////////////////
			trace("currentQuestionId===="+currentQuestionId);
			
			//SET QUESTION AND ANSWER OBJECT ARRAY TO SEND RESPONSE
			//questionIdObj_arr.push({QuestionId:_questionObj.thequestionId.toString(), AnswerId:"0"});//////////////////////////////////////////
			
			correct_feedback =  _questionObj.correctFeedback;
			incorrect_feedback = _questionObj.incorrectFeedback;
						
			//SET TEXT FIELD IN ANSWER OBJECTS
			//lettersArr:Array = new Array("A", "B", "C", "D", "E");
			var _answers:Array = _questionObj.answers;
			var btnNum:int = _answers.length;
			if( btnNum > 5){ btnNum = 5; }  //max is 5 buttons
			for(var index:int=0; index<btnNum; index++)
			{				
				var myFormat:TextFormat = new TextFormat();
				myFormat.font = myFont.fontName;
				myFormat.size = 14;
				myFormat.color = 0x666666;
				myFormat.bold = true;
				//trace("________________Here");				
				questionField = new TextField();					
				questionField.defaultTextFormat = myFormat;
				questionField.height = 40;
				questionField.width = 435;
				questionField.background = false;
				questionField.border = false;
				questionField.multiline = true;
				questionField.wordWrap = true;
				questionField.antiAliasType = AntiAliasType.NORMAL;
				questionField.x = 59;
				questionField.y = 9;
				//questionField.addEventListener(TextEvent.LINK, linkEvent);						
				//questionField.text = _answers[index];
				questionField.text = _answers[index].Answer; //////////////////////////////////////////////////////////////////////////////////////////////
				//questionField.htmlText = "<p><a href= 'event:answerSelected,"+_answers[index].AnswerId+"'>"+_answers[index].Answer+"</a></p>";
				//rowsArr[i].textHolder.htmlText = "<p><font size='12' color='#588527' ><bold><u><a href= 'event:viewLesson,"+item.id+"'>"+item.title +"</a></u></bold></font></p>"+"<p>"+ item.desc+"</p>";									
				btnAnswerId_arr[index] =  _answers[index].AnswerId;////////////////////////////////////////////////////Id asssociated with the button (answer)
				btnAnswerTxt_arr[index] =  _answers[index].Answer;
				trace( "btnAnswerId_arr[index]=========="+ btnAnswerId_arr[index] );
				trace( "btnAnswerId_arr[index]=========="+ btnAnswerTxt_arr[index] );
				this["textBckg_" + lettersArr[index] ].addChild(questionField);	
				previousAnswerTextFields_arr.push(questionField);////////////////////////////////////////
				
				this["textBckg_" + lettersArr[index] ].visible = true;
				//this["textBckg_" + lettersArr[index] ].answer_txt.text = _answers[index];
			}
			
			currCorrectAnswer = _questionObj.correctAnswerPosition;	
			currCorrectAnswerText = _questionObj.correctAnswer;
		}								
		
		
		//FUNCTION TO POPULATE THE ARRAY OF RESPONSE THAT WILL BE SEND IT TO SERVER
		//THIS ARRAY CONTAINS AN OBJECT OF STRING PAIRS FOR QUESTION ID AND THE SELECTED ANSWER ID
		private function populateResponseArray():void
		{			
			questionIdObj_arr.push({QuestionId:currentQuestionId, AnswerId:btnAnswerId_arr[currSelectedAnswer]});//////////////////////////////////////////
			questionAnsObj_arr.push({question_str:currentQuestionTxt,AnswerTxt:btnAnswerTxt_arr[currSelectedAnswer],correctAnswer_str:currCorrectAnswerText});
			trace("QuestionId========="+currentQuestionId+", AnswerId========"+btnAnswerId_arr[currSelectedAnswer] );
			trace("QuestionId========="+question_txt.text+", AnswerId========"+btnAnswerTxt_arr[currSelectedAnswer] );
			
		}
		
		/*******************************************************************
		 * FUNCTION:  SET QUESTION-  1ST TIME SCREEN 2
		  *******************************************************************/
		private function startQuiz(evt:MouseEvent):void
		{						
			this.gotoAndStop(2);						
			
			//SET BUTTONS DISPLAY PROPERTIES
			trace("lettersArr "+lettersArr);
			trace(lettersArr.length);
			for(var letter:int=0; letter<lettersArr.length; letter++)
			{
				trace(letter);
				trace(this+"      this");
				setupButton(lettersArr[letter]);
			}
			//trace("dwthisispos"+dwthisispos);
			arrayOfQuestions = quiz.questionsArr;
			numberOfQuestions = quiz.numOfQuestions;
			
			//Set first question & get Question object			
			currQuestionNum = 1;												
			
			setQuestion(currQuestionNum);								
		}
		
		/*******************************************************************
		 * FUNCTION:  CHECK ANSWER FOR CURRENT QUESTION
		  *******************************************************************/
		//private function checkAnswer( str:String):Boolean
		private function checkAnswer( str:String):void
		{
			switch (str) {
				case "A":
					currSelectedAnswer = 0;
					break;
				case "B":
					currSelectedAnswer = 1;
					break;
				case "C":
					currSelectedAnswer = 2;
					break;
				case "D":
					currSelectedAnswer = 3;
					break;
				case "E":
					currSelectedAnswer = 4;
					break;				
				default:
					trace("Not 0, 1, or 2");
			}
			
			populateResponseArray();
			
			if( currCorrectAnswer == currSelectedAnswer ){ setFeedback(true); }
			else {  setFeedback(false); }
		}


		/*******************************************************************
		 * FUNCTION:  SET FEEDBACK - SCREEN 3
		  *******************************************************************/		 
		private function setFeedback(answer:Boolean):void
		{			
			this.gotoAndStop(3);						
			//feedback_mc.continueBtn.buttonMode = true;
			//feedback_mc.continueBtn.mouseChildren = false;			
			feedback_mc.continueBtn.addEventListener(MouseEvent.CLICK, continueQuestions);
			function continueQuestions(evt:MouseEvent):void
			{
				if( currQuestionNum < numberOfQuestions )
				{   				
					gotoAndStop(2);						
					currQuestionNum++;																	
					setQuestion( currQuestionNum );										
				}else{ 
					gotoAndStop(4);
					setQuizComplete(); 
				}
			}
			
			if(answer == true)
			{
				correctQuestions++;
				//feedback_mc.gotoAndStop(1);						
				feedback_mc.feedback_txt.text = CORRECT_STR;
				feedback_mc.feedback_inst_txt.text = correct_feedback;				
			}
			else{
				feedback_mc.gotoAndStop(2);						
				feedback_mc.feedback_txt.text = INCORRECT_STR;
				feedback_mc.feedback_inst_txt.text = incorrect_feedback;				
			}			
		}		
		
		/*******************************************************************
		 * FUNCTION:  SET QUIZCOMPLETE - SCREEN 4
		  *******************************************************************/		
		private function setQuizComplete():void
		{									
			//SET HEADER TEXT
			questioncounter_txt.text = " " ;
			
			answered_number_txt.text = ""+ANSW1_SCREEN4+" "+correctQuestions+" "+ANSW2_SCREEN4+" "+numberOfQuestions+" " +ANSW3_SCREEN4;			
			complete_feedback_txt.text =  COMPLETE_SCREEN4;		
			
			email_btn.addEventListener(MouseEvent.CLICK, emailResults);
			print_btn.addEventListener(MouseEvent.CLICK, printResults);
			
			function emailResults(evt:MouseEvent):void
			{
				//%0A  line break			
				var emailSubject:String = "Your Quiz Results";
				var emailBody:String = "CEE: "+ conceptTitle_str +".%0AYou answered "+ correctQuestions +" of  " +numberOfQuestions + " questions correctly.";				
				
				//var conceptid:String = Object(parent).getConceptId(); //get the concept id from Lesson
				var questionString:String = "";
				var answerString:String = "";
				for(var i:int= 0; i<questionIdObj_arr.length; i++)
				{
					questionString =questionString+questionIdObj_arr[i].QuestionId+",";
					answerString=answerString+questionIdObj_arr[i].AnswerId+",";
					//questionIdObj_arr.push({QuestionId:currentQuestionId, AnswerId:btnAnswerId_arr[currSelectedAnswer]});//
				}
				var myDate = new Date();
				trace(myDate);
				var unixTime = Math.round(myDate.getTime()/1000);
				trace("unixTime"+unixTime);
				questionString = questionString.slice( 0, -1 );
				answerString = answerString.slice( 0, -1 );
				var urlstr:String ="http://ve.councilforeconed.org/library/email-check.php?v=4.0&t="+unixTime+"&cid="+quizidSET+"&qids="+questionString+"&aids="+answerString;				

				trace(urlstr);

	
				var emailUrl:URLRequest = new URLRequest( urlstr );				
								
				navigateToURL(emailUrl, "_self");
			}
			
			function printResults(evt:MouseEvent):void
			{			
				var myPrintJob:PrintJob = new PrintJob(); 
       			//var mSprite:Sprite = new Sprite();
				var printSprite:Sprite = printResultsPage();
				myPrintJob.start(); 
            	myPrintJob.addPage(printSprite); 
           		myPrintJob.send();
			}
			
			//SET EMAIL AND PRINT BUTTON ON OR OFF
			if( emailButtonOn == true ) { email_btn.visible = true; } else  { email_btn.visible = false; }
			if(  printButtonOn == true ) { print_btn.visible = true; } else  { print_btn.visible = false; }		
		}
		
		
		//PREPARE SPRITE TO PRINT
		function printResultsPage():Sprite
		{
			var mySprite:Sprite = new Sprite();
			var textfiled:TextField = new TextField();
			textfiled.width = 500;
			textfiled.height = 1200;
			textfiled.x=10;
			textfiled.y = 10;
			//textfiled.htmlText	
			textfiled.multiline = true;
			textfiled.condenseWhite = true; //remove white spaces
			
			var answerString:String = "";
				for(var i:int= 0; i<questionIdObj_arr.length; i++)
				{
					answerString =answerString+ "<b>Question:</b> "+questionAnsObj_arr[i].question_str+"<br><b>Response:</b> "+questionAnsObj_arr[i].AnswerTxt+"<br><b>Correct Answer:</b> "+questionAnsObj_arr[i].correctAnswer_str+"<br><br>";
				}
				
				var dwtextstring:String = "<b>Virtual Economics: "+ conceptTitle_str +"</b><br>You answered "+correctQuestions+" of "+numberOfQuestions+" questions correctly.<br>"+answerString+"<br>Copyright © Council for Economic Education. http://www.councilforeconed.org";
				textfiled.htmlText = dwtextstring;
				textfiled.wordWrap = true;
				
				//trace(urlstr);
			mySprite.addChild(textfiled);						
			return mySprite;
		}
			
		
		
		/*******************************************************************
		 * FUNCTIONS:  SET UP BUTTONS SCREEN 2  
		  *******************************************************************/		 
		private function setupButton(str:String):void
		{			
			trace("str"+str);
			trace("frame     "+this.currentFrame);
			trace("this start"+this);
			switch(str) 
			 {
				 
				  case "A" :
				  trace(this.textBckg_A);
				  trace(this["textBckg_" + str]);
				  this["textBckg_" + str].letter_txtb.gotoAndStop(2);
				  break;
		
				  case "B" :
				   trace(this.textBckg_B);
				  trace(this["textBckg_" + str]);
				  this["textBckg_" + str].letter_txtb.gotoAndStop(3);
				  break;
		
				  case "C" :
				  this["textBckg_" + str].letter_txtb.gotoAndStop(4);
				  break;
				  
				  case "D" :
				  this["textBckg_" + str].letter_txtb.gotoAndStop(5);
				  break;
				  
				  case "E" :
				  this["textBckg_" + str].letter_txtb.gotoAndStop(6);
				  break;
				  default :
				  trace("function not found");
			 }
			//this["textBckg_" + str ].letter_txt.text = str;
			this["textBckg_" + str].buttonMode = true;
			this["textBckg_" + str].mouseChildren = false;			
			
			this["textBckg_" + str].addEventListener(MouseEvent.CLICK, chooseAnswer );
			
			function chooseAnswer(evt:MouseEvent):void
			{
				//trace(""+str);
				checkAnswer(str);
			}
		}
		
		 
	} // Main class
 
} //end package