type = CALCULATED_ANSWER;
$this -> isContent = $this-> getIsContent();
}
/**
* function which redefines Question::createAnswersForm
* @param FormValidator $form
*/
function createAnswersForm($form)
{
$defaults = array();
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;
} else {
$defaults['answer'] = get_lang('DefaultTextInBlanks');
}
$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',
'',
array(
'id' => 'answer',
'onkeyup' => 'javascript: updateBlanks(this);'
),
array('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_PATH).'main/exercice/evalmathnotation.php',
array(
'class' => 'btn ajax',
'data-title' => get_lang('NotationList'),
'_target' => '_blank'
)
);
$form->addElement(
'label', null,
$notationListButton);
$form->addElement('label', null, get_lang('FormulaExample'));
$form->addElement('text', 'formula', get_lang('Formula'), array('id' => 'formula', 'class' => 'span4'));
$form->addRule('formula', get_lang('GiveFormula'), 'required');
$form->addElement('text', 'weighting', get_lang('Weighting'), array('id' => 'weighting', 'class' => 'span1'));
$form->setDefaults(array('weighting' => '10'));
$form->addElement('text', 'answerVariations', get_lang('AnswerVariations'), array('class' => 'span1'));
$form->addRule('answerVariations', get_lang('GiveAnswerVariations'),'required');
$form->setDefaults(array('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);
}
}
}
/**
* abstract function which creates the form to create / edit the answers of the question
* @param FormValidator $form
*/
function processAnswersCreation($form)
{
if (!self::isAnswered()) {
$table = Database::get_course_table(TABLE_QUIZ_ANSWER);
Database::delete(
$table,
array(
'c_id = ? AND question_id = ?' => array(
$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];
$replace = array("[", "]");
$newBlankItem = str_replace($replace, "", $blankItem);
$newBlankItem = "[".trim($newBlankItem)."]";
// 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();
$objAnswer = new Answer($this->id);
$objAnswer->createAnswer($auxAnswer, 1, '', $this->weighting, null);
$objAnswer->position = array();
$objAnswer->save();
}
}
}
/**
* @param null $feedback_type
* @param null $counter
* @param null $score
* @return null|string
*/
function return_header($feedback_type = null, $counter = null, $score = null)
{
$header = parent::return_header($feedback_type, $counter, $score);
$header .= '
'.get_lang("Answer").' |
';
return $header;
}
/**
* Returns true if the current question has been attempted to be answered
* @return boolean
*/
public function isAnswered()
{
$table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$result = Database::select(
'question_id',
$table,
array(
'where' => array(
'question_id = ? AND c_id = ?' => array(
$this->id,
$this->course['real_id']
)
)
)
);
return empty($result) ? false : true;
}
}