type = CALCULATED_ANSWER;
$this->isContent = $this->getIsContent();
}
/**
* {@inheritdoc}
*/
public function createAnswersForm($form)
{
$defaults = [];
//$defaults['answer'] = get_lang('
Example fill the form activity : calculate the Body Mass Index |
Age | [25] years old |
Sex | [M] (M or F) |
Weight | 95 Kg |
Height | 1.81 m |
Body Mass Index | [29] BMI =Weight/Size2 (Cf. Wikipedia article) |
');
$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('Please type your text below').', '.get_lang('and').' '.get_lang('use square brackets [...] to define one or more blanks'));
$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('Please type the text'), 'required');
$form->addRule('answer', get_lang('Please define at least one blank with square brackets [...]'), 'regex', '/\[.*\]/');
$form->addElement('label', null, get_lang('If you want only integer values write both limits without decimals'));
$form->addElement('html', '');
$notationListButton = Display::url(
get_lang('Formula notation'),
api_get_path(WEB_CODE_PATH).'exercise/evalmathnotation.php',
[
'class' => 'btn btn-info ajax',
'data-title' => get_lang('Formula notation'),
'_target' => '_blank',
]
);
$form->addElement(
'label',
null,
$notationListButton
);
$form->addElement('text', 'formula', [get_lang('Formula'), get_lang('Formula sample: sqrt( [x] / [y] ) * ( e ^ ( ln(pi) ) )')], ['id' => 'formula']);
$form->addRule('formula', get_lang('Please, write the formula'), 'required');
$form->addElement('text', 'weighting', get_lang('Score'), ['id' => 'weighting']);
$form->setDefaults(['weighting' => '10']);
$form->addElement('text', 'answerVariations', get_lang('Question variations'));
$form->addRule(
'answerVariations',
get_lang('GiveQuestion variations'),
'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 $exercise, $counter = null, $score = [])
{
$header = parent::return_header($exercise, $counter, $score);
$header .= '';
$header .= ''.get_lang('Answer').' | ';
if ($exercise->showExpectedChoice()) {
$header .= ''.get_lang('Your choice').' | ';
if ($exercise->showExpectedChoiceColumn()) {
$header .= ''.get_lang('Expected choice').' | ';
}
$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;
}
}