diff --git a/main/exercice/exercice_submit.php b/main/exercice/exercice_submit.php index f074d6a13e..2015f09694 100755 --- a/main/exercice/exercice_submit.php +++ b/main/exercice/exercice_submit.php @@ -25,6 +25,8 @@ require_once 'question.class.php'; require_once 'answer.class.php'; require_once 'exercise.lib.php'; + + // debug var. Set to 0 to hide all debug display. Set to 1 to display debug messages. $debug = 1; @@ -32,6 +34,7 @@ $debug = 1; $language_file = 'exercice'; require_once '../inc/global.inc.php'; + $this_section = SECTION_COURSES; // Notice for unauthorized people. @@ -323,7 +326,7 @@ if ($_configuration['live_exercise_tracking'] && $objExercise->type == ONE_PER_P // if the user has submitted the form -if ($formSent) { +if ($formSent && isset($_POST)) { if ($debug > 0) { error_log('$formSent was set'); } // Initializing @@ -371,7 +374,7 @@ if ($formSent) { $choice = $exerciseResult[$questionId]; if (isset($exe_id)) { //Manage the question and answer attempts - $objExercise->manage_answer($exe_id, $questionId, $choice,'exercise_show'); + $objExercise->manage_answer($exe_id, $questionId, $choice,'exercise_show', true, false,false); } //END of saving and qualifying } diff --git a/main/exercice/exercise.class.php b/main/exercice/exercise.class.php index 1ac46e1990..3029d138d1 100755 --- a/main/exercice/exercise.class.php +++ b/main/exercice/exercise.class.php @@ -16,6 +16,8 @@ define('EXERCISE_FEEDBACK_TYPE_END',0); define('EXERCISE_FEEDBACK_TYPE_DIRECT',1); define('EXERCISE_FEEDBACK_TYPE_EXAM',2); +require_once '../inc/lib/exercise_show_functions.lib.php'; + if(!class_exists('Exercise')): class Exercise { @@ -1638,9 +1640,13 @@ class Exercise { * @param int the choice the user selected * @param string function is called from 'exercise_show' or 'exercise_result' */ - function manage_answer($exeId, $questionId, $choice, $from = 'exercise_show') { + function manage_answer($exeId, $questionId, $choice, $from = 'exercise_show', $saved_results = true, $from_database = false, $show_result = true) { global $_configuration; - $exeId = intval($exeId); + + $exeId = intval($exeId); + $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); + $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER); + // Creates a temporary Question object $objQuestionTmp = Question :: read($questionId); @@ -1649,7 +1655,8 @@ class Exercise { $questionDescription = $objQuestionTmp->selectDescription(); $questionWeighting = $objQuestionTmp->selectWeighting(); $answerType = $objQuestionTmp->selectType(); - $quesId = $objQuestionTmp->selectId(); //added by priya saini + $quesId = $objQuestionTmp->selectId(); + $totalWeighting = 0; $totalScore = 0; @@ -1668,7 +1675,7 @@ class Exercise { $nbrAnswers = 1; } $user_answer = ''; - $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER); + // Get answer list for matching $sql_answer = 'SELECT id, answer FROM '.$table_ans.' WHERE question_id="'.Database::escape_string($questionId).'" '; @@ -1679,48 +1686,108 @@ class Exercise { } $real_answers = array(); + + for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) { - $answer = $objAnswerTmp->selectAnswer($answerId); - $answerComment = $objAnswerTmp->selectComment($answerId); - $answerCorrect = $objAnswerTmp->isCorrect($answerId); - $answerWeighting = $objAnswerTmp->selectWeighting($answerId); - $numAnswer=$objAnswerTmp->selectAutoId($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 : - $studentChoice=($choice == $numAnswer)?1:0; - if ($studentChoice) { - $questionScore+=$answerWeighting; - $totalScore+=$answerWeighting; - } + if ($from_database) { + $queryans = "select answer from ".$TBL_TRACK_ATTEMPT." where exe_id = '".$exeId."' and question_id= '".$questionId."'"; + $resultans = Database::query($queryans); + $choice = Database::result($resultans,0,"answer"); + + $numAnswer=$objAnswerTmp->selectAutoId($answerId); + + $studentChoice=($choice == $numAnswer)?1:0; + if ($studentChoice) { + $questionScore+=$answerWeighting; + $totalScore+=$answerWeighting; + } + } else { + $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) { + case MULTIPLE_ANSWER : + if ($from_database) { + $choice=array(); + $queryans = "SELECT * FROM ".$TBL_TRACK_ATTEMPT." where exe_id = '".$exeId."' and question_id= '".$questionId."'"; + $resultans = Database::query($queryans); + while ($row = Database::fetch_array($resultans)) { + $ind = $row['answer']; + $choice[$ind] = 1; + } + + $numAnswer=$objAnswerTmp->selectAutoId($answerId); + $studentChoice=$choice[$numAnswer]; if ($studentChoice) { - $real_answers[$answerId] = true; - } else { - $real_answers[$answerId] = false; + $questionScore+=$answerWeighting; + $totalScore+=$answerWeighting; } - } else { + } else { + $studentChoice=$choice[$numAnswer]; if ($studentChoice) { - $real_answers[$answerId] = false; + $questionScore+=$answerWeighting; + $totalScore+=$answerWeighting; + } + } + break; + case MULTIPLE_ANSWER_COMBINATION: + if ($from_database) { + + $queryans = "SELECT * from ".$TBL_TRACK_ATTEMPT." where exe_id = '".$exeId."' and question_id= '".$questionId."'"; + $resultans = Database::query($queryans); + while ($row = Database::fetch_array($resultans)) { + $ind = $row['answer']; + $choice[$ind] = 1; + } + $numAnswer=$objAnswerTmp->selectAutoId($answerId); + $studentChoice=$choice[$numAnswer]; + + if ($answerCorrect == 1) { + if ($studentChoice) { + $real_answers[$answerId] = true; + } else { + $real_answers[$answerId] = false; + } } else { - $real_answers[$answerId] = true; + if ($studentChoice) { + $real_answers[$answerId] = false; + } else { + $real_answers[$answerId] = true; + } + } + } else { + $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; + } } - } - $final_answer = true; - foreach($real_answers as $my_answer) { - if (!$my_answer) { - $final_answer = false; + $final_answer = true; + foreach($real_answers as $my_answer) { + if (!$my_answer) { + $final_answer = false; + } } } break; @@ -1731,24 +1798,19 @@ class Exercise { // 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]); @@ -1770,7 +1832,6 @@ class Exercise { */ $answer = ''; $j = 0; - //initialise answer tags $user_tags = array (); $correct_tags = array (); @@ -1800,8 +1861,26 @@ class Exercise { // adds the end of the text $answer .= $temp; break; - } - $choice[$j] = trim($choice[$j]); + } + if ($from_database) { + $queryfill = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT." WHERE exe_id = '".Database::escape_string($exeId)."' AND question_id= '".Database::escape_string($questionId)."'"; + $resfill = Database::query($queryfill); + $str = Database::result($resfill,0,'answer'); + + preg_match_all('#\[([^[]*)\]#', $str, $arr); + $str = str_replace('\r\n', '', $str); + $choice = $arr[1]; + + $tmp=strrpos($choice[$j],' / '); + $choice[$j]=api_substr($choice[$j],0,$tmp); + $choice[$j]=trim($choice[$j]); + + //Needed to let characters ' and " to work as part of an answer + $choice[$j] = stripslashes($choice[$j]); + } else { + $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)); @@ -1809,7 +1888,6 @@ class Exercise { $temp = api_substr($temp, $pos +1); //$answer .= ']'; } - $answer = ''; $real_correct_tags = $correct_tags; $chosen_list = array (); @@ -1818,7 +1896,6 @@ class Exercise { if ($i == 0) { $answer .= $real_text[0]; } - if (!$switchable_answer_set) { //needed to parse ' and " characters $user_tags[$i] = stripslashes($user_tags[$i]); @@ -1866,43 +1943,129 @@ class Exercise { $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 : - $numAnswer=$objAnswerTmp->selectAutoId($answerId); - if ($answerCorrect) { - if ($answerCorrect == $choice[$numAnswer]) { - $questionScore+=$answerWeighting; - $totalScore+=$answerWeighting; - $user_answer = ''.$answer_matching[$choice[$numAnswer]].''; + if ($from_database) { + $query = "SELECT answer, marks FROM ".$TBL_TRACK_ATTEMPT." WHERE exe_id = '".Database::escape_string($exeId)."' AND question_id= '".Database::escape_string($questionId)."'"; + $resq = Database::query($query); + $choice = Database::result($resq,0,'answer'); + $choice = str_replace('\r\n', '', $choice); + $choice = stripslashes($choice); + + $questionScore = Database::result($resq,0,"marks"); + if ($questionScore==-1) { + $totalScore+=0; } else { - $user_answer = ''.$answer_matching[$choice[$numAnswer]].''; - } - $matching[$numAnswer] = $choice[$numAnswer]; + $totalScore+=$questionScore; + } + $arrques[] = $questionName; + $arrans[] = $choice; + } else { + $studentChoice = $choice; + if ($studentChoice) { + //@todo verify this--> set to -1 because the question has'nt been corrected + $questionScore = -1; + $totalScore += 0; + } } break; + // for matching + case MATCHING : + if ($from_database) { + $sql_answer = 'SELECT id, answer FROM '.$table_ans.' WHERE question_id="'.Database::escape_string($questionId).'" AND correct=0'; + $res_answer = Database::query($sql_answer); + // getting the real answer + $real_list =array(); + while ($real_answer = Database::fetch_array($res_answer)) { + $real_list[$real_answer['id']]= $real_answer['answer']; + } + + $sql_select_answer = 'SELECT id, answer, correct, id_auto FROM '.$table_ans.' + WHERE question_id="'.Database::escape_string($questionId).'" AND correct <> 0 ORDER BY id_auto'; + + $res_answers = Database::query($sql_select_answer); + if ($show_result) { + echo '
| '.get_lang('ElementList').' | +'.get_lang('CorrespondsTo').' | +
| '.$s_answer_label.' | '.$user_answer.' / '.$real_list[$i_answer_correct_answer].' | '; + echo '
| '.text_filter($answer_matching[$answerId]).' | '.text_filter($user_answer).' / '.text_filter($answer_matching[$answerCorrect]).' | '; + echo '
| '; + ExerciseShowFunctions::display_fill_in_blanks_answer($answer,$exeId,$questionId); + echo ' | |
| '.ExerciseShowFunctions::display_free_answer($choice, $exeId, $questionId).' | +|
'.get_lang('OpenQuestionsAttempted').' : +
+'.get_lang('AttemptDetails').' :
+
'.get_lang('CourseName').' |
+ #course# |
+
| '.get_lang('TestAttempted').' | +#exercise# | +
| '.get_lang('StudentName').' | +#firstName# #lastName# | +
| '.get_lang('StudentEmail').' | +#mail# | +
'.get_lang('OpenQuestionsAttemptedAre').' :
| '.get_lang('Question').' | +#questionName# | + +
| '.get_lang('Answer').' | +#answer# | +
'.get_lang('ExerciseAttempted').'
+
'.get_lang('CourseName').' |
+ #course# |
+ |
| '.get_lang('TestAttempted').' | +#exercise# | +|
| '.get_lang('StudentName').' | + '.(api_is_western_name_order() ? '#firstName# #lastName# | ' : '#lastName# #firstName# | ').' +
| '.get_lang('StudentEmail').' | +#mail# | +