Add fill in blanks code with some minor corrections (function name fixed)

1.10.x
Julio Montoya 10 years ago
parent e9132e16fb
commit 65778a19be
  1. 74
      main/exercice/exercise.class.php
  2. 938
      main/exercice/fill_blanks.class.php
  3. 3
      main/exercice/stats.php
  4. 226
      main/inc/lib/exercise.lib.php
  5. 34
      main/inc/lib/exercise_show_functions.lib.php

@ -2497,13 +2497,83 @@ class Exercise
break;
// for fill in the blanks
case FILL_IN_BLANKS:
// insert the student result in the track_e_attempt table, field answer
// $answer is the answer like in the c_quiz_answer table for the question
// student datas are choice[]
$listCorrectAnswers = FillBlanks::getAnswerInfo($answer);
$switchableAnswerSet = $listCorrectAnswers["switchable"];
$answerWeighting = $listCorrectAnswers["tabweighting"];
// user choices is an array $choice
// get existing user data in n the BDD
if ($from_database) {
$queryfill = "SELECT answer
FROM $TBL_TRACK_ATTEMPT
WHERE exe_id = $exeId
AND question_id= ".intval($questionId);
$resfill = Database::query($queryfill);
$str = Database::result($resfill, 0, 'answer');
$listStudentResults = FillBlanks::getAnswerInfo($str, true);
$choice = $listStudentResults['studentanswer'];
}
// loop other all blanks words
if (!$switchableAnswerSet) {
// not switchable answer, must be in the same place than teacher order
for ($i=0; $i < count($listCorrectAnswers['tabwords']); $i++) {
$studentAnswer = trim($choice[$i]);
$correctAnswer = $listCorrectAnswers['tabwords'][$i];
$isAnswerCorrect = 0;
if (FillBlanks::isGoodStudentAnswer($studentAnswer, $correctAnswer)) {
// gives the related weighting to the student
$questionScore += $answerWeighting[$i];
// increments total score
$totalScore += $answerWeighting[$i];
$isAnswerCorrect = 1;
}
$listCorrectAnswers['studentanswer'][$i] = $studentAnswer;
$listCorrectAnswers['studentscore'][$i] = $isAnswerCorrect;
}
} else {
// switchable answer
$listStudentAsnwerTemp = $choice;
$listTeacherAnswerTemp = $listCorrectAnswers['tabwords'];
$listBadAnswerIndice = array();
// for every teacher answer, check if there is a student answer
for ($i=0; $i < count($listStudentAsnwerTemp); $i++) {
$studentAnswer = trim($listStudentAsnwerTemp[$i]);
$found = false;
for ($j=0; $j < count($listTeacherAnswerTemp); $j++) {
$correctAnswer = $listTeacherAnswerTemp[$j];
if (!$found) {
if (FillBlanks::isGoodStudentAnswer($studentAnswer, $correctAnswer)) {
$questionScore += $answerWeighting[$i];
$totalScore += $answerWeighting[$i];
$listTeacherAnswerTemp[$j] = "";
$found = true;
}
}
}
$listCorrectAnswers['studentanswer'][$i] = $studentAnswer;
if (!$found) {
$listCorrectAnswers['studentscore'][$i] = 0;
} else {
$listCorrectAnswers['studentscore'][$i] = 1;
}
}
}
$answer = FillBlanks::getAnswerInStudentAttempt($listCorrectAnswers);
// 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;
@ -2644,7 +2714,7 @@ class Exercise
if (isset($real_text[$i +1])) {
$answer .= $real_text[$i + 1];
}
}
}*/
break;
// for calculated answer
case CALCULATED_ANSWER:

File diff suppressed because it is too large Load Diff

@ -52,7 +52,8 @@ if (!empty($question_list)) {
$question_id,
$exercise_id,
$courseCode,
$sessionId
$sessionId,
$question_obj->type
);
$data[$question_id]['name'] = cut($question_obj->question, 100);

@ -548,170 +548,73 @@ HTML;
$s .= '</tr>';
} elseif ($answerType == FILL_IN_BLANKS) {
/*
* In the FILL_IN_BLANKS test
* you mustn't have [ and ] in the textarea
* you mustn't have :: in the textarea
* the text to find mustn't be empty or contains only spaces
* the text to find mustn't contains HTML tags
* the text to find mustn't contains char "
*/
// display the question, with field empty, for student to fill it,
// or filled to display the answer in the Question preview of the exercice/admin.php page
$displayForStudent = true;
$listAnswerInformations = FillBlanks::getAnswerInfo($answer);
$separatorStartRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorstart']);
$separatorEndRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorend']);
list($answer) = explode('::', $answer);
// $correct_answer_list array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
api_preg_match_all(
'/\[[^]]+\]/',
$answer,
$correct_answer_list
);
//Correct answers
$correctAnswerList = $listAnswerInformations['tabwords'];
// get student answer to display it if student go back to previous fillBlank answer question in a test
//Student's answer
$studentAnswerList = array();
if (isset($user_choice[0]['answer'])) {
api_preg_match_all(
'/\[[^]]+\]/',
$user_choice[0]['answer'],
$student_answer_list
);
$student_answer_list_tobecleaned = $student_answer_list[0];
$student_answer_list = array();
// here we got the student answer in a test
// let's clean up the results
/*
Array
(
[0] => Array
(
[0] => [<font color="red"><s>yer</s></font> / <font color="green"><b>ici</b></font>]
[1] => [<font color="red"><s>plop</s></font> / <font color="green"><b>/p</b></font>]
)
)
*/
for ($i = 0; $i < count(
$student_answer_list_tobecleaned
); $i++) {
$answer_corrected = $student_answer_list_tobecleaned[$i];
/*
* we got if student answer is wrong
* [<font color="red"><s>rrr</s></font> / <font color="green"><b>/p</b></font>]
* or if student answer is good
* [plop / <font color="green"><b>plop</b></font>]
* or if student didn't answer []
*/
$answer_corrected = api_preg_replace(
'| / <font color="green"><b>.*$|',
'',
$answer_corrected
);
/*
* we got [<font color="red"><s>rrr</s></font> or [plop or [
*/
$answer_corrected = api_preg_replace(
'/^\[/',
'',
$answer_corrected
);
/*
* we got <font color="red"><s>rrr</s></font> or plop
* non breakable spaces &nbsp;&nbsp;&nbsp; from /main/exercice/exercise.class.php have been removed l 2391 and l 2370
*/
$answer_corrected = api_preg_replace(
'|^<font color="red"><s>|',
'',
$answer_corrected
);
$answer_corrected = api_preg_replace(
'|</s></font>$|',
'',
$answer_corrected
);
$answer_corrected = '[' . $answer_corrected . ']';
/*
* we got [rrr] or [plop] or []
*/
$student_answer_list[] = $answer_corrected;
}
$arrayStudentAnswer = FillBlanks::getAnswerInfo($user_choice[0]['answer'], true);
$studentAnswerList = $arrayStudentAnswer['studentanswer'];
}
// If display preview of answer in test view for exemple, set the student answer to the correct answers
// If the question must be shown with the answer (in page exercice/admin.php) for teacher preview
// set the student-answer to the correct answer
if ($debug_mark_answer) {
// contain the rights answers surronded with brackets
$student_answer_list = $correct_answer_list[0];
$studentAnswerList = $correctAnswerList;
$displayForStudent = false;
}
/*
Split the response by bracket
tab_comments is an array with text surrounding the text to find
we add a space before and after the answer_question to be sure to
have a block of text before and after [xxx] patterns
so we have n text to find ([xxx]) and n+1 block of texts before,
between and after the text to find
*/
$tab_comments = api_preg_split(
'/\[[^]]+\]/',
' ' . $answer . ' '
);
if (!empty($correct_answer_list) && !empty($student_answer_list)) {
if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
$answer = "";
$i = 0;
foreach ($student_answer_list as $student_item) {
// remove surronding brackets
$student_response = api_substr(
$student_item,
1,
api_strlen($student_item) - 2
);
$size = strlen($student_item);
$attributes['class'] = self::detectInputAppropriateClass(
$size
);
$answer .= $tab_comments[$i] .
Display::input(
'text',
"choice[$questionId][]",
$student_response,
$attributes
);
$i++;
for ($i = 0; $i < count($listAnswerInformations["commonwords"]) - 1; $i++) {
// display the common word
$answer .= $listAnswerInformations["commonwords"][$i];
// display the blank word
$correctItem = $listAnswerInformations["tabwords"][$i];
$correctItemRegexp = $correctItem;
// replace / with \/ to allow the preg_replace bellow and all the regexp char
$correctItemRegexp = FillBlanks::getRegexpProtected($correctItemRegexp);
if (isset($studentAnswerList[$i])) {
// If student already started this test and answered this question,
// fill the blank with his previous answers
// may be "" if student viewed the question, but did not fill the blanks
$correctItem = $studentAnswerList[$i];
}
$attributes["style"] = "width:".$listAnswerInformations["tabinputsize"][$i]."px";
$answer .= FillBlanks::getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, $correctItem, $attributes, $answer, $listAnswerInformations, $displayForStudent, $i);
}
$answer .= $tab_comments[$i];
// display the last common word
$answer .= $listAnswerInformations["commonwords"][$i];
} else {
// display exercise with empty input fields
// every [xxx] are replaced with an empty input field
foreach ($correct_answer_list[0] as $item) {
$size = strlen($item);
$attributes['class'] = self::detectInputAppropriateClass(
$size
);
$attributes['class'] .= ' block_on_enter';
$answer = str_replace(
$item,
Display::input(
'text',
"choice[$questionId][]",
'',
$attributes
),
$answer
);
// display empty [input] with the right width for student to fill it
$separatorStartRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorstart']);
$separatorEndRegexp = FillBlanks::escapeForRegexp($listAnswerInformations['blankseparatorend']);
$answer = "";
for ($i = 0; $i < count($listAnswerInformations["commonwords"]) - 1; $i++) {
// display the common words
$answer .= $listAnswerInformations["commonwords"][$i];
// display the blank word
$attributes["style"] = "width:".$listAnswerInformations["tabinputsize"][$i]."px";
$correctItem = $listAnswerInformations["tabwords"][$i];
$correctItemRegexp = $correctItem;
// replace / with \/ to allow the preg_replace bellow and all the regexp char
$correctItemRegexp = FillBlanks::getRegexpProtected($correctItemRegexp);
$answer .= FillBlanks::getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, '', $attributes, $answer, $listAnswerInformations, $displayForStudent, $i);
}
/*$answer = api_preg_replace(
'/\[[^]]+\]/',
Display::input(
'text',
"choice[$questionId][]",
'',
$attributes
), $answer);*/
// display the last common word
$answer .= $listAnswerInformations["commonwords"][$i];
}
$html = '<div class="fill_blanks col-md-12">'.$answer.'</div>';
$s .= $html ;
$s .= $answer;
} elseif ($answerType == CALCULATED_ANSWER) {
/*
@ -3047,13 +2950,15 @@ HTML;
* @param int $exercise_id
* @param string $course_code
* @param int $session_id
* @param string $questionType
* @return int
*/
public static function get_number_students_question_with_answer_count(
$question_id,
$exercise_id,
$course_code,
$session_id
$session_id,
$questionType = ''
) {
$track_exercises = Database::get_main_table(
TABLE_STATISTIC_TRACK_E_EXERCISES
@ -3072,6 +2977,27 @@ HTML;
$courseId = api_get_course_int_id($course_code);
$session_id = intval($session_id);
if ($questionType == FILL_IN_BLANKS) {
$listStudentsId = array();
$listAllStudentInfo = CourseManager::get_student_list_from_course_code(
api_get_course_id(),
true
);
foreach ($listAllStudentInfo as $i => $listStudentInfo) {
$listStudentsId[] = $listStudentInfo['user_id'];
}
$listFillTheBlankResult = getFillTheBlankTabResult(
$exercise_id,
$question_id,
$listStudentsId,
'1970-01-01',
'3000-01-01'
);
return getNbResultFillBlankAll($listFillTheBlankResult);
}
if (empty($session_id)) {
$courseCondition = "
INNER JOIN $courseUser cu

@ -22,31 +22,33 @@ class ExerciseShowFunctions
* @param string Answer text
* @param int Exercise ID
* @param int Question ID
* @param int $resultsDisabled
* @return void
*/
static function display_fill_in_blanks_answer($feedback_type, $answer, $id, $questionId)
static function display_fill_in_blanks_answer($feedbackType, $answer, $id, $questionId, $resultsDisabled)
{
$answerHTML = FillBlanks::getHtmlDisplayForAnswer($answer, $resultsDisabled);
if (empty($id)) {
echo '<tr><td>'. Security::remove_XSS($answer).'</td></tr>';
echo '<tr><td>';
echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
echo '</td></tr>';
} else {
?>
<tr>
?>
<tr>
<td>
<?php
echo Security::remove_XSS($answer);
?>
<?php echo nl2br(Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY)); ?>
</td>
<?php
if (!api_is_allowed_to_edit(null,true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
<td>
<?php
$comm = Event::get_comments($id,$questionId);
?>
</td>
<?php } ?>
<?php
if (!api_is_allowed_to_edit(null,true) && $feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
<td>
<?php
$comm = Event::get_comments($id, $questionId);
?>
</td>
<?php } ?>
</tr>
<?php
<?php
}
}

Loading…
Cancel
Save