📄 questiontype.php
字号:
} return true; } /** * Deletes question from the question-type specific tables * * @return boolean Success/Failure * @param object $question The question being deleted */ function delete_question($questionid) { delete_records("question_calculated", "question", $questionid); delete_records("question_numerical_units", "question", $questionid); if ($datasets = get_records('question_datasets', 'question', $questionid)) { foreach ($datasets as $dataset) { if (! get_records_select( 'question_datasets', "question != $questionid AND datasetdefinition = $dataset->datasetdefinition;")){ delete_records('question_dataset_definitions', 'id', $dataset->datasetdefinition); delete_records('question_dataset_items', 'definition', $dataset->datasetdefinition); } } } delete_records("question_datasets", "question", $questionid); return true; } function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) { // Substitute variables in questiontext before giving the data to the // virtual type for printing $virtualqtype = $this->get_virtual_qtype(); if($unit = $virtualqtype->get_default_numerical_unit($question)){ $unit = $unit->unit; } else { $unit = ''; } // We modify the question to look like a numerical question $numericalquestion = fullclone($question); foreach ($numericalquestion->options->answers as $key => $answer) { $answer = fullclone($numericalquestion->options->answers[$key]); $numericalquestion->options->answers[$key]->answer = $this->substitute_variables($answer->answer, $state->options->dataset); } $numericalquestion->questiontext = parent::substitute_variables( $numericalquestion->questiontext, $state->options->dataset); //evaluate the equations i.e {=5+4) $qtext = ""; $qtextremaining = $numericalquestion->questiontext ; while (ereg('\{=([^[:space:]}]*)}', $qtextremaining, $regs1)) { $qtextsplits = explode($regs1[0], $qtextremaining, 2); $qtext =$qtext.$qtextsplits[0]; $qtextremaining = $qtextsplits[1]; if (empty($regs1[1])) { $str = ''; } else { if( $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])){ $str=$formulaerrors ; }else { eval('$str = '.$regs1[1].';'); } } $qtext = $qtext.$str ; } $numericalquestion->questiontext = $qtext.$qtextremaining ; // end replace equations $virtualqtype->print_question_formulation_and_controls($numericalquestion, $state, $cmoptions, $options); } function grade_responses(&$question, &$state, $cmoptions) { // Forward the grading to the virtual qtype // We modify the question to look like a numerical question $numericalquestion = fullclone($question); foreach ($numericalquestion->options->answers as $key => $answer) { $answer = $numericalquestion->options->answers[$key]->answer; // for PHP 4.x $numericalquestion->options->answers[$key]->answer = $this->substitute_variables($answer, $state->options->dataset); } $virtualqtype = $this->get_virtual_qtype(); return $virtualqtype->grade_responses($numericalquestion, $state, $cmoptions) ; } function response_summary($question, $state, $length=80) { // The actual response is the bit after the hyphen return substr($state->answer, strpos($state->answer, '-')+1, $length); } // ULPGC ecastro function check_response(&$question, &$state) { // Forward the checking to the virtual qtype // We modify the question to look like a numerical question $numericalquestion = clone($question); $numericalquestion->options = clone($question->options); foreach ($question->options->answers as $key => $answer) { $numericalquestion->options->answers[$key] = clone($answer); } foreach ($numericalquestion->options->answers as $key => $answer) { $answer = &$numericalquestion->options->answers[$key]; // for PHP 4.x $answer->answer = $this->substitute_variables($answer->answer, $state->options->dataset); } $virtualqtype = $this->get_virtual_qtype(); return $virtualqtype->check_response($numericalquestion, $state) ; } // ULPGC ecastro function get_actual_response(&$question, &$state) { // Substitute variables in questiontext before giving the data to the // virtual type $virtualqtype = $this->get_virtual_qtype(); $unit = $virtualqtype->get_default_numerical_unit($question); // We modify the question to look like a numerical question $numericalquestion = clone($question); $numericalquestion->options = clone($question->options); foreach ($question->options->answers as $key => $answer) { $numericalquestion->options->answers[$key] = clone($answer); } foreach ($numericalquestion->options->answers as $key => $answer) { $answer = &$numericalquestion->options->answers[$key]; // for PHP 4.x $answer->answer = $this->substitute_variables($answer->answer, $state->options->dataset); // apply_unit } $numericalquestion->questiontext = $this->substitute_variables( $numericalquestion->questiontext, $state->options->dataset); $responses = $virtualqtype->get_all_responses($numericalquestion, $state); $response = reset($responses->responses); $correct = $response->answer.' : '; $responses = $virtualqtype->get_actual_response($numericalquestion, $state); foreach ($responses as $key=>$response){ $responses[$key] = $correct.$response; } return $responses; } function create_virtual_qtype() { global $CFG; require_once("$CFG->dirroot/question/type/numerical/questiontype.php"); return new question_numerical_qtype(); } function supports_dataset_item_generation() { // Calcualted support generation of randomly distributed number data return 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); $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); $distriboptions = array('uniform' => get_string('uniform', 'qtype_datasetdependent'), 'loguniform' => get_string('loguniform', 'qtype_datasetdependent')); $mform->addElement('select', "calcdistribution[$idx]", get_string('calcdistribution', 'qtype_datasetdependent'), $distriboptions); } function custom_generator_set_data($datasetdefs, $formdata){ $idx = 1; foreach ($datasetdefs as $datasetdef){ if (ereg('^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$', $datasetdef->options, $regs)) { $defid = "$datasetdef->type-$datasetdef->category-$datasetdef->name"; $formdata["calcdistribution[$idx]"] = $regs[1]; $formdata["calcmin[$idx]"] = $regs[2]; $formdata["calcmax[$idx]"] = $regs[3]; $formdata["calclength[$idx]"] = $regs[4]; } $idx++; } return $formdata; } function custom_generator_tools($datasetdef) { if (ereg('^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$', $datasetdef->options, $regs)) { $defid = "$datasetdef->type-$datasetdef->category-$datasetdef->name"; for ($i = 0 ; $i<10 ; ++$i) { $lengthoptions[$i] = get_string(($regs[1] == 'uniform' ? 'decimals' : 'significantfigures'), 'quiz', $i); } return '<input type="submit" onclick="' . "getElementById('addform').regenerateddefid.value='$defid'; return true;" .'" value="'. get_string('generatevalue', 'quiz') . '"/><br/>' . '<input type="text" size="3" name="calcmin[]" ' . " value=\"$regs[2]\"/> & <input name=\"calcmax[]\" " . ' type="text" size="3" value="' . $regs[3] .'"/> ' . choose_from_menu($lengthoptions, 'calclength[]', $regs[4], // Selected '', '', '', true) . '<br/>' . choose_from_menu(array('uniform' => get_string('uniform', 'quiz'), 'loguniform' => get_string('loguniform', 'quiz')), 'calcdistribution[]', $regs[1], // Selected '', '', '', true); } else { return ''; } } function update_dataset_options($datasetdefs, $form) { // Do we have informatin about new options??? if (empty($form->definition) || empty($form->calcmin) || empty($form->calcmax) || empty($form->calclength) || empty($form->calcdistribution)) { // I guess not } else { // Looks like we just could have some new information here $uniquedefs = array_values(array_unique($form->definition)); foreach ($uniquedefs as $key => $defid) { if (isset($datasetdefs[$defid]) && is_numeric($form->calcmin[$key+1]) && is_numeric($form->calcmax[$key+1]) && is_numeric($form->calclength[$key+1])) { switch ($form->calcdistribution[$key+1]) { case 'uniform': case 'loguniform': $datasetdefs[$defid]->options = $form->calcdistribution[$key+1] . ':' . $form->calcmin[$key+1] . ':' . $form->calcmax[$key+1] . ':' . $form->calclength[$key+1]; break; default: notify("Unexpected distribution ".$form->calcdistribution[$key+1]); } } } } // Look for empty options, on which we set default values foreach ($datasetdefs as $defid => $def) { if (empty($def->options)) { $datasetdefs[$defid]->options = 'uniform:1.0:10.0:1'; } } return $datasetdefs; } function save_dataset_items($question, $fromform){ global $CFG ; // max datasets = 100 items $max100 = 100 ; if(isset($fromform->nextpageparam["forceregeneration"])) { $regenerate = $fromform->nextpageparam["forceregeneration"]; }else{ $regenerate = 0 ; } if (empty($question->options)) { $this->get_question_options($question); } //get the old datasets for this question $datasetdefs = $this->get_dataset_definitions($question->id, array()); // Handle generator options... $olddatasetdefs = fullclone($datasetdefs); $datasetdefs = $this->update_dataset_options($datasetdefs, $fromform); $maxnumber = -1; foreach ($datasetdefs as $defid => $datasetdef) { if (isset($datasetdef->id) && $datasetdef->options != $olddatasetdefs[$defid]->options) { // Save the new value for options update_record('question_dataset_definitions', $datasetdef); } // Get maxnumber if ($maxnumber == -1 || $datasetdef->itemcount < $maxnumber) { $maxnumber = $datasetdef->itemcount; } } // Handle adding and removing of dataset items $i = 1; ksort($fromform->definition); foreach ($fromform->definition as $key => $defid) { //if the delete button has not been pressed then skip the datasetitems //in the 'add item' part of the form. if ((!isset($fromform->addbutton)) && ($i > (count($datasetdefs)*$maxnumber))) { break; } $addeditem = new stdClass(); $addeditem->definition = $datasetdefs[$defid]->id; $addeditem->value = $fromform->number[$i]; $addeditem->itemnumber = ceil($i / count($datasetdefs)); if ($fromform->itemid[$i]) { // Reuse any previously used record $addeditem->id = $fromform->itemid[$i]; if (!update_record('question_dataset_items', $addeditem)) { error("Error: Unable to update dataset item"); } } else { if (!insert_record('question_dataset_items', $addeditem)) { error("Error: Unable to insert dataset item"); } } $i++; } if ($maxnumber < $addeditem->itemnumber){ $maxnumber = $addeditem->itemnumber; 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); } } } // adding supplementary items $numbertoadd =0; if (isset($fromform->addbutton) && $fromform->selectadd > 1 && $maxnumber < $max100 ) { $numbertoadd =$fromform->selectadd-1 ; if ( $max100 - $maxnumber < $numbertoadd ) { $numbertoadd = $max100 - $maxnumber ; } //add the other items. // Generate a new dataset item (or reuse an old one) foreach ($datasetdefs as $defid => $datasetdef) { if (isset($datasetdef->id)) { $datasetdefs[$defid]->items = get_records_sql( // Use number as key!! " SELECT itemnumber, definition, id, value FROM {$CFG->prefix}question_dataset_items WHERE definition = $datasetdef->id ORDER BY itemnumber"); } // echo "<pre>"; print_r($datasetdefs[$defid]->items); for ($numberadded =$maxnumber+1 ; $numberadded <= $maxnumber+$numbertoadd ; $numberadded++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -