Skip to content

Commit

Permalink
Extend bulk tester to allow per-category question testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
trampgeek committed May 20, 2018
1 parent be128db commit 60a900a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
5 changes: 3 additions & 2 deletions bulktest.php
Expand Up @@ -16,7 +16,7 @@

/**
* This script runs all the question tests for all deployed versions of all
* questions in a given context.
* questions in a given context and, optionally, a given question category.
* It is a modified version of the script from the qtype_stack plugin.
*
* @package qtype_coderunner
Expand All @@ -29,6 +29,7 @@

// Get the parameters from the URL.
$contextid = required_param('contextid', PARAM_INT);
$categoryid = optional_param('categoryid', NULL, PARAM_INT);

// Login and check permissions.
$context = context::instance_by_id($contextid);
Expand Down Expand Up @@ -57,7 +58,7 @@
echo $OUTPUT->heading($title);

// Run the tests.
list($numpasses, $failingtests, $missinganswers) = $bulktester->run_all_tests_for_context($context);
list($numpasses, $failingtests, $missinganswers) = $bulktester->run_all_tests_for_context($context, $categoryid);

// Display the final summary.
$bulktester->print_overall_result($numpasses, $failingtests, $missinganswers);
Expand Down
49 changes: 45 additions & 4 deletions bulktestindex.php
Expand Up @@ -19,7 +19,7 @@
* [A modified version of the script in qtype_stack with the same name.]
*
* @package qtype_coderunner
* @copyright 2016 Richard Lobb, The University of Canterbury
* @copyright 2016, 2017 Richard Lobb, The University of Canterbury
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand Down Expand Up @@ -56,6 +56,8 @@
echo html_writer::tag('p', get_string('unauthorisedbulktest', 'qtype_coderunner'));
} else {
echo html_writer::start_tag('ul');
$buttonstyle = 'border: 1px solid gray; padding: 2px 2px 0px 2px;';
$buttonstyle = 'border: 1px solid #F0F0F0; background-color: #FFFFC0; padding: 2px 2px 0px 2px;border: 4px solid white';
foreach ($availablequestionsbycontext as $contextid => $numcoderunnerquestions) {
$context = context::instance_by_id($contextid);
$name = $context->get_context_name(true, true);
Expand All @@ -65,9 +67,34 @@
$class = 'bulktest coderunner context normal';
}

echo html_writer::tag('li', html_writer::link(
new moodle_url('/question/type/coderunner/bulktest.php', array('contextid' => $contextid)),
$name . ' (' . $numcoderunnerquestions . ')'), array('class' => $class));
$testallurl = new moodle_url('/question/type/coderunner/bulktest.php', array('contextid' => $contextid));
$testalllink = html_writer::link($testallurl,
get_string('bulktestallincontext', 'qtype_coderunner'),
array('title'=>get_string('testalltitle', 'qtype_coderunner'),
'style'=>$buttonstyle));
$expandlink = html_writer::link('#expand',
get_string('expand', 'qtype_coderunner'),
array('class'=>'expander',
'title'=>get_string('expandtitle', 'qtype_coderunner'),
'style'=> $buttonstyle));
$li_text = $name . ' (' . $numcoderunnerquestions . ') ' . $testalllink . ' ' . $expandlink;
echo html_writer::start_tag('li', array('class' => $class));
echo $li_text;

$categories = $bulktester->get_categories_for_context($contextid);
echo html_writer::start_tag('ul', array('class'=>'expandable'));
foreach ($categories as $cat) {
if ($cat->count > 0) {
$url = new moodle_url('/question/type/coderunner/bulktest.php',
array('contextid' => $contextid, 'categoryid' => $cat->id));
$linktext = $cat->name . ' (' . $cat->count . ')';
$link = html_writer::link($url, $linktext, array('style'=>$buttonstyle));
echo html_writer::tag('li', $link,
array('title'=>get_string('testallincategory', 'qtype_coderunner')));
}
}
echo html_writer::end_tag('ul');
echo html_writer::end_tag('li');
}

echo html_writer::end_tag('ul');
Expand All @@ -78,4 +105,18 @@
}
}

echo <<<SCRIPT_END
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function ($) {
$("ul.expandable").css("display", "none");
$(".expander").click(function(e) {
e.preventDefault();
$(this).next("UL").toggle();
});
});
</script>
SCRIPT_END;

echo $OUTPUT->footer();
11 changes: 8 additions & 3 deletions classes/bulk_tester.php
Expand Up @@ -112,7 +112,7 @@ public function get_categories_for_context($contextid) {
WHERE qc.id = q.category AND q.qtype='coderunner') AS count
FROM {question_categories} qc
WHERE qc.contextid = :contextid
ORDER BY qc.id",
ORDER BY qc.name",
array('contextid' => $contextid));
}

Expand Down Expand Up @@ -240,17 +240,19 @@ public function get_all_coderunner_questions_in_context($contextid) {

/**
* Run the sample answer for all questions belonging to
* a given context that have a sample answer.
* a given context that have a sample answer. Optionally restrict to a
* specified question category.
*
* Do output as we go along.
*
* @param context $context the context to run the tests for.
* @param int $categoryid test only questions in this category. Default to all.
* @return array with three elements:
* int a count of how many tests passed
* array of messages relating to the questions with failures
* array of messages relating to the questions without sample answers
*/
public function run_all_tests_for_context(context $context) {
public function run_all_tests_for_context(context $context, $categoryid=null) {
global $DB, $OUTPUT;

// Load the necessary data.
Expand All @@ -268,6 +270,9 @@ public function run_all_tests_for_context(context $context) {
$missinganswers = array();

foreach ($categories as $category) {
if ($categoryid !== null && $category->id != $categoryid) {
continue;
}
$questionids = $DB->get_records_menu('question',
array('category' => $category->id, 'qtype' => 'coderunner'), 'name', 'id,name');
if (!$questionids) {
Expand Down
7 changes: 6 additions & 1 deletion lang/en/qtype_coderunner.php
Expand Up @@ -70,10 +70,11 @@
$string['brokencombinator'] = 'Expected {$a->numtests} test results, got {$a->numresults}. Perhaps excessive output or error in question?';
$string['brokentemplategrader'] = 'Bad output from grader: {$a->output}. Your program execution may have aborted (e.g. a timeout or memory limit exceeded).';
$string['bulkquestiontester'] = 'The <a href="{$a->link}">bulk tester script</a> tests that the sample answers for all questions in the current context are marked right';
$string['bulktestallincontext'] = 'Test all';
$string['bulktestcontinuefromhere'] = 'Run again or resume, starting from here';
$string['bulktestindextitle'] = 'CodeRunner bulk testing';
$string['bulktestrun'] = 'Run all the question tests for all the questions in the system (slow, admin only)';
$string['bulktesttitle'] = 'Running all the question tests in {$a}';
$string['bulktesttitle'] = 'Testing questions in {$a}';

$string['coderunnercategories'] = 'Categories with CodeRunner questions';
$string['coderunnercontexts'] = 'Contexts with CodeRunner questions';
Expand Down Expand Up @@ -135,6 +136,8 @@
$string['errorstring-submissionlimitexceeded'] = 'Sandbox submission limit reached';
$string['errorstring-submissionfailed'] = 'Submission to sandbox failed';
$string['errorstring-unknown'] = 'Unexpected error while executing your code. The sandbox server may be down or overloaded. Perhaps try again shortly?';
$string['expand'] = 'Expand';
$string['expandtitle'] = 'Show question categories';
$string['expected'] = 'Expected output';
$string['expectedcolhdr'] = 'Expected';
$string['expected_help'] = 'The expected output from the test. Seen by the template as {{TEST.expected}}.';
Expand Down Expand Up @@ -649,6 +652,8 @@ function should be applied, e.g. `{{STUDENT_ANSWER | e(\'py\')}}` is the student
environment. This can result in different
students seeing different random variants of the question. See the documentation
for details.';
$string['testalltitle'] = 'Test all questions in this context';
$string['testallincategory'] = 'Test all questions in this category';
$string['testcase'] = 'Test case {$a}';
$string['testcasecontrols'] = 'Test properties:';
$string['testcasecontrols_help'] = 'If \'Use as example\' is checked, this test will be automatically included in the
Expand Down
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -22,7 +22,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2018051200;
$plugin->version = 2018052000;
$plugin->requires = 2015051100;
$plugin->cron = 0;
$plugin->component = 'qtype_coderunner';
Expand Down

0 comments on commit 60a900a

Please sign in to comment.