📄 questiontype.php
字号:
if (isset($datasetdefs[$defid]->items[$numberadded]) && ! $regenerate ){ // echo "<p>Reuse an previously used record".$numberadded."id".$datasetdef->id."</p>"; } else { $datasetitem = new stdClass; $datasetitem->definition = $datasetdef->id ; $datasetitem->itemnumber = $numberadded; if ($this->supports_dataset_item_generation()) { $datasetitem->value = $this->generate_dataset_item($datasetdef->options); } else { $datasetitem->value = ''; } if (!insert_record('question_dataset_items', $datasetitem)) { error("Error: Unable to insert new dataset item"); } } }//for number added }// datasetsdefs end $maxnumber += $numbertoadd ; foreach ($datasetdefs as $key => $newdef) { if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) { $newdef->itemcount = $maxnumber; // Save the new value for options update_record('question_dataset_definitions', $newdef); } } } if (isset($fromform->deletebutton)) { if(isset($fromform->selectdelete)) $newmaxnumber = $maxnumber-$fromform->selectdelete ; else $newmaxnumber = $maxnumber-1 ; if ($newmaxnumber < 0 ) $newmaxnumber = 0 ; foreach ($datasetdefs as $datasetdef) { if ($datasetdef->itemcount == $maxnumber) { $datasetdef->itemcount= $newmaxnumber ; if (!update_record('question_dataset_definitions', $datasetdef)) { error("Error: Unable to update itemcount"); } } } } } function generate_dataset_item($options) { if (!ereg('^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$', $options, $regs)) { // Unknown options... return false; } if ($regs[1] == 'uniform') { $nbr = $regs[2] + ($regs[3]-$regs[2])*mt_rand()/mt_getrandmax(); return sprintf("%.".$regs[4]."f",$nbr); } else if ($regs[1] == 'loguniform') { $log0 = log(abs($regs[2])); // It would have worked the other way to $nbr = exp($log0 + (log(abs($regs[3])) - $log0)*mt_rand()/mt_getrandmax()); return sprintf("%.".$regs[4]."f",$nbr); } else { error("The distribution $regs[1] caused problems"); } return ''; } function comment_header($question) { //$this->get_question_options($question); $strheader = ''; $delimiter = ''; $answers = $question->options->answers; foreach ($answers as $answer) { if (is_string($answer)) { $strheader .= $delimiter.$answer; } else { $strheader .= $delimiter.$answer->answer; } $delimiter = '<br/><br/><br/>'; } return $strheader; } function comment_on_datasetitems($question, $data, $number) { /// Find a default unit: if (!empty($question->id) && $unit = get_record('question_numerical_units', 'question', $question->id, 'multiplier', 1.0)) { $unit = $unit->unit; } else { $unit = ''; } $answers = fullclone($question->options->answers); $stranswers = ''; $strmin = get_string('min', 'quiz'); $strmax = get_string('max', 'quiz'); $errors = ''; $delimiter = ': '; $virtualqtype = $this->get_virtual_qtype(); foreach ($answers as $answer) { $formula = parent::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 = '' ; }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/>'; $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>'; } else { $stranswers .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits'; } $stranswers .='<br/>'; } } return "$stranswers"; } function tolerance_types() { return array('1' => get_string('relative', 'quiz'), '2' => get_string('nominal', 'quiz'), '3' => get_string('geometric', 'quiz')); } function dataset_options($form, $name, $mandatory=true,$renameabledatasets=false) { // Takes datasets from the parent implementation but // filters options that are currently not accepted by calculated // It also determines a default selection... //$renameabledatasets not implemented anmywhere list($options, $selected) = parent::dataset_options($form, $name,'','qtype_calculated'); // list($options, $selected) = $this->dataset_optionsa($form, $name); foreach ($options as $key => $whatever) { if (!ereg('^'.LITERAL.'-', $key) && $key != '0') { unset($options[$key]); } } if (!$selected) { if ($mandatory){ $selected = LITERAL . "-0-$name"; // Default }else { $selected = "0"; // Default } } return array($options, $selected); } function construct_dataset_menus($form, $mandatorydatasets, $optionaldatasets) { $datasetmenus = array(); foreach ($mandatorydatasets as $datasetname) { if (!isset($datasetmenus[$datasetname])) { list($options, $selected) = $this->dataset_options($form, $datasetname); unset($options['0']); // Mandatory... $datasetmenus[$datasetname] = choose_from_menu ($options, 'dataset[]', $selected, '', '', "0", true); } } foreach ($optionaldatasets as $datasetname) { if (!isset($datasetmenus[$datasetname])) { list($options, $selected) = $this->dataset_options($form, $datasetname); $datasetmenus[$datasetname] = choose_from_menu ($options, 'dataset[]', $selected, '', '', "0", true); } } return $datasetmenus; } function print_question_grading_details(&$question, &$state, &$cmoptions, &$options) { $virtualqtype = $this->get_virtual_qtype(); $virtualqtype->print_question_grading_details($question, $state, $cmoptions, $options) ; } function get_correct_responses(&$question, &$state) { $virtualqtype = $this->get_virtual_qtype(); if($unit = $virtualqtype->get_default_numerical_unit($question)){ $unit = $unit->unit; } else { $unit = ''; } foreach ($question->options->answers as $answer) { if (((int) $answer->fraction) === 1) { $answernumerical = qtype_calculated_calculate_answer( $answer->answer, $state->options->dataset, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, $unit); return array('' => $answernumerical->answer); } } return null; } function substitute_variables($str, $dataset) { $formula = parent::substitute_variables($str, $dataset); if ($error = qtype_calculated_find_formula_errors($formula)) { return $error; } /// Calculate the correct answer if (empty($formula)) { $str = ''; } else if ($formula === '*'){ $str = '*'; } else { eval('$str = '.$formula.';'); } return $str; } /** * This function retrieve the item count of the available category shareable * wild cards that is added as a comment displayed when a wild card with * the same name is displayed in datasetdefinitions_form.php */ function get_dataset_definitions_category($form) { global $CFG; $datasetdefs = array(); $lnamemax = 30; if (!empty($form->category)) { $sql = "SELECT i.*,d.* FROM {$CFG->prefix}question_datasets d, {$CFG->prefix}question_dataset_definitions i WHERE i.id = d.datasetdefinition AND i.category = '$form->category' ; "; if ($records = get_records_sql($sql)) { foreach ($records as $r) { if ( !isset ($datasetdefs["$r->name"])) $datasetdefs["$r->name"] = $r->itemcount; } } } return $datasetdefs ; } /** * This function build a table showing the available category shareable * wild cards, their name, their definition (Min, Max, Decimal) , the item count * and the name of the question where they are used. * This table is intended to be add before the question text to help the user use * these wild cards */ function print_dataset_definitions_category($form) { global $CFG; $datasetdefs = array(); $lnamemax = 22; $namestr =get_string('name', 'quiz'); $minstr=get_string('min', 'quiz'); $maxstr=get_string('max', 'quiz'); $rangeofvaluestr=get_string('minmax','qtype_datasetdependent'); $questionusingstr = get_string('usedinquestion','qtype_calculated'); $itemscountstr = get_string('itemscount','qtype_datasetdependent'); $text =''; if (!empty($form->category)) { list($category) = explode(',', $form->category); $sql = "SELECT i.*,d.* FROM {$CFG->prefix}question_datasets d, {$CFG->prefix}question_dataset_definitions i WHERE i.id = d.datasetdefinition AND i.category = $category; " ; if ($records = get_records_sql($sql)) { foreach ($records as $r) { $sql1 = "SELECT q.* FROM {$CFG->prefix}question q WHERE q.id = $r->question "; if ( !isset ($datasetdefs["$r->type-$r->category-$r->name"])){ $datasetdefs["$r->type-$r->category-$r->name"]= $r; } if ($questionb = get_records_sql($sql1)) { $datasetdefs["$r->type-$r->category-$r->name"]->questions[$r->question]->name =$questionb[$r->question]->name ; } } } } if (!empty ($datasetdefs)){ $text ="<table width=\"100%\" border=\"1\"><tr><th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\" >$namestr</th><th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$rangeofvaluestr</th><th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$itemscountstr</th><th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$questionusingstr</th></tr>"; foreach ($datasetdefs as $datasetdef){ list($distribution, $min, $max,$dec) = explode(':', $datasetdef->options, 4); $text .="<tr><td valign=\"top\" align=\"center\"> $datasetdef->name </td><td align=\"center\" valign=\"top\"> $min <strong>-</strong> $max </td><td align=\"right\" valign=\"top\">$datasetdef->itemcount </td><td align=\"left\">"; foreach ($datasetdef->questions as $qu) { //limit the name length displayed if (!empty($qu->name)) { $qu->name = (strlen($qu->name) > $lnamemax) ? substr($qu->name, 0, $lnamemax).'...' : $qu->name; } else { $qu->name = ''; } $text .=" $qu->name <br/>"; } $text .="</td></tr>"; } $text .="</table>"; }else{ $text .=get_string('nosharedwildcard', 'qtype_calculated'); } return $text ; }/// BACKUP FUNCTIONS //////////////////////////// /* * Backup the data in the question * * This is used in question/backuplib.php */ function backup($bf,$preferences,$question,$level=6) { $status = true; $calculateds = get_records("question_calculated","question",$question,"id"); //If there are calculated-s if ($calculateds) { //Iterate over each calculateds foreach ($calculateds as $calculated) { $status = $status &&fwrite ($bf,start_tag("CALCULATED",$level,true)); //Print calculated contents
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -