I have the following PHP code which creates a python file out of some given code, executes it using some test cases, and grades the code based on how many test cases it passes. Note that every code example is a python function for some simple calculation.
<?php
if ($_POST["request_type"] === "release_grades") {
$_POST["request_type"] = "get_answers";
$answers = json_decode(very_simple_post_curl($back_address), TRUE);
$_POST["request_type"] = "get_quiz";
$questions = json_decode(very_simple_post_curl($back_address), TRUE);
foreach ($answers as $index => $answer) {
$answers[$index]["testcases"] = array();
$answers[$index]["constraints"] = array();
$data_original = $answer["AnswerText"];
$question = $questions[$answer["TestQuestionID"]];
$func_name = $question["QuestionFuncName"];
$testcases = $question["testcases"];
$answers[$index]["grade"] = $max_unscaled_grade = count($question["testcases"]);
foreach ($testcases as $key => $value) {
$data = $data_original . "
print(" . $func_name . $value["QuestionTestcaseArgs"] . ")";
$fh = file_put_contents('test.py', $data);
if ($fh === false)
die();
$output = trim(shell_exec("python test.py"));
if ($output === $value["QuestionTestcaseReturn"]) {
$answers[$index]["testcases"][$value["QuestionTestcaseID"]] = "PASSED";
} else {
$answers[$index]["testcases"][$value["QuestionTestcaseID"]] = "FAILED";
$answers[$index]["grade"] --;
}
}
$answers[$index]["grade"] /= $max_unscaled_grade;
$answers[$index]["grade"] *= intval($question["TestQuestionPoints"]);
}
}
Which iterates compares the result of some executed Python code with extracted test cases from the json below:
{"1":{"QuestionID":"1","QuestionDifficulty":"medium","QuestionText":"Write a function \"fibonacci\" that takes 1 integer parameter, n, which computes the fibonacci value for n.","QuestionFuncName":"fibonacci","testcases":[{"QuestionTestcaseID":"1","QuestionID":"1","QuestionTestcaseArgs":"(10, 10)","QuestionTestcaseReturn":"20"},{"QuestionTestcaseID":"2","QuestionID":"1","QuestionTestcaseArgs":"(3, 1)","QuestionTestcaseReturn":"4"},{"QuestionTestcaseID":"5","QuestionID":"1","QuestionTestcaseArgs":"(5)","QuestionTestcaseReturn":"5"},{"QuestionTestcaseID":"6","QuestionID":"1","QuestionTestcaseArgs":"(10)","QuestionTestcaseReturn":"55"}],"constraints":[{"QuestionConstraintID":"1","QuestionID":"1","QuestionConstraint":""}]}
However, I also have a function name for every code-input I execute, and before doing my test-case grading I want to see if the function names align, and modify the grade accordingly. How would I gather the function name from $data
, so that I can compare it with $func_name
?
EDIT: $data
looks like:
def add(a, b):
return a+b