diff --git a/main/exercice/answer.class.php b/main/exercice/answer.class.php index c991884788..b517523717 100755 --- a/main/exercice/answer.class.php +++ b/main/exercice/answer.class.php @@ -578,7 +578,7 @@ class Answer ]; $autoId = Database::insert($answerTable, $params); if ($autoId) { - $sql = "UPDATE $answerTable SET id = id_auto WHERE id_auto = $autoId"; + $sql = "UPDATE $answerTable SET id = iid, id_auto = iid WHERE iid = $autoId"; Database::query($sql); } } else { diff --git a/main/exercice/exercise.class.php b/main/exercice/exercise.class.php index 3e13a8e30e..f8a0219cdd 100755 --- a/main/exercice/exercise.class.php +++ b/main/exercice/exercise.class.php @@ -3140,7 +3140,7 @@ class Exercise //} } elseif ($answerType == FILL_IN_BLANKS) { //if ($origin!='learnpath') { - ExerciseShowFunctions::display_fill_in_blanks_answer($feedback_type, $answer,0,0); + ExerciseShowFunctions::display_fill_in_blanks_answer($feedback_type, $answer,0,0, $results_disabled); // } } elseif ($answerType == CALCULATED_ANSWER) { //if ($origin!='learnpath') { @@ -3460,7 +3460,8 @@ class Exercise $feedback_type, $answer, $exeId, - $questionId + $questionId, + $results_disabled ); break; case CALCULATED_ANSWER: diff --git a/main/exercice/fill_blanks.class.php b/main/exercice/fill_blanks.class.php index 882d9428b7..5bdb269aad 100755 --- a/main/exercice/fill_blanks.class.php +++ b/main/exercice/fill_blanks.class.php @@ -23,22 +23,22 @@ class FillBlanks extends Question public function __construct() { parent::__construct(); - $this -> type = FILL_IN_BLANKS; - $this -> isContent = $this-> getIsContent(); + $this->type = FILL_IN_BLANKS; + $this->isContent = $this->getIsContent(); } /** * function which redefines Question::createAnswersForm - * @param the formvalidator instance + * @param FormValidator $form */ - function createAnswersForm($form) + public function createAnswersForm($form) { $fillBlanksAllowedSeparator = self::getAllowedSeparator(); $defaults = array(); if (!empty($this->id)) { - $objectAnswer = new answer($this->id); + $objectAnswer = new Answer($this->id); $answer = $objectAnswer->selectAnswer(1); $listAnswersInfo = FillBlanks::getAnswerInfo($answer); @@ -69,7 +69,6 @@ class FillBlanks extends Question $setValues .= 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";'; } } - // javascript echo ''; // answer - $form->addElement ('label', null, '

'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')); - $form->addElement ('html_editor', 'answer', '','id="answer" cols="122" rows="6" onkeyup="javascript: updateBlanks(this);"', array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '350')); - $form -> addRule ('answer',get_lang('GiveText'),'required'); + $form->addElement('label', null, '

'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')); + $form->addElement( + 'html_editor', + 'answer', + '', + ['id' => 'answer', 'onkeyup' => "javascript: updateBlanks(this);"], + array('ToolbarSet' => 'TestQuestionDescription') + ); + $form->addRule('answer',get_lang('GiveText'),'required'); //added multiple answers - $form->addElement ('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable')); + $form->addElement('checkbox','multiple_answer','', get_lang('FillInBlankSwitchable')); $form->addElement('select', 'select_separator', get_lang("SelectFillTheBlankSeparator"), self::getAllowedSeparatorForSelect(), ' id="select_separator" style="width:150px" onchange="changeBlankSeparator()" '); - $form->addElement ('label', null, ''); + $form->addElement('label', null, ''); $form->addElement('html','
'); - global $text, $class; + global $text; // setting the save button here and not in the question class.php $form->addElement('html','
'.get_lang('DefineBlanks').'
'); - $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"'); + $form->addButtonSave($text, 'submitQuestion'); - if (!empty($this -> id)) { - $form -> setDefaults($defaults); + if (!empty($this->id)) { + $form->setDefaults($defaults); } else { - if ($this -> isContent == 1) { - $form -> setDefaults($defaults); + if ($this->isContent == 1) { + $form->setDefaults($defaults); } } } @@ -318,15 +323,20 @@ class FillBlanks extends Question // remove forbidden chars $matchingResult = str_replace("/\\/", "", $matchingResult); $matchingResult = str_replace('/"/', "", $matchingResult); + return $blankStartSeparator.$matchingResult.$blankEndSeparator; }, $answer ); // get the blanks weightings - $nb = preg_match_all('/'.$blankStartSeparatorRegexp.'[^'.$blankStartSeparatorRegexp.']*'.$blankEndSeparatorRegexp.'/', $answer, $blanks); + $nb = preg_match_all( + '/'.$blankStartSeparatorRegexp.'[^'.$blankStartSeparatorRegexp.']*'.$blankEndSeparatorRegexp.'/', + $answer, + $blanks + ); if (isset($_GET['editQuestion'])) { - $this -> weighting = 0; + $this->weighting = 0; } /* if we have some [tobefound] in the text @@ -356,7 +366,7 @@ class FillBlanks extends Question if ($i != $nb - 1) { $answer .= ","; } - // calculate the global weightning for the question + // calculate the global weighting for the question $this -> weighting += $form->getSubmitValue('weighting['.$i.']'); } @@ -387,10 +397,10 @@ class FillBlanks extends Question // Allow answers order switches $is_multiple = $form -> getSubmitValue('multiple_answer'); - $answer.='@'.$is_multiple; + $answer.= '@'.$is_multiple; - $this -> save(); - $objAnswer = new answer($this->id); + $this->save(); + $objAnswer = new Answer($this->id); $objAnswer->createAnswer($answer, 0, '', 0, 1); $objAnswer->save(); } @@ -423,8 +433,18 @@ class FillBlanks extends Question * @param $displayForStudent * @return string */ - public static function getFillTheBlankHtml($separatorStartRegexp, $separatorEndRegexp, $correctItemRegexp, $questionId, $correctItem, $attributes, $answer, $listAnswersInfo, $displayForStudent, $inBlankNumber) - { + public static function getFillTheBlankHtml( + $separatorStartRegexp, + $separatorEndRegexp, + $correctItemRegexp, + $questionId, + $correctItem, + $attributes, + $answer, + $listAnswersInfo, + $displayForStudent, + $inBlankNumber + ) { $result = ""; $inTabTeacherSolution = $listAnswersInfo['tabwords']; $inTeacherSolution = $inTabTeacherSolution[$inBlankNumber]; @@ -554,8 +574,9 @@ class FillBlanks extends Question /** * Return information about the answer - * @param string $answer : the text of the answer of the question - * @param bool $inIsStudentAnswer : true if it is a student answer and not the empty question model + * @param string $userAnswer : the text of the answer of the question + * @param bool $isStudentAnswer : true if it is a student answer and not the empty question model + * * @return array of information about the answer */ public static function getAnswerInfo($userAnswer = "", $isStudentAnswer = false) @@ -571,12 +592,13 @@ class FillBlanks extends Question $listAnswerResults['studentanswer'] = array(); $listAnswerResults['studentscore'] = array(); $listAnswerResults['blankseparatornumber'] = 0; + $listDoubleColon = array(); api_preg_match("/(.*)::(.*)$/s", $userAnswer, $listResult); if (count($listResult) < 2) { - $listDoubleColon[] = $listResult; - $listDoubleColon[] = ""; + $listDoubleColon[] = ''; + $listDoubleColon[] = ''; } else { $listDoubleColon[] = $listResult[1]; $listDoubleColon[] = $listResult[2]; @@ -586,11 +608,12 @@ class FillBlanks extends Question //make sure we only take the last bit to find special marks $listArobaseSplit = explode('@', $listDoubleColon[1]); + if (count($listArobaseSplit) < 2) { $listArobaseSplit[1] = ""; } - //take the complete string except after the last '::' + // take the complete string except after the last '::' $listDetails = explode(":", $listArobaseSplit[0]); // < number of item after the ::[score]:[size]:[separator_id]@ , here there are 3 @@ -621,7 +644,12 @@ class FillBlanks extends Question $blankCharEndForRegexp = self::escapeForRegexp($blankCharEnd); // get all blanks words - $listAnswerResults['wordsCount'] = preg_match_all('/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/', $listDoubleColon[0], $listWords); + $listAnswerResults['wordsCount'] = preg_match_all( + '/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/', + $listDoubleColon[0], + $listWords + ); + if ($listAnswerResults['wordsCount'] > 0) { $listAnswerResults['tabwordsbracket'] = $listWords[0]; // remove [ and ] in string @@ -640,7 +668,11 @@ class FillBlanks extends Question } // get all common words - $commonWords = preg_replace('/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/', "::", $listDoubleColon[0]); + $commonWords = preg_replace( + '/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/', + "::", + $listDoubleColon[0] + ); // if student answer, the second [] is the student answer, the third is if student scored or not $listBrackets = array(); @@ -649,11 +681,13 @@ class FillBlanks extends Question for ($i=0; $i < count($listAnswerResults['tabwords']); $i++) { $listBrackets[] = $listAnswerResults['tabwordsbracket'][$i]; $listWords[] = $listAnswerResults['tabwords'][$i]; - if ($i+1 < count($listAnswerResults['tabwords'])) { // should always be + if ($i+1 < count($listAnswerResults['tabwords'])) { + // should always be $i++; } $listAnswerResults['studentanswer'][] = $listAnswerResults['tabwords'][$i]; - if ($i+1 < count($listAnswerResults['tabwords'])) { // should always be + if ($i+1 < count($listAnswerResults['tabwords'])) { + // should always be $i++; } $listAnswerResults['studentscore'][] = $listAnswerResults['tabwords'][$i]; @@ -820,12 +854,12 @@ class FillBlanks extends Question // rebuild the sentence with student answer inserted for ($i=0; $i < count($listStudentAnswerInfo['commonwords']); $i++) { - $result .= $listStudentAnswerInfo['commonwords'][$i]; - $result .= $listStudentAnswerInfo['studentanswer'][$i]; + $result .= isset($listStudentAnswerInfo['commonwords'][$i]) ? $listStudentAnswerInfo['commonwords'][$i] : ''; + $result .= isset($listStudentAnswerInfo['studentanswer'][$i]) ? $listStudentAnswerInfo['studentanswer'][$i] : ''; } // the last common word (should be

) - $result .= $listStudentAnswerInfo['commonwords'][$i]; + $result .= isset($listStudentAnswerInfo['commonwords'][$i]) ? $listStudentAnswerInfo['commonwords'][$i] : ''; return $result; } diff --git a/main/exercice/question.class.php b/main/exercice/question.class.php index 6f9a3a1b12..354f8e8efd 100755 --- a/main/exercice/question.class.php +++ b/main/exercice/question.class.php @@ -772,18 +772,23 @@ abstract class Question // question already exists if(!empty($id)) { - $sql = "UPDATE $TBL_QUESTIONS - SET - question = '" . Database::escape_string($question) . "', - description = '" . Database::escape_string($description) . "', - ponderation = '" . Database::escape_string($weighting) . "', - position = '" . Database::escape_string($position) . "', - type = '" . Database::escape_string($type) . "', - picture = '" . Database::escape_string($picture) . "', - extra = '" . Database::escape_string($extra) . "', - level = '" . Database::escape_string($level) . "' - WHERE c_id = $c_id AND id = " . intval($id); - Database::query($sql); + + $params = [ + 'question' => $question, + 'description' => $description, + 'ponderation' => $weighting, + 'position' => $position, + 'type' => $type, + 'picture' => $picture, + 'extra' => $extra, + 'level' => $level, + ]; + + Database::update( + $TBL_QUESTIONS, + $params, + ['c_id = ? AND id = ?' => [$c_id, $id]] + ); $this->saveCategory($category); if (!empty($exerciseId)) { @@ -831,6 +836,7 @@ abstract class Question 'extra' => $extra, 'level' => $level ]; + $this->id = Database::insert($TBL_QUESTIONS, $params); if ($this->id) { diff --git a/main/inc/lib/exercise_show_functions.lib.php b/main/inc/lib/exercise_show_functions.lib.php index 91a8ce52b9..1acadee238 100755 --- a/main/inc/lib/exercise_show_functions.lib.php +++ b/main/inc/lib/exercise_show_functions.lib.php @@ -25,7 +25,7 @@ class ExerciseShowFunctions * @param int $resultsDisabled * @return void */ - static function display_fill_in_blanks_answer($feedbackType, $answer, $id, $questionId, $resultsDisabled) + public static function display_fill_in_blanks_answer($feedbackType, $answer, $id, $questionId, $resultsDisabled) { $answerHTML = FillBlanks::getHtmlDisplayForAnswer($answer, $resultsDisabled); if (empty($id)) { @@ -154,7 +154,8 @@ class ExerciseShowFunctions * @param string $studentChoice * @param string $answerComment */ - static function display_hotspot_answer($feedback_type, $answerId, $answer, $studentChoice, $answerComment, $in_results_disabled) { + static function display_hotspot_answer($feedback_type, $answerId, $answer, $studentChoice, $answerComment, $in_results_disabled) + { $hide_expected_answer = false; if ($feedback_type == 0 && $in_results_disabled == 2) { $hide_expected_answer = true;