diff --git a/main/exercise/MultipleAnswerTrueFalseDegreeCertainty.php b/main/exercise/MultipleAnswerTrueFalseDegreeCertainty.php index 1f9f33029b..338296845f 100644 --- a/main/exercise/MultipleAnswerTrueFalseDegreeCertainty.php +++ b/main/exercise/MultipleAnswerTrueFalseDegreeCertainty.php @@ -129,26 +129,20 @@ class MultipleAnswerTrueFalseDegreeCertainty extends Question if (is_object($answer)) { $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : ''; - if (isset($_POST['answer']) && isset($_POST['answer'][$i])) { - $defaults['answer['.$i.']'] = Security::remove_XSS($_POST['answer'][$i]); - } - $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : ''; - if (isset($_POST['comment']) && isset($_POST['comment'][$i])) { - $defaults['comment['.$i.']'] = Security::remove_XSS($_POST['comment'][$i]); - } - $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : ''; $correct = isset($answer->correct[$i]) ? $answer->correct[$i] : ''; $defaults['correct['.$i.']'] = $correct; - if (isset($_POST['correct']) && isset($_POST['correct'][$i])) { - $defaults['correct['.$i.']'] = Security::remove_XSS($_POST['correct'][$i]); - } $j = 1; if (!empty($optionData)) { foreach ($optionData as $id => $data) { - $form->addElement('radio', 'correct['.$i.']', null, null, $id); + $rdoCorrect = $form->addElement('radio', 'correct['.$i.']', null, null, $id); + + if (isset($_POST['correct']) && isset($_POST['correct'][$i]) && $id == $_POST['correct'][$i]) { + $rdoCorrect->setValue(Security::remove_XSS($_POST['correct'][$i])); + } + $j++; if ($j == 3) { break; @@ -161,7 +155,7 @@ class MultipleAnswerTrueFalseDegreeCertainty extends Question } $boxesNames[] = 'correct['.$i.']'; - $form->addElement( + $txtAnswer = $form->addElement( 'html_editor', 'answer['.$i.']', null, @@ -170,15 +164,23 @@ class MultipleAnswerTrueFalseDegreeCertainty extends Question ); $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required'); + if (isset($_POST['answer']) && isset($_POST['answer'][$i])) { + $txtAnswer->setValue(Security::remove_XSS($_POST['answer'][$i])); + } + // show comment when feedback is enable if ($objEx->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) { - $form->addElement( + $txtComment = $form->addElement( 'html_editor', 'comment['.$i.']', null, ['style' => 'vertical-align:middle;'], ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'] ); + + if (isset($_POST['comment']) && isset($_POST['comment'][$i])) { + $txtComment->setValue(Security::remove_XSS($_POST['comment'][$i])); + } } $form->addElement('html', ''); } @@ -187,8 +189,8 @@ class MultipleAnswerTrueFalseDegreeCertainty extends Question $form->addElement('html', '
'); // 3 scores - $form->addElement('text', 'option[1]', get_lang('Correct'), ['class' => 'span1', 'value' => '1']); - $form->addElement('text', 'option[2]', get_lang('Wrong'), ['class' => 'span1', 'value' => '-0.5']); + $txtOption1 = $form->addElement('text', 'option[1]', get_lang('Correct'), ['value' => '1']); + $txtOption2 = $form->addElement('text', 'option[2]', get_lang('Wrong'), ['value' => '-0.5']); $form->addElement('hidden', 'option[3]', 0); @@ -203,9 +205,8 @@ class MultipleAnswerTrueFalseDegreeCertainty extends Question if (!empty($this->extra)) { $scores = explode(':', $this->extra); if (!empty($scores)) { - for ($i = 1; $i <= 3; $i++) { - $defaults['option['.$i.']'] = $scores[$i - 1]; - } + $txtOption1->setValue($scores[0]); + $txtOption2->setValue($scores[1]); } } @@ -220,13 +221,11 @@ class MultipleAnswerTrueFalseDegreeCertainty extends Question $renderer->setElementTemplate('{element} ', 'submitQuestion'); $renderer->setElementTemplate('{element} ', 'moreAnswers'); $form->addElement('html', ''); - $defaults['correct'] = $correct; - if (!empty($this->id)) { - $form->setDefaults($defaults); - } else { + if (!empty($this->id) && !$form->isSubmitted()) { $form->setDefaults($defaults); } + $form->setConstants(['nb_answers' => $nbAnswers]); } diff --git a/main/exercise/multiple_answer_true_false.class.php b/main/exercise/multiple_answer_true_false.class.php index 9c689f996e..786a5730c6 100755 --- a/main/exercise/multiple_answer_true_false.class.php +++ b/main/exercise/multiple_answer_true_false.class.php @@ -120,7 +120,12 @@ class MultipleAnswerTrueFalse extends Question $j = 1; if (!empty($optionData)) { foreach ($optionData as $id => $data) { - $form->addElement('radio', 'correct['.$i.']', null, null, $id); + $rdoCorrect = $form->addElement('radio', 'correct['.$i.']', null, null, $id); + + if (isset($_POST['correct']) && isset($_POST['correct'][$i]) && $id == $_POST['correct'][$i]) { + $rdoCorrect->setValue(Security::remove_XSS($_POST['correct'][$i])); + } + $j++; if ($j == 3) { break; @@ -130,10 +135,6 @@ class MultipleAnswerTrueFalse extends Question } else { $form->addElement('radio', 'correct['.$i.']', null, null, 1); $form->addElement('radio', 'correct['.$i.']', null, null, 2); - - $defaults['answer['.$i.']'] = ''; - $defaults['comment['.$i.']'] = ''; - $defaults['correct['.$i.']'] = ''; } $form->addHtmlEditor( @@ -144,9 +145,13 @@ class MultipleAnswerTrueFalse extends Question ['ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'] ); + if (isset($_POST['answer']) && isset($_POST['answer'][$i])) { + $form->getElement("answer[$i]")->setValue(Security::remove_XSS($_POST['answer'][$i])); + } + // show comment when feedback is enable if ($obj_ex->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM) { - $form->addElement( + $txtComment = $form->addElement( 'html_editor', 'comment['.$i.']', null, @@ -157,6 +162,10 @@ class MultipleAnswerTrueFalse extends Question 'Height' => '100', ] ); + + if (isset($_POST['comment']) && isset($_POST['comment'][$i])) { + $txtComment->setValue(Security::remove_XSS($_POST['comment'][$i])); + } } $form->addHtml(''); @@ -194,9 +203,9 @@ class MultipleAnswerTrueFalse extends Question $renderer->setElementTemplate($doubtScoreInputTemplate, 'option[3]'); // 3 scores - $form->addElement('text', 'option[1]', get_lang('Correct'), ['class' => 'span1', 'value' => '1']); - $form->addElement('text', 'option[2]', get_lang('Wrong'), ['class' => 'span1', 'value' => '-0.5']); - $form->addElement('text', 'option[3]', get_lang('DoubtScore'), ['class' => 'span1', 'value' => '0']); + $txtOption1 = $form->addElement('text', 'option[1]', get_lang('Correct'), ['class' => 'span1', 'value' => '1']); + $txtOption2 = $form->addElement('text', 'option[2]', get_lang('Wrong'), ['class' => 'span1', 'value' => '-0.5']); + $txtOption3 = $form->addElement('text', 'option[3]', get_lang('DoubtScore'), ['class' => 'span1', 'value' => '0']); $form->addRule('option[1]', get_lang('ThisFieldIsRequired'), 'required'); $form->addRule('option[2]', get_lang('ThisFieldIsRequired'), 'required'); @@ -209,9 +218,9 @@ class MultipleAnswerTrueFalse extends Question $scores = explode(':', $this->extra); if (!empty($scores)) { - for ($i = 1; $i <= 3; $i++) { - $defaults['option['.$i.']'] = $scores[$i - 1]; - } + $txtOption1->setValue($scores[0]); + $txtOption2->setValue($scores[1]); + $txtOption3->setValue($scores[2]); } } @@ -227,9 +236,7 @@ class MultipleAnswerTrueFalse extends Question $form->addGroup($buttonGroup); } - if (!empty($this->id)) { - $form->setDefaults($defaults); - } else { + if (!empty($this->id) && !$form->isSubmitted()) { $form->setDefaults($defaults); } $form->setConstants(['nb_answers' => $nb_answers]); diff --git a/main/exercise/question.class.php b/main/exercise/question.class.php index 27ac42845c..21b099c642 100755 --- a/main/exercise/question.class.php +++ b/main/exercise/question.class.php @@ -1859,21 +1859,40 @@ abstract class Question $extraField->addElements($form, $this->iid); // default values - $defaults = []; - $defaults['questionName'] = $this->question; - $defaults['questionDescription'] = $this->description; - $defaults['questionLevel'] = $this->level; - $defaults['questionCategory'] = $this->category; - $defaults['feedback'] = $this->feedback; - $defaults['mandatory'] = $this->mandatory; // Came from he question pool - if (isset($_GET['fromExercise'])) { - $form->setDefaults($defaults); - } + if (isset($_GET['fromExercise']) + || (!isset($_GET['newQuestion']) || $isContent) + ) { + try { + $form->getElement('questionName')->setValue($this->question); + } catch (Exception $exception) { + } - if (!isset($_GET['newQuestion']) || $isContent) { - $form->setDefaults($defaults); + try { + $form->getElement('questionDescription')->setValue($this->description); + } catch (Exception $e) { + } + + try { + $form->getElement('questionLevel')->setValue($this->level); + } catch (Exception $e) { + } + + try { + $form->getElement('questionCategory')->setValue($this->category); + } catch (Exception $e) { + } + + try { + $form->getElement('feedback')->setValue($this->feedback); + } catch (Exception $e) { + } + + try { + $form->getElement('mandatory')->setValue($this->mandatory); + } catch (Exception $e) { + } } /*if (!empty($_REQUEST['myid'])) {