Permalink
Browse files

CONTRIB-490 Added support for scripted generation of questions. Calcu…

…lated type not yet supported (datasets too complicated ;)
  • Loading branch information...
nicolasconnault
nicolasconnault committed Jun 12, 2008
1 parent 2bf5901 commit 933f6db4b9fc32d7f5081a63adf7dfa4383632bc

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -133,6 +133,25 @@ function backup($bf, $preferences, $questionid, $level = 6) {
return question_backup_answers($bf, $preferences, $questionid, $level);
}
+ /**
+ * Runs all the code required to set up and save an essay question for testing purposes.
+ * Alternate DB table prefix may be used to facilitate data deletion.
+ */
+ function generate_test($name, $courseid = null) {
+ list($form, $question) = parent::generate_test($name, $courseid);
+ $form->questiontext = "What is the purpose of life?";
+ $form->feedback = "feedback";
+ $form->generalfeedback = "General feedback";
+ $form->fraction = 0;
+ $form->penalty = 0;
+
+ if ($courseid) {
+ $course = get_record('course', 'id', $courseid);
+ }
+
+ return $this->save_question($question, $form, $course);
+ }
+
// Restore method not needed.
}
//// END OF CLASS ////
@@ -37,7 +37,7 @@ function get_question_options(&$question) {
// We want an array with question ids as index and the positions as values
$sequence = array_flip(explode(',', $sequence));
array_walk($sequence, create_function('&$val', '$val++;'));
- //si une question est manquante l,indice est nul
+ //si une question est manquante l,indice est nul
foreach($sequence as $seq){
$question->options->questions[$seq]= '';
}
@@ -79,7 +79,7 @@ function save_question_options($question) {
if ($wrapped != ''){
// if we still have some old wrapped question ids, reuse the next of them
if (is_array($oldwrappedids) && $oldwrappedid = array_shift($oldwrappedids)) {
-
+
if( $oldqtype = get_field('question', 'qtype', 'id',$oldwrappedid->id)){
$wrapped->id = $oldwrappedid->id;
if($oldqtype != $wrapped->qtype ) {
@@ -115,7 +115,7 @@ function save_question_options($question) {
}
// Delete redundant wrapped questions
- if(is_array($oldwrappedids) && count($oldwrappedids)){
+ if(is_array($oldwrappedids) && count($oldwrappedids)){
foreach ($oldwrappedids as $id) {
delete_question($id) ;
}
@@ -282,25 +282,25 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$feedbackimg = '';
$feedback = '' ;
$correctanswer = '';
- $strfeedbackwrapped = $strfeedback;
+ $strfeedbackwrapped = $strfeedback;
// if($wrapped->qtype == 'numerical' ||$wrapped->qtype == 'shortanswer'){
- $testedstate = clone($state);
+ $testedstate = clone($state);
if ($correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $testedstate)) {
if ($options->readonly && $options->correct_responses) {
$delimiter = '';
if ($correctanswers) {
foreach ($correctanswers as $ca) {
switch($wrapped->qtype){
case 'numerical':
- case 'shortanswer':
+ case 'shortanswer':
$correctanswer .= $delimiter.$ca;
break ;
case 'multichoice':
if (isset($answers[$ca])){
$correctanswer .= $delimiter.$answers[$ca]->answer;
}
break ;
- }
+ }
$delimiter = ', ';
}
}
@@ -364,7 +364,7 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
}
}
if ($feedback !='' && $popup == ''){
- $strfeedbackwrapped = get_string('correctanswer', 'qtype_multianswer');
+ $strfeedbackwrapped = get_string('correctanswer', 'qtype_multianswer');
$feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback));
$popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedbackwrapped', FGCOLOR, '#FFFFFF');\" ".
" onmouseout=\"return nd();\" ";
@@ -379,18 +379,18 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
if (strlen(trim($answer->answer)) > $size ){
$size = strlen(trim($answer->answer));
}
- }
+ }
if (strlen(trim($response))> $size ){
$size = strlen(trim($response))+1;
}
$size = $size + rand(0,$size*0.15);
- $size > 60 ? $size = 60 : $size = $size;
+ $size > 60 ? $size = 60 : $size = $size;
$styleinfo = "size=\"$size\"";
/**
* Uncomment the following lines if you want to limit for small sizes.
- * Results may vary with browsers see MDL-3274
+ * Results may vary with browsers see MDL-3274
*/
- /*
+ /*
if ($size < 2) {
$styleinfo = 'style="width: 1.1em;"';
}
@@ -404,7 +404,7 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$styleinfo = 'style="width: 2.8em;"';
}
*/
-
+
echo "<input $style $readonly $popup name=\"$inputname\"";
echo " type=\"text\" value=\"".s($response, true)."\" ".$styleinfo." /> ";
if (!empty($feedback) && !empty($USER->screenreader)) {
@@ -448,15 +448,15 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
if(! isset($question->options->questions[$positionkey])){
echo $regs[0];
}else {
- echo '<div class="error" >'.get_string('questionnotfound','qtype_multianswer',$positionkey).'</div>';
+ echo '<div class="error" >'.get_string('questionnotfound','qtype_multianswer',$positionkey).'</div>';
}
}
}
// Print the final piece of question text:
echo $qtextremaining;
$this->print_question_submit_buttons($question, $state, $cmoptions, $options);
- echo '</div>';
+ echo '</div>';
}
function grade_responses(&$question, &$state, $cmoptions) {
@@ -702,6 +702,33 @@ function restore_recode_answer($state, $restore) {
return $answer_field;
}
+ /**
+ * Runs all the code required to set up and save an essay question for testing purposes.
+ * Alternate DB table prefix may be used to facilitate data deletion.
+ */
+ function generate_test($name, $courseid = null) {
+ list($form, $question) = parent::generate_test($name, $courseid);
+ $question->category = $form->category;
+ $form->questiontext = "This question consists of some text with an answer embedded right here {1:MULTICHOICE:Wrong answer#Feedback for this wrong answer~Another wrong answer#Feedback for the other wrong answer~=Correct answer#Feedback for correct answer~%50%Answer that gives half the credit#Feedback for half credit answer} and right after that you will have to deal with this short answer {1:SHORTANSWER:Wrong answer#Feedback for this wrong answer~=Correct answer#Feedback for correct answer~%50%Answer that gives half the credit#Feedback for half credit answer} and finally we have a floating point number {2:NUMERICAL:=23.8:0.1#Feedback for correct answer 23.8~%50%23.8:2#Feedback for half credit answer in the nearby region of the correct answer}.
+
+Note that addresses like www.moodle.org and smileys :-) all work as normal:
+ a) How good is this? {:MULTICHOICE:=Yes#Correct~No#We have a different opinion}
+ b) What grade would you give it? {3:NUMERICAL:=3:2}
+
+Good luck!
+";
+ $form->feedback = "feedback";
+ $form->generalfeedback = "General feedback";
+ $form->fraction = 0;
+ $form->penalty = 0.1;
+ $form->versioning = 0;
+
+ if ($courseid) {
+ $course = get_record('course', 'id', $courseid);
+ }
+
+ return $this->save_question($question, $form, $course);
+ }
}
//// END OF CLASS ////
@@ -669,6 +669,34 @@ function replace_file_links($question, $fromcourseid, $tocourseid, $url, $destin
}
}
}
+
+ /**
+ * Runs all the code required to set up and save an essay question for testing purposes.
+ * Alternate DB table prefix may be used to facilitate data deletion.
+ */
+ function generate_test($name, $courseid = null) {
+ list($form, $question) = parent::generate_test($name, $courseid);
+ $question->category = $form->category;
+ $form->questiontext = "How old is the sun?";
+ $form->generalfeedback = "General feedback";
+ $form->penalty = 0.1;
+ $form->single = 1;
+ $form->shuffleanswers = 1;
+ $form->answernumbering = 'abc';
+ $form->noanswers = 3;
+ $form->answer = array('Ancient', '5 billion years old', '4.5 billion years old');
+ $form->fraction = array(0.3, 0.9, 1);
+ $form->feedback = array('True, but lacking in accuracy', 'Close, but no cigar!', 'Yep, that is it!');
+ $form->correctfeedback = 'Excellent!';
+ $form->incorrectfeedback = 'Nope!';
+ $form->partiallycorrectfeedback = 'Not bad';
+
+ if ($courseid) {
+ $course = get_record('course', 'id', $courseid);
+ }
+
+ return $this->save_question($question, $form, $course);
+ }
}
// Register this question type with the question bank.
@@ -107,7 +107,7 @@ function save_question_options($question) {
// Insert all the new answers
foreach ($question->answer as $key => $dataanswer) {
- if (!isset( $question->deleteanswer[$key] ) && !( trim($dataanswer) == 0 && $question->fraction[$key]== 0 &&trim($question->feedback[$key])=='')) {
+ if (!isset( $question->deleteanswer[$key] ) && !( trim($dataanswer) == 0 && $question->fraction[$key]== 0 &&trim($question->feedback[$key])=='')) {
$answer = new stdClass;
$answer->question = $question->id;
if (trim($dataanswer) == '*') {
@@ -148,7 +148,7 @@ function save_question_options($question) {
$result->notice = get_string('invalidnumerictolerance', 'quiz');
}
}
-
+
// Save options
if (isset($options->id)) { // reusing existing record
if (! update_record('question_numerical', $options)) {
@@ -181,7 +181,7 @@ function save_question_options($question) {
if (!empty($result->notice)) {
return $result;
}
-
+
return true;
}
@@ -329,11 +329,11 @@ function get_tolerance_interval(&$answer) {
$tolerance = abs($tolerance); // important - otherwise min and max are swapped
// $answer->tolerance 0 or something else
if ((float)$answer->tolerance == 0.0 && abs((float)$answer->answer) <= $tolerance ){
- $tolerance = (float) ("1.0e-".ini_get('precision')) * abs((float)$answer->answer) ; //tiny fraction
+ $tolerance = (float) ("1.0e-".ini_get('precision')) * abs((float)$answer->answer) ; //tiny fraction
} else if ((float)$answer->tolerance != 0.0 && abs((float)$answer->tolerance) < abs((float)$answer->answer) && abs((float)$answer->answer) <= $tolerance){
- $tolerance = (1+("1.0e-".ini_get('precision')) )* abs((float) $answer->tolerance) ;//tiny fraction
- }
-
+ $tolerance = (1+("1.0e-".ini_get('precision')) )* abs((float) $answer->tolerance) ;//tiny fraction
+ }
+
$max = $answer->answer + $tolerance;
$min = $answer->answer - $tolerance;
break;
@@ -370,13 +370,13 @@ function apply_unit($rawresponse, $units) {
$search = array(' ', ',');
$replace = array('', '.');
$rawresponse = str_replace($search, $replace, trim($rawresponse));
-
+
// Apply any unit that is present.
if (ereg('^([+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][-+]?[0-9]+)?)([^0-9].*)?$',
$rawresponse, $responseparts)) {
-
+
if (!empty($responseparts[5])) {
-
+
if (isset($tmpunits[$responseparts[5]])) {
// Valid number with unit.
return (float)$responseparts[1] / $tmpunits[$responseparts[5]];
@@ -393,7 +393,7 @@ function apply_unit($rawresponse, $units) {
// Invalid number. Must be wrong.
return false;
}
-
+
/// BACKUP FUNCTIONS ////////////////////////////
/**
@@ -483,6 +483,34 @@ function restore($old_question_id,$new_question_id,$info,$restore) {
return $status;
}
+ /**
+ * Runs all the code required to set up and save an essay question for testing purposes.
+ * Alternate DB table prefix may be used to facilitate data deletion.
+ */
+ function generate_test($name, $courseid = null) {
+ list($form, $question) = default_questiontype::generate_test($name, $courseid);
+ $question->category = $form->category;
+
+ $form->questiontext = "What is 674 * 36?";
+ $form->generalfeedback = "Thank you";
+ $form->penalty = 0.1;
+ $form->defaultgrade = 1;
+ $form->noanswers = 3;
+ $form->answer = array('24264', '24264', '1');
+ $form->tolerance = array(10, 100, 0);
+ $form->fraction = array(1, 0.5, 0);
+ $form->nounits = 2;
+ $form->unit = array(0 => null, 1 => null);
+ $form->multiplier = array(1, 0);
+ $form->feedback = array('Very good', 'Close, but not quite there', 'Well at least you tried....');
+
+ if ($courseid) {
+ $course = get_record('course', 'id', $courseid);
+ }
+
+ return $this->save_question($question, $form, $course);
+ }
+
}
// INITIATION - Without this line the question type is not in use.
@@ -440,6 +440,34 @@ function print_question_grading_details(&$question, &$state, $cmoptions, $option
}
}
}
+
+ /**
+ * Runs all the code required to set up and save an essay question for testing purposes.
+ * Alternate DB table prefix may be used to facilitate data deletion.
+ */
+ function generate_test($name, $courseid = null) {
+ list($form, $question) = parent::generate_test($name, $courseid);
+ $question->category = $form->category;
+
+ $form->questiontext = "What is the purpose of life, the universe, and everything";
+ $form->generalfeedback = "Congratulations, you may have solved my biggest problem!";
+ $form->penalty = 0.1;
+ $form->usecase = false;
+ $form->defaultgrade = 1;
+ $form->noanswers = 3;
+ $form->answer = array('42', 'who cares?', 'Be happy');
+ $form->fraction = array(1, 0.6, 0.8);
+ $form->feedback = array('True, but what does that mean?', 'Well you do, dont you?', 'Yes, but thats not funny...');
+ $form->correctfeedback = 'Excellent!';
+ $form->incorrectfeedback = 'Nope!';
+ $form->partiallycorrectfeedback = 'Not bad';
+
+ if ($courseid) {
+ $course = get_record('course', 'id', $courseid);
+ }
+
+ return $this->save_question($question, $form, $course);
+ }
}
//// END OF CLASS ////
@@ -345,6 +345,27 @@ function restore_recode_answer($state, $restore) {
}
}
+ /**
+ * Runs all the code required to set up and save an essay question for testing purposes.
+ * Alternate DB table prefix may be used to facilitate data deletion.
+ */
+ function generate_test($name, $courseid = null) {
+ list($form, $question) = parent::generate_test($name, $courseid);
+ $question->category = $form->category;
+
+ $form->questiontext = "This question is really stupid";
+ $form->penalty = 1;
+ $form->defaultgrade = 1;
+ $form->correctanswer = 0;
+ $form->feedbacktrue = array('Can you justify such a hasty judgment?');
+ $form->feedbackfalse = array('Wisdom has spoken!');
+
+ if ($courseid) {
+ $course = get_record('course', 'id', $courseid);
+ }
+
+ return $this->save_question($question, $form, $course);
+ }
}
//// END OF CLASS ////

0 comments on commit 933f6db

Please sign in to comment.