Ngôn ngữ VBA (Microsoft Visual Basic for Applications)

Một phần của tài liệu tìm hiểu và xây dựng hệ thống framwork hỗ trợ các hình thức trắc nghiệm (Trang 157 - 173)

Tất cả các ứng dụng của Microsoft Officeđều có ngôn ngữ script của nó, đó chính là VBA. Ngôn ngữ này có thể được sử dụng để thực hiện các nhiệm vụ trong ứng dụng. Nói 1 cách dễ hiểu thì tất cả các thao tác trên ứng dụng thông qua thanh menuvàbànphímđềucóthểđượcdiễntảlại bằngngônngữnày.Vìvậy,có1cách giúp cho người chưa quen với đối tượng của MS Word xác định được các công việc

phải làm khi thực hiện automation là sử dụng các macro của chính MS Word ghi lại các thao tác muốn automate.

Ví dụ bạn muốn automate MS Word để tạo 1 file mới, gõ vào 1 đoạn text rồi lưulại nhưng bạn không biết phải sử dụng cáchàm nào. Bạn có thể thông quacác macro để biết được điều đó.

Trước tiên, bạn phải ghi lại 1 macro mới.

Hình9-2RecordmacrotrongMSWord

Sauđó, bạnthựchiệncácthaotácmuốnautomaterồidừngviệcghimacrolại. Và cuối cùng, bạn chỉ việc xem MS Word đã thao tác như thế nào thông qua macro vừaghiđược.

126

Hình9-3EditmacrotrongMSWord

127

9.2.4 Ứngdụngtrong hệthống

Để có thể thực hiện automation, trước tiên phải cho phép chương trình tham chiếu đến thư viện xử lý của Word.

Hình9-5AddreferenceđếnWordObjectLibrarytrong.NET

9.2.4.1 Import

Thao tác import trong hệ thống cần phải xử lý cả việc import hình ảnh. Mà như đã biết, các hình ảnh được chèn vào trong file .doc thì sẽ được lưu cùng với file .doc đó. Do vậy, để lấy được hình cụ thể, chúng em phải tiến hành lưu tạm file .doc dưới dạng trang web, rồi tìm trong source của trang web liên kết đến hình ảnh cụ thể để copy lưu vào trong cơ sở dữ liệu.

Object missing = System.Reflection.Missing.Value; object saveFileName = uniqueFileName;

object fileFormat = Word.WdSaveFormat.wdFormatHTML;

// Luu vao file tam duoi dang html

wordApp.ActiveDocument.SaveAs(

ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref mis sing,

ref missing, ref missing, ref missing, ref missing);

128

wordApp.ActiveDocument.Close(ref missing, ref missing, ref mi ssing);

StreamReader r = new StreamReader(uniqueFileName); string str = r.ReadToEnd(); r.Close(); int n1 = str.IndexOf("<body"); char tempChar = '<'; while(tempChar != '>') { tempChar = str[n1]; n1++; } int n2 = str.IndexOf("</body>");

string strFinal = str.Substring(n1, n2 - n1);

Dựavàocácdấuphâncáchquyđịnhsẵn,chúngemsẽtáchđượcnộidungcác câu hỏi, và trong mỗi câu hỏi sẽ lọc ra được đâu là nội dung hỏi và đâu ra nội dung cáclựa chọn (đơn thuần chỉ làthao tác xửlý chuỗi). Cụ thể, một câuhỏi trong file muốn import phải có định dạng như sau:

[!!!]Nộidunghỏi

<!!!>Nộidunglựachọnthứ1* <!!!> Nội dung lựa chọn thứ 2 <!!!>Nộidunglựachọnthứ3 <!!!> Nội dung lựa chọn thứ 4 * <!!!> Nội dung lựa chọn thứ 5 Trongđó:

- [!!!] là dấu quy định phân cách các câu hỏi. - <!!!> là dấu quy định phân cách các lựa chọn. - *làdấuquyđịnhlựachọnđúng.

- Số lựa chọn và số lựa chọn đúng là không có giới hạn.

xml và lưu vào CSDL.

129

9.2.4.2 Export

Hệ thống hỗtrợ việc export 1đề thira file .doc theo1 template đềthi có sẵn. Template đề thi được tạo ra theo ý thích của các giáo viên và được copy vào trong thư mục WordFile Temlates của hệ thống. 1 template đề thi phải đáp ứng các yêu cầu sau:

- Có 1 bookmark có tên là assName đặt tại vị trí giáo viên muốn hiển thị tên bàithi.

- Có 1 bookmark có tên là assHeader đặt tại vị trí giáo viên muốn hiển thị các ghi chú đầu bài thi (header)

- Có1 bookmark có tên làassFooter đặt tại vị trígiáo viên muốn hiểnthị các ghi chú cuối bài thi (footer)

- Có 1 bookmark có tên là assSections đặt tại vị trí giáo viên muốn bắt đầu hiểnthịcácsection,cáccâuhỏicủabàithi.

- Phần nào giáo viên không muốn hiển thị thì bookmark đánh dấu phần đó không cần phải được tạo ra.

Khicó yêucầuexport1đềthirafile, chươngtrìnhsẽtìmvịtrícác bookmark, nếu bookmark tồn tại trong file template được chọn thì chương trình sẽ ghi đoạn văn bản phù hợp vào vị trí bookmark đó.

foreach (Word.Bookmark brm in privateWordApp.ActiveDocument.B ookmarks)

if (brm.Name == "assHeader")

ReplaceBookmarkText("assHeader", privateAssessment.HeaderNote);

Trường hợpđoạn vănbản cần ghi cóchứa tag image(tức là phải chèn1 hình vào file) thì chương trình sẽ thực hiện thêm 1 đối tượng Word.Shape vào tập hợp hình ảnh mà văn bản đang được xử lý quản lý. Khi lưu văn bản, các hình ảnh cũng sẽđượclưutrữtheo.

private void TypeString(Word.ApplicationClass privateWordApp, string

strText) {

int pos = strText.IndexOf("<img"); while (pos >= 0)

{

privateWordApp.Selection.TypeText(strText.Substring(0, pos));

strText = strText.Remove(0, pos);

130

pos = strText.IndexOf("src="); strText.Remove(0, pos+1);

pos = strText.IndexOf("\"");

string image = strText.Substring(0, pos); Object oFalse = false;

Object oTrue = true;

Object oMissing = Type.Missing;

// them 1 hinh anh vao tap hop InlineShapes

privateWordApp.Selection.InlineShapes.AddPicture(image, ref oFalse, ref oTrue, ref oMissing);

pos = strText.IndexOf("/img>"); strText.Remove(0, pos);

} pos = strText.IndexOf("<img");

privateWordApp.Selection.TypeText(strText); }

131

Chương 10 Tổng kết

10.1Kết luận

Sau thời gian nghiên cứu, tìm hiểu các hình thức trắc nghiệm và phát triển ứng dụng, đề tài đã đạt được các mục tiêu sau:

o Xây dựng được một cổng dịch vụ thicử là một hệ thống liên hoàn ph

ối

hợp nhuần nhuyễn giữa môi trường web và windows trong việc quản lý ngân hàng câu hỏi, đề thi và tổ chức thi trắc nghiệm.

o Cómột khung ứng dụng (framework) hết sứclinh động,có tính tiến hó

a

cao giúp cho việc mở rộng và nâng cấp hệ thống thật sự dễ dàng và hiệu quả.

o Xây dựng được các ứng dụng trên web và trên windows tiện dụng, ổ

n

địnhvànhiềutínhnănghữuích, cóthểứngdụngđượcngaytrongthựctế trong việc soạn câu hỏi, đề thi trắc nghiệm và thực hiện việc đánh giá, kiểmtranhanhchóng.

o CấutrúccâuhỏitrắcnghiệmtuântheođặctảIMSQTI–mộtđặctảXM

L,

làđặc tả quốctế được sửdụng rộng rãinhất trênthế giới. Nhờđó,cơ sở dữ liệu ngân hàng câu hỏi của hệ thống có khả năng trao đổi, có thể phân phốihay sửdụng lạiởbất cứhệ thốngnào khác trênthế giớicósử dụng đặc tả này.

10.2Hướng phát triển

Thêm nhiều loại câu hỏi thêm nữa

Hỗ trợ thêm nhiều phiên bản khác nhau cho từng loại câu hỏi

Hoàn thiện thêm các chức năng giúp thuận tiện hơn cho người sử dụng

132

Phuï luïc A -Export databasetheo đặctả IMSQTI

Nội dung file XSLT làm nhiệm vụ export: <?xml version="1.0"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="@*|node()">

</xsl:template>

<xsl:template match="assessmentItem">

<xsl:copy>

<xsl:apply-templates select="@* | text() | node()" />

</xsl:copy>

</xsl:template>

<xsl:template match="responseDeclaration">

<xsl:copy>

<xsl:apply-templates select="@* | text() | node()" />

</xsl:copy>

</xsl:template>

<xsl:template match="responseDeclaration/node()

| responseDeclaration/text()

| responseDeclaration/node()/node()

| responseDeclaration/text()/node()">

<xsl:copy>

<xsl:apply-templates select="@* | text() | node()" />

</xsl:copy>

</xsl:template>

<xsl:template match="outcomeDeclaration">

<xsl:copy>

<xsl:apply-templates select="@* | text() | node()" />

</xsl:copy>

</xsl:template>

<xsl:template match="itemBody">

<xsl:copy>

<xsl:apply-templates select="@* | text() | node()" />

</xsl:copy>

</xsl:template>

<xsl:template match="itemBody/node() | itemBody/text()

| itemBody/node()/node() | itemBody/node()/text()">

<xsl:copy>

<xsl:apply-templates select="@* | text() | node()" />

</xsl:copy>

133

</xsl:template>

<xsl:template match="itemBody/questionView">

</xsl:template>

<xsl:template match="responseProcessing">

<xsl:copy>

</xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:copy> <xsl:apply-templates select="*" /> </xsl:copy> </xsl:template>

<xsl:template match="text()">

<xsl:copy> <xsl:apply-templates select="*" /> </xsl:copy> </xsl:template> </xsl:stylesheet> 134

Phuï luïc B - Phầnlưutrữ XML của một số dạngcâu hỏitrong đặctả IMSQTI

o CâuhỏiinlineChoiceInteraction:

<?xml version="1.0" encoding="UTF-8"?>

<assessmentItem identifier="choiceMultiple"

title="Composition of Water"

adaptive="false" timeDependent="false"> <responseDeclaration identifier="RESPONSE"

cardinality="multiple" baseType="identifier"> <correctResponse>

<value>H</value> <value>O</value> </correctResponse>

<mapping lowerBound="0" upperBound="2" defaultValue="- 2">

<mapEntry mapKey="H" mappedValue="1"/> <mapEntry mapKey="O" mappedValue="1"/> <mapEntry mapKey="Cl" mappedValue="-1"/> </mapping>

</responseDeclaration>

<outcomeDeclaration identifier="SCORE" cardinality="single "

baseType="integer"/> <itemBody>

<choiceInteraction responseIdentifier="MR01" shuffle="tr ue"

maxChoices="4">

<prompt>Which of the following elements are used to fo rm

water?</prompt>

<simpleChoice identifier="H"

fixed="false">Hydrogen</simpleChoice> <simpleChoice identifier="He"

fixed="false">Helium</simpleChoice> <simpleChoice identifier="C"

fixed="false">Carbon</simpleChoice> <simpleChoice identifier="O"

fixed="false">Oxygen</simpleChoice> <simpleChoice identifier="N" fixed="false">Nitrogen</simpleChoice> <simpleChoice identifier="Cl" fixed="false">Chlorine</simpleChoice> </choiceInteraction> </itemBody> <responseProcessing template="http://www.imsglobal.org/xml/imsqti_item_v2p0/rpMap Respo nse" templateLocation="../RPTemplates/rpMapResponse.xml"/> </assessmentItem>

135

o CâuhỏitextEntryInteraction:

<?xml version="1.0" encoding="UTF-8"?>

<assessmentItem identifier="textEntry" title="Richard III (Ta ke

3)"

adaptive="false" timeDependent="false">

<responseDeclaration identifier="RESPONSE" cardinality="si ngle" baseType="string"> <correctResponse> <value>York</value> </correctResponse> <mapping defaultValue="0">

<mapEntry mapKey="York" mappedValue="1"/> <mapEntry mapKey="york" mappedValue="0.5"/> </mapping>

</responseDeclaration>

<outcomeDeclaration identifier="SCORE" cardinality="single "

baseType="float"/> <itemBody>

<p>Identify the missing word in this famous quote from Shakespeare's Richard III.</p>

<blockquote>

<p>Now is the winter of our discontent<br/> Made glori ous

summer by this sun of

<textEntryInteraction responseIdentifier="RESPONSE"

expectedLength="15"/>;<br/>

And all the clouds that lour'd upon our house<br/> In

the deep bosom of the ocean buried.</p> </blockquote> </itemBody> <responseProcessing template="http://www.imsglobal.org/xml/imsqti_item_v2p0/rpMap Respo nse" templateLocation="../RPTemplates/rpMapResponse.xml"/> </assessmentItem>

136

o CâuhỏiorderInteraction:

<?xml version="1.0" encoding="UTF-8"?>

<assessmentItem identifier="order" title="Grand Prix of Bahra in"

adaptive="false" timeDependent="false">

<responseDeclaration identifier="RESPONSE"

cardinality="ordered" baseType="identifier"> <correctResponse> <value>DriverC</value> <value>DriverA</value> <value>DriverB</value> </correctResponse> </responseDeclaration>

<outcomeDeclaration identifier="SCORE" cardinality="single "

baseType="integer"/> <itemBody>

<orderInteraction responseIdentifier="RESPONSE"

shuffle="true">

<prompt>The following F1 drivers finished on the podiu m in

the first ever Grand Prix of

Bahrain. Can you rearrange them into the correct finishing order?</prompt>

<simpleChoice identifier="DriverA">Rubens Barrichello</simpleChoice>

<simpleChoice identifier="DriverB">Jenson Button</simpleChoice>

<simpleChoice identifier="DriverC">Michael Schumacher</simpleChoice> </orderInteraction> </itemBody> <responseProcessing template="http://www.imsglobal.org/xml/imsqti_item_v2p0/rp Match

Correct" templateLocation="../RPTemplates/rpMatchCorrect.xml"

/>

137

TÀI LIỆU THAM KHẢO

[1] www.msdn.microsoft.com [2] www.imsglobal.org [3] www.el.edu.net.vn [4] www.codeproject.com [5] www.dnzone.com [6] www.asp.net [7] www.w3.org

Một phần của tài liệu tìm hiểu và xây dựng hệ thống framwork hỗ trợ các hình thức trắc nghiệm (Trang 157 - 173)