Tuesday, March 3, 2015

Adobe Flash : XML : วิธีสร้างข้อสอบ โดยนำข้อสอบเข้ามาจาก ไฟล์แบบ XML


วิธีสร้างข้อสอบ โดยนำแบบทดสอบเข้ามาจาก ไฟล์แบบ XML
ข้อดีคือ
    สามารถแก้ไขข้อสอบ โดยไม่จำเป็นต้องแก้ไขไฟล์โปรแกรม Flash หรือ ไฟล์   .swf

ตัวอย่างแบบทดสอบ
    1)   ตัวอย่างนี้จะโหลดแบบทดสอบจากเอกสาร XML
    2)   จับเวลาการทำแบบทดสอบ ให้เวลาข้อละ 10 วินาที
    3)   สรุปผลคะแนน
    5)   บันทึกผลคะแนนลงเอกสาร แบบ Text ไฟล์

 ภาพ ตัวอย่างข้อสอบ 3 ข้อ

 ข้อ 1
  ข้อ 2
 ข้อ 3

สรุป คะแนน

------------------------------------



  ขั้นตอน

      สร้างแบบทดสอบด้วย Text editor เช่น  Notepad หรือ Edit plus และบันทึกเป็นเอกสาร แบบ XML



<?xml version="1.0" encoding="utf-8"?>
<QUIZ>

     <QUESTION a1="Reading" a2="Singing" a3="Painting" a4="eating" v1="true" v2="false" v3="false" v4="false">what am I doing ?</QUESTION>

     <QUESTION a1="John" v1="false" a2="Pasatorn" v2= "true" a3="Peter" v3="false" a4="water melon" v4="false" >Who am I ?</QUESTION>

     <QUESTION a1="Khonkean" v1="false" a2="Ubon" v2="false" a3="Bangkok" v3="false"  a4="Udon" v4="true">Where am I ?</QUESTION>

</QUIZ>



ที่โปรแกรม Flash
       - ประกอบด้วย 3  เลเยอร์
            - as3 เลเยอร์ เก็บโปรแกรม action script

            - GUI เลเยอร์ สร้าง User Interface สำหรับติดต่อกับผู้ใช้ และโหลดแบบทดสอบเข้ามาแสดงในส่วนนี้
             - Timer เลเยอร์  แสดงตัวจับเวลา

     
       



Action Script / Frame

Layer: as3 , Frame : 1

import fl.controls.RadioButtonGroup;    // อิมพอร์ท คลาส  RadioButtonGroup เพื่อจัดการ เกี่ยวกับปุ่ม (Button)
import flash.events.MouseEvent;          // อิมพอร์ท คลาส MouseEvent เพื่อจัดการเหตุการณ์(event) ที่มาจากเมาส์
import flash.utils.Timer;                          // อิมพอร์ท คลาส  Timer สำหรับจัดการด้านเวลา
import flash.filesystem.*;

stop();                                                    // หยุดอยู่ที่ frame นี้

var score_right = 0;                               // สร้างตัวแปร score แล้วกำหนดค่าให้เท่ากับ 0
var score_wrong = 0; 
var myXML:XML;                                  // สร้างตัวแปร  myXML เป็นชนิด  XML Object
var myLoader:URLLoader = new URLLoader();       // สร้างตัวแปรออปเจค ชื่อ myLoader เพื่อโหลดไฟล์ ตาม path ที่ระบุ
myLoader.load(new URLRequest("quiz-question.xml"));    // อ่านนื้อหาในไฟล์  question.xml (แบบทดสอบ)
myLoader.addEventListener(Event.COMPLETE, processXML); // ถ้าโหลดไฟล์สำเร็จ (สถานะเป็น COMPLETE) ให้เรียกฟังก์ชัน  processXML มาประมวลผล
function processXML(event:Event):void           // ฟังก์ชัน  processXML 
{
myXML = XML(event.target.data);            // กำหนดให้ ตัวแปร  myXML มีค่าเป็น อาร์เรย์ข้อมูลที่อ่านจากไฟล์ XML

qn1.text = myXML.QUESTION[0];            // นำข้อมูลจาก  myXML.QUESTION[0] ใส่ใน qn1 ซึ่งเป็น ชนิด text

ans1.visible = ans2.visible = ans3.visible = ans4.visible = btn1.visible = true;
// ให้แสดงปุ่มตัวเลือก  ที่  1 - 4 และ ปุ่ม  btn1

ans1.label = myXML.QUESTION[0].@a1;   // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  a1 ใส่ใน label ชื่อ ans1
ans2.label = myXML.QUESTION[0].@a2;   // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  a2 ใส่ใน label ชื่อ ans2
ans3.label = myXML.QUESTION[0].@a3;   // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  a3 ใส่ใน label ชื่อ ans3
ans4.label = myXML.QUESTION[0].@a4;   // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  a4 ใส่ใน label ชื่อ ans4

ans1.value=myXML.QUESTION[0].@v1;    // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  v1 กำหนดให้กับ ans1
ans2.value=myXML.QUESTION[0].@v2;    // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  v2 กำหนดให้กับ ans2
ans3.value=myXML.QUESTION[0].@v3;     // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  v3 กำหนดให้กับ ans3
ans4.value=myXML.QUESTION[0].@v4;     // นำข้อมูลจากไฟล์ XML ที่ index 0 , แอททริบิว  v4 กำหนดให้กับ ans4

myTimer.start();                        // เรียก  myTimer ให้ทำงานพร้อมสั่งให้ start เพื่อเริ่มจับเวลาทำข้อสอบ
}

btn1.addEventListener(MouseEvent.CLICK, clickhandler);

function clickhandler(event:MouseEvent):void
{
myTimer.reset();                          // รีเซ็ทตัวจับเวลา
myTimer.start();                          // เริ่มต้นจับเวลา
timer.text="10 seconds remain";           // แสดงข้อความ

if (ans1.group.selection.value == "true") // ถ้าเลือกคำตอบ 1 
{
++score_right;                        // ให้ บวกคะแนน 1 คะแนน
nextFrame();                          // กำหนดให้ทำงานที่ frame ถัดไป
}
else                                      // ถ้าเลือกคำตอบอื่น
{
++score_wrong;
nextFrame();                          // กำหนดให้ทำงานที่ frame ถัดไป
}
}

var myTimer:Timer = new Timer(1000);         // สร้างอินสแตนท์(instance) ของ Timer
myTimer.addEventListener(TimerEvent.TIMER, timerHandler); // เรียกฟังก์ชัน  timerHandler
function timerHandler(event:TimerEvent):void // สร้างฟังก์ชัน timerHandler เพื่อสร้างตัวจับเวลา
{
timer.text = 10 - myTimer.currentCount + "  seconds remain";

if (myTimer.currentCount % 10 == 0)      // ถ้านับ เวลา ครบ 10 วินาที
{
myTimer.reset();                     // รีเซ็ทตัวจับเวลา
myTimer.start();                     // เริ่มต้นจับเวลา
timer.text="10 seconds remain";      // แสดงข้อความ  10 seconds remain
nextFrame();                         // กำหนดให้ทำงานที่ frame ถัดไป
}
}


Layer: as3 , Frame : 2

qn2.text = myXML.QUESTION[1];
ans5.label = myXML.QUESTION[1]. @ a1;
ans6.label = myXML.QUESTION[1]. @ a2;
ans7.label = myXML.QUESTION[1]. @ a3;
ans8.label = myXML.QUESTION[1]. @ a4;

ans5.value=myXML.QUESTION[1].@v1;
ans6.value=myXML.QUESTION[1].@v2;
ans7.value=myXML.QUESTION[1].@v3;
ans8.value=myXML.QUESTION[1].@v4;

btn2.addEventListener(MouseEvent.CLICK, clickhandler1);
function clickhandler1(event:MouseEvent):void
{
myTimer.reset();
myTimer.start();
timer.text="10 seconds remain";
if (ans5.group.selection.value == "true")
{
score_right +=  1;
nextFrame();
}
else
{
++score_wrong;
nextFrame();
}
}


Layer: as3 , Frame : 3

   qn3.text = myXML.QUESTION[2];
 ans9.label = myXML.QUESTION[2]. @ a1;
ans10.label = myXML.QUESTION[2]. @ a2;
ans11.label = myXML.QUESTION[2]. @ a3;
ans12.label = myXML.QUESTION[2]. @ a4;

ans9.value=myXML.QUESTION[2].@v1;
ans10.value=myXML.QUESTION[2].@v2;
ans11.value=myXML.QUESTION[2].@v3;
ans12.value=myXML.QUESTION[2].@v4;

btn3.addEventListener(MouseEvent.CLICK, clickhandler2);
function clickhandler2(event:MouseEvent):void
{
myTimer.reset();
myTimer.start();
if (ans10.group.selection.value == "true")
{
++score_right;

nextFrame();
}
else
{  
   ++score_wrong;
nextFrame();
}
}


Layer: as3 , Frame : 4

totalScore.text = score_right + "/3";
myTimer.stop();
timer.text="";



------------------ Reference / Memo -------------

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html#File()


http://stackoverflow.com/questions/16122172/filereference-vs-file/16123731#16123731

The File class extends the FileReference.
FileReference is safe to be used in the FlashPlayer (in the browser) because it won't let you modify the files in the user machine.

If you want to open a file, you need to as for the user to open it for you with: FileReference.browse().
To save a file, you need to ask the user to save it: FileReference.save()

With File class you can open, modify and save files without those dialogs.
Furthermore, the File class gives you a bunch of useful properties like: File.desktopDirectory, File.documentsDirectory and such.
You can check if a file exists with the exists property and have a much restriction when manipulating file in the user file system.

You can read more about the FileReference and File classes in the docs.


http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html#methodSummary

No comments:

Post a Comment