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

Put fake records in the qbank, and fudge question loading, so we can preview.

This commit changes the purpose of test.php. Now that script creates the test
question records, and gives you links to preview them.
parent 765a3f91
No related branches found
No related tags found
No related merge requests found
......@@ -35,4 +35,15 @@ require_once($CFG->libdir . '/questionlib.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_stack extends question_type {
public function make_question($questiondata) {
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/unittest/simpletestlib.php');
require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
$q = test_question_maker::make_question('stack', $questiondata->questiontext);
$q->id = $questiondata->id;
$q->category = $questiondata->category;
return $q;
}
}
......@@ -165,7 +165,7 @@ class qtype_stack_test_helper extends question_test_helper {
test_question_maker::initialise_a_question($q);
$q->name = 'test-2';
$q->name = 'test-3';
$q->questionvariables = '';
$q->questiontext = '<p>1. Give an example of an odd function by typing
an expression which represents it.
......@@ -204,7 +204,7 @@ class qtype_stack_test_helper extends question_test_helper {
$node = new stack_potentialresponse_node($sans, $tans, 'AlgEquiv', null);
$node->add_branch(0, '=', 0, '', -1, '', 'odd-0-0');
$node->add_branch(1, '=', 1, '', -1, '', 'odd-0-1');
$q->prts['PotResTree_odd'] = new stack_potentialresponse_tree('PotResTree_odd',
$q->prts['odd'] = new stack_potentialresponse_tree('odd',
'', true, 0.25, $feedbackvars->get_session(), array($node));
$feedbackvars = new stack_cas_keyval('sa = subst(x=-x,ans2)-ans2', null, null, 't');
......@@ -213,7 +213,7 @@ class qtype_stack_test_helper extends question_test_helper {
$node = new stack_potentialresponse_node($sans, $tans, 'AlgEquiv', null);
$node->add_branch(0, '=', 0, '', -1, '', 'odd-0-0');
$node->add_branch(1, '=', 1, '', -1, '', 'odd-0-1');
$q->prts['PotResTree_even'] = new stack_potentialresponse_tree('PotResTree_even',
$q->prts['even'] = new stack_potentialresponse_tree('even',
'', true, 0.25, $feedbackvars->get_session(), array($node));
$feedbackvars = new stack_cas_keyval('sa1 = subst(x=-x,ans3)+ans3; sa2 = ans3-subst(x=-x,ans3)');
......@@ -230,7 +230,7 @@ class qtype_stack_test_helper extends question_test_helper {
$node1->add_branch(0, '+', 0, '', -1, 'Your answer is not an even function. Look, \[ f(x)-f(-x)=@sa2@ \neq 0.\]', 'oddeven-1-0');
$node1->add_branch(1, '+', 0.5, '', -1, '', 'EVEN');
$q->prts['PotResTree_oddeven'] = new stack_potentialresponse_tree('PotResTree_oddeven',
$q->prts['oddeven'] = new stack_potentialresponse_tree('oddeven',
'', true, 0.25, $feedbackvars->get_session(), array($node0, $node1));
$sans = new stack_cas_casstring('ans4', 't');
......@@ -238,7 +238,7 @@ class qtype_stack_test_helper extends question_test_helper {
$node = new stack_potentialresponse_node($sans, $tans, 'AlgEquiv', null);
$node->add_branch(0, '=', 0, '', -1, '', 'unique-0-0');
$node->add_branch(1, '=', 1, '', -1, '', 'unique-0-1');
$q->prts['PotResTree_unique'] = new stack_potentialresponse_tree('PotResTree_unique',
$q->prts['unique'] = new stack_potentialresponse_tree('unique',
'', true, 0.25, null, array($node));
$q->options = new stack_options();
......
......@@ -35,25 +35,75 @@ $context = context_system::instance();
require_capability('moodle/site:config', $context);
$PAGE->set_context($context);
$PAGE->set_url('/question/type/stack/test.php');
$title = 'Display test Stack question';
$title = 'Stack test questions';
$PAGE->set_title($title);
$testquestions = test_question_maker::get_test_helper('stack')->get_test_questions();
$testquestions = array_combine($testquestions, $testquestions);
// Make sure the global question category exists.
$category = $DB->get_record('question_categories', array('contextid' => $context->id, 'name' => 'Stack test questions'));
if (!$category) {
$category = new stdClass();
$category->name = 'Stack test questions';
$category->contextid = $context->id;
$category->info = '';
$category->infoformat = FORMAT_HTML;
$category->stamp = make_unique_id_code();
$category->parent = 0;
$category->sort = 999;
$category->id = $DB->insert_record('question_categories', $category);
}
$options = new question_display_options();
$q = test_question_maker::make_question('stack', $question);
$quba = question_engine::make_questions_usage_by_activity('qtype_stack', $context);
$quba->set_preferred_behaviour('deferredfeedback');
$slot = $quba->add_question($q, 1);
$quba->start_question($slot);
// See what existing and expected question definitions we have.
$existing = $DB->get_records('question', array('qtype' => 'stack', 'category' => $category->id),
'questiontext', 'questiontext, id');
$known = test_question_maker::get_test_helper('stack')->get_test_questions();
// Create any missing ones.
foreach ($known as $name) {
if (array_key_exists($name, $existing)) {
continue;
}
$question = new stdClass();
$question->id = 0;
$question->category = $category->id;
$question->parent = 0;
$question->qtype = 'stack';
$question->name = 'Fake Stack question ' . $name;
$question->questiontext = $name;
$question->questiontextformat = FORMAT_HTML;
$question->generalfeedback = '';
$question->generalfeedbackformat = FORMAT_HTML;
$question->defaultmark = 1;
$question->penalty = 0.3333333;
$question->length = 1;
$question->stamp = make_unique_id_code();
$question->version = make_unique_id_code();
$question->hidden = 0;
$question->timecreated = time();
$question->timemodified = time();
$question->createdby = $USER->id;
$question->modifiedby = $USER->id;
$question->id = $DB->insert_record('question', $question);
$existing[$name] = $question;
}
ksort($existing);
// Prepare some bits for output.
$qburl = new moodle_url('/question/edit.php', array('courseid' => $PAGE->course->id,
'category' => $category->id . ',' . $context->id));
$previewurl = new moodle_url('/question/preview.php', array('courseid' => $PAGE->course->id));
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
echo html_writer::tag('p', 'Which question do you want to display: ' .
$OUTPUT->single_select($PAGE->url, 'question', $testquestions, $question));
echo html_writer::tag('p', 'These faked Stack questions can also be found in ' .
html_writer::link($qburl, 'the top level of the question bank') . '.');
echo $quba->render_question($slot, $options);
echo html_writer::start_tag('ul');
foreach ($existing as $name => $q) {
echo html_writer::tag('li', html_writer::tag('b', $name) . ' ' .
html_writer::link(new moodle_url($previewurl, array('id' => $q->id)),
'[preview]'));
}
echo html_writer::end_tag('ul');
echo $OUTPUT->footer();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment