public $regenerate;
public $noofitems;
+
+ public $outsidelimit = false ;
+
+ public $commentanswers = array();
/**
* Add question-type specific form fields.
*
$mform->addElement('static', "divider[$j]", '', '<hr />');
$j++;
}
+ $mform->addElement('header', 'updateanswershdr', get_string('answerstoleranceparam', 'qtype_datasetdependent'));
+ $mform->addElement('submit', 'updateanswers', get_string('updatetolerancesparam', 'qtype_datasetdependent'));
+ $mform->setAdvanced('updateanswers',true);
+
+ $answers = fullclone($this->question->options->answers);
+ $key1 =1;
+ foreach ($answers as $key => $answer) {
+ if ('' === $answer->answer){
+ }else if ('*' === $answer->answer){
+ $mform->addElement('static', 'answercomment['.($this->noofitems+$key1).']', $answer->answer);
+ $mform->addElement('hidden', 'tolerance['.$key.']', '');
+ $mform->setAdvanced('tolerance['.$key.']',true);
+ $mform->addElement('hidden', 'tolerancetype['.$key.']', '');
+ $mform->setAdvanced('tolerancetype['.$key.']',true);
+ $mform->addElement('hidden', 'correctanswerlength['.$key.']', '');
+ $mform->setAdvanced('correctanswerlength['.$key.']',true);
+ $mform->addElement('hidden', 'correctanswerformat['.$key.']', '');
+ $mform->setAdvanced('correctanswerformat['.$key.']',true);
+ }else {
+ $mform->addElement('static', 'answercomment['.($this->noofitems+$key1).']', $answer->answer);
+ $mform->addElement('text', 'tolerance['.$key.']', get_string('tolerance', 'qtype_calculated'));
+ $mform->setAdvanced('tolerance['.$key.']',true);
+ $mform->addElement('select', 'tolerancetype['.$key.']', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
+ $mform->setAdvanced('tolerancetype['.$key.']',true);
+
+ $mform->addElement('select', 'correctanswerlength['.$key.']', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
+ $mform->setAdvanced('correctanswerlength['.$key.']',true);
+
+ $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
+ $mform->addElement('select', 'correctanswerformat['.$key.']', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
+ $mform->setAdvanced('correctanswerformat['.$key.']',true);
+ $mform->addElement('static', 'dividertolerance', '', '<hr />');
+ $mform->setAdvanced('dividertolerance',true);
+ }
+ $key1++;
+ }
- if ('' != $strquestionlabel){
- $mform->addElement('static', 'answercomment['.($this->noofitems+1).']', $strquestionlabel);
- }
$addremoveoptions = Array();
$addremoveoptions['1']='1';
for ($i=10; $i<=100 ; $i+=10){
$addgrp[] =& $mform->createElement('select', "selectadd", get_string('additem', 'qtype_datasetdependent'), $addremoveoptions);
$addgrp[] = & $mform->createElement('static',"stat","Items",get_string('item(s)', 'qtype_datasetdependent'));
$mform->addGroup($addgrp, 'addgrp', '', ' ', false);
- $mform->addElement('static', "divideradd", '', '');
+ $mform->addElement('static', "divideradd", '', '');
// $mform->closeHeaderBefore('divideradd');
if ($this->noofitems > 0) {
$mform->addElement('header', 'additemhdr', get_string('delete', 'moodle'));
$repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
}
}
-
+ // if ($this->outsidelimit){
+ $mform->addElement('static','outsidelimit','','');
+ // }
//------------------------------------------------------------------------------------------------------------------------------
//non standard name for button element needed so not using add_action_buttons
+ if ( !($this->noofitems==0) ){
$mform->addElement('submit', 'backtoquiz', get_string('savechanges'));
$mform->closeHeaderBefore('backtoquiz');
-
+ }
//hidden elements
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
function set_data($question){
$formdata = array();
-
+ if (isset($question->options)){
+ $answers = $question->options->answers;
+ if (count($answers)) {
+ // $key = 0;
+ // echo"<p> set data answers outside <pre>"; print_r($answers);echo"</pre></p>";
+ foreach ($answers as $key => $answer){
+ // $default_values['answers['.$key.']'] = $answer->answer;
+ // $default_values['fraction['.$key.']'] = $answer->fraction;
+ // echo"<p> set data answers $key <pre>"; print_r($answer);echo"</pre></p>";
+
+ $formdata['tolerance['.$key.']'] = $answer->tolerance;
+ $formdata['tolerancetype['.$key.']'] = $answer->tolerancetype;
+ $formdata['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
+ $formdata['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
+ // $default_values['feedback['.$key.']'] = $answer->feedback;
+ // $key++;
+ }
+ }
+}
//fill out all data sets and also the fields for the next item to add.
$j = $this->noofitems * count($this->datasetdefs);
for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--){
}
$j--;
}
- $formdata['answercomment['.$itemnumber.']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, $itemnumber);
+ $comment = $this->qtypeobj->comment_on_datasetitems($question->id,$question->options->answers, $data, $itemnumber);
+ // echo"<p> comment outside <pre>"; print_r($comment);echo"</pre></p>";
+ if ($comment->outsidelimit) {
+ $this->outsidelimit=$comment->outsidelimit ;
+ // echo"<p> comment outside $comment->outsidelimit</p>";
+ }
+ $totalcomment='';
+ foreach ($question->options->answers as $key => $answer) {
+ $totalcomment .= $comment->stranswers[$key].'<br/>';
+ }
+
+ $formdata['answercomment['.$itemnumber.']'] = $totalcomment ;
}
$formdata['nextpageparam[forceregeneration]'] = $this->regenerate;
}
//default answercomment will get ignored if answer element is not in the form.
- $formdata['answercomment['.($this->noofitems+1).']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, ($this->noofitems+1));
+ $comment = $this->qtypeobj->comment_on_datasetitems($question->id,$question->options->answers, $data, ($this->noofitems+1));
+ if ($comment->outsidelimit) {
+ $this->outsidelimit=$comment->outsidelimit ;
+ // echo"<p> comment outside $comment->outsidelimit</p>";
+ }
+ $key1 = 1;
+ foreach ($question->options->answers as $key => $answer) {
+ $formdata['answercomment['.($this->noofitems+$key1).']'] = $comment->stranswers[$key];
+ $key1++;
+ }
+ if ($this->outsidelimit){
+ $formdata['outsidelimit']= '<span class="error">'.get_string('oneanswertrueansweroutsidelimits', 'qtype_datasetdependent').'</span>';
+ }
$formdata = $this->qtypeobj->custom_generator_set_data($this->datasetdefs, $formdata);
parent::set_data((object)($formdata + (array)$question));
function validation($data, $files) {
$errors = array();
- if (isset($data['backtoquiz']) && ($this->noofitems==0)){
+ 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');
}
return $errors;
}
break;
case 'datasetitems':
$this->save_dataset_items($question, $form);
+ $this->save_question_calculated($question, $form);
break;
default:
print_error('invalidwizardpage', 'question');
return $datasetdefs;
}
+ function save_question_calculated($question, $fromform){
+ global $DB;
+
+ foreach ($question->options->answers as $key => $answer) {
+ if ($options = $DB->get_record('question_calculated', array('answer' => $key))) {
+ $options->tolerance = trim($fromform->tolerance[$key]);
+ $options->tolerancetype = trim($fromform->tolerancetype[$key]);
+ $options->correctanswerlength = trim($fromform->correctanswerlength[$key]);
+ $options->correctanswerformat = trim($fromform->correctanswerformat[$key]);
+ if (! $DB->update_record('question_calculated', $options)) {
+ $mess->name = $this->name();
+ $mess->id = $options->id ;
+ notify ( get_string('errorupdatingoptions','question',$mess));
+ }
+ }
+ }
+ }
+
+
function save_dataset_items($question, $fromform){
global $CFG, $DB;
// max datasets = 100 items
$i++;
}
- if ($maxnumber < $addeditem->itemnumber){
+ if (isset($addeditem->itemnumber) && $maxnumber < $addeditem->itemnumber){
$maxnumber = $addeditem->itemnumber;
foreach ($datasetdefs as $key => $newdef) {
if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) {
function comment_header($question) {
//$this->get_question_options($question);
- $strheader = '';
+ $strheader = array();
$delimiter = '';
$answers = $question->options->answers;
- foreach ($answers as $answer) {
+ foreach ($answers as $key => $answer) {
if (is_string($answer)) {
$strheader .= $delimiter.$answer;
} else {
return $strheader;
}
- function comment_on_datasetitems($question, $data, $number) {
+ function comment_on_datasetitems($questionid, $answers,$data, $number) {
global $DB;
+ $comment = new stdClass;
+ $comment->stranswers = array();
+ $comment->outsidelimit = false ;
+ $comment->answers = array();
/// Find a default unit:
- if (!empty($question->id) && $unit = $DB->get_record('question_numerical_units', array('question'=> $question->id, 'multiplier' => 1.0))) {
+ if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units', array('question'=> $questionid, 'multiplier' => 1.0))) {
$unit = $unit->unit;
} else {
$unit = '';
}
- $answers = fullclone($question->options->answers);
- $stranswers = '';
+ $answers = fullclone($answers);
$strmin = get_string('min', 'quiz');
$strmax = get_string('max', 'quiz');
$errors = '';
$delimiter = ': ';
$virtualqtype = $this->get_virtual_qtype();
- foreach ($answers as $answer) {
- // $formula = $answer->answer;
+ foreach ($answers as $key => $answer) {
$formula = $this->substitute_variables($answer->answer,$data);
- /* foreach ($data as $name => $value) {
- $formula = str_replace('{'.$name.'}', $value, $formula);
- }*/
$formattedanswer = qtype_calculated_calculate_answer(
$answer->answer, $data, $answer->tolerance,
$answer->tolerancetype, $answer->correctanswerlength,
$answer->correctanswerformat, $unit);
if ( $formula === '*'){
- $answer->min = '' ;
+ $answer->min = ' ';
+ $formattedanswer->answer = $answer->answer ;
}else {
eval('$answer->answer = '.$formula.';') ;
$virtualqtype->get_tolerance_interval($answer);
}
if ($answer->min === '') {
// This should mean that something is wrong
- $stranswers .= " -$formattedanswer->answer".'<br/><br/>';
- } else {
- $stranswers .= $formula.' = '.$formattedanswer->answer.'<br/>' ;
- $stranswers .= $strmin. $delimiter.$answer->min.'---';
- $stranswers .= $strmax.$delimiter.$answer->max;
- $stranswers .='<br/>';
+ $comment->stranswers[$key] = " $formattedanswer->answer".'<br/><br/>';
+ } else if ($formula === '*'){
+ $comment->stranswers[$key] = $formula.' = '.get_string('anyvalue','qtype_calculated').'<br/><br/><br/>';
+ }else{
+ $comment->stranswers[$key]= $formula.' = '.$formattedanswer->answer.'<br/>' ;
+ $comment->stranswers[$key] .= $strmin. $delimiter.$answer->min.'---';
+ $comment->stranswers[$key] .= $strmax.$delimiter.$answer->max;
+ $comment->stranswers[$key] .='<br/>';
$correcttrue->correct = $formattedanswer->answer ;
$correcttrue->true = $answer->answer ;
if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max){
- $stranswers .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
+ $comment->outsidelimit = true ;
+ $comment->answers[$key] = $key;
+ $comment->stranswers[$key] .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
}else {
- $stranswers .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
+ $comment->stranswers[$key] .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
}
- $stranswers .='<br/>';
+ $comment->stranswers[$key] .='';
}
}
- return "$stranswers";
+ return fullclone($comment);
}
function tolerance_types() {