public function is_valid_number($x) { return parent::is_valid_number($x); }
public function validation($data, $files) { $errors = parent::validation($data, $files); // Verifying for errors in {=...} in question text. $errors = $this->validate_text($errors, 'questiontext', $data['questiontext']['text']); $errors = $this->validate_text($errors, 'generalfeedback', $data['generalfeedback']['text']); // Check that the answers use datasets. $answers = $data['answer']; $mandatorydatasets = array(); foreach ($answers as $key => $answer) { $problems = qtype_calculated_find_formula_errors($answer); if ($problems) { $errors['answeroptions[' . $key . ']'] = $problems; } $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer); $errors = $this->validate_text($errors, 'feedback[' . $key . ']', $data['feedback'][$key]['text']); } if (empty($mandatorydatasets)) { foreach ($answers as $key => $answer) { $errors['answeroptions[' . $key . ']'] = get_string('atleastonewildcard', 'qtype_calculated'); } } // Validate the answer format. foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if (trim($answer)) { if ($data['correctanswerformat'][$key] == 2 && $data['correctanswerlength'][$key] == '0') { $errors['answerdisplay[' . $key . ']'] = get_string('zerosignificantfiguresnotallowed', 'qtype_calculated'); } } } return $errors; }
public function validation($data, $files) { // verifying for errors in {=...} in question text; $qtext = ""; $qtextremaining = $data['questiontext']['text']; $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']); foreach ($possibledatasets as $name => $value) { $qtextremaining = str_replace('{'.$name.'}', '1', $qtextremaining); } while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) { $qtextsplits = explode($regs1[0], $qtextremaining, 2); $qtext = $qtext.$qtextsplits[0]; $qtextremaining = $qtextsplits[1]; if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) { if (!isset($errors['questiontext'])) { $errors['questiontext'] = $formulaerrors.':'.$regs1[1]; } else { $errors['questiontext'] .= '<br/>'.$formulaerrors.':'.$regs1[1]; } } } $errors = parent::validation($data, $files); // Check that the answers use datasets. $answers = $data['answer']; $mandatorydatasets = array(); foreach ($answers as $key => $answer) { $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer); } if (empty($mandatorydatasets)) { foreach ($answers as $key => $answer) { $errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_calculated'); } } // Validate the answer format. foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if (trim($answer)) { if ($data['correctanswerformat'][$key] == 2 && $data['correctanswerlength'][$key] == '0') { $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed', 'qtype_calculated'); } } } return $errors; }
public function test_question_saving_pi() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('numerical'); $formdata = test_question_maker::get_question_form_data('numerical'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; qtype_numerical_edit_form::mock_submit((array) $formdata); $form = qtype_numerical_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); foreach ($questiondata as $property => $value) { if (!in_array($property, array('options'))) { $this->assertAttributeEquals($value, $property, $actualquestiondata); } } foreach ($questiondata->options as $optionname => $value) { if (!in_array($optionname, array('answers'))) { $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options); } } foreach ($questiondata->options->answers as $ansindex => $answer) { $actualanswer = array_shift($actualquestiondata->options->answers); foreach ($answer as $ansproperty => $ansvalue) { // This question does not use 'answerformat', will ignore it. if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer); } } } }