Skip to content

Commit

Permalink
MDL-17366 intergating changes done in calculated, new common function…
Browse files Browse the repository at this point in the history
…s and new lang strings
  • Loading branch information
ppichet committed May 9, 2010
1 parent e25a358 commit 2fa8350
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 78 deletions.
34 changes: 18 additions & 16 deletions question/type/calculatedsimple/edit_calculatedsimple_form.php
Expand Up @@ -68,10 +68,12 @@ function question_edit_calculatedsimple_form(&$submiturl, &$question, &$category
if(!$this->reload ){ // use database data as this is first pass
// question->id == 0 so no stored datasets
// else get datasets
// echo "<p>question <pre>";print_r($question);echo "</pre></p>";
if ( !empty($question->id)) {
if (empty($question->options)) {

/* if (empty($question->options)) {
$this->get_question_options($question);
}
}*/
$this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, array());

if(!empty($this->datasetdefs)){
Expand Down Expand Up @@ -285,7 +287,7 @@ function definition_inner(&$mform) {
global $QTYPES;
$this->qtypeobj =& $QTYPES[$this->qtype()];
$strquestionlabel = $this->qtypeobj->comment_header($this->nonemptyanswer);
$label = get_string("sharedwildcards", "qtype_datasetdependent");
$label = get_string("sharedwildcards", "qtype_calculated");
$mform->addElement('hidden', 'synchronize', 0);
$mform->addElement('hidden', 'initialcategory', 1);
$mform->setType('initialcategory', PARAM_INT);
Expand Down Expand Up @@ -331,7 +333,7 @@ function definition_inner(&$mform) {
if(!empty($this->datasetdefs)){// unnecessary test
$j = (($this->noofitems) * count($this->datasetdefs))+1;//
foreach ($this->datasetdefs as $defkey => $datasetdef){
$mform->addElement('static', "na[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name));
$mform->addElement('static', "na[$j]", get_string('param', 'qtype_calculated', $datasetdef->name));
$this->qtypeobj->custom_generator_tools_part($mform, $idx, $j);
$mform->addElement('hidden', "datasetdef[$idx]");
$mform->setType("datasetdef[$idx]", PARAM_RAW);
Expand Down Expand Up @@ -369,25 +371,25 @@ function definition_inner(&$mform) {
$a->name = '{'.$datasetdef->name.'}' ;
$a->value = $datasetdef->items[$itemnumber]->value ;
if (stristr($number,',')){
$this->numbererrors["number[$j]"]=get_string('nocommaallowed', 'qtype_datasetdependent');
$this->numbererrors["number[$j]"]=get_string('nocommaallowed', 'qtype_calculated');
$numbererrors .= $this->numbererrors['number['.$j.']']."<br />";

}else {
$this->numbererrors["number[$j]"]= get_string('notvalidnumber','qtype_datasetdependent',$a);
$this->numbererrors["number[$j]"]= get_string('notvalidnumber','qtype_calculated',$a);
$numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
//$comment->outsidelimit = false ;
}
}else if( stristr($number,'x')){ // hexa will pass the test
$a = new stdClass;
$a->name = '{'.$datasetdef->name.'}' ;
$a->value = $datasetdef->items[$itemnumber]->value ;
$this->numbererrors['number['.$j.']']= get_string('hexanotallowed','qtype_datasetdependent',$a);
$this->numbererrors['number['.$j.']']= get_string('hexanotallowed','qtype_calculated',$a);
$numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
} else if( is_nan($number)){
$a = new stdClass;
$a->name = '{'.$datasetdef->name.'}' ;
$a->value = $datasetdef->items[$itemnumber]->value ;
$this->numbererrors["number[$j]"]= get_string('notvalidnumber','qtype_datasetdependent',$a);
$this->numbererrors["number[$j]"]= get_string('notvalidnumber','qtype_calculated',$a);
$numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
// $val = 1.0 ;
}
Expand All @@ -396,8 +398,8 @@ function definition_inner(&$mform) {
}
if($this->noofitems != 0 ) {
if (empty($numbererrors )){
if(!isset($question->id)) $question->id = 0 ;
$comment = $this->qtypeobj->comment_on_datasetitems($question->id,$question->questiontext,$this->nonemptyanswer, $data, $itemnumber);//$this->
if(!isset($this->question->id)) $this->question->id = 0 ;
$comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj,$this->question->id,$this->question->questiontext,$this->nonemptyanswer, $data, $itemnumber);//$this->
if ($comment->outsidelimit) {
$this->outsidelimit=$comment->outsidelimit ;
}
Expand Down Expand Up @@ -458,7 +460,7 @@ function definition_inner(&$mform) {
}else {
$mform->addElement('header', 'additemhdr1', get_string('wildcardvalues', 'qtype_calculatedsimple'));
$mform->closeHeaderBefore('additemhdr1');
// $mform->addElement('header', '', get_string('itemno', 'qtype_datasetdependent', ""));
// $mform->addElement('header', '', get_string('itemno', 'qtype_calculated', ""));
if( !empty($this->numbererrors) || $this->outsidelimit) {
$mform->addElement('static', "alert", '', '<span class="error">'.get_string('useadvance', 'qtype_calculatedsimple').'</span>');
}
Expand Down Expand Up @@ -680,7 +682,7 @@ function validation($data, $files) {
}
if ( count($mandatorydatasets )==0){
foreach ($answers as $key => $answer){
$errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
$errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_calculated');
}
}
foreach ($answers as $key => $answer){
Expand Down Expand Up @@ -773,14 +775,14 @@ function validation($data, $files) {
foreach ($numbers as $key => $number){
if(! is_numeric($number)){
if (stristr($number,',')){
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_calculated');
}else {
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_calculated');
}
}else if( stristr($number,'x')){
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_calculated');
} else if( is_nan($number)){
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_datasetdependent');
$errors['number['.$key.']'] = get_string('notvalidnumber', 'qtype_calculated');
}
}
*/
Expand Down
11 changes: 11 additions & 0 deletions question/type/calculatedsimple/lang/en/qtype_calculatedsimple.php
Expand Up @@ -26,6 +26,17 @@
$string['addingcalculatedsimple'] = 'Adding a Simple Calculated question';
$string['atleastonewildcard'] = 'There must be at least one wild card <strong>{x..}</strong> present in the correct answer formulas';
$string['calculatedsimple'] = 'Calculated Simple';
$string['calculatedsimple_help'] = '<p>Calculatedsimple questions are similar to calculated questions but can be edited in a single page process, calculated needing a three pages. These question types offers a way to create individual numerical question by the use of wildcards {a},{b}that are substituted with individual values when the quiz is taken.</p>
<p>The answers are the result of formulas that can use accepted operators as +-*/ and % where % is the modulo operator.
It is also possible to use some PHP-style mathematical function.</p>
<p>Among these there are 24 single-argument function:<b>
abs, acos, acosh, asin, asinh, atan, atanh, ceil, cos, cosh, deg2rad, exp, expm1, floor, log, log10, log1p, rad2deg, round, sin, sinh, sqrt, tan, tanh
</b>and two two-argument functions<b>
atan2, pow
</b>and the functions <b>min</b> and <b>max</b> that can take two or more arguments.</p>
<p>It is also possible to use the function <b>pi()</b></p>
<p>Possible usage is for example <b>sin({a}) + cos({b}) * 2</b>.</p>
';
$string['calculatedsimplesummary'] = 'A simpler version of calculated questions which are like numerical questions but with the numbers used selected randomly from a set when the quiz is taken.';
$string['converttocalculated'] = 'Save as a new regular calculated question';
$string['editingcalculatedsimple'] = 'Editing a Simple Calculated question';
Expand Down
67 changes: 5 additions & 62 deletions question/type/calculatedsimple/questiontype.php
Expand Up @@ -230,16 +230,16 @@ function response_summary($question, $state, $length=80, $formatting=true) {
function custom_generator_tools_part(&$mform, $idx, $j){

$minmaxgrp = array();
$minmaxgrp[] =& $mform->createElement('text', "calcmin[$idx]", get_string('calcmin', 'qtype_datasetdependent'));
$minmaxgrp[] =& $mform->createElement('text', "calcmax[$idx]", get_string('calcmax', 'qtype_datasetdependent'));
$mform->addGroup($minmaxgrp, 'minmaxgrp', get_string('minmax', 'qtype_datasetdependent'), ' - ', false);
$minmaxgrp[] =& $mform->createElement('text', "calcmin[$idx]", get_string('calcmin', 'qtype_calculated'));
$minmaxgrp[] =& $mform->createElement('text', "calcmax[$idx]", get_string('calcmax', 'qtype_calculated'));
$mform->addGroup($minmaxgrp, 'minmaxgrp', get_string('minmax', 'qtype_calculated'), ' - ', false);
$mform->setType("calcmin[$idx]", PARAM_NUMBER);
$mform->setType("calcmax[$idx]", PARAM_NUMBER);

$precisionoptions = range(0, 10);
$mform->addElement('select', "calclength[$idx]", get_string('calclength', 'qtype_datasetdependent'), $precisionoptions);
$mform->addElement('select', "calclength[$idx]", get_string('calclength', 'qtype_calculated'), $precisionoptions);

$distriboptions = array('uniform' => get_string('uniform', 'qtype_datasetdependent'), 'loguniform' => get_string('loguniform', 'qtype_datasetdependent'));
$distriboptions = array('uniform' => get_string('uniform', 'qtype_calculated'), 'loguniform' => get_string('loguniform', 'qtype_calculated'));
$mform->addElement('hidden', "calcdistribution[$idx]", 'uniform');
$mform->setType("calcdistribution[$idx]", PARAM_INT);

Expand All @@ -264,63 +264,6 @@ function comment_header($answers) {
return $strheader;
}

function comment_on_datasetitems($questionid, $answers,$data, $number) {
global $DB,$QTYPES;
$comment = new stdClass;
$comment->stranswers = array();
$comment->outsidelimit = false ;
$comment->answers = array();
/// Find a default unit:
if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units', array('question'=> $questionid, 'multiplier' => 1.0))) {
$unit = $unit->unit;
} else {
$unit = '';
}

$answers = fullclone($answers);
$strmin = get_string('min', 'quiz');
$strmax = get_string('max', 'quiz');
$errors = '';
$delimiter = ': ';
$virtualqtype = & $QTYPES['numerical'] ; //$this->get_virtual_qtype($question);
foreach ($answers as $key => $answer) {
$formula = $this->substitute_variables($answer->answer,$data);
$formattedanswer = qtype_calculated_calculate_answer(
$answer->answer, $data, $answer->tolerance,
$answer->tolerancetype, $answer->correctanswerlength,
$answer->correctanswerformat, $unit);
if ( $formula === '*'){
$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
$comment->stranswers[$key] = " $formattedanswer->answer".'<br/><br/>';
} else if ($formula === '*'){
$comment->stranswers[$key] = $formula.' = '.get_string('anyvalue','qtype_calculated').'<br/><br/>';
}else{
$comment->stranswers[$key]= $formula.' = '.$formattedanswer->answer.'' ;
$comment->stranswers[$key] .= "<br/>".$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){
$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 {
$comment->stranswers[$key] .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
}
$comment->stranswers[$key] .='';
}
}
return fullclone($comment);
}

function tolerance_types() {
return array('1' => get_string('relative', 'quiz'),
'2' => get_string('nominal', 'quiz'),
Expand Down

0 comments on commit 2fa8350

Please sign in to comment.