public function validation($data, $files)
 {
     $errors = parent::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];
             }
         }
     }
     $answers = $data['answer'];
     $answercount = 0;
     $maxgrade = false;
     $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
     $mandatorydatasets = array();
     foreach ($answers as $key => $answer) {
         $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
     }
     if (count($mandatorydatasets) == 0) {
         foreach ($answers as $key => $answer) {
             $errors['answeroptions[' . $key . ']'] = get_string('atleastonewildcard', 'qtype_calculated');
         }
     }
     $totalfraction = 0;
     $maxfraction = -1;
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         $fraction = (double) $data['fraction'][$key];
         if (empty($trimmedanswer) && $trimmedanswer != '0' && empty($fraction)) {
             continue;
         }
         if (empty($trimmedanswer)) {
             $errors['answeroptions[' . $key . ']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
         }
         if ($trimmedanswer != '' || $answercount == 0) {
             // Verifying for errors in {=...} in answer text.
             $qanswer = '';
             $qanswerremaining = $trimmedanswer;
             $possibledatasets = $this->qtypeobj->find_dataset_names($trimmedanswer);
             foreach ($possibledatasets as $name => $value) {
                 $qanswerremaining = str_replace('{' . $name . '}', '1', $qanswerremaining);
             }
             while (preg_match('~\\{=([^[:space:]}]*)}~', $qanswerremaining, $regs1)) {
                 $qanswersplits = explode($regs1[0], $qanswerremaining, 2);
                 $qanswer = $qanswer . $qanswersplits[0];
                 $qanswerremaining = $qanswersplits[1];
                 if (!empty($regs1[1]) && ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1]))) {
                     if (!isset($errors['answeroptions[' . $key . ']'])) {
                         $errors['answeroptions[' . $key . ']'] = $formulaerrors . ':' . $regs1[1];
                     } else {
                         $errors['answeroptions[' . $key . ']'] .= '<br/>' . $formulaerrors . ':' . $regs1[1];
                     }
                 }
             }
         }
         if ($trimmedanswer != '') {
             if ('2' == $data['correctanswerformat'][$key] && '0' == $data['correctanswerlength'][$key]) {
                 $errors['correctanswerlength[' . $key . ']'] = get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
             }
             if (!is_numeric($data['tolerance'][$key])) {
                 $errors['tolerance[' . $key . ']'] = get_string('xmustbenumeric', 'qtype_numerical', get_string('acceptederror', 'qtype_numerical'));
             }
             if ($data['fraction'][$key] > 0) {
                 $totalfraction += $data['fraction'][$key];
             }
             if ($data['fraction'][$key] > $maxfraction) {
                 $maxfraction = $data['fraction'][$key];
             }
             $answercount++;
         }
     }
     if ($answercount == 0) {
         $errors['answeroptions[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
         $errors['answeroptions[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
     } else {
         if ($answercount == 1) {
             $errors['answeroptions[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
         }
     }
     // Perform sanity checks on fractional grades.
     if ($data['single'] == 1) {
         if ($maxfraction != 1) {
             $errors['answeroptions[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction * 100);
         }
     } else {
         $totalfraction = round($totalfraction, 2);
         if ($totalfraction != 1) {
             $totalfraction = $totalfraction * 100;
             $errors['answeroptions[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
         }
     }
     return $errors;
 }
 function validation($data, $files)
 {
     // echo code left for testing period
     // echo "<p>question <pre>";print_r($this->question);echo "</pre></p>";
     // echo "<p>data <pre>";print_r($data);echo "</pre></p>";
     $errors = parent::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);
     }
     // echo "numericalquestion qtextremaining <pre>";print_r($possibledatasets);
     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];
             }
         }
     }
     $answers = $data['answer'];
     $answercount = 0;
     $maxgrade = false;
     $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
     $mandatorydatasets = array();
     foreach ($answers as $key => $answer) {
         $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
     }
     if (count($mandatorydatasets) == 0) {
         //  $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
         foreach ($answers as $key => $answer) {
             $errors['answer[' . $key . ']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
         }
     }
     if ($data['multichoice'] == 1) {
         foreach ($answers as $key => $answer) {
             $trimmedanswer = trim($answer);
             if ($trimmedanswer != '' || $answercount == 0) {
                 //verifying for errors in {=...} in answer text;
                 $qanswer = "";
                 $qanswerremaining = $trimmedanswer;
                 $possibledatasets = $this->qtypeobj->find_dataset_names($trimmedanswer);
                 foreach ($possibledatasets as $name => $value) {
                     $qanswerremaining = str_replace('{' . $name . '}', '1', $qanswerremaining);
                 }
                 //     echo "numericalquestion qanswerremaining <pre>";print_r($possibledatasets);
                 while (preg_match('~\\{=([^[:space:]}]*)}~', $qanswerremaining, $regs1)) {
                     $qanswersplits = explode($regs1[0], $qanswerremaining, 2);
                     $qanswer = $qanswer . $qanswersplits[0];
                     $qanswerremaining = $qanswersplits[1];
                     if (!empty($regs1[1]) && ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1]))) {
                         if (!isset($errors['answer[' . $key . ']'])) {
                             $errors['answer[' . $key . ']'] = $formulaerrors . ':' . $regs1[1];
                         } else {
                             $errors['answer[' . $key . ']'] .= '<br/>' . $formulaerrors . ':' . $regs1[1];
                         }
                     }
                 }
             }
             if ($trimmedanswer != '') {
                 if ('2' == $data['correctanswerformat'][$key] && '0' == $data['correctanswerlength'][$key]) {
                     $errors['correctanswerlength[' . $key . ']'] = get_string('zerosignificantfiguresnotallowed', 'quiz');
                 }
                 if (!is_numeric($data['tolerance'][$key])) {
                     $errors['tolerance[' . $key . ']'] = get_string('mustbenumeric', 'qtype_calculated');
                 }
                 if ($data['fraction'][$key] == 1) {
                     $maxgrade = true;
                 }
                 $answercount++;
             }
             //check grades
             $totalfraction = 0;
             $maxfraction = 0;
             if ($answer != '') {
                 if ($data['fraction'][$key] > 0) {
                     $totalfraction += $data['fraction'][$key];
                 }
                 if ($data['fraction'][$key] > $maxfraction) {
                     $maxfraction = $data['fraction'][$key];
                 }
             }
         }
         if ($answercount == 0) {
             $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
             $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
         } elseif ($answercount == 1) {
             $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
         }
         /// Perform sanity checks on fractional grades
         if ($data['single']) {
             if ($maxfraction > 0.999) {
                 $maxfraction = $maxfraction * 100;
                 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
             }
         } else {
             $totalfraction = round($totalfraction, 2);
             if ($totalfraction != 1) {
                 $totalfraction = $totalfraction * 100;
                 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
             }
         }
         if ($answercount == 0) {
             $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
         }
         if ($maxgrade == false) {
             $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
         }
     }
     return $errors;
 }
 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']);
     $errors = $this->validate_text($errors, 'correctfeedback', $data['correctfeedback']['text']);
     $errors = $this->validate_text($errors, 'partiallycorrectfeedback', $data['partiallycorrectfeedback']['text']);
     $errors = $this->validate_text($errors, 'incorrectfeedback', $data['incorrectfeedback']['text']);
     $answers = $data['answer'];
     $answercount = 0;
     $maxgrade = false;
     $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
     $mandatorydatasets = array();
     foreach ($answers as $key => $answer) {
         $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
     }
     if (count($mandatorydatasets) == 0) {
         foreach ($answers as $key => $answer) {
             $errors['answeroptions[' . $key . ']'] = get_string('atleastonewildcard', 'qtype_calculated');
         }
     }
     $totalfraction = 0;
     $maxfraction = -1;
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         $fraction = (double) $data['fraction'][$key];
         if (empty($trimmedanswer) && $trimmedanswer != '0' && empty($fraction)) {
             continue;
         }
         if (empty($trimmedanswer)) {
             $errors['answeroptions[' . $key . ']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
         }
         if ($trimmedanswer != '' || $answercount == 0) {
             // Verifying for errors in {=...} in answer text.
             $errors = $this->validate_text($errors, 'answeroptions[' . $key . ']', $answer);
             $errors = $this->validate_text($errors, 'feedback[' . $key . ']', $data['feedback'][$key]['text']);
         }
         if ($trimmedanswer != '') {
             if ('2' == $data['correctanswerformat'][$key] && '0' == $data['correctanswerlength'][$key]) {
                 $errors['correctanswerlength[' . $key . ']'] = get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
             }
             if (!is_numeric($data['tolerance'][$key])) {
                 $errors['tolerance[' . $key . ']'] = get_string('xmustbenumeric', 'qtype_numerical', get_string('acceptederror', 'qtype_numerical'));
             }
             if ($data['fraction'][$key] > 0) {
                 $totalfraction += $data['fraction'][$key];
             }
             if ($data['fraction'][$key] > $maxfraction) {
                 $maxfraction = $data['fraction'][$key];
             }
             $answercount++;
         }
     }
     if ($answercount == 0) {
         $errors['answeroptions[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
         $errors['answeroptions[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
     } else {
         if ($answercount == 1) {
             $errors['answeroptions[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
         }
     }
     // Perform sanity checks on fractional grades.
     if ($data['single'] == 1) {
         if ($maxfraction != 1) {
             $errors['answeroptions[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction * 100);
         }
     } else {
         $totalfraction = round($totalfraction, 2);
         if ($totalfraction != 1) {
             $totalfraction = $totalfraction * 100;
             $errors['answeroptions[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
         }
     }
     return $errors;
 }