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);
     }
     //     echo "numericalquestion qtextremaining <pre>";print_r($possibledatasets);
     while (ereg('\\{=([^[: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) {
         //  $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
         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');
     }
     return $errors;
 }
Exemplo n.º 2
0
 function check_file_access($question, $state, $options, $contextid, $component, $filearea, $args)
 {
     $itemid = reset($args);
     if ($component == 'question' && $filearea == 'answerfeedback') {
         // check if answer id exists
         $result = $options->feedback && array_key_exists($itemid, $question->options->answers);
         if (!$result) {
             return false;
         }
         // check response
         if (!$this->check_response($question, $state)) {
             return false;
         }
         return true;
     } else {
         if ($filearea == 'instruction') {
             // TODO: should it be display all the time like questiontext?
             // check if question id exists
             if ($itemid != $question->id) {
                 return false;
             } else {
                 return true;
             }
         } else {
             return parent::check_file_access($question, $state, $options, $contextid, $component, $filearea, $args);
         }
     }
     return true;
 }
 /**
  * Runs all the code required to set up and save an essay question for testing purposes.
  * Alternate DB table prefix may be used to facilitate data deletion.
  */
 function generate_test($name, $courseid = null)
 {
     list($form, $question) = parent::generate_test($name, $courseid);
     $form->feedback = 1;
     $form->multiplier = array(1, 1);
     $form->shuffleanswers = 1;
     $form->noanswers = 1;
     $form->qtype = 'calculated';
     #@TODO.N. Do we change this line? (Change next ones!)
     $question->qtype = 'calculatedobjects';
     $form->answers = array('{apples} + {oranges}');
     $form->fraction = array(1);
     $form->tolerance = array(0.01);
     $form->tolerancetype = array(1);
     $form->correctanswerlength = array(2);
     $form->correctanswerformat = array(1);
     $form->questiontext = "What is {apples} + {oranges}?";
     if ($courseid) {
         $course = get_record('course', 'id', $courseid);
     }
     $new_question = $this->save_question($question, $form, $course);
     $dataset_form = new stdClass();
     $dataset_form->nextpageparam["forceregeneration"] = 1;
     $dataset_form->calcmin = array(1 => 1.0, 2 => 1.0);
     $dataset_form->calcmax = array(1 => 10.0, 2 => 10.0);
     $dataset_form->calclength = array(1 => 1, 2 => 1);
     $dataset_form->number = array(1 => 5.4, 2 => 4.9);
     $dataset_form->itemid = array(1 => '', 2 => '');
     $dataset_form->calcdistribution = array(1 => 'uniform', 2 => 'uniform');
     $dataset_form->definition = array(1 => "1-0-a", 2 => "1-0-b");
     $dataset_form->nextpageparam = array('forceregeneration' => false);
     $dataset_form->addbutton = 1;
     $dataset_form->selectadd = 1;
     $dataset_form->courseid = $courseid;
     $dataset_form->cmid = 0;
     $dataset_form->id = $new_question->id;
     $this->save_dataset_items($new_question, $dataset_form);
     return $new_question;
 }
Exemplo n.º 4
0
 function check_file_access($question, $state, $options, $contextid, $component, $filearea, $args)
 {
     $itemid = reset($args);
     if (empty($question->maxgrade)) {
         $question->maxgrade = $question->defaultgrade;
     }
     if (in_array($filearea, array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
         $result = $options->feedback && $itemid == $question->id;
         if (!$result) {
             return false;
         }
         if ($state->raw_grade >= $question->maxgrade / 1.01) {
             $feedbacktype = 'correctfeedback';
         } else {
             if ($state->raw_grade > 0) {
                 $feedbacktype = 'partiallycorrectfeedback';
             } else {
                 $feedbacktype = 'incorrectfeedback';
             }
         }
         if ($feedbacktype != $filearea) {
             return false;
         }
         return true;
     } else {
         if ($component == 'question' && $filearea == 'answerfeedback') {
             return $options->feedback && array_key_exists($itemid, $question->options->answers);
         } else {
             return parent::check_file_access($question, $state, $options, $contextid, $component, $filearea, $args);
         }
     }
 }