📄 questiontype.php
字号:
<?php // $Id: questiontype.php,v 1.26.2.14 2009/02/18 18:04:12 pichetp Exp $/////////////////// CALCULATED /////////////////////// QUESTION TYPE CLASS //////////////////require_once("$CFG->dirroot/question/type/datasetdependent/abstractqtype.php");class question_calculated_qtype extends question_dataset_dependent_questiontype { // Used by the function custom_generator_tools: var $calcgenerateidhasbeenadded = false; function name() { return 'calculated'; } function get_question_options(&$question) { // First get the datasets and default options global $CFG; if (!$question->options->answers = get_records_sql( "SELECT a.*, c.tolerance, c.tolerancetype, c.correctanswerlength, c.correctanswerformat " . "FROM {$CFG->prefix}question_answers a, " . " {$CFG->prefix}question_calculated c " . "WHERE a.question = $question->id " . "AND a.id = c.answer ". "ORDER BY a.id ASC")) { notify('Error: Missing question answer for calculated question ' . $question->id . '!'); return false; }/* if(false === parent::get_question_options($question)) { return false; } if (!$options = get_records('question_calculated', 'question', $question->id)) { notify("No options were found for calculated question #{$question->id}! Proceeding with defaults."); // $options = new Array(); $options= new stdClass; $options->tolerance = 0.01; $options->tolerancetype = 1; // relative $options->correctanswerlength = 2; $options->correctanswerformat = 1; // decimals } // For historic reasons we also need these fields in the answer objects. // This should eventually be removed and related code changed to use // the values in $question->options instead. foreach ($question->options->answers as $key => $answer) { $answer = &$question->options->answers[$key]; // for PHP 4.x $answer->calcid = $options->id; $answer->tolerance = $options->tolerance; $answer->tolerancetype = $options->tolerancetype; $answer->correctanswerlength = $options->correctanswerlength; $answer->correctanswerformat = $options->correctanswerformat; }*/ $virtualqtype = $this->get_virtual_qtype(); $virtualqtype->get_numerical_units($question); if( isset($question->export_process)&&$question->export_process){ $question->options->datasets = $this->get_datasets_for_export($question); } return true; } function get_datasets_for_export(&$question){ $datasetdefs = array(); if (!empty($question->id)) { global $CFG; $sql = "SELECT i.* FROM {$CFG->prefix}question_datasets d, {$CFG->prefix}question_dataset_definitions i WHERE d.question = '$question->id' AND d.datasetdefinition = i.id "; if ($records = get_records_sql($sql)) { foreach ($records as $r) { $def = $r ; if ($def->category=='0'){ $def->status='private'; } else { $def->status='shared'; } $def->type ='calculated' ; list($distribution, $min, $max,$dec) = explode(':', $def->options, 4); $def->distribution=$distribution; $def->minimum=$min; $def->maximum=$max; $def->decimals=$dec ; if ($def->itemcount > 0 ) { // get the datasetitems $def->items = array(); $sql1= (" SELECT itemnumber, definition, id, value FROM {$CFG->prefix}question_dataset_items WHERE definition = '$def->id' order by itemnumber ASC "); if ($items = get_records_sql($sql1)){ $n = 0; foreach( $items as $ii){ $n++; $def->items[$n] = new stdClass; $def->items[$n]->itemnumber=$ii->itemnumber; $def->items[$n]->value=$ii->value; } $def->number_of_items=$n ; } } $datasetdefs["1-$r->category-$r->name"] = $def; } } } return $datasetdefs ; } function save_question_options($question) { //$options = $question->subtypeoptions; // Get old answers: global $CFG; if (isset($question->answer) && !isset($question->answers)) { $question->answers = $question->answer; } // Get old versions of the objects if (!$oldanswers = get_records('question_answers', 'question', $question->id, 'id ASC')) { $oldanswers = array(); } if (!$oldoptions = get_records('question_calculated', 'question', $question->id, 'answer ASC')) { $oldoptions = array(); } // Save the units. $virtualqtype = $this->get_virtual_qtype(); $result = $virtualqtype->save_numerical_units($question); if (isset($result->error)) { return $result; } else { $units = &$result->units; } // Insert all the new answers foreach ($question->answers as $key => $dataanswer) { if ( trim($dataanswer) != '' ) { $answer = new stdClass; $answer->question = $question->id; $answer->answer = trim($dataanswer); $answer->fraction = $question->fraction[$key]; $answer->feedback = trim($question->feedback[$key]); if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it $answer->id = $oldanswer->id; if (! update_record("question_answers", $answer)) { $result->error = "Could not update question answer! (id=$answer->id)"; return $result; } } else { // This is a completely new answer if (! $answer->id = insert_record("question_answers", $answer)) { $result->error = "Could not insert question answer!"; return $result; } } // Set up the options object if (!$options = array_shift($oldoptions)) { $options = new stdClass; } $options->question = $question->id; $options->answer = $answer->id; $options->tolerance = trim($question->tolerance[$key]); $options->tolerancetype = trim($question->tolerancetype[$key]); $options->correctanswerlength = trim($question->correctanswerlength[$key]); $options->correctanswerformat = trim($question->correctanswerformat[$key]); // Save options if (isset($options->id)) { // reusing existing record if (! update_record('question_calculated', $options)) { $result->error = "Could not update question calculated options! (id=$options->id)"; return $result; } } else { // new options if (! insert_record('question_calculated', $options)) { $result->error = "Could not insert question calculated options!"; return $result; } } } } // delete old answer records if (!empty($oldanswers)) { foreach($oldanswers as $oa) { delete_records('question_answers', 'id', $oa->id); } } // delete old answer records if (!empty($oldoptions)) { foreach($oldoptions as $oo) { delete_records('question_calculated', 'id', $oo->id); } } if( isset($question->import_process)&&$question->import_process){ $this->import_datasets($question); } // Report any problems. if (!empty($result->notice)) { return $result; } return true; } function import_datasets($question){ $n = count($question->dataset); foreach ($question->dataset as $dataset) { // name, type, option, $datasetdef = new stdClass(); $datasetdef->name = $dataset->name; $datasetdef->type = 1 ; $datasetdef->options = $dataset->distribution.':'.$dataset->min.':'.$dataset->max.':'.$dataset->length; $datasetdef->itemcount=$dataset->itemcount; if ( $dataset->status =='private'){ $datasetdef->category = 0; $todo='create' ; }else if ($dataset->status =='shared' ){ if ($sharedatasetdefs = get_records_select( 'question_dataset_definitions', "type = '1' AND name = '$dataset->name' AND category = '$question->category' ORDER BY id DESC;" )) { // so there is at least one $sharedatasetdef = array_shift($sharedatasetdefs); if ( $sharedatasetdef->options == $datasetdef->options ){// identical so use it $todo='useit' ; $datasetdef =$sharedatasetdef ; } else { // different so create a private one $datasetdef->category = 0; $todo='create' ; } }else { // no so create one $datasetdef->category =$question->category ; $todo='create' ; } } if ( $todo=='create'){ if (!$datasetdef->id = insert_record( 'question_dataset_definitions', $datasetdef)) { error("Unable to create dataset $defid"); } } // Create relation to the dataset: $questiondataset = new stdClass; $questiondataset->question = $question->id; $questiondataset->datasetdefinition = $datasetdef->id; if (!insert_record('question_datasets', $questiondataset)) { error("Unable to create relation to dataset $dataset->name $todo"); } if ($todo=='create'){ // add the items foreach ($dataset->datasetitem as $dataitem ){ $datasetitem = new stdClass; $datasetitem->definition=$datasetdef->id ; $datasetitem->itemnumber = $dataitem->itemnumber ; $datasetitem->value = $dataitem->value ; if (!insert_record('question_dataset_items', $datasetitem)) { error("Unable to insert dataset item $item->itemnumber with $item->value for $datasetdef->name"); } } } } } function create_runtime_question($question, $form) { $question = parent::create_runtime_question($question, $form); $question->options->answers = array(); foreach ($form->answers as $key => $answer) { $a->answer = trim($form->answer[$key]); $a->fraction = $form->fraction[$key];//new $a->tolerance = $form->tolerance[$key]; $a->tolerancetype = $form->tolerancetype[$key]; $a->correctanswerlength = $form->correctanswerlength[$key]; $a->correctanswerformat = $form->correctanswerformat[$key]; $question->options->answers[] = clone($a); } return $question; } function validate_form($form) { switch($form->wizardpage) { case 'question': $calculatedmessages = array(); if (empty($form->name)) { $calculatedmessages[] = get_string('missingname', 'quiz'); } if (empty($form->questiontext)) { $calculatedmessages[] = get_string('missingquestiontext', 'quiz'); } // Verify formulas foreach ($form->answers as $key => $answer) { if ('' === trim($answer)) { $calculatedmessages[] = get_string('missingformula', 'quiz'); } if ($formulaerrors = qtype_calculated_find_formula_errors($answer)) { $calculatedmessages[] = $formulaerrors; } if (! isset($form->tolerance[$key])) { $form->tolerance[$key] = 0.0; } if (! is_numeric($form->tolerance[$key])) { $calculatedmessages[] = get_string('tolerancemustbenumeric', 'quiz'); } } if (!empty($calculatedmessages)) { $errorstring = "The following errors were found:<br />"; foreach ($calculatedmessages as $msg) { $errorstring .= $msg . '<br />'; } error($errorstring); } break; default: return parent::validate_form($form); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -