Skip to content
Snippets Groups Projects
Commit 3d474b52 authored by Tim Hunt's avatar Tim Hunt
Browse files

Save the new question test when the form is submitted

parent dedee458
Branches
Tags
No related merge requests found
...@@ -48,6 +48,13 @@ $testcase = optional_param('testcase', null, PARAM_INT); ...@@ -48,6 +48,13 @@ $testcase = optional_param('testcase', null, PARAM_INT);
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST); $questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST);
$question = question_bank::load_question($questionid); $question = question_bank::load_question($questionid);
$context = $question->get_context(); $context = $question->get_context();
if ($testcase) {
$testcasedata = $DB->get_record('qtype_stack_qtests',
array('questionid' => $question->id, 'testcase' => $testcase), '*', MUST_EXIST);
} else {
$testcasedata = new stdClass();
$testcasedata->questionid = $question->id;
}
// Check permissions. // Check permissions.
require_login(); require_login();
...@@ -86,7 +93,46 @@ if ($mform->is_cancelled()) { ...@@ -86,7 +93,46 @@ if ($mform->is_cancelled()) {
} else if ($data = $mform->get_data()) { } else if ($data = $mform->get_data()) {
// Process form submission. // Process form submission.
// TODO. $transaction = $DB->start_delegated_transaction();
if (!$testcase) {
// Find the first unused testcase number.
$testcase = $DB->get_field_sql('
SELECT COALESCE(MIN(qt.testcase), 0) + 1
FROM {qtype_stack_qtests} qt
LEFT JOIN {qtype_stack_qtests} qt2 ON qt2.questionid = qt.questionid AND
qt2.testcase = qt.testcase + 1
WHERE qt.questionid = ? AND qt2.id IS NULL
', array($questionid));
$testcasedata->testcase = $testcase;
$DB->insert_record('qtype_stack_qtests', $testcasedata);
}
// Save the input data.
$DB->delete_records('qtype_stack_qtest_inputs', array('questionid' => $question->id, 'testcase' => $testcase));
foreach ($question->inputs as $inputname => $notused) {
$testinput = new stdClass();
$testinput->questionid = $question->id;
$testinput->testcase = $testcase;
$testinput->inputname = $inputname;
$testinput->value = $data->$inputname;
$DB->insert_record('qtype_stack_qtest_inputs', $testinput);
}
// Save the expected outcome data.
$DB->delete_records('qtype_stack_qtest_expected', array('questionid' => $question->id, 'testcase' => $testcase));
foreach ($question->prts as $prtname => $notused) {
$expected = new stdClass();
$expected->questionid = $question->id;
$expected->testcase = $testcase;
$expected->prtname = $prtname;
$expected->expectedscore = $data->{$prtname . 'score'};
$expected->expectedpenalty = $data->{$prtname . 'penalty'};
$expected->expectedanswernote = $data->{$prtname . 'answernote'};
$DB->insert_record('qtype_stack_qtest_expected', $expected);
}
$transaction->allow_commit();
redirect($backurl); redirect($backurl);
} }
......
...@@ -56,9 +56,12 @@ class qtype_stack_question_test_form extends moodleform { ...@@ -56,9 +56,12 @@ class qtype_stack_question_test_form extends moodleform {
foreach ($question->prts as $prtname => $prt) { foreach ($question->prts as $prtname => $prt) {
$elements = array( $elements = array(
$mform->createElement('text', $prtname . 'score', get_string('score', 'qtype_stack'), array('size' => 2)), $mform->createElement('text', $prtname . 'score',
$mform->createElement('text', $prtname . 'penalty', get_string('penalty', 'qtype_stack'), array('size' => 2)), get_string('score', 'qtype_stack'), array('size' => 2)),
$mform->createElement('text', $prtname . 'answernote', get_string('answernote', 'qtype_stack'), array('size' => 15)), $mform->createElement('text', $prtname . 'penalty',
get_string('penalty', 'qtype_stack'), array('size' => 2)),
$mform->createElement('text', $prtname . 'answernote',
get_string('answernote', 'qtype_stack'), array('size' => 15)),
); );
// TODO change the answernote field to be a dropdown list of the answer notes // TODO change the answernote field to be a dropdown list of the answer notes
// that this PRT can generate. // that this PRT can generate.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment