用于计算SCORM 1.2中的测验分数的Javascript

时间:2012-08-10 10:13:11

标签: javascript xhtml cdata scorm scorm1.2

我有一个SCORM 1.2内容包,其中包含一些包含测验问题的HTML页面。点击提交按钮后,javascript会计算得分并返回。我当前的java脚本适用于两个问题,如果我在一个页面中有更多问题,那么我必须通过包含一个新变量来修改javascript。我想知道是否有任何通用的方法为'n'问题编写相同的javascript;无论问题数量如何变化,都可以计算得分。感谢你。

我的Java脚本:( calculate.js)

<script type="text/javascript">
<![CDATA[
var numQuestions = 2;
var rawScore = 0;
var actualScore = 0;
var question0;
var question1;
var key0 = 0;
var key1 = 1;
function getAnswer()
        {
            doLMSSetValue("cmi.interactions.0.id","key0b8");
            doLMSSetValue("cmi.interactions.0.type","choice");
            doLMSSetValue("cmi.interactions.0.correct_responses.0.pattern",
                          "0");

            for (var i=0; i < 2; i++)
            {
               if (document.getElementById("quizForm8").key0b8[i].checked)
               {
                  question0 = document.getElementById("quizForm8").key0b8[i].value;
                  doLMSSetValue("cmi.interactions.0.student_response",question0);
                  break;
               }
            }

            doLMSSetValue("cmi.interactions.1.id","key1b8");
            doLMSSetValue("cmi.interactions.1.type","choice");
            doLMSSetValue("cmi.interactions.1.correct_responses.0.pattern",
                          "1");

            for (var i=0; i < 2; i++)
            {
               if (document.getElementById("quizForm8").key1b8[i].checked)
               {
                  question1 = document.getElementById("quizForm8").key1b8[i].value;
                  doLMSSetValue("cmi.interactions.1.student_response",question1);
                  break;
               }
            }

           }
        function calcRawScore(){

            if (question0 == key0)
            {
               doLMSSetValue("cmi.interactions.0.result","correct");
               rawScore++;
            }
            else
            {
               doLMSSetValue("cmi.interactions.0.result","wrong");
            }
            if (question1 == key1)
            {
               doLMSSetValue("cmi.interactions.1.result","correct");
               rawScore++;
            }
            else
            {
               doLMSSetValue("cmi.interactions.1.result","wrong");
            }
        }

        function calcScore2()
        {
           computeTime();  // the student has stopped here.

           document.getElementById("quizForm8").submitB.disabled = true;

           getAnswer();

           calcRawScore();

           actualScore = Math.round(rawScore / numQuestions * 100);
        alert("Your score is " + actualScore + "%")   

           doLMSSetValue( "cmi.core.score.raw", actualScore+"" );

           var mode = doLMSGetValue( "cmi.core.lesson_mode" );

               if ( mode != "review"  &&  mode != "browse" ){
                 if ( actualScore < 50 )
                 {
                   doLMSSetValue( "cmi.core.lesson_status", "failed" );
                 }
                 else 
                 {
                   doLMSSetValue( "cmi.core.lesson_status", "passed" );
                 }

                 doLMSSetValue( "cmi.core.exit", "" );
                 } 

         exitPageStatus = true;


         doLMSCommit();

         doLMSFinish();

        }
]]>
</script>

(X)HTML页面

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
 <!-- Other Code -->
      <body>
        <div id="outer">
          <div class="QuizTestIdevice" id="id8">
            <script src="calculate.js" type="text/javascript"></script>
            <form name="quizForm8" id="quizForm8" action="javascript:calcScore2();">
              <div class="iDevice_inner">
                <div class="passrate" value="50"></div>
                <div class="question">
                  <div id="taquestion0b8">
                    1&gt; TEXT FOR QUESTION 1.
                  </div><br />
                  True<input type="radio" name="key0b8" value="0" id="taoptionAnswer0q0b8" /> 
                  False<input type="radio" name="key0b8" value="1" id="taoptionAnswer1q0b8" />
                </div><br />
                <div class="question">
                  <div id="taquestion1b8">
                    2&gt; TEXT FOR QUESTION 2.
                  </div><br />
                  True<input type="radio" name="key1b8" value="0" id="taoptionAnswer0q1b8" /> 
                  False<input type="radio" name="key1b8" value="1" id="taoptionAnswer1q1b8" />
                </div><br />
                <input type="submit" name="submitB" value="SUBMIT ANSWERS" />
              </div>
            </form>
          </div>
        </div>
      </body>
    </html>

1 个答案:

答案 0 :(得分:1)

如果你想保持你的java脚本不可更改,那么你的HTML必须承担责任。

<form name="quizForm8" id="quizForm8" action="javascript:calcScore2();">

应更改为如下,

<form name="quizForm8" id="quizForm8" action="javascript:calcScore2(5);"> //5 -> Argument for 5 questions

每当问题计数发生变化时,都需要更改参数。

同时更改您的javascript一次,

function calcScore2("n")
{ 
numQuestions = n;
.
.