diff --git a/question/type/ddmarker/questiontype.php b/question/type/ddmarker/questiontype.php index dbe2462c41c..b12bf1ee2de 100644 --- a/question/type/ddmarker/questiontype.php +++ b/question/type/ddmarker/questiontype.php @@ -124,7 +124,7 @@ class qtype_ddmarker extends qtype_ddtoimage_base { array('questionid' => $formdata->id), '', 'no, id'); foreach (array_keys($formdata->drags) as $dragno) { - if (!empty($formdata->drags[$dragno]['label'])) { + if ($formdata->drags[$dragno]['label'] !== '') { $drag = new stdClass(); $drag->questionid = $formdata->id; $drag->no = $dragno + 1; @@ -144,10 +144,9 @@ class qtype_ddmarker extends qtype_ddtoimage_base { } else { $drag->id = $DB->insert_record('qtype_ddmarker_drags', $drag); } - } - } + if (!empty($olddragids)) { list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids)); $DB->delete_records_select('qtype_ddmarker_drags', "id $sql", $params); diff --git a/question/type/ddmarker/tests/helper.php b/question/type/ddmarker/tests/helper.php index f84185e2e8d..bbc06108820 100644 --- a/question/type/ddmarker/tests/helper.php +++ b/question/type/ddmarker/tests/helper.php @@ -35,7 +35,7 @@ defined('MOODLE_INTERNAL') || die(); */ class qtype_ddmarker_test_helper extends question_test_helper { public function get_test_questions() { - return array('fox', 'maths', 'mkmap'); + return array('fox', 'maths', 'mkmap', 'zerodrag'); } /** @@ -195,4 +195,69 @@ class qtype_ddmarker_test_helper extends question_test_helper { return $fromform; } + + /** + * Return the test data needed by the question generator (the data that + * would come from saving the editing form). + * @return stdClass date to create a ddmarkers question where one of the drag items has text '0'. + */ + public function get_ddmarker_question_form_data_zerodrag() { + global $CFG, $USER; + $fromform = new stdClass(); + + $bgdraftitemid = 0; + file_prepare_draft_area($bgdraftitemid, null, null, null, null); + $fs = get_file_storage(); + $filerecord = new stdClass(); + $filerecord->contextid = context_user::instance($USER->id)->id; + $filerecord->component = 'user'; + $filerecord->filearea = 'draft'; + $filerecord->itemid = $bgdraftitemid; + $filerecord->filepath = '/'; + $filerecord->filename = 'mkmap.png'; + $fs->create_file_from_pathname($filerecord, $CFG->dirroot . + '/question/type/ddmarker/tests/fixtures/mkmap.png'); + + $fromform->name = 'Drag digits'; + $fromform->questiontext = array( + 'text' => 'Put 0 in the left of the image, and 1 in the right.', + 'format' => FORMAT_HTML, + ); + $fromform->defaultmark = 2; + $fromform->generalfeedback = array( + 'text' => '', + 'format' => FORMAT_HTML, + ); + $fromform->bgimage = $bgdraftitemid; + $fromform->shuffleanswers = 0; + + $fromform->drags = array( + array('label' => '0', 'noofdrags' => 1), + array('label' => '1', 'noofdrags' => 1), + ); + + $fromform->drops = array( + array('shape' => 'Rectangle', 'coords' => '0,0;272,389', 'choice' => 1), + array('shape' => 'Rectangle', 'coords' => '272,0;272,389', 'choice' => 2), + ); + + test_question_maker::set_standard_combined_feedback_form_data($fromform); + + $fromform->penalty = '0.3333333'; + $fromform->hint = array( + array( + 'text' => 'Hint 1.', + 'format' => FORMAT_HTML, + ), + array( + 'text' => 'Hint 2.', + 'format' => FORMAT_HTML, + ), + ); + $fromform->hintshownumcorrect = array(1, 1); + $fromform->hintclearwrong = array(0, 1); + $fromform->hintoptions = array(0, 1); + + return $fromform; + } } diff --git a/question/type/ddmarker/tests/questiontype_test.php b/question/type/ddmarker/tests/questiontype_test.php index 4fac4557f93..8bd3692113e 100644 --- a/question/type/ddmarker/tests/questiontype_test.php +++ b/question/type/ddmarker/tests/questiontype_test.php @@ -37,7 +37,7 @@ require_once($CFG->dirroot . '/question/type/ddmarker/tests/helper.php'); * @copyright 2012 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class qtype_ddmarker_test extends basic_testcase { +class qtype_ddmarker_test extends advanced_testcase { /** @var qtype_ddmarker instance of the question type class to test. */ protected $qtype; @@ -56,4 +56,17 @@ class qtype_ddmarker_test extends basic_testcase { public function test_can_analyse_responses() { $this->assertTrue($this->qtype->can_analyse_responses()); } + + public function test_save_question() { + $this->resetAfterTest(); + $this->setAdminUser(); + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $cat = $questiongenerator->create_question_category(array()); + + $dd = $questiongenerator->create_question('ddmarker', 'zerodrag', + array('category' => $cat->id)); + $actual = question_bank::load_question_data($dd->id); + + $this->assertCount(2, $actual->options->drags); + } }