Эх сурвалжийг харах

Add exact question export to SCORM see BT#11321

Julio 8 жил өмнө
parent
commit
91ab44dc37

+ 33 - 30
main/exercise/export/scorm/scorm_classes.php

@@ -199,6 +199,9 @@ class ScormAnswerMultipleChoice extends Answer
 		$jstmpw = 'questions_answers_ponderation['.$this->questionJSId.'] = new Array();';
 		$jstmpw .= 'questions_answers_ponderation['.$this->questionJSId.'][0] = 0;';
 
+        $jstmpw .= 'questions_answers_correct['.$this->questionJSId.'] = new Array();';
+        $jstmpw .= 'questions_answers_correct['.$this->questionJSId.'][0] = 0;';
+
 		//not sure if we are going to export also the MULTIPLE_ANSWER_COMBINATION to SCORM
 		//if ($type == MCMA  || $type == MULTIPLE_ANSWER_COMBINATION ) {
 		if ($type == MCMA) {
@@ -227,36 +230,37 @@ class ScormAnswerMultipleChoice extends Answer
 			}
 			$js .= 'questions_answers['.$this->questionJSId.'] = new Array('.substr($jstmp,0,-1).');'."\n";
 			$js .= 'questions_answers_correct['.$this->questionJSId.'] = new Array('.substr($jstmpc,0,-1).');'."\n";
-			if ($type == MCMA) {
-				$js .= 'questions_types['.$this->questionJSId.'] = \'mcma\';'."\n";
-			} else {
-				$js .= 'questions_types['.$this->questionJSId.'] = \'exact\';'."\n";
-			}
+            $js .= 'questions_types['.$this->questionJSId.'] = \'mcma\';'."\n";
 			$js .= $jstmpw;
 		} elseif ($type == MULTIPLE_ANSWER_COMBINATION) {
-			//To this items we show the ThisItemIsNotExportable
-			$qId = $this->questionJSId;
-			$js = '';
-			$html = '<tr><td colspan="2"><table width="100%">';
-			// some javascript must be added for that kind of questions
-			$html .= '<tr>
-				<td>
-				<textarea name="question_'.$qId.'_free" id="question_'.$qId.'_exact" rows="20" cols="100"></textarea>
-				</td>
-				</tr>';
-			$html .= '</table></td></tr>';
-			// currently the exact answers cannot be displayed, so ignore the textarea
-			$html = '<tr><td colspan="2">'.get_lang('ThisItemIsNotExportable').'</td></tr>';
-			$js .= 'questions_answers['.$this->questionJSId.'] = new Array();'."\n";
-			$js .= 'questions_answers_correct['.$this->questionJSId.'] = new Array();'."\n";
-			$js .= 'questions_types['.$this->questionJSId.'] = \'exact\';'."\n";
-			$jstmpw = 'questions_answers_ponderation['.$this->questionJSId.'] = new Array();'."\n";
-			$jstmpw .= 'questions_answers_ponderation['.$this->questionJSId.'][0] = 0;'."\n";
-			$jstmpw .= 'questions_answers_ponderation['.$this->questionJSId.'][1] = 0;'.";\n";
-			$js .= $jstmpw;
-			return array($js, $html);
+	    	$js = '';
+            $id = 1;
+            $jstmp = '';
+            $jstmpc = '';
+            foreach ($this->answer as $i => $answer) {
+                $identifier = 'question_'.$this->questionJSId.'_exact_'.$i;
+                $html .=
+                    '<tr>
+					<td align="center" width="5%">
+					<input name="'.$identifier.'" id="'.$identifier.'" value="'.$i.'" type="checkbox" />
+					</td>
+					<td width="95%">
+					<label for="'.$identifier.'">' . $this->answer[$i] . '</label>
+					</td>
+					</tr>';
+
+                $jstmp .= $i.',';
+                if ($this->correct[$i]) {
+                    $jstmpc .= $i.',';
+                }
+                $jstmpw .= 'questions_answers_ponderation['.$this->questionJSId.']['.$i.'] = '.$this->weighting[$i].";";
+                $jstmpw .= 'questions_answers_correct['.$this->questionJSId.']['.$i.'] = '.$this->correct[$i].";";
+                $id++;
+            }
+            $js .= 'questions_answers['.$this->questionJSId.'] = new Array('.substr($jstmp,0,-1).');';
+            $js .= 'questions_types['.$this->questionJSId.'] = "exact";';
+            $js .= $jstmpw;
 		} else {
-			//$questionTypeLang = get_lang('MultipleChoiceUniqueAnswer');
 			$id = 1;
 			$jstmp = '';
 			$jstmpc = '';
@@ -266,7 +270,7 @@ class ScormAnswerMultipleChoice extends Answer
 				$html .=
 					'<tr>
 					<td align="center" width="5%">
-					<input name="'.$identifier_name.'" id="'.$identifier.'" value="'.$i.'" type="radio"/>
+					<input name="'.$identifier_name.'" id="'.$identifier.'" value="'.$i.'" type="checkbox"/>
 					</td>
 					<td width="95%">
 					<label for="'.$identifier.'">' . $this->answer[$i] . '</label>
@@ -539,11 +543,10 @@ class ScormAnswerFree extends Answer
 		$html = '<tr><td colspan="2">';
         $html .= '<textarea minlength="20" name="'.$identifier.'" id="'.$identifier.'" ></textarea>';
         $html .= '</td></tr>';
-        $score = $this->selectWeighting(1);
 		$js .= 'questions_answers['.$this->questionJSId.'] = new Array();';
 		$js .= 'questions_answers_correct['.$this->questionJSId.'] = "";';
 		$js .= 'questions_types['.$this->questionJSId.'] = \'free\';';
-		$jstmpw = 'questions_answers_ponderation['.$this->questionJSId.'] = "'.$score.'";';
+		$jstmpw = 'questions_answers_ponderation['.$this->questionJSId.'] = "0";';
 		$js .= $jstmpw;
 
 		return array($js, $html);

+ 35 - 47
main/lp/js/api_wrapper.js

@@ -14,8 +14,7 @@
 var _debug = false;
 var findAPITries = 0;
 var _apiHandle = null; //private variable
-var errMsgLocate = "Unable to locate the LMS's API implementation"
-
+var errMsgLocate = "Unable to locate the LMS's API implementation";
 var _NoError = 0;
 var _GeneralException = 101;
 var _ServerBusy = 102;
@@ -34,11 +33,12 @@ var _IncorrectDataType = 405;
  * Using the singleton pattern to ensure there's only one API object.
  * @return	object The API object as given by the LMS
  */
-var API = new function ()
+var API = new function()
 {
     if (_apiHandle == null) {
         _apiHandle = getAPI();
     }
+
     return _apiHandle;
 }
 
@@ -96,14 +96,14 @@ function ErrorHandler()
                 errDescription += "\n";
                 errDescription += api.LMSGetDiagnostic(null);
             }
-            alert(errDescription);
+            console.log(errDescription);
         } else {
             var errDescription = API.LMSGetErrorString(errCode);
             if (_debug) {
                 errDescription += "\n";
                 errDescription += api.LMSGetDiagnostic(null);
             }
-            alert(errDescription);
+            console.log(errDescription);
         }
     }
     return errCode;
@@ -161,7 +161,6 @@ function doLMSGetValue(name)
             var errDescription = API.LMSGetErrorString(errCode);
             alert("LMSGetValue(" + name + ") failed. \n" + errDescription);
             return "";
-            return value.toString();
         }
     }
 }
@@ -400,8 +399,8 @@ function checkAnswers(interrupted)
     var tmpScore = 0;
     var status = 'not attempted';
     var scoreMax = 0;
-    for (var i=0; i<questions.length;i++) {
-        if(questions[i] != undefined && questions[i] != null){
+    for (var i=0; i < questions.length; i++) {
+        if (questions[i] != undefined && questions[i] != null){
             var idQuestion = questions[i];
             var type = questions_types[idQuestion];
             var interactionScore = 0;
@@ -410,7 +409,7 @@ function checkAnswers(interrupted)
             var interactionType = '';
 
             if (type == 'mcma') {
-                var interactionType = 'choice';
+                interactionType = 'choice';
                 var myScore = 0;
                 for(var j=0; j<questions_answers[idQuestion].length;j++) {
                     var idAnswer = questions_answers[idQuestion][j];
@@ -423,7 +422,7 @@ function checkAnswers(interrupted)
                 interactionScore = myScore;
                 scoreMax += questions_score_max[idQuestion];
             } else if (type == 'mcua') {
-                var interactionType = 'choice';
+                interactionType = 'choice';
                 var myScore = 0;
                 for (var j=0; j<questions_answers[idQuestion].length;j++) {
                     var idAnswer = questions_answers[idQuestion][j];
@@ -442,7 +441,7 @@ function checkAnswers(interrupted)
                 interactionScore = myScore;
                 scoreMax += questions_score_max[idQuestion];
             } else if (type == 'tf') {
-                var interactionType = 'true-false';
+                interactionType = 'true-false';
                 var myScore = 0;
                 for (var j = 0; j < questions_answers[idQuestion].length; j++) {
                     var idAnswer = questions_answers[idQuestion][j];
@@ -463,7 +462,7 @@ function checkAnswers(interrupted)
                 interactionScore = myScore;
                 scoreMax += questions_score_max[idQuestion];
             } else if (type == 'fib') {
-                var interactionType = 'fill-in';
+                interactionType = 'fill-in';
                 var myScore = 0;
                 for (var j = 0; j < questions_answers[idQuestion].length; j++) {
                     var idAnswer = questions_answers[idQuestion][j];
@@ -484,7 +483,7 @@ function checkAnswers(interrupted)
                 interactionScore = myScore;
                 scoreMax += questions_score_max[idQuestion];
             } else if (type == 'matching') {
-                var interactionType = 'matching';
+                interactionType = 'matching';
                 var myScore = 0;
                 for (var j = 0; j < questions_answers[idQuestion].length; j++) {
                     var idAnswer = questions_answers[idQuestion][j];
@@ -508,20 +507,21 @@ function checkAnswers(interrupted)
                 scoreMax += questions_score_max[idQuestion];
             } else if (type == 'free') {
                 //ignore for now as a score cannot be given
-                var interactionType = 'free';
+                interactionType = 'free';
                 var answer = document.getElementById('question_'+(idQuestion)+'_free');
                 if (answer && answer.value) {
                     interactionAnswers += answer.value
                 }
 
-                interactionScore = questions_score_max[idQuestion];
+                //interactionScore = questions_score_max[idQuestion];
+                interactionScore = 0;
                 scoreMax += questions_score_max[idQuestion];
 
                 //interactionAnswers = document.getElementById('question_'+(idQuestion)+'_free').value;
                 //correct responses work by pattern, see SCORM Runtime Env Doc
                 //interactionCorrectResponses += questions_answers_correct[idQuestion].toString();
             } else if (type == 'hotspot') {
-                var interactionType = 'sequencing';
+                interactionType = 'sequencing';
                 interactionScore = 0;
                 //if(question_score && question_score[idQuestion]){
                 //	interactionScore = question_score[idQuestion];
@@ -533,60 +533,48 @@ function checkAnswers(interrupted)
                 //	interactionCorrectResponses += questions_answers_correct[idQuestion][k].toString()+',';
                 //}
             } else if (type == 'exact') {
-                var interactionType = 'exact';
+                interactionType = 'exact';
                 interactionScore = 0;
-                // not yet implemented see scorm_classes.php ScormAnswerMultipleChoice::export() function
-                /*
-
-                var myScore = 0;
                 var real_answers = new Array();
-
-                for(var j=0; j<questions_answers[idQuestion].length;j++) {
+                console.log(questions_answers_correct[idQuestion]);
+                for (var j = 0; j < questions_answers[idQuestion].length; j++) {
                     var idAnswer = questions_answers[idQuestion][j];
-                    var answer   = document.getElementById('question_'+(idQuestion)+'_multiple_'+(idAnswer));
-                    if (answer.checked) {
-                        if(questions_answers_ponderation[idQuestion][idAnswer] != 0 ) {
+                    var answer = document.getElementById('question_' + (idQuestion) + '_exact_' + (idAnswer));
+
+                    if (answer.checked == true) {
+                        if (questions_answers_correct[idQuestion][idAnswer] != 0) {
                             real_answers[j] = true;
                         } else {
                             real_answers[j] = false;
                         }
                     } else {
-                        if(questions_answers_ponderation[idQuestion][idAnswer] != 0) {
+                        if (questions_answers_correct[idQuestion][idAnswer] != 0) {
                             real_answers[j] = false;
                         } else {
                             real_answers[j] = true;
                         }
                     }
-                    //alert(real_answers[j] +' ' + answer.checked + ' ' + questions_answers_ponderation[idQuestion][idAnswer]);
                 }
 
                 var final_answer = true;
-                for(var z=0; z<real_answers.length ;z++) {
-                     if (!real_answers[z]) {
-                         final_answer = false;
-                     }
-                 }
+                for (var z = 0; z < real_answers.length; z++) {
+                    if (real_answers[z] == false) {
+                        final_answer = false;
+                    }
+                }
 
-                 if (final_answer) {
+                if (final_answer) {
                      //getting only the first score where we save the weight of all the question
-                    myScore += questions_answers_ponderation[idQuestion][1];
+                     interactionScore = questions_answers_ponderation[idQuestion][1];
                 }
-
-                interactionScore = myScore;
-                //correct responses work by pattern, see SCORM Runtime Env Doc
-                //for(k=0;k<questions_answers_correct[idQuestion].length;k++)
-                //{
-                //	interactionCorrectResponses += questions_answers_correct[idQuestion][k].toString()+',';
-                //}
                 scoreMax += questions_score_max[idQuestion];
-                */
             }
             tmpScore += interactionScore;
 
-            doLMSSetValue('cmi.interactions.'+idQuestion+'.id','Q'+idQuestion);
-            doLMSSetValue('cmi.interactions.'+idQuestion+'.type',interactionType);
-            doLMSSetValue('cmi.interactions.'+idQuestion+'.student_response',interactionAnswers);
-            doLMSSetValue('cmi.interactions.'+idQuestion+'.result',interactionScore);
+            doLMSSetValue('cmi.interactions.'+idQuestion+'.id', 'Q'+idQuestion);
+            doLMSSetValue('cmi.interactions.'+idQuestion+'.type', interactionType);
+            doLMSSetValue('cmi.interactions.'+idQuestion+'.student_response', interactionAnswers);
+            doLMSSetValue('cmi.interactions.'+idQuestion+'.result', interactionScore);
         }
     }
     doLMSSetValue('cmi.core.score.min', 0);