function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $answers = $data['subanswers'];
     $questions = $data['subquestions'];
     $questioncount = 0;
     $answercount = 0;
     foreach ($questions as $key => $question) {
         $trimmedquestion = trim($question);
         $trimmedanswer = trim($answers[$key]);
         if ($trimmedquestion != '') {
             $questioncount++;
         }
         if ($trimmedanswer != '' || $trimmedquestion != '') {
             $answercount++;
         }
         if ($trimmedquestion != '' && $trimmedanswer == '') {
             $errors['subanswers[' . $key . ']'] = get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
         }
     }
     $numberqanda = new stdClass();
     $numberqanda->q = 2;
     $numberqanda->a = 3;
     if ($questioncount < 1) {
         $errors['subquestions[0]'] = get_string('notenoughqsandas', 'qtype_match', $numberqanda);
     }
     if ($questioncount < 2) {
         $errors['subquestions[1]'] = get_string('notenoughqsandas', 'qtype_match', $numberqanda);
     }
     if ($answercount < 3) {
         $errors['subanswers[2]'] = get_string('notenoughqsandas', 'qtype_match', $numberqanda);
     }
     return $errors;
 }
 function validation($data, $files)
 {
     global $QTYPES;
     $errors = parent::validation($data, $files);
     // Check the answers.
     $answercount = 0;
     $maxgrade = false;
     $answers = $data['answer'];
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         if ($trimmedanswer != '') {
             $answercount++;
             if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
                 $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
             }
             if ($data['fraction'][$key] == 1) {
                 $maxgrade = true;
             }
         } else {
             if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) {
                 $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
                 $answercount++;
             }
         }
     }
     if ($answercount == 0) {
         $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical');
     }
     if ($maxgrade == false) {
         $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
     }
     $QTYPES['numerical']->validate_numerical_options($data, $errors);
     return $errors;
 }
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $answers = $data['answer'];
     $answercount = 0;
     $totalfraction = 0;
     $maxfraction = -1;
     foreach ($answers as $key => $answer) {
         //check no of choices
         $trimmedanswer = trim($answer);
         if (!empty($trimmedanswer)) {
             $answercount++;
         }
         //check grades
         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', 1);
     }
     $totalfraction = round($totalfraction, 2);
     if ($totalfraction != 1) {
         $totalfraction = $totalfraction * 100;
         $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
     }
     return $errors;
 }
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $answers = $data['answer'];
     $answercount = 0;
     $maxgrade = false;
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         if ($trimmedanswer !== '') {
             $answercount++;
             if ($data['fraction'][$key] == 1) {
                 $maxgrade = true;
             }
         } else {
             if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) {
                 $errors["answer[{$key}]"] = get_string('answermustbegiven', 'qtype_shortanswer');
                 $answercount++;
             }
         }
     }
     if ($answercount == 0) {
         $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_shortanswer', 1);
     }
     if ($maxgrade == false) {
         $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
     }
     return $errors;
 }
 function validation($data)
 {
     $errors = parent::validation($data);
     $answers = $data['subanswers'];
     $questions = $data['subquestions'];
     $questioncount = 0;
     foreach ($questions as $key => $question) {
         $trimmedquestion = trim($question);
         $trimmedanswer = trim($answers[$key]);
         if (!empty($trimmedanswer) && !empty($trimmedquestion)) {
             $questioncount++;
         }
         if (!empty($trimmedquestion) && empty($trimmedanswer)) {
             $errors['subanswers[' . $key . ']'] = get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
         }
     }
     if ($questioncount == 0) {
         $errors['subquestions[0]'] = get_string('notenoughquestions', 'qtype_match', 3);
         $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
         $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
     } elseif ($questioncount == 1) {
         $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
         $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
     } elseif ($questioncount == 2) {
         $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
     }
     return $errors;
 }
 public function validation($data, $files)
 {
     global $DB;
     $errors = parent::validation($data, $files);
     if (isset($data->categorymoveto)) {
         list($category) = explode(',', $data['categorymoveto']);
     } else {
         list($category) = explode(',', $data['category']);
     }
     $saquestions = question_bank::get_qtype('randomsamatch')->get_sa_candidates($category);
     $numberavailable = count($saquestions);
     if ($saquestions === false) {
         $a = new stdClass();
         $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
         $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
     } else {
         if ($numberavailable < $data['choose']) {
             $a = new stdClass();
             $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
             $a->nosaquestions = $numberavailable;
             $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
         }
     }
     return $errors;
 }
Beispiel #7
0
 function validation($data, $files)
 {
     //TODO would be nice to parse the question text here and output some error
     //messages if there is a problem with the text.
     $errors = parent::validation($data, $files);
     //extra check to make sure there is something in the htmlarea besides a <br />
     $questiontext = trim(strip_tags($data['questiontext']));
     if ($questiontext == '') {
         $errors['questiontext'] = get_string('err_required', 'form');
     }
     return $errors;
 }
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $answers = $data['answer'];
     $answercount = 0;
     $totalfraction = 0;
     $maxfraction = -1;
     foreach ($answers as $key => $answer) {
         //check no of choices
         $trimmedanswer = trim($answer['text']);
         $fraction = (double) $data['fraction'][$key];
         if ($trimmedanswer === '' && empty($fraction)) {
             continue;
         }
         if ($trimmedanswer === '') {
             $errors['fraction[' . $key . ']'] = get_string('errgradesetanswerblank', 'qtype_turmultiplechoice');
         }
         $answercount++;
         //check grades
         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_turmultiplechoice', 2);
         $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_turmultiplechoice', 2);
     } else {
         if ($answercount == 1) {
             $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_turmultiplechoice', 2);
         }
     }
     /// Perform sanity checks on fractional grades
     if ($data['single']) {
         if ($maxfraction != 1) {
             $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_turmultiplechoice', $maxfraction * 100);
         }
     } else {
         $totalfraction = round($totalfraction, 2);
         if ($totalfraction != 1) {
             $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_turmultiplechoice', $totalfraction * 100);
         }
     }
     return $errors;
 }
Beispiel #9
0
 function validation($data, $files)
 {
     global $QTYPES;
     $errors = parent::validation($data, $files);
     $saquestions = $QTYPES['randomsamatch']->get_sa_candidates($data['category']);
     $numberavailable = count($saquestions);
     if ($saquestions === false) {
         $a = new object();
         $a->catname = get_field('question_categories', 'name', 'id', $data['category']);
         $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
     } elseif ($numberavailable < $data['choose']) {
         $a = new object();
         $a->catname = get_field('question_categories', 'name', 'id', $data['category']);
         $a->nosaquestions = $numberavailable;
         $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
     }
     return $errors;
 }
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     // cleanup :
     $data['conditions'] = rtrim(str_replace("\r\n", "\n", $data['conditions']), "\n");
     $data['feedbackperconditions'] = rtrim(str_replace("\r\n", "\n", $data['feedbackperconditions']), "\n");
     $conditions = explode("\n", $data['conditions']);
     $feedbackperconditions = explode("\n", $data['feedbackperconditions']);
     if (count($feedbackperconditions) > count($conditions)) {
         $errors['conditions'] = get_string('badnumfeedbackperconditions', 'qtype_multinumerical');
         $errors['feedbackperconditions'] = get_string('badnumfeedbackperconditions', 'qtype_multinumerical');
     }
     foreach ($feedbackperconditions as $feedbackpercondition) {
         if (trim($feedbackpercondition) && strpos($feedbackpercondition, '|') === false) {
             $errors['feedbackperconditions'] = get_string('badfeedbackperconditionsyntax', 'qtype_multinumerical');
         }
     }
     return $errors;
 }
Beispiel #11
0
 public function validation($fromform, $files)
 {
     $errors = parent::validation($fromform, $files);
     // Don't allow both 'no inline response' and 'no attachments' to be selected,
     // as these options would result in there being no input requested from the user.
     if ($fromform['responseformat'] == 'noinline' && !$fromform['attachments']) {
         $errors['attachments'] = get_string('mustattach', 'qtype_essay');
     }
     // If 'no inline response' is set, force the teacher to require attachments;
     // otherwise there will be nothing to grade.
     if ($fromform['responseformat'] == 'noinline' && !$fromform['attachmentsrequired']) {
         $errors['attachmentsrequired'] = get_string('mustrequire', 'qtype_essay');
     }
     // Don't allow the teacher to require more attachments than they allow; as this would
     // create a condition that it's impossible for the student to meet.
     if ($fromform['attachments'] != -1 && $fromform['attachments'] < $fromform['attachmentsrequired']) {
         $errors['attachmentsrequired'] = get_string('mustrequirefewer', 'qtype_essay');
     }
     return $errors;
 }
 function validation($data)
 {
     $errors = parent::validation($data);
     $answers = $data['answer'];
     $answercount = 0;
     $maxgrade = false;
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         if ($trimmedanswer !== '') {
             $answercount++;
             if ($data['fraction'][$key] == 1) {
                 $maxgrade = true;
             }
         }
     }
     if ($answercount == 0) {
         $errors['answer[0]'] = get_string('notenoughanswers', 'quiz', 1);
     }
     if ($maxgrade == false) {
         $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
     }
     return $errors;
 }
 function validation($data)
 {
     $errors = parent::validation($data, null);
     $colcount = matrix_qtype::count_form_rows_or_cols($data, false);
     $rowcount = matrix_qtype::count_form_rows_or_cols($data);
     if ($rowcount == 0) {
         $errors['rowshort[0]'] = get_string('mustdefine1by1', 'qtype_matrix');
     }
     if ($colcount == 0) {
         $errors['colshort[0]'] = get_string('mustdefine1by1', 'qtype_matrix');
     }
     if (array_key_exists('grademethod', $data)) {
         $grademethod = $data['grademethod'];
     } else {
         $grademethod = matrix_qtype::single_default_grademethod();
     }
     if ($grademethod == QTYPE_MATRIX_GRADING_WEIGHTED && empty($data['multiple'])) {
         $errors['multiple'] = get_string('weightednomultiple', 'qtype_matrix');
     }
     $gradeclass = matrix_qtype::grade_class($grademethod, $data['multiple']);
     $matrixerrors = $gradeclass->validate_defining_form_matrix($data);
     $errors = array_merge($errors, $matrixerrors);
     return $errors ? $errors : true;
 }
 public function validation($fromform, $files)
 {
     $errors = parent::validation($fromform, $files);
     $inputs = $this->get_input_names_from_question_text();
     $prts = $this->get_prt_names_from_question();
     $fixingdollars = array_key_exists('fixdollars', $fromform);
     $this->options = new stack_options();
     $this->options->set_option('multiplicationsign', $fromform['multiplicationsign']);
     $this->options->set_option('complexno', $fromform['complexno']);
     $this->options->set_option('inversetrig', $fromform['inversetrig']);
     $this->options->set_option('matrixparens', $fromform['matrixparens']);
     $this->options->set_option('sqrtsign', (bool) $fromform['sqrtsign']);
     $this->options->set_option('simplify', (bool) $fromform['questionsimplify']);
     $this->options->set_option('assumepos', (bool) $fromform['assumepositive']);
     // We slightly break the usual conventions of validation, in that rather
     // than building up $errors as an array of strings, we initially build it
     // up as an array of arrays, then at the end remove any empty arrays,
     // and implod (' ', ...) any arrays that are non-empty. This makes our
     // rather complex validation easier to implement.
     // 1) Validate all the fixed question fields.
     // Question variables.
     $errors = $this->validate_cas_keyval($errors, $fromform['questionvariables'], 'questionvariables');
     // Question text.
     $errors['questiontext'] = array();
     $errors = $this->validate_cas_text($errors, $fromform['questiontext']['text'], 'questiontext', $fixingdollars);
     // Check for whitespace following placeholders.
     $sloppytags = $this->get_sloppy_tags($fromform['questiontext']['text']);
     foreach ($sloppytags as $sloppytag) {
         $errors['questiontext'][] = stack_string('questiontextplaceholderswhitespace', $sloppytag);
     }
     foreach ($inputs as $inputname => $counts) {
         list($numinputs, $numvalidations) = $counts;
         if ($numinputs == 0 && $numvalidations == 0) {
             if (!$fromform[$inputname . 'deleteconfirm']) {
                 $errors['questiontext'][] = stack_string('inputremovedconfirmbelow', $inputname);
             }
             continue;
         }
         if ($numinputs == 0) {
             $errors['questiontext'][] = stack_string('questiontextmustcontain', '[[input:' . $inputname . ']]');
         } else {
             if ($numinputs > 1) {
                 $errors['questiontext'][] = stack_string('questiontextonlycontain', '[[input:' . $inputname . ']]');
             }
         }
         if ($numvalidations == 0) {
             $errors['questiontext'][] = stack_string('questiontextmustcontain', '[[validation:' . $inputname . ']]');
         } else {
             if ($numvalidations > 1) {
                 $errors['questiontext'][] = stack_string('questiontextonlycontain', '[[validation:' . $inputname . ']]');
             }
         }
     }
     if (empty($inputs) && !empty($prts)) {
         $errors['questiontext'][] = stack_string('noprtsifnoinputs');
     }
     // Default mark.
     if (empty($inputs) && $fromform['defaultmark'] != 0) {
         $errors['defaultmark'][] = stack_string('defaultmarkzeroifnoprts');
     }
     // Penalty.
     $penalty = $fromform['penalty'];
     if (!is_numeric($penalty) || $penalty < 0 || $penalty > 1) {
         $errors['penalty'][] = stack_string('penaltyerror');
     }
     // Specific feedback.
     $errors['specificfeedback'] = array();
     $errors = $this->validate_cas_text($errors, $fromform['specificfeedback']['text'], 'specificfeedback', $fixingdollars);
     $errors['specificfeedback'] += $this->check_no_placeholders(stack_string('specificfeedback'), $fromform['specificfeedback']['text'], array('input', 'validation'));
     // General feedback.
     $errors['generalfeedback'] = array();
     $errors = $this->validate_cas_text($errors, $fromform['generalfeedback']['text'], 'generalfeedback', $fixingdollars);
     $errors['generalfeedback'] += $this->check_no_placeholders(get_string('generalfeedback', 'question'), $fromform['generalfeedback']['text']);
     // Question note.
     $errors['questionnote'] = array();
     if ('' == $fromform['questionnote']) {
         if (!(false === strpos($fromform['questionvariables'], 'rand'))) {
             $errors['questionnote'][] = stack_string('questionnotempty');
         }
     } else {
         // Note, the 'questionnote' does not have an editor field and hence no 'text' sub-clause.
         $errors = $this->validate_cas_text($errors, $fromform['questionnote'], 'questionnote', $fixingdollars);
     }
     $errors['questionnote'] += $this->check_no_placeholders(stack_string('questionnote'), $fromform['questionnote']);
     // 2) Validate all inputs.
     foreach ($inputs as $inputname => $counts) {
         list($numinputs, $numvalidations) = $counts;
         if ($numinputs == 0 && $numvalidations == 0 && !$fromform[$inputname . 'deleteconfirm']) {
             $errors[$inputname . 'deleteconfirm'][] = stack_string('youmustconfirm');
         }
         if (strlen($inputname) > 18 && !isset($fromform[$inputname . 'deleteconfirm'])) {
             $errors['questiontext'][] = stack_string('inputnamelength', $inputname);
         }
         if (array_key_exists($inputname . 'modelans', $fromform)) {
             $errors = $this->validate_cas_string($errors, $fromform[$inputname . 'modelans'], $inputname . 'modelans', $inputname . 'modelans');
             // TODO: find out if this input type acutally requires options, rather than
             // the hard-coded check here.
             if (false) {
                 $errors = $this->validate_cas_string($errors, $fromform[$inputname . 'options'], $inputname . 'options', $inputname . 'options', false);
             } else {
                 if ($fromform[$inputname . 'options']) {
                     $errors[$inputname . 'options'][] = stack_string('optionsnotrequired');
                 }
             }
         }
     }
     // 3) Validate all prts.
     foreach ($prts as $prtname => $count) {
         if ($count == 0) {
             if (!$fromform[$prtname . 'prtdeleteconfirm']) {
                 $errors['specificfeedback'][] = stack_string('prtremovedconfirmbelow', $prtname);
                 $errors[$prtname . 'prtdeleteconfirm'][] = stack_string('youmustconfirm');
             }
         } else {
             if ($count > 1) {
                 $errors['specificfeedback'][] = stack_string('questiontextfeedbackonlycontain', '[[feedback:' . $prtname . ']]');
             }
         }
         $errors = $this->validate_prt($errors, $fromform, $prtname, $fixingdollars);
     }
     // 4) Validate all hints.
     foreach ($fromform['hint'] as $index => $hint) {
         $errors = $this->validate_cas_text($errors, $hint['text'], 'hint[' . $index . ']', $fixingdollars);
     }
     // Clear out any empty $errors elements, ready for the next check.
     foreach ($errors as $field => $messages) {
         if (empty($messages)) {
             unset($errors[$field]);
         }
     }
     // If everything else is OK, try executing the CAS code to check for errors.
     if (empty($errors)) {
         $errors = $this->validate_question_cas_code($errors, $fromform, $fixingdollars);
     }
     // Convert the $errors array from our array of arrays format to the
     // standard array of strings format.
     foreach ($errors as $field => $messages) {
         if ($messages) {
             $errors[$field] = implode(' ', $messages);
         } else {
             unset($errors[$field]);
         }
     }
     return $errors;
 }
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     if ($this->col_count($data) == 0) {
         $errors['colshort[0]'] = qtype_matrix::get_string('mustdefine1by1');
     }
     if ($this->row_count($data) == 0) {
         $errors['rowshort[0]'] = qtype_matrix::get_string('mustdefine1by1');
     }
     $grading = qtype_matrix::grading($data['grademethod']);
     $grading_errors = $grading->validation($data);
     $errors = array_merge($errors, $grading_errors);
     return $errors ? $errors : true;
 }
 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;
 }
 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;
 }
Beispiel #18
0
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $questiontext = $data['questiontext'];
     $choices = $data['choices'];
     // Check the whether the slots are valid.
     $errorsinquestiontext = $this->validate_slots($questiontext['text'], $choices);
     if ($errorsinquestiontext) {
         $errors['questiontext'] = $errorsinquestiontext;
     }
     foreach ($choices as $key => $choice) {
         $answer = $choice['answer'];
         // Check whether the HTML tags are allowed tags.
         $tagerror = $this->get_illegal_tag_error($answer);
         if ($tagerror) {
             $errors['choices[' . $key . ']'] = $tagerror;
         }
     }
     return $errors;
 }
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     // Check the answers.
     $answercount = 0;
     $maxgrade = false;
     $answers = $data['answer'];
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         if ($trimmedanswer != '') {
             $answercount++;
             if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
                 $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
             }
             if ($data['fraction'][$key] == 1) {
                 $maxgrade = true;
             }
         } else {
             if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key])) {
                 $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
                 $answercount++;
             }
         }
     }
     if ($answercount == 0) {
         $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical');
     }
     if ($maxgrade == false) {
         $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
     }
     // Check units.
     $alreadyseenunits = array();
     if (isset($data['unit'])) {
         foreach ($data['unit'] as $key => $unit) {
             $trimmedunit = trim($unit);
             if ($trimmedunit != '' && in_array($trimmedunit, $alreadyseenunits)) {
                 $errors["unit[{$key}]"] = get_string('errorrepeatedunit', 'qtype_numerical');
                 if (trim($data['multiplier'][$key]) == '') {
                     $errors["multiplier[{$key}]"] = get_string('errornomultiplier', 'qtype_numerical');
                 }
             } else {
                 $alreadyseenunits[] = $trimmedunit;
             }
         }
     }
     return $errors;
 }
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $questiondisplay = qtype_multianswer_extract_question($data['questiontext']);
     if (isset($questiondisplay->options->questions)) {
         $subquestions = fullclone($questiondisplay->options->questions);
         if (count($subquestions)) {
             $sub = 1;
             foreach ($subquestions as $subquestion) {
                 $prefix = 'sub_' . $sub . '_';
                 $answercount = 0;
                 $maxgrade = false;
                 $maxfraction = -1;
                 foreach ($subquestion->answer as $key => $answer) {
                     if (is_array($answer)) {
                         $answer = $answer['text'];
                     }
                     $trimmedanswer = trim($answer);
                     if ($trimmedanswer !== '') {
                         $answercount++;
                         if ($subquestion->qtype == 'numerical' && !($this->is_valid_number($trimmedanswer) || $trimmedanswer == '*')) {
                             $errors[$prefix . 'answer[' . $key . ']'] = get_string('answermustbenumberorstar', 'qtype_numerical');
                         }
                         if ($subquestion->fraction[$key] == 1) {
                             $maxgrade = true;
                         }
                         if ($subquestion->fraction[$key] > $maxfraction) {
                             $maxfraction = $subquestion->fraction[$key];
                         }
                     }
                 }
                 if ($answercount == 0) {
                     if ($subquestion->qtype == 'multichoice') {
                         $errors[$prefix . 'answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
                     } else {
                         $errors[$prefix . 'answer[0]'] = get_string('notenoughanswers', 'question', 1);
                     }
                 }
                 if ($maxgrade == false) {
                     $errors[$prefix . 'fraction[0]'] = get_string('fractionsnomax', 'question');
                 }
                 $sub++;
             }
         } else {
             $errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
         }
     }
     if (($this->negativediff > 0 || $this->usedinquiz && ($this->negativediff > 0 || $this->negativediff < 0 || $this->qtypechange)) && !$this->confirm) {
         $errors['confirm'] = get_string('confirmsave', 'qtype_multianswer', $this->negativediff);
     }
     return $errors;
 }
 /**
  * Validating the data returning from the client.
  * 
  * The check the basic error as well as the formula error by evaluating one instantiation.
  */
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     global $basic_unit_conversion_rules;
     global $QTYPES;
     $qt =& $QTYPES[$this->qtype()];
     $form = (object) $data;
     try {
         $tmpquestion->options->extra->retrymarkseq = $form->retrymarkseq;
         $qt->get_trial_mark_fraction(&$tmpquestion, 0);
     } catch (Exception $e) {
         $errors["retrymarkseq"] = $e->getMessage();
     }
     $validanswers = $qt->check_form_answers($form);
     if (isset($validanswers->error)) {
         $errors = $errors + $validanswers->errors;
         $validanswers = $validanswers->answers;
     }
     $res = $qt->get_subquestion_structure($form->questiontext, $validanswers);
     if (isset($res->errors)) {
         $errors = $errors + $res->errors;
     }
     try {
         $vars = new question_variables($form->varsrandom);
         // checking random variables by instantiating one set
     } catch (Exception $e) {
         $errors["varsrandom"] = $e->getMessage();
         return $errors;
     }
     try {
         $vars->add_local_variables($form->varsglobal);
     } catch (Exception $e) {
         $errors["varsglobal"] = $e->getMessage();
         return $errors;
     }
     $globalvars = $vars->get_variables();
     /// Attempt to compute the answer so that it can see whether it is wrong
     foreach ($validanswers as $idx => $ans) {
         $unitcheck = new answer_unit_conversion();
         if ($ans->unitpenalty < 0 || $ans->unitpenalty > 1) {
             $errors["unitpenalty[{$idx}]"] = get_string('error_unitpenalty', 'qtype_coordinates') . $e->getMessage();
         }
         try {
             $unitcheck->parse_targets($ans->postunit);
         } catch (Exception $e) {
             $errors["postunit[{$idx}]"] = get_string('error_validation_parse_rule', 'qtype_coordinates') . $e->getMessage();
         }
         try {
             $unitcheck->parse_targets($ans->preunit);
         } catch (Exception $e) {
             $errors["preunit[{$idx}]"] = get_string('error_validation_parse_rule', 'qtype_coordinates') . $e->getMessage();
         }
         try {
             $unitcheck->assign_additional_rules($ans->otherrule);
             $unitcheck->reparse_all_rules();
         } catch (Exception $e) {
             $errors["otherrule[{$idx}]"] = get_string('error_validation_parse_rule', 'qtype_coordinates') . $e->getMessage();
         }
         try {
             $entry = $basic_unit_conversion_rules[$ans->ruleid];
             if ($entry === null || $entry[1] === null) {
                 throw new Exception(get_string('error_validation_ruleid', 'qtype_coordinates'));
             }
             $unitcheck->assign_default_rules($ans->ruleid, $entry[1]);
             $unitcheck->reparse_all_rules();
         } catch (Exception $e) {
             $errors["ruleid[{$idx}]"] = $e->getMessage();
         }
         try {
             $vars->add_local_variables($ans->vars1);
         } catch (Exception $e) {
             $errors["vars1[{$idx}]"] = get_string('error_validation_eval', 'qtype_coordinates') . $e->getMessage();
             continue;
         }
         try {
             $input = $qt->evaluate_answer($ans, $globalvars);
         } catch (Exception $e) {
             $errors["answer[{$idx}]"] = get_string('error_validation_eval', 'qtype_coordinates') . $e->getMessage();
             continue;
         }
         try {
             $qt->add_special_correctness_variables($vars, $input, $input);
             $vars->add_local_variables($ans->vars2);
         } catch (Exception $e) {
             $errors["vars2[{$idx}]"] = get_string('error_validation_eval', 'qtype_coordinates') . $e->getMessage();
             continue;
         }
         try {
             $correctness = $qt->grade_response_correctness($ans, $globalvars, '(' . implode(',', $input->coordinates) . ')', $unitcheck);
         } catch (Exception $e) {
             $errors["correctness[{$idx}]"] = get_string('error_validation_eval', 'qtype_coordinates') . $e->getMessage();
             continue;
         }
     }
     return $errors;
 }
Beispiel #22
0
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     if ($data['coderunnertype'] == 'Undefined') {
         $errors['coderunner_type_group'] = get_string('questiontype_required', 'qtype_coderunner');
     }
     if ($data['cputimelimitsecs'] != '' && (!ctype_digit($data['cputimelimitsecs']) || intval($data['cputimelimitsecs']) <= 0)) {
         $errors['sandboxcontrols'] = get_string('badcputime', 'qtype_coderunner');
     }
     if ($data['memlimitmb'] != '' && (!ctype_digit($data['memlimitmb']) || intval($data['memlimitmb']) < 0)) {
         $errors['sandboxcontrols'] = get_string('badmemlimit', 'qtype_coderunner');
     }
     if ($data['sandboxparams'] != '' && json_decode($data['sandboxparams']) === null) {
         $errors['sandboxcontrols'] = get_string('badsandboxparams', 'qtype_coderunner');
     }
     if ($data['templateparams'] != '' && json_decode($data['templateparams']) === null) {
         $errors['templateparams'] = get_string('badtemplateparams', 'qtype_coderunner');
     }
     if ($data['prototypetype'] == 0 && $data['grader'] !== 'qtype_coderunner_combinator_template_grader') {
         // Unless it's a prototype or uses a combinator-template grader
         // it needs at least one testcase
         $testCaseErrors = $this->validate_test_cases($data);
         $errors = array_merge($errors, $testCaseErrors);
     }
     if ($data['grader'] === 'qtype_coderunner_combinator_template_grader' && $data['enablecombinator'] == false) {
         $errors['combinatorcontrols'] = get_string('combinator_required', 'qtype_coderunner');
     }
     if ($data['prototypetype'] == 2 && ($data['saved_prototype_type'] != 2 || $data['typename'] != $data['coderunnertype'])) {
         // User-defined prototype, either newly created or undergoing a name change
         $typeName = trim($data['typename']);
         if ($typeName === '') {
             $errors['prototypecontrols'] = get_string('empty_new_prototype_name', 'qtype_coderunner');
         } else {
             if (!$this->is_valid_new_type($typeName)) {
                 $errors['prototypecontrols'] = get_string('bad_new_prototype_name', 'qtype_coderunner');
             }
         }
     }
     if (trim($data['penaltyregime']) != '') {
         $bits = explode(',', $data['penaltyregime']);
         $n = count($bits);
         for ($i = 0; $i < $n; $i++) {
             $bit = trim($bits[$i]);
             if ($bit === '...') {
                 if ($i != $n - 1 || $n < 3 || floatval($bits[$i - 1]) <= floatval($bits[$i - 2])) {
                     $errors['markinggroup'] = get_string('bad_dotdotdot', 'qtype_coderunner');
                 }
             }
         }
     }
     $result_columns_json = trim($data['resultcolumns']);
     if ($result_columns_json !== '') {
         $resultcolumns = json_decode($result_columns_json);
         if ($resultcolumns === null) {
             $errors['resultcolumns'] = get_string('resultcolumnsnotjson', 'qtype_coderunner');
         } else {
             if (!is_array($resultcolumns)) {
                 $errors['resultcolumns'] = get_string('resultcolumnsnotlist', 'qtype_coderunner');
             } else {
                 foreach ($resultcolumns as $col) {
                     if (!is_array($col) || count($col) < 2) {
                         $errors['resultcolumns'] = get_string('resultcolumnspecbad', 'qtype_coderunner');
                         break;
                     }
                     foreach ($col as $el) {
                         if (!is_string($el)) {
                             $errors['resultcolumns'] = get_string('resultcolumnspecbad', 'qtype_coderunner');
                             break;
                         }
                     }
                 }
             }
         }
     }
     return $errors;
 }
 public function validation($data, $files)
 {
     global $CFG;
     require_once $CFG->dirroot . '/question/type/regexp/locallib.php';
     $errors = parent::validation($data, $files);
     $answers = $data['answer'];
     $data['fraction'][0] = 1;
     $grades = $data['fraction'];
     $answercount = 0;
     $illegalmetacharacters = ". ^ \$ * + ? { } \\";
     foreach ($answers as $key => $answer) {
         $trimmedanswer = trim($answer);
         if ($trimmedanswer !== '') {
             $answercount++;
             $parenserror = '';
             $metacharserror = '';
             // we do not check parenthesis and square brackets in Answer 1 (correct answer)
             if ($key > 0) {
                 $markedline = '';
                 for ($i = 0; $i < strlen($trimmedanswer); $i++) {
                     $markedline .= ' ';
                 }
                 $parenserror = check_my_parens($trimmedanswer, $markedline);
                 if ($parenserror) {
                     $errors["answer[{$key}]"] = get_string("regexperrorparen", "qtype_regexp") . '<br />';
                     $markedline = $parenserror;
                 }
                 // we do not test unescaped metacharacters in Answers expressions for incorrect responses (grade = None)
                 if ($data['fraction'][$key] > 0) {
                     $metacharserror = check_unescaped_metachars($trimmedanswer, $markedline);
                     if ($metacharserror) {
                         $errormessage = get_string("illegalcharacters", "qtype_regexp", $illegalmetacharacters);
                         if (empty($errors["answer[{$key}]"])) {
                             $errors["answer[{$key}]"] = $errormessage;
                         } else {
                             $errors["answer[{$key}]"] .= $errormessage;
                         }
                     }
                 }
                 if ($metacharserror || $parenserror) {
                     $answerstringchunks = splitstring($trimmedanswer);
                     $nbchunks = count($answerstringchunks);
                     $errors["answer[{$key}]"] .= '<pre><div class="displayvalidationerrors">';
                     if ($metacharserror) {
                         $illegalcharschunks = splitstring($metacharserror);
                         for ($i = 0; $i < $nbchunks; $i++) {
                             $errors["answer[{$key}]"] .= '<br />' . $answerstringchunks[$i] . '<br />' . $illegalcharschunks[$i];
                         }
                     } elseif ($parenserror) {
                         $illegalcharschunks = splitstring($parenserror);
                         for ($i = 0; $i < $nbchunks; $i++) {
                             $errors["answer[{$key}]"] .= '<br />' . $answerstringchunks[$i] . '<br />' . $illegalcharschunks[$i];
                         }
                     }
                     $errors["answer[{$key}]"] .= '</div></pre>';
                 }
             }
         } else {
             if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) {
                 if ($key === 0) {
                     $errors['answer[0]'] = get_string('answer1mustbegiven', 'qtype_regexp');
                 } else {
                     $errors["answer[{$key}]"] = get_string('answermustbegiven', 'qtype_regexp');
                 }
                 $answercount++;
             }
         }
     }
     return $errors;
 }
 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)
 {
     $errors = parent::validation($data, $files);
     $questiondisplay = qtype_multianswer_extract_question($data['questiontext']);
     //                   echo "<p> questiondisplay ".$data['questiontext']['text']." <pre>";print_r($questiondisplay);echo "</pre></p>";
     if (isset($questiondisplay->options->questions)) {
         $subquestions = fullclone($questiondisplay->options->questions);
         if (count($subquestions)) {
             $sub = 1;
             foreach ($subquestions as $subquestion) {
                 $prefix = 'sub_' . $sub . '_';
                 $answercount = 0;
                 $maxgrade = false;
                 $maxfraction = -1;
                 if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && $this->savedquestiondisplay->options->questions[$sub]->qtype != $questiondisplay->options->questions[$sub]->qtype) {
                     $storemess = " STORED QTYPE " . $question_type_names[$this->savedquestiondisplay->options->questions[$sub]->qtype];
                 }
                 foreach ($subquestion->answer as $key => $answer) {
                     $trimmedanswer = trim($answer);
                     if ($trimmedanswer !== '') {
                         $answercount++;
                         if ($subquestion->qtype == 'numerical' && !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
                             $errors[$prefix . 'answer[' . $key . ']'] = get_string('answermustbenumberorstar', 'qtype_numerical');
                         }
                         if ($subquestion->fraction[$key] == 1) {
                             $maxgrade = true;
                         }
                         if ($subquestion->fraction[$key] > $maxfraction) {
                             $maxfraction = $subquestion->fraction[$key];
                         }
                     }
                 }
                 if ($answercount == 0) {
                     if ($subquestion->qtype == 'multichoice') {
                         $errors[$prefix . 'answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
                     } else {
                         $errors[$prefix . 'answer[0]'] = get_string('notenoughanswers', 'quiz', 1);
                     }
                 }
                 if ($maxgrade == false) {
                     $errors[$prefix . 'fraction[0]'] = get_string('fractionsnomax', 'question');
                 }
                 $sub++;
             }
         } else {
             $errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
         }
     }
     // $question = qtype_multianswer_extract_question($data['questiontext']);
     //  if (isset $question->options->questions
     if (($this->negative_diff > 0 || $this->used_in_quiz && ($this->negative_diff > 0 || $this->negative_diff < 0 || $this->qtype_change)) && $this->confirm == 0) {
         $errors['confirm'] = get_string('confirmsave', 'qtype_multianswer', $this->negative_diff);
     }
     return $errors;
 }
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     if (isset($this->questiondisplay->options->questions)) {
         $subquestions = fullclone($this->questiondisplay->options->questions);
         if (count($subquestions)) {
             $sub = 1;
             foreach ($subquestions as $subquestion) {
                 $prefix = 'sub_' . $sub . '_';
                 $answercount = 0;
                 $maxgrade = false;
                 $maxfraction = -1;
                 foreach ($subquestion->answer as $key => $answer) {
                     $trimmedanswer = trim($answer);
                     if ($trimmedanswer !== '') {
                         $answercount++;
                         if ($subquestion->qtype == 'numerical' && !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
                             $errors[$prefix . 'answer[' . $key . ']'] = get_string('answermustbenumberorstar', 'qtype_numerical');
                         }
                         if ($subquestion->fraction[$key] == 1) {
                             $maxgrade = true;
                         }
                         if ($subquestion->fraction[$key] > $maxfraction) {
                             $maxfraction = $subquestion->fraction[$key];
                         }
                     }
                 }
                 if ($answercount == 0) {
                     if ($subquestion->qtype == 'multichoice') {
                         $errors[$prefix . 'answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
                     } else {
                         $errors[$prefix . 'answer[0]'] = get_string('notenoughanswers', 'quiz', 1);
                     }
                 }
                 if ($maxgrade == false) {
                     $errors[$prefix . 'fraction[0]'] = get_string('fractionsnomax', 'question');
                 }
                 $sub++;
             }
         } else {
             $errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
         }
     }
     return $errors;
 }
 function validation($data, $files)
 {
     global $QTYPES;
     // 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');
         }
     }
     // regular calculated
     foreach ($answers as $key => $answer) {
         //check no of choices
         // the * for everykind of answer not actually implemented
         $trimmedanswer = trim($answer);
         if ($trimmedanswer != '' || $answercount == 0) {
             $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
             if (FALSE !== $eqerror) {
                 $errors['answer[' . $key . ']'] = $eqerror;
             }
         }
         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
         //TODO how should grade checking work here??
         /*if ($answer != '') {
               if ($data['fraction'][$key] > 0) {
                   $totalfraction += $data['fraction'][$key];
               }
               if ($data['fraction'][$key] > $maxfraction) {
                   $maxfraction = $data['fraction'][$key];
               }
           }*/
     }
     //grade checking :
     /// Perform sanity checks on fractional grades
     /*if ( ) {
                     if ($maxfraction != 1) {
                         $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);
                     }
                 }
                 $units = $data['unit'];
                 if (count($units)) {
                     foreach ($units as $key => $unit){
                         if (is_numeric($unit)){
                             $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
                         }
                         $trimmedunit = trim($unit);
                         $trimmedmultiplier = trim($data['multiplier'][$key]);
                         if (!empty($trimmedunit)){
                             if (empty($trimmedmultiplier)){
                                 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
                             }
                             if (!is_numeric($trimmedmultiplier)){
                                 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
                             }
     
                         }
                     }
                 }*/
     $QTYPES['numerical']->validate_numerical_options($data, $errors);
     if ($answercount == 0) {
         $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
     }
     if ($maxgrade == false) {
         $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
     }
     return $errors;
 }
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     //verifying for errors in {=...} in question text;
     $qtext = "";
     $qtextremaining = $data['questiontext'];
     $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
     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']);
     $mandatorydatasets = array();
     foreach ($answers as $key => $answer) {
         $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
     }
     if (count($mandatorydatasets) == 0) {
         foreach ($answers as $key => $answer) {
             $errors['answer[' . $key . ']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
         }
     }
     foreach ($answers as $key => $answer) {
         //check no of choices
         // the * for everykind of answer not actually implemented
         $trimmedanswer = trim($answer);
         if ($trimmedanswer != '' || $answercount == 0) {
             $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
             if (FALSE !== $eqerror) {
                 $errors['answer[' . $key . ']'] = $eqerror;
             }
         }
         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
         //TODO how should grade checking work here??
         /*if ($answer != '') {
               if ($data['fraction'][$key] > 0) {
                   $totalfraction += $data['fraction'][$key];
               }
               if ($data['fraction'][$key] > $maxfraction) {
                   $maxfraction = $data['fraction'][$key];
               }
           }*/
     }
     //grade checking :
     /// Perform sanity checks on fractional grades
     /*if ( ) {
           if ($maxfraction != 1) {
               $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);
           }
       }*/
     $units = $data['unit'];
     if (count($units)) {
         foreach ($units as $key => $unit) {
             if (is_numeric($unit)) {
                 $errors['unit[' . $key . ']'] = get_string('mustnotbenumeric', 'qtype_calculated');
             }
             $trimmedunit = trim($unit);
             $trimmedmultiplier = trim($data['multiplier'][$key]);
             if (!empty($trimmedunit)) {
                 if (empty($trimmedmultiplier)) {
                     $errors['multiplier[' . $key . ']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
                 }
                 if (!is_numeric($trimmedmultiplier)) {
                     $errors['multiplier[' . $key . ']'] = get_string('mustbenumeric', 'qtype_calculated');
                 }
             }
         }
     }
     if ($answercount == 0) {
         $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
     }
     if ($maxgrade == false) {
         $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
     }
     if (isset($data['backtoquiz']) && $this->noofitems == 0) {
         $errors['warning'] = get_string('warning', 'mnet');
     }
     if ($this->outsidelimit) {
         //   if(!isset($errors['warning'])) $errors['warning']=' ';
         $errors['outsidelimits'] = get_string('oneanswertrueansweroutsidelimits', 'qtype_calculated');
     }
     /*Here we use the already done the error analysis so that 
      * we could force all wild cards values display if there is an error in values.
      * as using a , in a number */
     $numbers = $data['number'];
     foreach ($numbers as $key => $number) {
         if (!is_numeric($number)) {
             if (stristr($number, ',')) {
                 $errors['number[' . $key . ']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
             } else {
                 $errors['number[' . $key . ']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
             }
         } else {
             if (stristr($number, 'x')) {
                 $errors['number[' . $key . ']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
             } else {
                 if (is_nan($number)) {
                     $errors['number[' . $key . ']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
                 }
             }
         }
     }
     if ($this->noofitems == 0) {
         $errors['warning'] = get_string('warning', 'mnet');
     }
     return $errors;
 }
Beispiel #29
0
 public function validation($data, $files) {
     $errors = parent::validation($data, $files);
     $errors = $this->validate_answers($data, $errors);
     $errors = $this->validate_numerical_options($data, $errors);
     return $errors;
 }
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $questiontext = $data['questiontext'];
     $choices = $data['choices'];
     //check the whether the slots are valid
     $errorsinquestiontext = $this->validate_slots($questiontext['text'], $choices);
     if ($errorsinquestiontext) {
         $errors['questiontext'] = $errorsinquestiontext;
     }
     foreach ($choices as $key => $choice) {
         $answer = $choice['answer'];
         //check whether the html-tags are allowed tags
         $validtags = $this->get_html_tags($answer);
         if (is_array($validtags)) {
             continue;
         }
         if ($validtags) {
             $errors['choices[' . $key . ']'] = $validtags;
         }
     }
     return $errors;
 }