From 77b9d1542e92b366266d9282459afb0d3bec2fc5 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Fri, 4 Jul 2008 02:50:44 +0200 Subject: [PATCH] [svn r15719] "Multiple answers" in fill in blank option added (see FS#2683) --- main/exercice/exercice_submit.php | 41 +++-- main/exercice/exercise_result.class.php | 27 +-- main/exercice/exercise_result.php | 211 +++++++++++++++++++----- main/exercice/exercise_show.php | 192 +++++++++++++++------ main/exercice/fill_blanks.class.php | 76 ++++++--- 5 files changed, 405 insertions(+), 142 deletions(-) diff --git a/main/exercice/exercice_submit.php b/main/exercice/exercice_submit.php index 0e6c448103..81cacfb215 100644 --- a/main/exercice/exercice_submit.php +++ b/main/exercice/exercice_submit.php @@ -1,20 +1,23 @@ 0){echo str_repeat(' ',0).'$choice is an array'."
\n";} if($exerciseType == 1) @@ -190,7 +195,7 @@ if($formSent) // if it is the last question (only for a sequential exercise) if($exerciseType == 1 || $questionNum >= $nbrQuestions) - { + { if($debug>0){echo str_repeat(' ',0).'Redirecting to exercise_result.php - Remove debug option to let this happen'."
\n";} // goes to the script that will show the result of the exercise header("Location: exercise_result.php?origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id"); @@ -206,7 +211,7 @@ if(!isset($_SESSION['objExercise']) || $origin == 'learnpath' || $_SESSION['objE // construction of Exercise $objExercise=new Exercise(); unset($_SESSION['questionList']); - + // if the specified exercise doesn't exist or is disabled if(!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath') )) { @@ -444,7 +449,7 @@ else $number_of_hotspot_questions = 0; $onsubmit = ''; $i=0; - + foreach($questionList as $questionId) { $i++; diff --git a/main/exercice/exercise_result.class.php b/main/exercice/exercise_result.class.php index 9f9724b646..5ac5677a51 100644 --- a/main/exercice/exercise_result.class.php +++ b/main/exercice/exercise_result.class.php @@ -1,20 +1,23 @@ "; + print_r($user_tags); + echo "
"; + print_r($correct_tags); + print_r($real_text); + echo ""; + + */ + + $answer=''; + $real_correct_tags = $correct_tags; + $chosen_list=array(); + + for($i=1;$i'; + } + else + { + // adds a tabulation if no word has been typed by the student + $answer.='   '; + } + } + else + { // multiple fill in blank magic + if (in_array($user_tags[$i],$correct_tags)) + { + $chosen_list[]=$user_tags[$i]; + $correct_tags=array_diff($correct_tags,$chosen_list); + + // gives the related weighting to the student + $questionScore+=$answerWeighting[$i-1]; + // increments total score + $totalScore+=$answerWeighting[$i-1]; + // adds the word in green at the end of the string + $answer.=stripslashes($user_tags[$i]); + } // else if the word entered by the student IS NOT the same as the one defined by the professor + elseif(!empty($user_tags[$i])) + { + // adds the word in red at the end of the string, and strikes it + $answer.=''.stripslashes($user_tags[$i]).''; + } + else + { + // adds a tabulation if no word has been typed by the student + $answer.='   '; + } + } + // adds the correct word, followed by ] to close the blank + $answer.=' / '.$real_correct_tags[$i].']'; + $answer.=$real_text[$i]; + } + + /* + $answer=''; + // the loop will stop at the end of the text + while(1) + { + // quits the loop if there are no more blanks + if(($pos = strpos($temp,'[')) === false) { - // gives the related weighting to the student - $questionScore+=$answerWeighting[$j]; + // adds the end of the text + $answer.=$temp; + // TeX parsing + $texstring = api_parse_tex($texstring); + $answer=str_replace("{texcode}",$texstring,$answer); + break; + } - // increments total score - $totalScore+=$answerWeighting[$j]; + // adds the piece of text that is before the blank and ended by [ + $answer.=substr($temp,0,$pos+1); + $temp=substr($temp,$pos+1); - // adds the word in green at the end of the string - $answer.=stripslashes($choice[$j]); + // quits the loop if there are no more blanks + if(($pos = strpos($temp,']')) === false) + { + // adds the end of the text + $answer.=$temp; + break; } - // else if the word entered by the student IS NOT the same as the one defined by the professor - elseif(!empty($choice[$j])) + + $choice[$j]=trim($choice[$j]); + + if (!$multiple_answer_set) + { + // if the word entered by the student IS the same as the one defined by the professor + if(strtolower(substr($temp,0,$pos)) == stripslashes(strtolower($choice[$j]))) + { + // gives the related weighting to the student + $questionScore+=$answerWeighting[$j]; + + // increments total score + $totalScore+=$answerWeighting[$j]; + + // adds the word in green at the end of the string + $answer.=stripslashes($choice[$j]); + } + } + else + { + + } + + // else if the word entered by the student IS NOT the same as the one defined by the professor + if(!empty($choice[$j])) { // adds the word in red at the end of the string, and strikes it $answer.=''.stripslashes($choice[$j]).''; @@ -576,6 +712,7 @@ $exerciseTitle=api_parse_tex($exerciseTitle); $temp=substr($temp,$pos+1); } + */ break; // for free answer diff --git a/main/exercice/exercise_show.php b/main/exercice/exercise_show.php index f71c3f4ae3..4526a5a07a 100644 --- a/main/exercice/exercise_show.php +++ b/main/exercice/exercise_show.php @@ -1,27 +1,30 @@ selectId(); //added by priya saini // destruction of the Question object - unset($objQuestionTmp); + unset($objQuestionTmp); @@ -551,74 +555,162 @@ $result =api_sql_query($query, __FILE__, __LINE__); $objAnswerTmp=new Answer($questionId); $nbrAnswers=$objAnswerTmp->selectNbrAnswers(); $questionScore=0; + for($answerId=1;$answerId <= $nbrAnswers;$answerId++) - { + { $answer=$objAnswerTmp->selectAnswer($answerId); $answerComment=$objAnswerTmp->selectComment($answerId); $answerCorrect=$objAnswerTmp->isCorrect($answerId); $answerWeighting=$objAnswerTmp->selectWeighting($answerId); - list($answer,$answerWeighting)=explode('::',$answer); + + // the question is encoded like this + // [A] B [C] D [E] F::10,10,10@1 + // number 1 before the "@" means that is a multiple fill in blank question + // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10 + // means that is a normal fill blank question + $multiple=explode('@',$answer); + + // is multiple fill blank or not + $multiple_answer_set=false; + if ($multiple[1]==1) + { + $multiple_answer_set=true; + } + + list($answer,$answerWeighting)=explode('::',$multiple[0]); + // splits weightings that are joined with a comma $answerWeighting=explode(',',$answerWeighting); // we save the answer because it will be modified $temp=$answer; + // TeX parsing // 1. find everything between the [tex] and [/tex] tags $startlocations=strpos($temp,'[tex]'); $endlocations=strpos($temp,'[/tex]'); if($startlocations !== false && $endlocations !== false) - { + { $texstring=substr($temp,$startlocations,$endlocations-$startlocations+6); // 2. replace this by {texcode} $temp=str_replace($texstring,'{texcode}',$temp); - } + } $j=0; // the loop will stop at the end of the text $i=0; - while(1) + //normal fill in blank + if (!$multiple_answer_set) + { + while(1) { - // quits the loop if there are no more blanks - if(($pos = strpos($temp,'[')) === false) + // quits the loop if there are no more blanks + if(($pos = strpos($temp,'[')) === false) { - // adds the end of the text - $answer.=$temp; - // TeX parsing - $texstring = api_parse_tex($texstring); - break; + // adds the end of the text + $answer.=$temp; + // TeX parsing + $texstring = api_parse_tex($texstring); + break; } - $temp=substr($temp,$pos+1); - // quits the loop if there are no more blanks - if(($pos = strpos($temp,']')) === false) + $temp=substr($temp,$pos+1); + // quits the loop if there are no more blanks + if(($pos = strpos($temp,']')) === false) { break; } - $queryfill = "select answer from ".$TBL_TRACK_ATTEMPT." where exe_id = '".Database::escape_string($id)."' and question_id= '".Database::escape_string($questionId)."'"; - $resfill = api_sql_query($queryfill, __FILE__, __LINE__); - $str=mysql_result($resfill,0,"answer"); - preg_match_all ('#\[([^[/]*)/#', $str, $arr); - $choice = $arr[1]; - $choice[$j]=trim($choice[$j]); - // if the word entered by the student IS the same as the one defined by the professor - if(strtolower(substr($temp,0,$pos)) == stripslashes(strtolower($choice[$j]))) + + $queryfill = "select answer from ".$TBL_TRACK_ATTEMPT." where exe_id = '".Database::escape_string($id)."' and question_id= '".Database::escape_string($questionId)."'"; + $resfill = api_sql_query($queryfill, __FILE__, __LINE__); + $str=mysql_result($resfill,0,"answer"); + preg_match_all ('#\[([^[/]*)/#', $str, $arr); + + $choice = $arr[1]; + $choice[$j]=trim($choice[$j]); + // if the word entered by the student IS the same as the one defined by the professor + if(strtolower(substr($temp,0,$pos)) == stripslashes(strtolower($choice[$j]))) { - // gives the related weighting to the student - $questionScore+=$answerWeighting[$j]; - // increments total score - $totalScore+=$answerWeighting[$j]; + // gives the related weighting to the student + $questionScore+=$answerWeighting[$j]; + // increments total score + $totalScore+=$answerWeighting[$j]; } - // else if the word entered by the student IS NOT the same as the one defined by the professor - $j++; - $temp=substr($temp,$pos+1); - $i=$i+1; - } $answer = $str; + // else if the word entered by the student IS NOT the same as the one defined by the professor + $j++; + $temp=substr($temp,$pos+1); + $i=$i+1; + } + $answer = $str; + } + else + { //multiple fill in blank + while(1) + { + // quits the loop if there are no more blanks + if(($pos = strpos($temp,'[')) === false) + { + // adds the end of the text + $answer.=$temp; + // TeX parsing + $texstring = api_parse_tex($texstring); + //$answer=str_replace("{texcode}",$texstring,$answer); + break; + } + // adds the piece of text that is before the blank and ended by [ + $real_text[]=substr($temp,0,$pos+1); + $answer.=substr($temp,0,$pos+1); + $temp=substr($temp,$pos+1); + + // quits the loop if there are no more blanks + if(($pos = strpos($temp,']')) === false) + { + // adds the end of the text + //$answer.=$temp; + break; + } + + $queryfill = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT." WHERE exe_id = '".Database::escape_string($id)."' and question_id= '".Database::escape_string($questionId)."'"; + $resfill = api_sql_query($queryfill, __FILE__, __LINE__); + $str=mysql_result($resfill,0,"answer"); + preg_match_all ('#\[([^[/]*)/#', $str, $arr); + $choice = $arr[1]; + + $choice[$j]=trim($choice[$j]); + $user_tags[]=stripslashes(strtolower($choice[$j])); + $correct_tags[]=strtolower(substr($temp,0,$pos)); + + $j++; + $temp=substr($temp,$pos+1); + $i=$i+1; + } + /* + echo "
";
+					print_r($user_tags);
+					echo "
"; + print_r($correct_tags); + print_r($answerWeighting); + echo "
"; + */ + $answer=''; + for($i=0;$i - + + } + ?> type = FILL_IN_BLANKS; } @@ -55,21 +60,35 @@ class FillBlanks extends Question { * function which redifines Question::createAnswersForm * @param the formvalidator instance */ - function createAnswersForm ($form) { - - - + function createAnswersForm ($form) + { $defaults = array(); if(!empty($this->id)) { $objAnswer = new answer($this->id); - $a_answer = explode('::', $objAnswer->selectAnswer(1)); + + // the question is encoded like this + // [A] B [C] D [E] F::10,10,10@1 + // number 1 before the "@" means that is a multiple fill in blank question + // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10 + // means that is a normal fill blank question + + $is_set_multiple = explode('@', $objAnswer->selectAnswer(1)); + if ($is_set_multiple[1]) + { + $defaults['multiple_answer']=1; + } + else + { + $defaults['multiple_answer']=0; + } + $a_answer = explode('::', $is_set_multiple[0]); $defaults['answer'] = $a_answer[0]; $a_weightings = explode(',',$a_answer[1]); } else - { + { $defaults['answer'] = get_lang('DefaultTextInBlanks'); } @@ -103,7 +122,6 @@ class FillBlanks extends Question { foreach($a_weightings as $i=>$weighting) { echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";'; - } } echo '} @@ -118,10 +136,13 @@ class FillBlanks extends Question { $form -> addElement ('textarea', 'answer',get_lang('Answer'),'id="answer" cols="65" rows="6" onkeyup="updateBlanks(this)"'); $form -> addRule ('answer',get_lang('GiveText'),'required'); $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/'); - - + + //added multiple answers + $form -> addElement ('checkbox','multiple_answer','', get_lang('FillInBlankMultiple')); + $form -> addElement('html','
'); + $form -> setDefaults($defaults); } @@ -140,9 +161,11 @@ class FillBlanks extends Question { // get the blanks weightings $nb = preg_match_all('/\[[^\]]*\]/', $answer, $blanks); - if(isset($_GET['editQuestion'])){ + if(isset($_GET['editQuestion'])) + { $this -> weighting = 0; } + if($nb>0) { $answer .= '::'; @@ -153,6 +176,9 @@ class FillBlanks extends Question { } $answer = substr($answer,0,-1); } + $is_multiple = $form -> getSubmitValue('multiple_answer'); + $answer.='@'.$is_multiple; + $this -> save(); $objAnswer = new answer($this->id); $objAnswer->createAnswer($answer,0,'',0,'');