type = CALCULATED_ANSWER;
$this->isContent = $this->getIsContent();
}
/**
* {@inheritdoc}
*/
public function createAnswersForm($form)
{
$defaults = [];
$defaults['answer'] = get_lang('DefaultTextInBlanks');
if (!empty($this->id)) {
$objAnswer = new Answer($this->id);
$preArray = explode('@@', $objAnswer->selectAnswer(1));
$defaults['formula'] = array_pop($preArray);
$defaults['answer'] = array_shift($preArray);
$defaults['answer'] = preg_replace("/\[.*\]/", '', $defaults['answer']);
$defaults['weighting'] = $this->weighting;
}
$lowestValue = '1.00';
$highestValue = '20.00';
// javascript //
echo '';
// answer
$form->addElement('label', null, '
'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank'));
$form->addElement(
'html_editor',
'answer',
Display::return_icon('fill_field.png'),
[
'id' => 'answer',
'onkeyup' => 'javascript: updateBlanks(this);',
],
[
'ToolbarSet' => 'TestQuestionDescription',
'Width' => '100%',
'Height' => '350',
]
);
$form->addRule('answer', get_lang('GiveText'), 'required');
$form->addRule('answer', get_lang('DefineBlanks'), 'regex', '/\[.*\]/');
$form->addElement('label', null, get_lang('IfYouWantOnlyIntegerValuesWriteBothLimitsWithoutDecimals'));
$form->addElement('html', '
');
$notationListButton = Display::url(
get_lang('NotationList'),
api_get_path(WEB_CODE_PATH).'exercise/evalmathnotation.php',
[
'class' => 'btn btn-info ajax',
'data-title' => get_lang('NotationList'),
'_target' => '_blank',
]
);
$form->addElement(
'label',
null,
$notationListButton
);
$form->addElement('text', 'formula', [get_lang('Formula'), get_lang('FormulaExample')], ['id' => 'formula']);
$form->addRule('formula', get_lang('GiveFormula'), 'required');
$form->addElement('text', 'weighting', get_lang('Weighting'), ['id' => 'weighting']);
$form->setDefaults(['weighting' => '10']);
$form->addElement('text', 'answerVariations', get_lang('AnswerVariations'));
$form->addRule(
'answerVariations',
get_lang('GiveAnswerVariations'),
'required'
);
$form->setDefaults(['answerVariations' => '1']);
global $text;
// setting the save button here and not in the question class.php
$form->addButtonSave($text, 'submitQuestion');
if (!empty($this->id)) {
$form->setDefaults($defaults);
} else {
if ($this->isContent == 1) {
$form->setDefaults($defaults);
}
}
}
/**
* {@inheritdoc}
*/
public function processAnswersCreation($form, $exercise)
{
if (!self::isAnswered()) {
$table = Database::get_course_table(TABLE_QUIZ_ANSWER);
Database::delete(
$table,
[
'c_id = ? AND question_id = ?' => [
$this->course['real_id'],
$this->id,
],
]
);
$answer = $form->getSubmitValue('answer');
$formula = $form->getSubmitValue('formula');
$lowestValues = $form->getSubmitValue('lowestValue');
$highestValues = $form->getSubmitValue('highestValue');
$answerVariations = $form->getSubmitValue('answerVariations');
$this->weighting = $form->getSubmitValue('weighting');
// Create as many answers as $answerVariations
for ($j = 0; $j < $answerVariations; $j++) {
$auxAnswer = $answer;
$auxFormula = $formula;
$nb = preg_match_all('/\[[^\]]*\]/', $auxAnswer, $blanks);
if ($nb > 0) {
for ($i = 0; $i < $nb; $i++) {
$blankItem = $blanks[0][$i];
// take random float values when one or both edge values have a decimal point
$randomValue =
(strpos($lowestValues[$i], '.') !== false ||
strpos($highestValues[$i], '.') !== false) ?
mt_rand($lowestValues[$i] * 100, $highestValues[$i] * 100) / 100 : mt_rand($lowestValues[$i], $highestValues[$i]);
$auxAnswer = str_replace($blankItem, $randomValue, $auxAnswer);
$auxFormula = str_replace($blankItem, $randomValue, $auxFormula);
}
$math = new EvalMath();
$result = $math->evaluate($auxFormula);
$result = number_format($result, 2, '.', '');
// Remove decimal trailing zeros
$result = rtrim($result, '0');
// If it is an integer (ends in .00) remove the decimal point
if (mb_substr($result, -1) === '.') {
$result = str_replace('.', '', $result);
}
// Attach formula
$auxAnswer .= " [".$result."]@@".$formula;
}
$this->save($exercise);
$objAnswer = new Answer($this->id);
$objAnswer->createAnswer($auxAnswer, 1, '', $this->weighting, '');
$objAnswer->position = [];
$objAnswer->save();
}
}
}
/**
* {@inheritdoc}
*/
public function return_header($exercise, $counter = null, $score = null)
{
$header = parent::return_header($exercise, $counter, $score);
$header .= '';
$header .= ''.get_lang('Answer').' | ';
if ($exercise->showExpectedChoice()) {
$header .= ''.get_lang('YourChoice').' | ';
$header .= ''.get_lang('ExpectedChoice').' | ';
$header .= ''.get_lang('Status').' | ';
}
$header .= '
';
return $header;
}
/**
* Returns true if the current question has been attempted to be answered.
*
* @return bool
*/
public function isAnswered()
{
$table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$result = Database::select(
'question_id',
$table,
[
'where' => [
'question_id = ? AND c_id = ?' => [
$this->id,
$this->course['real_id'],
],
],
]
);
return empty($result) ? false : true;
}
}