type = CALCULATED_ANSWER; $this -> isContent = $this-> getIsContent(); } /** * function which redefines Question::createAnswersForm * @param the formvalidator instance */ 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', '','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->addRule('answer', get_lang('DefineBlanks'),'regex','/\[.*\]/'); $form->addElement('label', null, get_lang('IfYouWantOnlyIntegerValuesWriteBothLimitsWithoutDecimals')); $form->addElement('html', '
'); $form->addElement('label', null, get_lang('FormulaExample').': √ x ÷ y  × e (ln(pi)) = sqrt([x]/[y])*(e^(ln(pi)))'); $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, $class; // setting the save button here and not in the question class.php $form->addElement('style_submit_button', 'submitQuestion', $text, 'class="'.$class.'"'); 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) { global $charset; $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'); //Remove previous answers Database::delete("c_quiz_answer", array('question_id = ?' => $this->id)); // 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)."]"; $randomValue = mt_rand($lowestValues[$i],$highestValues[$i]); //$randomValue = mt_rand($lowestValues[$i]*100,$highestValues[$i]*100)/100; $auxAnswer = str_replace($blankItem, $randomValue, $auxAnswer); $auxFormula = str_replace($blankItem, $randomValue, $auxFormula); } require_once(api_get_path(LIBRARY_PATH).'evalmath.class.php'); $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, array()); $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 .= ''; return $header; } }
'.get_lang("Answer").'