Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'MDL-31065_22' of git://github.com/timhunt/moodle into M…

…OODLE_22_STABLE
  • Loading branch information...
commit b19a440a04f103697d53ac03590cdca185d1e45e 2 parents 787c2ee + 4eac963
@stronk7 stronk7 authored
Showing with 461 additions and 68 deletions.
  1. +1 −0  lang/en/question.php
  2. +5 −5 question/behaviour/adaptive/simpletest/testwalkthrough.php
  3. +1 −1  question/behaviour/immediatecbm/simpletest/testwalkthrough.php
  4. +3 −3 question/behaviour/interactive/simpletest/testwalkthrough.php
  5. +0 −23 question/engine/simpletest/helpers.php
  6. +1 −0  question/type/calculated/lang/en/qtype_calculated.php
  7. +28 −8 question/type/calculated/questiontype.php
  8. +37 −0 question/type/calculated/simpletest/helper.php
  9. +20 −0 question/type/calculated/simpletest/testquestion.php
  10. +107 −0 question/type/calculated/simpletest/testquestiontype.php
  11. +4 −3 question/type/numerical/question.php
  12. +10 −1 question/type/numerical/questiontype.php
  13. +1 −2  question/type/numerical/simpletest/helper.php
  14. +35 −0 question/type/numerical/simpletest/testquestion.php
  15. +16 −1 question/type/numerical/simpletest/testquestiontype.php
  16. +3 −1 question/type/questionbase.php
  17. +10 −0 question/type/shortanswer/questiontype.php
  18. +133 −0 question/type/shortanswer/simpletest/helper.php
  19. +23 −8 question/type/shortanswer/simpletest/testquestion.php
  20. +22 −11 question/type/shortanswer/simpletest/testquestiontype.php
  21. +1 −1  question/type/truefalse/simpletest/testquestiontype.php
View
1  lang/en/question.php
@@ -99,6 +99,7 @@
$string['deletequestionscheck'] = 'Are you absolutely sure you want to delete the following questions?<br /><br />{$a}';
$string['deletingbehaviour'] = 'Deleting question behaviour \'{$a}\'';
$string['deletingqtype'] = 'Deleting question type \'{$a}\'';
+$string['didnotmatchanyanswer'] = '[Did not match any answer]';
$string['disabled'] = 'Disabled';
$string['disterror'] = 'The distribution {$a} caused problems';
$string['donothing'] = 'Don\'t copy or move files or change links.';
View
10 question/behaviour/adaptive/simpletest/testwalkthrough.php
@@ -238,7 +238,7 @@ public function test_adaptive_multichoice2() {
public function test_adaptive_shortanswer_partially_right() {
// Create a short answer question
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive');
// Check the initial state.
@@ -307,7 +307,7 @@ public function test_adaptive_shortanswer_partially_right() {
public function test_adaptive_shortanswer_wrong_right_wrong() {
// Create a short answer question
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive', 6);
// Check the initial state.
@@ -390,7 +390,7 @@ public function test_adaptive_shortanswer_wrong_right_wrong() {
public function test_adaptive_shortanswer_invalid_after_complete() {
// Create a short answer question
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive');
// Check the initial state.
@@ -472,7 +472,7 @@ public function test_adaptive_shortanswer_invalid_after_complete() {
public function test_adaptive_shortanswer_zero_penalty() {
// Create a short answer question
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
// Disable penalties for this question
$sa->penalty = 0;
$this->start_attempt_at_question($sa, 'adaptive');
@@ -529,7 +529,7 @@ public function test_adaptive_shortanswer_zero_penalty() {
public function test_adaptive_shortanswer_try_to_submit_blank() {
// Create a short answer question with correct answer true.
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive');
// Check the initial state.
View
2  question/behaviour/immediatecbm/simpletest/testwalkthrough.php
@@ -188,7 +188,7 @@ public function test_immediatecbm_feedback_multichoice_try_to_submit_blank() {
public function test_immediatecbm_feedback_shortanswer_try_to_submit_no_certainty() {
// Create a short answer question with correct answer true.
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'immediatecbm');
// Check the initial state.
View
6 question/behaviour/interactive/simpletest/testwalkthrough.php
@@ -244,7 +244,7 @@ public function test_interactive_finish_when_try_again_showing() {
public function test_interactive_shortanswer_try_to_submit_blank() {
// Create a short answer question.
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$sa->hints = array(
new question_hint(0, 'This is the first hint.', FORMAT_HTML),
new question_hint(0, 'This is the second hint.', FORMAT_HTML),
@@ -420,7 +420,7 @@ public function test_interactive_feedback_multichoice_multiple_reset() {
public function test_interactive_regrade_changing_num_tries_leaving_open() {
// Create a multichoice multiple question.
- $q = test_question_maker::make_a_shortanswer_question();
+ $q = test_question_maker::make_question('shortanswer');
$q->hints = array(
new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true),
new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true),
@@ -453,7 +453,7 @@ public function test_interactive_regrade_changing_num_tries_leaving_open() {
public function test_interactive_regrade_changing_num_tries_finished() {
// Create a multichoice multiple question.
- $q = test_question_maker::make_a_shortanswer_question();
+ $q = test_question_maker::make_question('shortanswer');
$q->hints = array(
new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true),
new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true),
View
23 question/engine/simpletest/helpers.php
@@ -341,29 +341,6 @@ public static function make_a_matching_question() {
}
/**
- * Makes a shortanswer question with correct ansewer 'frog', partially
- * correct answer 'toad' and defaultmark 1.
- * @return qtype_shortanswer_question
- */
- public static function make_a_shortanswer_question() {
- question_bank::load_question_definition_classes('shortanswer');
- $sa = new qtype_shortanswer_question();
- self::initialise_a_question($sa);
- $sa->name = 'Short answer question';
- $sa->questiontext = 'Name an amphibian: __________';
- $sa->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
- $sa->usecase = false;
- $sa->answers = array(
- 13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
- 14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
- 15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
- );
- $sa->qtype = question_bank::get_qtype('shortanswer');
-
- return $sa;
- }
-
- /**
* Makes a truefalse question with correct ansewer true, defaultmark 1.
* @return qtype_essay_question
*/
View
1  question/type/calculated/lang/en/qtype_calculated.php
@@ -29,6 +29,7 @@
$string['addsets'] = 'Add set(s)';
$string['answerhdr'] = 'Answer';
$string['answerstoleranceparam'] = 'Answers tolerance parameters';
+$string['answerwithtolerance'] = '{$a->answer} (±{$a->tolerance} {$a->tolerancetype})';
$string['anyvalue'] = 'Any value';
$string['atleastoneanswer'] = 'You need to provide at least one answer.';
$string['atleastonerealdataset']='There should be at least one real dataset in question text';
View
36 question/type/calculated/questiontype.php
@@ -686,6 +686,15 @@ public function delete_question($questionid, $contextid) {
parent::delete_question($questionid, $contextid);
}
+ public function get_random_guess_score($questiondata) {
+ foreach ($questiondata->options->answers as $aid => $answer) {
+ if ('*' == trim($answer->answer)) {
+ return max($answer->fraction - $questiondata->options->unitpenalty, 0);
+ }
+ }
+ return 0;
+ }
+
public function supports_dataset_item_generation() {
// Calcualted support generation of randomly distributed number data
return true;
@@ -1200,7 +1209,7 @@ public function construct_dataset_menus($form, $mandatorydatasets,
public function substitute_variables($str, $dataset) {
global $OUTPUT;
- // testing for wrong numerical values
+ // testing for wrong numerical values
// all calculations used this function so testing here should be OK
foreach ($dataset as $name => $value) {
@@ -1220,6 +1229,7 @@ public function substitute_variables($str, $dataset) {
}
return $str;
}
+
public function evaluate_equations($str, $dataset) {
$formula = $this->substitute_variables($str, $dataset);
if ($error = qtype_calculated_find_formula_errors($formula)) {
@@ -1228,7 +1238,6 @@ public function evaluate_equations($str, $dataset) {
return $str;
}
-
public function substitute_variables_and_eval($str, $dataset) {
$formula = $this->substitute_variables($str, $dataset);
if ($error = qtype_calculated_find_formula_errors($formula)) {
@@ -1793,21 +1802,32 @@ public function get_possible_responses($questiondata) {
$virtualqtype = $this->get_virtual_qtype();
$unit = $virtualqtype->get_default_numerical_unit($questiondata);
+ $tolerancetypes = $this->tolerance_types();
+
+ $starfound = false;
foreach ($questiondata->options->answers as $aid => $answer) {
$responseclass = $answer->answer;
- if ($responseclass != '*') {
- $responseclass = $virtualqtype->add_unit($questiondata, $responseclass, $unit);
+ if ($responseclass === '*') {
+ $starfound = true;
+ } else {
+ $a = new stdClass();
+ $a->answer = $virtualqtype->add_unit($questiondata, $responseclass, $unit);
+ $a->tolerance = $answer->tolerance;
+ $a->tolerancetype = $tolerancetypes[$answer->tolerancetype];
- $ans = new qtype_numerical_answer($answer->id, $answer->answer, $answer->fraction,
- $answer->feedback, $answer->feedbackformat, $answer->tolerance);
- list($min, $max) = $ans->get_tolerance_interval();
- $responseclass .= " ($min..$max)";
+ $responseclass = get_string('answerwithtolerance', 'qtype_calculated', $a);
}
$responses[$aid] = new question_possible_response($responseclass,
$answer->fraction);
}
+
+ if (!$starfound) {
+ $responses[0] = new question_possible_response(
+ get_string('didnotmatchanyanswer', 'question'), 0);
+ }
+
$responses[null] = question_possible_response::no_response();
return array($questiondata->id => $responses);
View
37 question/type/calculated/simpletest/helper.php
@@ -77,6 +77,43 @@ public function make_calculated_question_sum() {
return $q;
}
+
+ /**
+ * Makes a calculated question about summing two numbers.
+ * @return qtype_calculated_question
+ */
+ public function get_calculated_question_data_sum() {
+ question_bank::load_question_definition_classes('calculated');
+ $qdata = new stdClass();
+ test_question_maker::initialise_question_data($qdata);
+
+ $qdata->qtype = 'calculated';
+ $qdata->name = 'Simple sum';
+ $qdata->questiontext = 'What is {a} + {b}?';
+ $qdata->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
+
+ $qdata->options = new stdClass();
+ $qdata->options->unitgradingtype = 0;
+ $qdata->options->unitpenalty = 0.0;
+ $qdata->options->showunits = qtype_numerical::UNITNONE;
+ $qdata->options->unitsleft = 0;
+ $qdata->options->synchronize = 0;
+
+ $qdata->options->answers = array(
+ 13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0.001),
+ 14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
+ FORMAT_HTML, 0.001),
+ 17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
+ );
+ foreach ($qdata->options->answers as $answer) {
+ $answer->correctanswerlength = 2;
+ $answer->correctanswerformat = 1;
+ }
+
+ $qdata->options->units = array();
+
+ return $qdata;
+ }
}
View
20 question/type/calculated/simpletest/testquestion.php
@@ -109,6 +109,26 @@ public function test_classify_response() {
$question->classify_response(array('answer' => '')));
}
+ public function test_classify_response_no_star() {
+ $question = test_question_maker::make_question('calculated');
+ unset($question->answers[17]);
+ $question->start_attempt(new question_attempt_step(), 1);
+ $values = $question->vs->get_values();
+
+ $this->assertEqual(array(
+ new question_classified_response(13, $values['a'] + $values['b'], 1.0)),
+ $question->classify_response(array('answer' => $values['a'] + $values['b'])));
+ $this->assertEqual(array(
+ new question_classified_response(14, $values['a'] - $values['b'], 0.0)),
+ $question->classify_response(array('answer' => $values['a'] - $values['b'])));
+ $this->assertEqual(array(
+ new question_classified_response(0, 7 * $values['a'], 0.0)),
+ $question->classify_response(array('answer' => 7 * $values['a'])));
+ $this->assertEqual(array(
+ question_classified_response::no_response()),
+ $question->classify_response(array('answer' => '')));
+ }
+
public function test_get_variants_selection_seed_q_not_synchronised() {
$question = test_question_maker::make_question('calculated');
$this->assertEqual($question->stamp, $question->get_variants_selection_seed());
View
107 question/type/calculated/simpletest/testquestiontype.php
@@ -0,0 +1,107 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Unit tests for (some of) question/type/calculated/questiontype.php.
+ *
+ * @package qtype_calculated
+ * @copyright 2012 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot . '/question/type/calculated/questiontype.php');
+
+
+/**
+ * Unit tests for question/type/calculated/questiontype.php.
+ *
+ * @copyright 2012 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class qtype_calculated_test extends UnitTestCase {
+ public static $includecoverage = array(
+ 'question/type/questiontypebase.php',
+ 'question/type/calculated/questiontype.php'
+ );
+
+ protected $tolerance = 0.00000001;
+ protected $qtype;
+
+ public function setUp() {
+ $this->qtype = new qtype_calculated();
+ }
+
+ public function tearDown() {
+ $this->qtype = null;
+ }
+
+ public function test_name() {
+ $this->assertEqual($this->qtype->name(), 'calculated');
+ }
+
+ public function test_can_analyse_responses() {
+ $this->assertTrue($this->qtype->can_analyse_responses());
+ }
+
+ public function test_get_random_guess_score() {
+ $q = test_question_maker::get_question_data('calculated');
+ $q->options->answers[17]->fraction = 0.1;
+ $this->assertEqual(0.1, $this->qtype->get_random_guess_score($q));
+ }
+
+ protected function get_possible_response($ans, $tolerance, $type) {
+ $a = new stdClass();
+ $a->answer = $ans;
+ $a->tolerance = $tolerance;
+ $a->tolerancetype = get_string($type, 'qtype_numerical');
+ return get_string('answerwithtolerance', 'qtype_calculated', $a);
+ }
+
+ public function test_get_possible_responses() {
+ $q = test_question_maker::get_question_data('calculated');
+
+ $this->assertEqual(array(
+ $q->id => array(
+ 13 => new question_possible_response(
+ $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1.0),
+ 14 => new question_possible_response(
+ $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0.0),
+ 17 => new question_possible_response('*', 0.0),
+ null => question_possible_response::no_response()
+ ),
+ ), $this->qtype->get_possible_responses($q));
+ }
+
+ public function test_get_possible_responses_no_star() {
+ $q = test_question_maker::get_question_data('calculated');
+ unset($q->options->answers[17]);
+
+ $this->assertEqual(array(
+ $q->id => array(
+ 13 => new question_possible_response(
+ $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1),
+ 14 => new question_possible_response(
+ $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0),
+ 0 => new question_possible_response(
+ get_string('didnotmatchanyanswer', 'question'), 0),
+ null => question_possible_response::no_response()
+ ),
+ ), $this->qtype->get_possible_responses($q));
+ }
+}
View
7 question/type/numerical/question.php
@@ -258,15 +258,16 @@ public function classify_response(array $response) {
}
list($value, $unit, $multiplier) = $this->ap->apply_units($response['answer'], $selectedunit);
$ans = $this->get_matching_answer($value, $multiplier);
- if (!$ans) {
- return array($this->id => question_classified_response::no_response());
- }
$resp = $response['answer'];
if ($this->has_separate_unit_field()) {
$resp = $this->ap->add_unit($resp, $unit);
}
+ if (!$ans) {
+ return array($this->id => new question_classified_response(0, $resp, 0));
+ }
+
return array($this->id => new question_classified_response($ans->id,
$resp,
$this->apply_unit_penalty($ans->fraction, $ans->unitisright)));
View
11 question/type/numerical/questiontype.php
@@ -418,10 +418,13 @@ public function get_possible_responses($questiondata) {
$unit = $this->get_default_numerical_unit($questiondata);
+ $starfound = false;
foreach ($questiondata->options->answers as $aid => $answer) {
$responseclass = $answer->answer;
- if ($responseclass != '*') {
+ if ($responseclass === '*') {
+ $starfound = true;
+ } else {
$responseclass = $this->add_unit($questiondata, $responseclass, $unit);
$ans = new qtype_numerical_answer($answer->id, $answer->answer, $answer->fraction,
@@ -433,6 +436,12 @@ public function get_possible_responses($questiondata) {
$responses[$aid] = new question_possible_response($responseclass,
$answer->fraction);
}
+
+ if (!$starfound) {
+ $responses[0] = new question_possible_response(
+ get_string('didnotmatchanyanswer', 'question'), 0);
+ }
+
$responses[null] = question_possible_response::no_response();
return array($questiondata->id => $responses);
View
3  question/type/numerical/simpletest/helper.php
@@ -72,8 +72,7 @@ public function make_numerical_question_pi() {
}
/**
- * Makes a numerical question with correct ansewer 3.14, and various incorrect
- * answers with different feedback.
+ * Makes a numerical question with a choice (select menu) of units.
* @return qtype_numerical_question
*/
public function make_numerical_question_unit() {
View
35 question/type/numerical/simpletest/testquestion.php
@@ -213,6 +213,22 @@ public function test_classify_response() {
$num->classify_response(array('answer' => '')));
}
+ public function test_classify_response_no_star() {
+ $num = test_question_maker::make_question('numerical');
+ unset($num->answers[17]);
+ $num->start_attempt(new question_attempt_step(), 1);
+
+ $this->assertEqual(array(
+ new question_classified_response(15, '3.1', 0.0)),
+ $num->classify_response(array('answer' => '3.1')));
+ $this->assertEqual(array(
+ new question_classified_response(0, '42', 0.0)),
+ $num->classify_response(array('answer' => '42')));
+ $this->assertEqual(array(
+ question_classified_response::no_response()),
+ $num->classify_response(array('answer' => '')));
+ }
+
public function test_classify_response_unit() {
$num = test_question_maker::make_question('numerical', 'unit');
$num->start_attempt(new question_attempt_step(), 1);
@@ -240,6 +256,25 @@ public function test_classify_response_unit() {
$num->classify_response(array('answer' => '')));
}
+ public function test_classify_response_unit_no_star() {
+ $num = test_question_maker::make_question('numerical', 'unit');
+ unset($num->answers[17]);
+ $num->start_attempt(new question_attempt_step(), 1);
+
+ $this->assertEqual(array(
+ new question_classified_response(0, '42 cm', 0)),
+ $num->classify_response(array('answer' => '42', 'unit' => 'cm')));
+ $this->assertEqual(array(
+ new question_classified_response(0, '3.0', 0)),
+ $num->classify_response(array('answer' => '3.0', 'unit' => '')));
+ $this->assertEqual(array(
+ new question_classified_response(0, '3.0 m', 0)),
+ $num->classify_response(array('answer' => '3.0', 'unit' => 'm')));
+ $this->assertEqual(array(
+ question_classified_response::no_response()),
+ $num->classify_response(array('answer' => '', 'unit' => '')));
+ }
+
public function test_classify_response_currency() {
$num = test_question_maker::make_question('numerical', 'currency');
$num->start_attempt(new question_attempt_step(), 1);
View
17 question/type/numerical/simpletest/testquestiontype.php
@@ -101,7 +101,22 @@ public function test_get_possible_responses() {
$q->id => array(
13 => new question_possible_response('42 m (41.5..42.5)', 1),
14 => new question_possible_response('*', 0.1),
- null => question_possible_response::no_response()),
+ null => question_possible_response::no_response()
+ ),
+ ), $this->qtype->get_possible_responses($q));
+ }
+
+ public function test_get_possible_responses_no_star() {
+ $q = $this->get_test_question_data();
+ unset($q->options->answers[14]);
+
+ $this->assertEqual(array(
+ $q->id => array(
+ 13 => new question_possible_response('42 m (41.5..42.5)', 1),
+ 0 => new question_possible_response(
+ get_string('didnotmatchanyanswer', 'question'), 0),
+ null => question_possible_response::no_response()
+ ),
), $this->qtype->get_possible_responses($q));
}
}
View
4 question/type/questionbase.php
@@ -709,8 +709,10 @@ public function classify_response(array $response) {
$ans = $this->get_matching_answer($response);
if (!$ans) {
- return array($this->id => question_classified_response::no_response());
+ return array($this->id => new question_classified_response(
+ 0, $response['answer'], 0));
}
+
return array($this->id => new question_classified_response(
$ans->id, $response['answer'], $ans->fraction));
}
View
10 question/type/shortanswer/questiontype.php
@@ -139,10 +139,20 @@ public function get_random_guess_score($questiondata) {
public function get_possible_responses($questiondata) {
$responses = array();
+ $starfound = false;
foreach ($questiondata->options->answers as $aid => $answer) {
$responses[$aid] = new question_possible_response($answer->answer,
$answer->fraction);
+ if ($answer->answer === '*') {
+ $starfound = true;
+ }
+ }
+
+ if (!$starfound) {
+ $responses[0] = new question_possible_response(
+ get_string('didnotmatchanyanswer', 'question'), 0);
}
+
$responses[null] = question_possible_response::no_response();
return array($questiondata->id => $responses);
View
133 question/type/shortanswer/simpletest/helper.php
@@ -0,0 +1,133 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Test helpers for the shortanswer question type.
+ *
+ * @package qtype_shortanswer
+ * @copyright 2012 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+
+defined('MOODLE_INTERNAL') || die();
+
+
+/**
+ * Test helper class for the shortanswer question type.
+ *
+ * @copyright 2011 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class qtype_shortanswer_test_helper extends question_test_helper {
+ public function get_test_questions() {
+ return array('frogtoad', 'frogonly');
+ }
+
+ /**
+ * Makes a shortanswer question with correct ansewer 'frog', partially
+ * correct answer 'toad' and defaultmark 1. This question also has a
+ * '*' match anything answer.
+ * @return qtype_shortanswer_question
+ */
+ public function make_shortanswer_question_frogtoad() {
+ question_bank::load_question_definition_classes('shortanswer');
+ $sa = new qtype_shortanswer_question();
+ test_question_maker::initialise_a_question($sa);
+ $sa->name = 'Short answer question';
+ $sa->questiontext = 'Name an amphibian: __________';
+ $sa->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
+ $sa->usecase = false;
+ $sa->answers = array(
+ 13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
+ 14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
+ 15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
+ );
+ $sa->qtype = question_bank::get_qtype('shortanswer');
+
+ return $sa;
+ }
+
+ /**
+ * Gets the question data for a shortanswer question with with correct
+ * ansewer 'frog', partially correct answer 'toad' and defaultmark 1.
+ * This question also has a '*' match anything answer.
+ * @return stdClass
+ */
+ public function get_shortanswer_question_data_frogtoad() {
+ $qdata = new stdClass();
+ test_question_maker::initialise_question_data($qdata);
+
+ $qdata->qtype = 'shortanswer';
+ $qdata->name = 'Short answer question';
+ $qdata->questiontext = 'Name an amphibian: __________';
+ $qdata->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
+
+ $qdata->options = new stdClass();
+ $qdata->options->usecase = false;
+ $qdata->options->answers = array(
+ 13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
+ 14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
+ 15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
+ );
+
+ return $qdata;
+ }
+
+ /**
+ * Makes a shortanswer question with just the correct ansewer 'frog', and
+ * no other answer matching.
+ * @return qtype_shortanswer_question
+ */
+ public function make_shortanswer_question_frogonly() {
+ question_bank::load_question_definition_classes('shortanswer');
+ $sa = new qtype_shortanswer_question();
+ test_question_maker::initialise_a_question($sa);
+ $sa->name = 'Short answer question';
+ $sa->questiontext = 'Name the best amphibian: __________';
+ $sa->generalfeedback = 'Generalfeedback: you should have said frog.';
+ $sa->usecase = false;
+ $sa->answers = array(
+ 13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
+ );
+ $sa->qtype = question_bank::get_qtype('shortanswer');
+
+ return $sa;
+ }
+
+ /**
+ * Gets the question data for a shortanswer questionwith just the correct
+ * ansewer 'frog', and no other answer matching.
+ * @return stdClass
+ */
+ public function get_shortanswer_question_data_frogonly() {
+ $qdata = new stdClass();
+ test_question_maker::initialise_question_data($qdata);
+
+ $qdata->qtype = 'shortanswer';
+ $qdata->name = 'Short answer question';
+ $qdata->questiontext = 'Name the best amphibian: __________';
+ $qdata->generalfeedback = 'Generalfeedback: you should have said frog.';
+
+ $qdata->options = new stdClass();
+ $qdata->options->usecase = false;
+ $qdata->options->answers = array(
+ 13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
+ );
+
+ return $qdata;
+ }
+}
View
31 question/type/shortanswer/simpletest/testquestion.php
@@ -26,8 +26,8 @@
defined('MOODLE_INTERNAL') || die();
-require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
+require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
/**
@@ -108,7 +108,7 @@ public function test_compare_string_with_wildcard() {
}
public function test_is_complete_response() {
- $question = test_question_maker::make_a_shortanswer_question();
+ $question = test_question_maker::make_question('shortanswer');
$this->assertFalse($question->is_complete_response(array()));
$this->assertFalse($question->is_complete_response(array('answer' => '')));
@@ -118,7 +118,7 @@ public function test_is_complete_response() {
}
public function test_is_gradable_response() {
- $question = test_question_maker::make_a_shortanswer_question();
+ $question = test_question_maker::make_question('shortanswer');
$this->assertFalse($question->is_gradable_response(array()));
$this->assertFalse($question->is_gradable_response(array('answer' => '')));
@@ -128,7 +128,7 @@ public function test_is_gradable_response() {
}
public function test_grading() {
- $question = test_question_maker::make_a_shortanswer_question();
+ $question = test_question_maker::make_question('shortanswer');
$this->assertEqual(array(0, question_state::$gradedwrong),
$question->grade_response(array('answer' => 'x')));
@@ -139,26 +139,26 @@ public function test_grading() {
}
public function test_get_correct_response() {
- $question = test_question_maker::make_a_shortanswer_question();
+ $question = test_question_maker::make_question('shortanswer');
$this->assertEqual(array('answer' => 'frog'),
$question->get_correct_response());
}
public function test_get_question_summary() {
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$qsummary = $sa->get_question_summary();
$this->assertEqual('Name an amphibian: __________', $qsummary);
}
public function test_summarise_response() {
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$summary = $sa->summarise_response(array('answer' => 'dog'));
$this->assertEqual('dog', $summary);
}
public function test_classify_response() {
- $sa = test_question_maker::make_a_shortanswer_question();
+ $sa = test_question_maker::make_question('shortanswer');
$sa->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array(
@@ -174,4 +174,19 @@ public function test_classify_response() {
question_classified_response::no_response()),
$sa->classify_response(array('answer' => '')));
}
+
+ public function test_classify_response_no_star() {
+ $sa = test_question_maker::make_question('shortanswer', 'frogonly');
+ $sa->start_attempt(new question_attempt_step(), 1);
+
+ $this->assertEqual(array(
+ new question_classified_response(13, 'frog', 1.0)),
+ $sa->classify_response(array('answer' => 'frog')));
+ $this->assertEqual(array(
+ new question_classified_response(0, 'toad', 0.0)),
+ $sa->classify_response(array('answer' => 'toad')));
+ $this->assertEqual(array(
+ question_classified_response::no_response()),
+ $sa->classify_response(array('answer' => '')));
+ }
}
View
33 question/type/shortanswer/simpletest/testquestiontype.php
@@ -27,6 +27,7 @@
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/shortanswer/questiontype.php');
+require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
/**
@@ -52,12 +53,7 @@ public function tearDown() {
}
protected function get_test_question_data() {
- $q = new stdClass();
- $q->id = 1;
- $q->options->answers[1] = (object) array('answer' => 'frog', 'fraction' => 1);
- $q->options->answers[2] = (object) array('answer' => '*', 'fraction' => 0.1);
-
- return $q;
+ return test_question_maker::get_question_data('shortanswer');
}
public function test_name() {
@@ -69,18 +65,33 @@ public function test_can_analyse_responses() {
}
public function test_get_random_guess_score() {
- $q = $this->get_test_question_data();
+ $q = test_question_maker::get_question_data('shortanswer');
+ $q->options->answers[15]->fraction = 0.1;
$this->assertEqual(0.1, $this->qtype->get_random_guess_score($q));
}
public function test_get_possible_responses() {
- $q = $this->get_test_question_data();
+ $q = test_question_maker::get_question_data('shortanswer');
+
+ $this->assertEqual(array(
+ $q->id => array(
+ 13 => new question_possible_response('frog', 1),
+ 14 => new question_possible_response('toad', 0.8),
+ 15 => new question_possible_response('*', 0),
+ null => question_possible_response::no_response()
+ ),
+ ), $this->qtype->get_possible_responses($q));
+ }
+
+ public function test_get_possible_responses_no_star() {
+ $q = test_question_maker::get_question_data('shortanswer', 'frogonly');
$this->assertEqual(array(
$q->id => array(
- 1 => new question_possible_response('frog', 1),
- 2 => new question_possible_response('*', 0.1),
- null => question_possible_response::no_response()),
+ 13 => new question_possible_response('frog', 1),
+ 0 => new question_possible_response(get_string('didnotmatchanyanswer', 'question'), 0),
+ null => question_possible_response::no_response()
+ ),
), $this->qtype->get_possible_responses($q));
}
}
View
2  question/type/truefalse/simpletest/testquestiontype.php
@@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();
-require_once($CFG->dirroot . '/question/type//truefalse/questiontype.php');
+require_once($CFG->dirroot . '/question/type/truefalse/questiontype.php');
/**
Please sign in to comment.
Something went wrong with that request. Please try again.