|
|
|
|
|
|
|
selectNbrAnswers();
$questionScore=0;
if ($answerType == FREE_ANSWER) {
$nbrAnswers = 1;
}
// get answer list for matching
$sql_answer = 'SELECT id, answer FROM '.$table_ans.' WHERE question_id="'.Database::escape_string($questionId).'" ';
$res_answer = Database::query($sql_answer);
$answer_matching =array();
while ($real_answer = Database::fetch_array($res_answer)) {
$answer_matching[$real_answer['id']]= $real_answer['answer'];
}
$real_answers = array();
// We're inside *one* question. Go through each possible answer for this question
for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
//select answer of *position*=$answerId
$answer=$objAnswerTmp->selectAnswer($answerId);
$answerComment=$objAnswerTmp->selectComment($answerId);
$answerCorrect=$objAnswerTmp->isCorrect($answerId);
$answerWeighting=$objAnswerTmp->selectWeighting($answerId);
$numAnswer=$objAnswerTmp->selectAutoId($answerId);
switch ($answerType) {
// for unique answer
case UNIQUE_ANSWER :
// if the student choice is equal to the answer ID
// then give him the corresponding score
// (maybe a negative score, positive score or 0)
// Positive score should only be given when we are going over the right answer
$studentChoice=($choice == $numAnswer)?1:0;
if($studentChoice) {
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
}
break;
// for multiple answers
case MULTIPLE_ANSWER :
$studentChoice=$choice[$numAnswer];
if($studentChoice) {
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
}
break;
case MULTIPLE_ANSWER_COMBINATION :
$studentChoice=$choice[$numAnswer];
if ($answerCorrect == 1) {
if ($studentChoice) {
$real_answers[$answerId] = true;
} else {
$real_answers[$answerId] = false;
}
} else {
if ($studentChoice) {
$real_answers[$answerId] = false;
} else {
$real_answers[$answerId] = true;
}
}
break;
// for fill in the blanks
case FILL_IN_BLANKS :
// the question is encoded like this
// [A] B [C] D [E] F::10,10,10@1
// number 1 before the "@" means that is a switchable 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
// first we explode the "::"
$pre_array = explode('::', $answer);
// is switchable fill blank or not
$last = count($pre_array)-1;
$is_set_switchable = explode('@', $pre_array[$last]);
$switchable_answer_set=false;
if (isset($is_set_switchable[1]) && $is_set_switchable[1]==1)
{
$switchable_answer_set=true;
}
$answer = '';
for ($k=0; $k<$last; $k++)
{
$answer .= $pre_array[$k];
}
// splits weightings that are joined with a comma
$answerWeighting = explode(',',$is_set_switchable[0]);
// we save the answer because it will be modified
$temp=$answer;
// TeX parsing
// 1. find everything between the [tex] and [/tex] tags
$startlocations=api_strpos($temp,'[tex]');
$endlocations=api_strpos($temp,'[/tex]');
if($startlocations !== false && $endlocations !== false)
{
$texstring=api_substr($temp,$startlocations,$endlocations-$startlocations+6);
// 2. replace this by {texcode}
$temp=str_replace($texstring,'{texcode}',$temp);
}
$answer='';
$j=0;
//initialise answer tags
$user_tags=array();
$correct_tags=array();
$real_text=array();
// the loop will stop at the end of the text
while(1)
{
// quits the loop if there are no more blanks (detect '[')
if(($pos = api_strpos($temp,'[')) === false)
{
// adds the end of the textsolution
$answer=$temp;
// TeX parsing - replacement of texcode tags
$texstring = api_parse_tex($texstring);
$answer=str_replace("{texcode}",$texstring,$answer);
$real_text[] = $answer;
break; //no more "blanks", quit the loop
}
// adds the piece of text that is before the blank
//and ends with '[' into a general storage array
$real_text[]=api_substr($temp,0,$pos+1);
$answer.=api_substr($temp,0,$pos+1);
//take the string remaining (after the last "[" we found)
$temp=api_substr($temp,$pos+1);
// quit the loop if there are no more blanks, and update $pos to the position of next ']'
if(($pos = api_strpos($temp,']')) === false)
{
// adds the end of the text
$answer.=$temp;
break;
}
$choice[$j]=trim($choice[$j]);
$user_tags[]=api_strtolower($choice[$j]);
//put the contents of the [] answer tag into correct_tags[]
$correct_tags[]=api_strtolower(api_substr($temp,0,$pos));
$j++;
$temp=api_substr($temp,$pos+1);
//$answer .= ']';
}
$answer='';
$real_correct_tags = $correct_tags;
$chosen_list=array();
for($i=0;$i'.$user_tags[$i].'';
}
else
{
// adds a tabulation if no word has been typed by the student
$answer.=' ';
}
} else {
// switchable fill in the blanks
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];
// increments total score
$totalScore+=$answerWeighting[$i];
// adds the word in green at the end of the string
$answer.=$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.=''.$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].']';
if ( isset( $real_text[$i+1] ) ) {
$answer.=$real_text[$i+1];
}
}
break;
// for free answer
case FREE_ANSWER :
$studentChoice=$choice;
if($studentChoice)
{
//Score is at -1 because the question has'nt been corected
$questionScore=-1;
$totalScore+=0;
}
break;
// for matching
case MATCHING :
if ($answerCorrect) {
if ($answerCorrect == $choice[$numAnswer]) {
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
$user_answer = ''.$answer_matching[$choice[$numAnswer]].'';
} else {
$user_answer = ''.$answer_matching[$choice[$numAnswer]].'';
}
$matching[$numAnswer] = $choice[$numAnswer];
}
break;
// for hotspot with no order
case HOT_SPOT :
$studentChoice=$choice[$answerId];
if ($studentChoice) { //the answer was right
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
}
break;
// for hotspot with fixed order
case HOT_SPOT_ORDER :
$studentChoice=$choice['order'][$answerId];
if($studentChoice == $answerId)
{
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
$studentChoice = true;
}
else
{
$studentChoice = false;
}
break;
} // end switch Answertype
//display answers (if not matching type, or if the answer is correct)
if ($answerType != MATCHING || $answerCorrect) {
if ($answerType == UNIQUE_ANSWER || $answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_COMBINATION) {
if ($origin!='learnpath') {
display_unique_or_multiple_answer($answerType, $studentChoice, $answer, $answerComment, $answerCorrect);
}
} elseif($answerType == FILL_IN_BLANKS) {
if ($origin!='learnpath') {
display_fill_in_blanks_answer($answer);
}
} elseif($answerType == FREE_ANSWER) {
// to store the details of open questions in an array to be used in mail
$arrques[] = $questionName;
$arrans[] = $choice;
if($origin != 'learnpath') {
display_free_answer($choice);
}
}
elseif($answerType == HOT_SPOT)
{
if ($origin != 'learnpath') {
display_hotspot_answer($answerId, $answer, $studentChoice, $answerComment);
}
}
elseif($answerType == HOT_SPOT_ORDER)
{
display_hotspot_order_answer($answerId, $answer, $studentChoice, $answerComment);
}
elseif($answerType==MATCHING)
{
if ($origin != 'learnpath') {
echo '';
echo '| '.api_parse_tex($answer_matching[$answerId]).' | '.api_parse_tex($user_answer).' / '.api_parse_tex($answer_matching[$answerCorrect]).' | ';
echo ' ';
}
}
}
} // end for that loops over all answers of the current question
//
if ($answerType == MULTIPLE_ANSWER_COMBINATION) {
$final_answer = true;
foreach($real_answers as $my_answer) {
if (!$my_answer) {
$final_answer = false;
}
}
if ($final_answer) {
//getting only the first score where we save the weight of all the question
$answerWeighting=$objAnswerTmp->selectWeighting(1);
$questionScore+=$answerWeighting;
$totalScore+=$answerWeighting;
}
}
// if answer is hotspot. To the difference of exercise_show.php, we use the results from the session (from_db=0)
// TODO Change this, because it is wrong to show the user some results that haven't been stored in the database yet
if ($answerType == HOT_SPOT || $answerType == HOT_SPOT_ORDER) {
// We made an extra table for the answers
if($origin != 'learnpath') {
echo " |
"; //echo Security::remove_XSS($questionId);
?>