-
Notifications
You must be signed in to change notification settings - Fork 99
/
embedq.php
346 lines (320 loc) · 10.1 KB
/
embedq.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
//IMathAS: Embed a Question via iFrame
//(c) 2010 David Lippman
$init_skip_csrfp = true;
$inline_choicemap = !empty($CFG['GEN']['choicesalt']) ? $CFG['GEN']['choicesalt'] : 'test';
require_once "./init_without_validate.php";
unset($init_skip_csrfp);
header('P3P: CP="ALL CUR ADM OUR"');
$public = '?public=true';
$publica = '&public=true';
if((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO']=='https')) {
$urlmode = 'https://';
} else {
$urlmode = 'http://';
}
require_once "./assessment/displayq2.php";
$_SESSION = array();
$prefdefaults = array(
'mathdisp'=>1,
'graphdisp'=>1,
'drawentry'=>1,
'useed'=>1,
'livepreview'=>1);
$prefcookie = json_decode($_COOKIE["embedquserprefs"] ?? '', true);
$_SESSION['userprefs'] = array();
foreach($prefdefaults as $key=>$def) {
if ($prefcookie!==null && isset($prefcookie[$key])) {
$_SESSION['userprefs'][$key] = filter_var($prefcookie[$key], FILTER_SANITIZE_NUMBER_INT);
} else {
$_SESSION['userprefs'][$key] = $def;
}
}
foreach(array('graphdisp','mathdisp','useed') as $key) {
$_SESSION[$key] = $_SESSION['userprefs'][$key];
}
$showtips = 2;
$useeqnhelper = 4;
$courseUIver = 1;
$_SESSION['drill']['cid'] = 0;
$_SESSION['drill']['sa'] = 0;
$_SESSION['secsalt'] = "12345";
$cid = "embedq";
$myrights = 5;
if (empty($_GET['id'])) {
echo 'Need to supply an id';
exit;
}
$qsetid=Sanitize::onlyInt($_GET['id']);
$page_formAction = "embedq.php?id=$qsetid";
if (isset($_GET['theme'])) {
$theme = preg_replace('/\W/','',$_GET['theme']);
$_SESSION['coursetheme'] = $theme . '.css';
$page_formAction .= "&theme=$theme";
} else {
$_SESSION['coursetheme'] = $coursetheme;
}
if (isset($_GET['noscores'])) {
$page_formAction .= '&noscores=true';
}
if (isset($_GET['showans'])) {
//options:
// 0: never
// 1: after wrong attempts
// 2: after all attempts *default
// 3: always
$page_formAction .= '&showans='.Sanitize::onlyInt($_GET['showans']);
} else {
$_GET['showans'] = 2;
}
if (isset($_GET['noresults'])) {
$page_formAction .= '&noresults=true';
}
if (isset($_GET['noregen'])) {
$page_formAction .= '&noregen=true';
}
if (isset($_GET['resizer'])) {
$page_formAction .= '&resizer=true';
}
if ($_GET['showans']==3) {//show always
$showans = 1;
} else {
$showans = 0;
}
$qcol = array();
if (isset($_POST['seed']) && isset($_POST['check'])) {
$rawscores = array();
list($score,$rawscores[0]) = scoreq(0,$qsetid,$_POST['seed'],$_POST['qn0']);
if (strpos($score,'~')===false) {
$after = round($score,1);
if ($after < 0) { $after = 0;}
} else {
$fparts = explode('~',$score);
$after = array();
foreach ($fparts as $k=>$fpart) {
$after[$k] = round($fpart,2);
if ($after[$k]<0) {$after[$k]=0;}
}
$after = implode('~',$after);
}
if (empty($_GET['noresults'])) {
$qcol = explode('~',$rawscores[0]);
}
$page_scoreMsg = printscore($after,$qsetid,$_POST['seed']);
$pts = getpts($after);
$page_scoreMsg .= '<script type="text/javascript">
function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
if (inIframe()) {
window.parent.postMessage('.$pts.',"*");
}
</script>';
$seed = $_POST['seed'];
if ($_GET['showans']==2) {
$showans = 1;
} else if ($_GET['showans']==1 && getpts($after)<1) {
$showans = 1;
}
} else {
$page_scoreMsg = '';
$seed = rand(1,9999);
$lastanswers = array();
}
$flexwidth = true; //tells header to use non _fw stylesheet
$useeditor = 1;
if (isset($_GET['resizer'])) {
$placeinhead = '<script type="text/javascript" src="'.$staticroot.'/javascript/iframeSizer_contentWindow_min.js"></script>';
}
if (isset($_GET['frame_id'])) {
$frameid = preg_replace('/[^\w:.-]/','',$_GET['frame_id']);
$placeinhead .= '<script type="text/javascript">
function sendresizemsg() {
if(self != top){
var default_height = Math.max(
document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight,
document.documentElement.offsetHeight);
window.parent.postMessage( JSON.stringify({
subject: "lti.frameResize",
height: default_height,
frame_id: "'.$frameid.'"
}), "*");
}
}
if (mathRenderer == "Katex") {
window.katexDoneCallback = sendresizemsg;
} else if (typeof MathJax != "undefined") {
if (MathJax.startup) {
MathJax.startup.promise = MathJax.startup.promise.then(sendLTIresizemsg);
} else if (MathJax.Hub) {
MathJax.Hub.Queue(function () {
sendresizemsg();
});
}
} else {
$(function() {
sendresizemsg();
});
}
</script>';
if ($_SESSION['mathdisp']==1 || $_SESSION['mathdisp']==3) {
//in case MathJax isn't loaded yet
$placeinhead .= '<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function () {
sendresizemsg();
});
</script>';
}
}
require_once "./assessment/header.php";
if ($page_scoreMsg != '' && !isset($_GET['noscores'])) {
echo '<div class="review">' . _('Score on last question:') . $page_scoreMsg;
echo '</div>';
}
echo "<form id=\"qform\" method=\"post\" enctype=\"multipart/form-data\" action=\"" . Sanitize::encodeStringForDisplay($page_formAction) . "\" onsubmit=\"doonsubmit(this)\">\n";
echo "<input type=\"hidden\" name=\"seed\" value=\"" . Sanitize::encodeStringForDisplay($seed) . "\" />";
displayq(0,$qsetid,$seed,$showans,true,0,false,false,false,$qcol);
echo "<p><input type=submit name=\"check\" value=\"" . _('Check Answer') . "\">\n";
if (empty($_GET['noregen'])) {
echo " <input type=submit name=\"next\" value=\"" . _('New Question') . "\"/>\n";
}
echo '</p>';
echo '</form>';
/*
if ($showans) {
echo "<form id=\"qform\" method=\"post\" enctype=\"multipart/form-data\" action=\"" . Sanitize::encodeStringForDisplay($page_formAction) . "\" onsubmit=\"doonsubmit()\">\n";
echo "<p>" . _('Displaying last question with solution') . " <input type=submit name=\"next\" value=\"" . _('New Question') . "\"/></p>\n";
displayq(0,$qsetid,$seed,2,true,0,false,false,false,$qcol);
echo "</form>\n";
} else {
$doshowans = 0;
echo "<form id=\"qform\" method=\"post\" enctype=\"multipart/form-data\" action=\"" . Sanitize::encodeStringForDisplay($page_formAction) . "\" onsubmit=\"doonsubmit()\">\n";
echo "<input type=\"hidden\" name=\"seed\" value=\"" . Sanitize::encodeStringForDisplay($seed) . "\" />";
$lastanswers = array();
displayq(0,$qsetid,$seed,$doshowans,true,0,false,false,false,$qcol);
echo "<input type=submit name=\"check\" value=\"" . _('Check Answer') . "\">\n";
echo "</form>\n";
}
*/
require_once "./footer.php";
function getansweights($code,$seed) {
$foundweights = false;
if (($p = strpos($code,'answeights'))!==false || strpos($code,'anstypes')===false) {
$p = strpos($code,"\n",$p);
$weights = sandboxgetweights($code,$seed);
if (is_array($weights)) {
return $weights;
}
}
if (!$foundweights) {
preg_match('/anstypes\s*=(.*)/',$code,$match);
$n = substr_count($match[1],',')+1;
if ($n>1) {
$weights = array_fill(0,$n-1,round(1/$n,3));
$weights[] = 1-array_sum($weights);
return $weights;
} else {
return array(1);
}
}
}
function sandboxgetweights($code,$seed) {
$GLOBALS['RND']->srand($seed);
try {
eval(interpret('control','multipart',$code));
} catch (Throwable $thrownerror) {
if ($GLOBALS['myrights']>10) {
echo '<p>Caught error in evaluating a function in a question: ';
echo Sanitize::encodeStringForDisplay($thrownerror->getMessage());
echo '</p>';
}
}
if (!isset($answeights)) {
return array(1);
} else if (is_array($answeights)) {
return $answeights;
} else {
return explode(',',$answeights);
}
}
function printscore($sc,$qsetid,$seed) {
global $DBH,$imasroot,$staticroot;
$poss = 1;
if (strpos($sc,'~')===false) {
$sc = str_replace('-1','N/A',$sc);
$out = sprintf(_('%1$s out of %2$s'), $sc, $poss);
$pts = $sc;
if (!is_numeric($pts)) { $pts = 0;}
} else {
$stm = $DBH->prepare("SELECT control FROM imas_questionset WHERE id=:id");
$stm->execute(array(':id'=>$qsetid));
$control = $stm->fetchColumn(0);
$ptposs = getansweights($control,$seed);
for ($i=0; $i<count($ptposs)-1; $i++) {
$ptposs[$i] = round($ptposs[$i]*$poss,2);
}
//adjust for rounding
$diff = $poss - array_sum($ptposs);
$ptposs[count($ptposs)-1] += $diff;
$pts = getpts($sc);
$sc = str_replace('-1','N/A',$sc);
//$sc = str_replace('~',', ',$sc);
$scarr = explode('~',$sc);
foreach ($scarr as $k=>$v) {
if ($ptposs[$k]==0) {
$pm = 'gchk'; $alt=_('Correct');
} else if (!is_numeric($v) || $v==0) {
$pm = 'redx'; $alt=_('Incorrect');
} else if (abs($v-$ptposs[$k])<.011) {
$pm = 'gchk'; $alt=_('Correct');
} else {
$pm = 'ychk'; $alt=_('Partially correct');
}
$bar = "<img src=\"$staticroot/img/$pm.gif\" alt=\"$alt\"/>";
$scarr[$k] = "$bar $v/{$ptposs[$k]}";
}
$sc = implode(', ',$scarr);
//$ptposs = implode(', ',$ptposs);
$out = sprintf(_('%1$s out of %2$s (parts: %3$s)'), $pts, $poss, $sc);
}
$bar = '<span class="scorebarholder">';
if ($poss==0) {
$w = 30;
} else {
$w = round(30*$pts/$poss);
}
if ($w==0) {$w=1;}
if ($w < 15) {
$color = "#f".dechex(floor(16*($w)/15))."0";
} else if ($w==15) {
$color = '#ff0';
} else {
$color = "#". dechex(floor(16*(2-$w/15))) . "f0";
}
$bar .= '<span class="scorebarinner" style="background-color:'.$color.';width:'.$w.'px;"> </span></span> ';
return $bar . $out;
}
function getpts($sc) {
if (strpos($sc,'~')===false) {
if ($sc>0) {
return $sc;
} else {
return 0;
}
} else {
$sc = explode('~',$sc);
$tot = 0;
foreach ($sc as $s) {
if ($s>0) {
$tot+=$s;
}
}
return round($tot,1);
}
}
?>