Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding or deleting more than 1 dataitems in one step in datasetitems_…

…form.php
  • Loading branch information...
commit 844aed057314133eb9c23e09cdc80e2d719b5c40 1 parent e882741
pichetp authored
2  lang/en_utf8/qtype_datasetdependent.php
View
@@ -15,6 +15,7 @@
$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['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';
90 question/type/calculated/questiontype.php
View
@@ -32,26 +32,27 @@ function get_question_options(&$question) {
}
/*
- 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 @@ function get_question_options(&$question) {
$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 @@ function save_question_options($question) {
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 @@ function create_runtime_question($question, $form) {
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 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$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 @@ function update_dataset_options($datasetdefs, $form) {
}
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 @@ function save_dataset_items($question, $fromform){
}
}
}
+ // 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 @@ function comment_on_datasetitems($question, $data, $number) {
// 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/>';
39 question/type/datasetdependent/datasetitems_form.php
View
@@ -85,17 +85,42 @@ function definition() {
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 @@ function definition() {
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 @@ function set_data($question){
}
$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 @@ function set_data($question){
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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.