forked from drlippman/IMathAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblems.php
104 lines (85 loc) · 3.48 KB
/
problems.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
require_once "init_without_validate.php";
require_once 'assess2/AssessStandalone.php';
$GLOBALS['hide-sronly'] = true;
$_SESSION['graphdisp'] = 1;
$_SESSION['userprefs']['drawentry'] = 1;
function processContent($matches) {
$content = $matches[1];
$processedContent = makepretty($content);
return "`" . $processedContent . "`";
}
$a2 = new AssessStandalone($DBH);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$rawData = file_get_contents('php://input');
$data = json_decode($rawData, true);
if (!is_array($data)) {
throw new Exception('Received content contained invalid JSON!');
}
$qtype = $data['qtype'];
$control = $data['control'];
$qtext = $data['qtext'];
$solution = $data['solution'];
$seed = isset($data['seed']) ? $data["seed"] : rand(0,10000);
$stype = isset($data["stype"]) ? $data["stype"] : "template";
$input = '{"email":"[email protected]","id":"36","uniqueid":"1699501100492899","adddate":"1699501100","lastmoddate":"1699501137","ownerid":"1","author":"Nguyen,Vu","userights":"0","license":"1","description":"Algebra problem","qtype":"multipart","control":"","qcontrol":"","qtext":"","answer":"","solution":"","extref":"","hasimg":"0","deleted":"0","avgtime":"0","ancestors":"","ancestorauthors":"","otherattribution":"","importuid":"","replaceby":"0","broken":"0","solutionopts":"6","sourceinstall":"","meantimen":"1","meantime":"19","vartime":"0","meanscoren":"1","meanscore":"50","varscore":"0","isrand":"1"}';
$qn = 27;
$line = json_decode($input, true);
$line["qtype"] = $qtype;
$line["control"] = $control;
$line["qtext"] = $qtext;
if ($stype == "template") {
$line["solution"] = $solution;
} else {
$line["solution"] = "";
}
$a2->setQuestionData($qn, $line);
$state = array(
'seeds' => array($qn => $seed),
'qsid' => array($qn => $qn),
'stuanswers' => array(),
'stuanswersval' => array(),
'scorenonzero' => array(($qn+1) => -1),
'scoreiscorrect' => array(($qn+1) => -1),
'partattemptn' => array($qn => array()),
'rawscores' => array($qn => array())
);
$a2->setState($state);
$disp = $a2->displayQuestion($qn, [
'showans' => false,
'showallparts' => false,
'printformat' => true
]);
$question = $a2->getQuestion();
if ($stype == "template") {
$originalSolution = $question->getSolutionContent();
} else {
$vars = $question->getVarsOutput();
$sanitizedVars = [];
foreach ($vars as $key => $value) {
$sanitizedKey = ltrim($key, '$');
$sanitizedVars[$sanitizedKey] = $value;
}
extract($sanitizedVars);
ob_start();
eval($solution);
$originalSolution = ob_get_clean();
}
$prettySolution = preg_replace_callback('/`([^`]*)`/', 'processContent', $originalSolution);
$response = array(
"question" => $question->getQuestionContent(),
"originalSolution" => $originalSolution,
"solution" => $prettySolution,
"seed" => $seed,
"jsparams" => $disp["jsparams"],
"vars" => $question->getVarsOutput(),
"answers" => $question->getCorrectAnswersForParts()
);
// Send response
header('Content-Type: application/json');
echo json_encode($response);
} else {
header('HTTP/1.0 405 Method Not Allowed');
header('Content-Type: application/json');
echo json_encode(['status' => false, 'message' => 'Method Not Allowed']);
}