adding or deleting more than 1 dataitems in one step in datasetitems_form.php

This commit is contained in:
pichetp
2007-05-22 02:29:33 +00:00
parent e882741ec2
commit 844aed0573
3 changed files with 103 additions and 28 deletions
+2
View File
@@ -15,6 +15,7 @@ $string['existingcategory3'] = 'a link from an already existing set of links tha
$string['forceregeneration'] = 'force regeneration';
$string['getnextnow'] = 'Get New \'Item to Add\' Now';
$string['itemno'] = 'Item $a';
$string['item(s)'] = 'item(s)';
$string['itemscount']='Items<br/>Count';
$string['itemtoadd'] = 'Item To Add';
$string['keptcategory1'] = 'a literal from the same category reusable set of literals as before';
@@ -23,6 +24,7 @@ $string['keptcategory3'] = 'a link from the same category reusable set of links
$string['keptlocal1'] = 'a literal from the same question private set of literals as before';
$string['keptlocal2'] = 'a file from the same question private set of files as before';
$string['keptlocal3'] = 'a link from the same question private set of links as before';
$string['lastitem(s)'] = 'last items(s)';
$string['loguniform'] = 'Loguniform';
$string['minmax'] = 'Range of Values';
$string['newcategory1'] = 'a literal from a new set of literals that may also be used by other questions in this category';
+70 -20
View File
@@ -32,26 +32,27 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
}
/*
if(false === parent::get_question_options($question)) {
if(false === parent::get_question_options($question)) {
return false;
}
if (!$options = get_record('question_calculated', 'question', $question->id)) {
if (!$options = get_records('question_calculated', 'question', $question->id)) {
notify("No options were found for calculated question
#{$question->id}! Proceeding with defaults.");
$options = new stdClass;
// $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) {
foreach ($question->options->answers as $key => $answer) {
$answer = &$question->options->answers[$key]; // for PHP 4.x
$answer->calcid = $options->id;
$answer->calcid = $options->id;
$answer->tolerance = $options->tolerance;
$answer->tolerancetype = $options->tolerancetype;
$answer->correctanswerlength = $options->correctanswerlength;
@@ -60,13 +61,13 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
$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)) {
@@ -156,8 +157,8 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
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)) {
@@ -276,7 +277,7 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
foreach ($form->answers as $key => $answer) {
$a->answer = trim($form->answer[$key]);
$a->fraction = $form->fraction[$key];//new
$a->tolerance = $form->tolerance[$key];
$a->tolerance = $form->tolerance[$key];
$a->tolerancetype = $form->tolerancetype[$key];
$a->correctanswerlength = $form->correctanswerlength[$key];
$a->correctanswerformat = $form->correctanswerformat[$key];
@@ -373,17 +374,15 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
$numericalquestion->questiontext, $state->options->dataset);
$virtualqtype->print_question_formulation_and_controls($numericalquestion, $state, $cmoptions, $options);
}
function grade_responses(&$question, &$state, $cmoptions) {
// Forward the grading to the virtual qtype
// 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) {
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) ;
}
@@ -557,6 +556,10 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
}
function save_dataset_items($question, $fromform){
// max datasets = 100 items
$max100 = 100 ;
$regenerate = optional_param('forceregeneration', 0, PARAM_BOOL);
// echo "<pre>"; print_r($fromform);
if (empty($question->options)) {
$this->get_question_options($question);
}
@@ -616,19 +619,66 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
}
}
}
// 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++){
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 = '';
}
//pp echo "<pre>"; print_r( $datasetitem );
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)) {
// Simply decrease itemcount where == $maxnumber
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--;
$datasetdef->itemcount= $newmaxnumber ;
if (!update_record('question_dataset_definitions',
$datasetdef)) {
error("Error: Unable to update itemcount");
}
}
}
--$maxnumber;
}
}
}
function generate_dataset_item($options) {
if (!ereg('^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$',
@@ -753,7 +803,7 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
// This should mean that something is wrong
$stranswers .= " -$calculated->answer".'<br/><br/>';
} else {
$stranswers .= $formula.' = '.$calculated->answer. '<br/>';
$stranswers .= $formula.' = '.$calculated->answer.'<br/>' ;
$stranswers .= $strmin. $delimiter.$calculated->min.'---';
$stranswers .= $strmax.$delimiter.$calculated->max;
$stranswers .='<br/>';
@@ -85,17 +85,42 @@ class question_dataset_dependent_items_form extends moodleform {
if ('' != $strquestionlabel){
$mform->addElement('static', 'answercomment['.($this->noofitems+1).']', $strquestionlabel);
}
$mform->closeHeaderBefore('addbutton');
$mform->addElement('submit', 'addbutton', get_string('additem', 'qtype_datasetdependent'));
$addremoveoptions = Array();
$addremoveoptions['1']='1';
for ($i=10; $i<=100 ; $i+=10){
$addremoveoptions["$i"]="$i";
}
$mform->addElement('header', 'additemhdr', get_string('add', 'moodle'));
$mform->closeHeaderBefore('additemhdr');
if ($this->qtypeobj->supports_dataset_item_generation()){
$radiogrp = array();
$radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('reuseifpossible', 'qtype_datasetdependent'), 0);
$radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregeneration', 'qtype_datasetdependent'), 1);
$mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), null, false);
$mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), "<br/>", false);
}
$mform->addElement('submit', 'getnextbutton', get_string('getnextnow', 'qtype_datasetdependent'));
$mform->addElement('static', "dividera", '', '<hr />');
$addgrp = array();
$addgrp[] =& $mform->createElement('submit', 'addbutton', get_string('add', 'moodle'));
$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->closeHeaderBefore('divideradd');
if ($this->noofitems > 0) {
$mform->addElement('header', 'additemhdr', get_string('delete', 'moodle'));
$deletegrp = array();
$deletegrp[] =& $mform->createElement('submit', 'deletebutton', get_string('delete', 'moodle'));
$deletegrp[] =& $mform->createElement('select', "selectdelete", get_string('deleteitem', 'qtype_datasetdependent')."1", $addremoveoptions);
$deletegrp[] = & $mform->createElement('static',"stat","Items",get_string('lastitem(s)', 'qtype_datasetdependent'));
$mform->addGroup($deletegrp, 'deletegrp', '', ' ', false);
// $mform->addElement('static', "dividerdelete", '', '<hr />');
// $mform->closeHeaderBefore('dividerdelete');
} else {
$mform->addElement('static','warning','','<span class="error">'.get_string('youmustaddatleastoneitem', 'qtype_datasetdependent').'</span>');
}
//------------------------------------------------------------------------------------------------------------------------------
$j = $this->noofitems * count($this->datasetdefs);
@@ -113,9 +138,6 @@ class question_dataset_dependent_items_form extends moodleform {
if ('' != $strquestionlabel){
$repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
}
if ($i == $this->noofitems) {//last item
$mform->addElement('submit', 'deletebutton', get_string('deletelastitem', 'qtype_datasetdependent'));
}
}
$mform->setType('number', PARAM_NUMBER);
$mform->setType('itemid', PARAM_INT);
@@ -160,7 +182,8 @@ class question_dataset_dependent_items_form extends moodleform {
}
$formdata['nextpageparam[forceregeneration]'] = $this->regenerate;
$formdata['selectdelete'] = '1';
$formdata['selectadd'] = '1';
$j = $this->noofitems * count($this->datasetdefs)+1;
$data = array(); // data for comment_on_datasetitems later
//dataset generation dafaults
@@ -203,7 +226,7 @@ class question_dataset_dependent_items_form extends moodleform {
function validation($data){
$errors = array();
if (isset($data['backtoquiz']) && ($this->noofitems==0)){
$errors['addbutton'] = get_string('youmustaddatleastoneitem', 'qtype_datasetdependent');
$errors['warning'] = get_string('warning', 'mnet');
}
return $errors;
}