function sandboxgetweights($code, $seed) { srand($seed); eval(interpret('control', 'multipart', $code)); if (!isset($answeights)) { if (!is_array($anstypes)) { $anstypes = explode(",", $anstypes); } $n = count($anstypes); if ($n > 1) { $answeights = array_fill(0, $n - 1, round(1 / $n, 5)); $answeights[] = 1 - array_sum($answeights); } else { $answeights = array(1); } } else { if (!is_array($answeights)) { $answeights = explode(',', $answeights); } } return $answeights; }
function get_status($disabled = false, $makediff = true, $stats = false) { global $somethingstaged, $gitpath; static $firstrun; if (!isset($firstrun)) { $firstrun = true; } else { $firstrun = false; } if (!$firstrun) { echo close_and_add_filelist_parent(); } $result = array('lines' => array()); $somethingstaged = false; clearstatcache(); $h = start_command($gitpath, array('status', '--porcelain'), false); if ($h === false) { return error('command failed to start'); } else { close_stdin($h); $err = ''; $out = ''; while (!is_done($h)) { $line = get_stdout_line($h); if ($line != '') { debug($line); $parsed = parse_line($line); $int = interpret($parsed, $disabled, $makediff, $stats); if ($int !== false) { if (isset($int['dir'])) { $list = array(); $list = add_directory_listing($parsed['dir'], $disabled, $makediff, $stats, $list); $result['lines'] = array_merge($result['lines'], $list); } else { $result['lines'][] = $int; } } flush(); } } $exit = get_exit_code($h); if ($exit !== 0) { $errors = get_all_data($h, array('stdout', 'stderr')); if (!is_array($errors)) { $errors = array(); } return error("command failed with exitcode " . $exit . ":\n" . implode(' ', $errors)); } $result['output'] = get_all_data($h); $result['outputhash'] = sha1($result['output']); $result['disable_commit'] = false; make_one_hash($result); } clean_up($h); return $result; }
function printq($qn, $qsetid, $seed, $pts) { global $isfinal, $imasroot; srand($seed); $query = "SELECT qtype,control,qcontrol,qtext,answer,hasimg FROM imas_questionset WHERE id='{$qsetid}'"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); $qdata = mysql_fetch_array($result, MYSQL_ASSOC); if ($qdata['hasimg'] > 0) { $query = "SELECT var,filename,alttext FROM imas_qimages WHERE qsetid='{$qsetid}'"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); while ($row = mysql_fetch_row($result)) { ${$row[0]} = "<img src=\"{$imasroot}/assessment/qimages/{$row[1]}\" alt=\"{$row[2]}\" />"; } } eval(interpret('control', $qdata['qtype'], $qdata['control'])); eval(interpret('qcontrol', $qdata['qtype'], $qdata['qcontrol'])); $toevalqtxt = interpret('qtext', $qdata['qtype'], $qdata['qtext']); $toevalqtxt = str_replace('\\', '\\\\', $toevalqtxt); $toevalqtxt = str_replace(array('\\\\n', '\\\\"', '\\\\$', '\\\\{'), array('\\n', '\\"', '\\$', '\\{'), $toevalqtxt); srand($seed + 1); eval(interpret('answer', $qdata['qtype'], $qdata['answer'])); srand($seed + 2); $la = ''; if (isset($choices) && !isset($questions)) { $questions =& $choices; } if (isset($variable) && !isset($variables)) { $variables =& $variable; } if ($displayformat == "select") { unset($displayformat); } //pack options if (isset($ansprompt)) { $options['ansprompt'] = $ansprompt; } if (isset($displayformat)) { $options['displayformat'] = $displayformat; } if (isset($answerformat)) { $options['answerformat'] = $answerformat; } if (isset($questions)) { $options['questions'] = $questions; } if (isset($answers)) { $options['answers'] = $answers; } if (isset($answer)) { $options['answer'] = $answer; } if (isset($questiontitle)) { $options['questiontitle'] = $questiontitle; } if (isset($answertitle)) { $options['answertitle'] = $answertitle; } if (isset($answersize)) { $options['answersize'] = $answersize; } if (isset($variables)) { $options['variables'] = $variables; } if (isset($domain)) { $options['domain'] = $domain; } if (isset($answerboxsize)) { $options['answerboxsize'] = $answerboxsize; } if (isset($hidepreview)) { $options['hidepreview'] = $hidepreview; } if (isset($matchlist)) { $options['matchlist'] = $matchlist; } if (isset($noshuffle)) { $options['noshuffle'] = $noshuffle; } if (isset($reqdecimals)) { $options['reqdecimals'] = $reqdecimals; } if (isset($grid)) { $options['grid'] = $grid; } if (isset($background)) { $options['background'] = $background; } if ($qdata['qtype'] == "multipart") { if (!is_array($anstypes)) { $anstypes = explode(",", $anstypes); } $laparts = explode("&", $la); foreach ($anstypes as $kidx => $anstype) { list($answerbox[$kidx], $tips[$kidx], $shans[$kidx]) = makeanswerbox($anstype, $kidx, $laparts[$kidx], $options, $qn + 1); } } else { list($answerbox, $tips[0], $shans[0]) = makeanswerbox($qdata['qtype'], $qn, $la, $options, 0); } echo "<div class=q>"; if ($isfinal) { echo "<div class=\"trq{$qn}\">\n"; } else { echo "<div class=m id=\"trq{$qn}\">\n"; } echo "<div class=qnum>" . ($qn + 1) . ") "; if (isset($_POST['points'])) { echo '<br/>' . $pts . 'pts'; } echo "</div>\n"; //end qnum div echo "<div class=floatl><div>\n"; //echo $toevalqtext; eval("\$evaledqtext = \"{$toevalqtxt}\";"); echo printfilter(filter($evaledqtext)); echo "</div>\n"; //end question div if (strpos($toevalqtxt, '$answerbox') === false) { if (is_array($answerbox)) { foreach ($answerbox as $iidx => $abox) { echo printfilter(filter("<div>{$abox}</div>\n")); echo "<div class=spacer> </div>\n"; } } else { //one question only echo printfilter(filter("<div>{$answerbox}</div>\n")); } } echo "</div>\n"; //end floatl div echo "</div>"; //end m div if (!$isfinal) { echo "<div class=cbutn>\n"; echo "<p><input type=button value=\"+1\" onclick=\"incspace({$qn},1)\"><input type=button value=\"+.5\" onclick=\"incspace({$qn},.5)\"><input type=button value=\"+.25\" onclick=\"incspace({$qn},.25)\"><input type=button value=\"+.1\" onclick=\"incspace({$qn},.1)\"><br/>"; echo "<input type=button value=\"-1\" onclick=\"incspace({$qn},-1)\"><input type=button value=\"-.5\" onclick=\"incspace({$qn},-.5)\"><input type=button value=\"-.25\" onclick=\"incspace({$qn},-.25)\"><input type=button value=\"-.1\" onclick=\"incspace({$qn},-.1)\"></p>"; echo "</div>\n"; //end cbutn div } echo " "; echo "</div>\n"; //end q div if (!isset($showanswer)) { return $shans; } else { return $showanswer; } }
function getqvalues($code, $type) { $partialcredit = array(); $qtol = array(); $feedbacktxtdef = array(); $qtold = array(); $code = substr($code, 0, strpos($code, '//end stored')); eval(interpret('control', $type, $code)); if (!isset($hinttext)) { $nhints = 0; } else { $nhints = count($hinttext); } if ($type == 'multipart') { $qtypes = explode(',', $anstypes); $nparts = count($qtypes); $qparts = array(); for ($n = 0; $n < $nparts; $n++) { if ($qtypes[$n] == 'number') { if (isset($reltolerance[$n])) { $qtol[$n] = 'rel'; $qtold[$n] = $reltolerance[$n]; } else { if (isset($abstolerance[$n])) { $qtol[$n] = 'abs'; $qtold[$n] = $abstolerance[$n]; } } $qparts[$n] = count($partialcredit[$n]) / 2; } else { if ($qtypes[$n] == 'choices') { $qparts[$n] = count($questions[$n]); } } } return array($nparts, $qtypes, $qparts, $nhints, $displayformat, $questions, $feedbacktxt, $feedbacktxtdef, $feedbacktxtessay, $answer, $hinttext, $partialcredit, $qtol, $qtold, $answerboxsize, $displayformat, $scoremethod, $noshuffle); } else { if ($type == 'number') { if (isset($reltolerance)) { $qtol[0] = 'rel'; $qtold[0] = $reltolerance; } else { if (isset($abstolerance)) { $qtol[0] = 'abs'; $qtold[0] = $abstolerance; } } $qparts = array(count($partialcredit) / 2); } else { if ($type == 'choices') { $qparts = array(count($questions)); } else { if ($type == 'essay') { $qparts = array(0); } } } return array(1, array($type), $qparts, $nhints, array($displayformat), array($questions), array($feedbacktxt), array($feedbacktxtdef), array($feedbacktxtessay), array($answer), $hinttext, array($partialcredit), $qtol, $qtold, array($answerboxsize), array($displayformat), array($scoremethod), array($noshuffle)); } }
function tokenize($str, $anstype, $countcnt) { global $allowedmacros; global $mathfuncs; global $disallowedwords, $disallowedvar; $i = 0; $connecttolast = 0; $len = strlen($str); $syms = array(); $lastsym = array('', -1); while ($i < $len) { $intype = 0; $out = ''; $c = $str[$i]; $len = strlen($str); if ($c == '/' && $str[$i + 1] == '/') { //comment while ($c != "\n" && $i < $len) { $i++; $c = $str[$i]; } $i++; $c = $str[$i]; $intype = 7; } else { if ($c == '$') { //is var $intype = 1; //read to end of var do { $out .= $c; $i++; if ($i == $len) { break; } $c = $str[$i]; } while ($c >= "a" && $c <= "z" || $c >= "A" && $c <= "Z" || $c >= '0' && $c <= '9' || $c == '_'); //if [ then array ref - read and connect as part of variable token if ($c == '[') { $connecttolast = 1; } //check if allowed var if (in_array($out, $disallowedvar)) { echo sprintf(_('Eeek.. unallowed var %s!'), $out); return array(array('', 9)); } } else { if ($c >= "a" && $c <= "z" || $c >= "A" && $c <= "Z") { //is str $intype = 2; //string like function name do { $out .= $c; $i++; if ($i == $len) { break; } $c = $str[$i]; } while ($c >= "a" && $c <= "z" || $c >= "A" && $c <= "Z" || $c >= '0' && $c <= '9' || $c == '_'); //check if it's a special word, and set type appropriately if it is if ($out == 'if' || $out == 'where' || $out == 'for') { $intype = 8; } else { if ($out == 'else' || $out == 'elseif') { $intype = 8; if ($out == 'else' && substr($str, $i, 3) == ' if') { $out = 'elseif'; $i += 3; } if ($lastsym[1] == 7) { array_pop($syms); $lastsym = $syms[count($syms) - 1]; } } else { if ($out == 'e') { $out = "exp(1)"; $intype = 3; } else { if ($out == 'pi') { $out = "(M_PI)"; $intype = 3; } else { if ($out == 'userid') { $out = '"userid"'; $intype = 6; } else { //eat whitespace while ($c == ' ') { $i++; $c = $str[$i]; } //could be sin^-1 or sin^(-1) - check for them and rewrite if needed if ($c == '^' && substr($str, $i + 1, 2) == '-1') { $i += 3; $out = 'arc' . $out; $c = $str[$i]; while ($c == ' ') { $i++; $c = $str[$i]; } } else { if ($c == '^' && substr($str, $i + 1, 4) == '(-1)') { $i += 3; $out = 'arc' . $out; $c = $str[$i]; while ($c == ' ') { $i++; $c = $str[$i]; } } } //if there's a ( then it's a function if ($c == '(' && $out != 'e' && $out != 'pi') { //rewrite logs if ($out == 'log') { $out = 'log10'; } else { if ($out == 'ln') { $out = 'log'; } else { //check it's and OK function if (!in_array($out, $allowedmacros)) { echo sprintf(_('Eeek.. unallowed macro %s'), $out); return array(array('', 9)); } } } //rewrite arctrig into atrig for PHP $out = str_replace(array("arcsin", "arccos", "arctan", "arcsinh", "arccosh", "arctanh"), array("asin", "acos", "atan", "asinh", "acosh", "atanh"), $out); //connect upcoming parens to function $connecttolast = 2; } else { //not a function, so what is it? if ($out == 'true' || $out == 'false' || $out == 'null') { //we like this - it's an acceptable unquoted string } else { // //an unquoted string! give a warning to instructor, //but treat as a quoted string. if (isset($GLOBALS['teacherid'])) { echo sprintf(_('Warning... unquoted string %s.. treating as string'), $out); } $out = "'{$out}'"; $intype = 6; } } } } } } } } else { if ($c >= '0' && $c <= '9' || $c == '.' && ($str[$i + 1] >= '0' && $str[$i + 1] <= '9')) { //is num $intype = 3; //number $cont = true; //handle . 3 which needs to act as concat if ($lastsym[0] == '.') { $syms[count($syms) - 1][0] .= ' '; } do { $out .= $c; $lastc = $c; $i++; if ($i == $len) { break; } $c = $str[$i]; if ($c >= '0' && $c <= '9' || $c == '.' && $str[$i + 1] != '.' && $lastc != '.') { //is still num } else { if ($c == 'e' || $c == 'E') { //might be scientific notation: 5e6 or 3e-6 $d = $str[$i + 1]; if ($d >= '0' && $d <= '9') { $out .= $c; $i++; if ($i == $len) { break; } $c = $str[$i]; } else { if (($d == '-' || $d == '+') && ($str[$i + 2] >= '0' && $str[$i + 2] <= '9')) { $out .= $c . $d; $i += 2; if ($i >= $len) { break; } $c = $str[$i]; } else { $cont = false; } } } else { $cont = false; } } } while ($cont); } else { if ($c == '(' || $c == '{' || $c == '[') { //parens or curlys if ($c == '(') { $intype = 4; //parens $leftb = '('; $rightb = ')'; } else { if ($c == '{') { $intype = 5; //curlys $leftb = '{'; $rightb = '}'; } else { if ($c == '[') { $intype = 11; //array index brackets $leftb = '['; $rightb = ']'; } } } $thisn = 1; $inq = false; $j = $i + 1; $len = strlen($str); while ($j < $len) { //read terms until we get to right bracket at same nesting level //we have to avoid strings, as they might contain unmatched brackets $d = $str[$j]; if ($inq) { //if inquote, leave if same marker (not escaped) if ($d == $qtype && $str[$j - 1] != '\\') { $inq = false; } } else { if ($d == '"' || $d == "'") { $inq = true; //entering quotes $qtype = $d; } else { if ($d == $leftb) { $thisn++; //increase nesting depth } else { if ($d == $rightb) { $thisn--; //decrease nesting depth if ($thisn == 0) { //read inside of brackets, send recursively to interpreter $inside = interpretline(substr($str, $i + 1, $j - $i - 1), $anstype, $countcnt + 1); if ($inside == 'error') { //was an error, return error token return array(array('', 9)); } //if curly, make sure we have a ;, unless preceeded by a $ which //would be a variable variable if ($rightb == '}' && $lastsym[0] != '$') { $out .= $leftb . $inside . ';' . $rightb; } else { $out .= $leftb . $inside . $rightb; } $i = $j + 1; break; } } else { if ($d == '/' && $str[$j + 1] == '/') { //comment inside brackers while ($d != "\n" && $j < $len) { $j++; $d = $str[$j]; } } else { if ($d == "\n") { //echo "unmatched parens/brackets - likely will cause an error"; } } } } } } $j++; } if ($j == $len) { $i = $j; echo _('unmatched parens/brackets - likely will cause an error'); } else { $c = $str[$i]; } } else { if ($c == '"' || $c == "'") { //string $intype = 6; $qtype = $c; do { $out .= $c; $i++; if ($i == $len) { break; } $lastc = $c; $c = $str[$i]; } while (!($c == $qtype && $lastc != '\\')); $out .= $c; $i++; $c = $str[$i]; } else { if ($c == "\n") { //end of line $intype = 7; $i++; if ($i < $len) { $c = $str[$i]; } } else { if ($c == ';') { //end of line $intype = 7; $i++; if ($i < $len) { $c = $str[$i]; } } else { //no type - just append string. Could be operators $out .= $c; $i++; if ($i < $len) { $c = $str[$i]; } } } } } } } } } while ($c == ' ') { //eat up extra whitespace $i++; if ($i == $len) { break; } $c = $str[$i]; if ($c == '.' && $intype == 3) { //if 3 . needs space to act like concat $out .= ' '; } } //if parens or array index needs to be connected to func/var, do it if ($connecttolast > 0 && $intype != $connecttolast) { //if func is loadlibrary, need to do so now so allowedmacros //will be expanded before reading the rest of the code if ($lastsym[0] == "loadlibrary") { loadlibrary(substr($out, 1, strlen($out) - 2)); array_pop($syms); $connecttolast = 0; } else { if ($lastsym[0] == 'importcodefrom' || $lastsym[0] == 'includecodefrom') { $out = intval(substr($out, 1, strlen($out) - 2)); $query = "SELECT control,qtype FROM imas_questionset WHERE id='{$out}'"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); if (mysql_num_rows($result) == 0) { //was an error, return error token return array(array('', 9)); } else { //$inside = interpretline(mysql_result($result,0,0),$anstype); $inside = interpret('control', $anstype, mysql_result($result, 0, 0), $countcnt + 1); if (mysql_result($result, 0, 1) != $anstype) { //echo 'Imported code question type does not match current question answer type'; } } if ($inside == 'error') { //was an error, return error token return array(array('', 9)); } else { array_pop($syms); $connecttolast = 0; $syms[] = array('{$included=true;' . $inside . ';$included=false;}', 5); //type: curly $syms[] = array('', 7); //end of line; $lastsym = array('', 7); } } else { $syms[count($syms) - 1][0] .= $out; $connecttolast = 0; if ($c == '[') { // multidim array ref? $connecttolast = 1; } } } } else { //add to symbol list, avoid repeat end-of-lines. if ($intype != 7 || $lastsym[1] != 7) { $lastsym = array($out, $intype); $syms[] = array($out, $intype); } } } return $syms; }
return $context->offset == $context->length; } $a = microtime(true); for ($i = 0; $i < 100000; ++$i) { fnmatch('foo*bar*baz', 'foo-bar-baz'); } echo 'fnmatch: ', number_format(microtime(true) - $a, 2), PHP_EOL; $a = microtime(true); for ($i = 0; $i < 100000; ++$i) { preg_match('/^foo.*bar.*baz$/', 'foo-bar-baz'); } echo 'preg_match: ', number_format(microtime(true) - $a, 2), PHP_EOL; $a = microtime(true); $tmp = [['vm_match', 'foo'], ['vm_substr', 'bar'], ['vm_substr', 'baz'], ['vm_eof', 0]]; for ($i = 0; $i < 100000; ++$i) { interpret('foo-bar-baz', $tmp); // substr('foo-bar-baz', -3) == 'baz'; // [['vm_match', 'foo'], ['vm_substr', 'bar'], ['vm_substr', 'baz'], ['vm_eof']]); } echo 'interpret: ', number_format(microtime(true) - $a, 2), PHP_EOL; $a = microtime(true); for ($i = 0; $i < 100000; ++$i) { contains('foo-bar-baz', array('foo', 'bar', 'baz')); } echo 'contains: ', number_format(microtime(true) - $a, 2), PHP_EOL; $a = microtime(true); for ($i = 0; $i < 100000; ++$i) { match('foo*bar*baz', 'foo-bar-baz', 0, 0); } echo 'match: ', number_format(microtime(true) - $a, 2), PHP_EOL; function match($first, $second, $i, $j) {
function sandboxgetweights($code, $seed) { srand($seed); $code = interpret('control', 'multipart', $code); if (($p = strrpos($code, 'answeights')) !== false) { $np = strpos($code, "\n", $p); if ($np !== false) { $code = substr($code, 0, $np) . ';if(isset($answeights)){return;};' . substr($code, $np); } else { $code .= ';if(isset($answeights)){return;};'; } //$code = str_replace("\n",';if(isset($answeights)){return;};'."\n",$code); } else { $p = strrpos($code, 'answeights'); $np = strpos($code, "\n", $p); if ($np !== false) { $code = substr($code, 0, $np) . ';if(isset($anstypes)){return;};' . substr($code, $np); } else { $code .= ';if(isset($answeights)){return;};'; } //$code = str_replace("\n",';if(isset($anstypes)){return;};'."\n",$code); } eval($code); if (!isset($answeights)) { if (!is_array($anstypes)) { $anstypes = explode(",", $anstypes); } $n = count($anstypes); if ($n > 1) { $answeights = array_fill(0, $n - 1, round(1 / $n, 3)); $answeights[] = 1 - array_sum($answeights); } else { $answeights = array(1); } } else { if (!is_array($answeights)) { $answeights = explode(',', $answeights); } } $sum = array_sum($answeights); if ($sum == 0) { $sum = 1; } foreach ($answeights as $k => $v) { $answeights[$k] = $v / $sum; } return $answeights; }
$translation['string'] = '("(.*)")'; $layout['float'] = '#\\$r(é|e)el#'; $translation['float'] = "([\\-]?[0-9]+(\\.[0-9]+)?)"; $layout['integer'] = '#\\$entier#'; $translation['integer'] = "(-?[0-9]+)"; $layout['char'] = '#\\$caract(é|e|è)re#'; $translation['char'] = "(\\'\\w\\')"; $layout['array'] = '#\\$tableau#'; $translation['array'] = "(\\[(([\\w+(,\\w)?]*|(?R))*)\\])"; foreach ($layout as $key => $value) { $structureTest = preg_replace($value, $translation[$key], $structureTest); } echo $structureTest . '<br /><br />'; echo preg_replace('#' . $structureTest . '#', $tradToC, $code); } interpret(); ?> /** $patterns[0] = '#(\$motClef|\$motCl(é|e))#i'; $remplacements[0] = '\s(algorithme|procedure|fonction|si|sinon|fin_?si|tant_?que|fin_?tant_?que|selon|autrement|' . 'fin_?selon|pour|fin_?pour|et|ou|mod|div)\s'; $patterns[1] = '#\$variable#i'; $remplacements[1] = '(\w+) ?'; $patterns[2] = '#\$op(é|e)rateur#i'; $remplacements[2] = '(\+|\-|\/|\*|\sdiv\s|\smod\s)';
function sandboxgetweights($code, $seed) { srand($seed); eval(interpret('control', 'multipart', $code)); if (!isset($answeights)) { return false; } else { if (is_array($answeights)) { return $answeights; } else { return explode(',', $answeights); } } }
function printq($qn, $qsetid, $seed, $pts, $showpts) { global $isfinal, $imasroot, $urlmode; $isbareprint = true; srand($seed); $query = "SELECT qtype,control,qcontrol,qtext,answer,hasimg FROM imas_questionset WHERE id='{$qsetid}'"; $result = mysql_query($query) or die("Query failed : {$query} " . mysql_error()); $qdata = mysql_fetch_array($result, MYSQL_ASSOC); if ($qdata['hasimg'] > 0) { $query = "SELECT var,filename,alttext FROM imas_qimages WHERE qsetid='{$qsetid}'"; $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error()); while ($row = mysql_fetch_row($result)) { if (isset($GLOBALS['CFG']['GEN']['AWSforcoursefiles']) && $GLOBALS['CFG']['GEN']['AWSforcoursefiles'] == true) { ${$row[0]} = "<img src=\"{$urlmode}s3.amazonaws.com/{$GLOBALS['AWSbucket']}/qimages/{$row[1]}\" alt=\"" . htmlentities($row[2], ENT_QUOTES) . "\" />"; } else { ${$row[0]} = "<img src=\"{$imasroot}/assessment/qimages/{$row[1]}\" alt=\"" . htmlentities($row[2], ENT_QUOTES) . "\" />"; } } } eval(interpret('control', $qdata['qtype'], $qdata['control'])); eval(interpret('qcontrol', $qdata['qtype'], $qdata['qcontrol'])); $toevalqtxt = interpret('qtext', $qdata['qtype'], $qdata['qtext']); $toevalqtxt = str_replace('\\', '\\\\', $toevalqtxt); $toevalqtxt = str_replace(array('\\\\n', '\\\\"', '\\\\$', '\\\\{'), array('\\n', '\\"', '\\$', '\\{'), $toevalqtxt); srand($seed + 1); eval(interpret('answer', $qdata['qtype'], $qdata['answer'])); srand($seed + 2); $la = ''; if (isset($choices) && !isset($questions)) { $questions =& $choices; } if (isset($variable) && !isset($variables)) { $variables =& $variable; } if ($displayformat == "select") { unset($displayformat); } //pack options if (isset($ansprompt)) { $options['ansprompt'] = $ansprompt; } if (isset($displayformat)) { $options['displayformat'] = $displayformat; } if (isset($answerformat)) { $options['answerformat'] = $answerformat; } if (isset($questions)) { $options['questions'] = $questions; } if (isset($answers)) { $options['answers'] = $answers; } if (isset($answer)) { $options['answer'] = $answer; } if (isset($questiontitle)) { $options['questiontitle'] = $questiontitle; } if (isset($answertitle)) { $options['answertitle'] = $answertitle; } if (isset($answersize)) { $options['answersize'] = $answersize; } if (isset($variables)) { $options['variables'] = $variables; } if (isset($domain)) { $options['domain'] = $domain; } if (isset($answerboxsize)) { $options['answerboxsize'] = $answerboxsize; } if (isset($hidepreview)) { $options['hidepreview'] = $hidepreview; } if (isset($matchlist)) { $options['matchlist'] = $matchlist; } if (isset($noshuffle)) { $options['noshuffle'] = $noshuffle; } if (isset($reqdecimals)) { $options['reqdecimals'] = $reqdecimals; } if (isset($grid)) { $options['grid'] = $grid; } if (isset($background)) { $options['background'] = $background; } if ($qdata['qtype'] == "multipart") { if (!is_array($anstypes)) { $anstypes = explode(",", $anstypes); } $laparts = explode("&", $la); foreach ($anstypes as $kidx => $anstype) { list($answerbox[$kidx], $tips[$kidx], $shans[$kidx]) = makeanswerbox($anstype, $kidx, $laparts[$kidx], $options, $qn + 1); } } else { list($answerbox, $tips[0], $shans[0]) = makeanswerbox($qdata['qtype'], $qn, $la, $options, 0); } echo "<div class=q>"; if ($isfinal) { echo "<div class=\"trq{$qn}\">\n"; } else { echo "<div class=m id=\"trq{$qn}\">\n"; } if ($showpts) { echo $qn + 1 . '. (' . $pts . ' pts) '; } echo "<div>\n"; //echo $toevalqtext; eval("\$evaledqtext = \"{$toevalqtxt}\";"); echo printfilter(filter($evaledqtext)); echo "</div>\n"; //end question div if (strpos($toevalqtxt, '$answerbox') === false) { if (is_array($answerbox)) { foreach ($answerbox as $iidx => $abox) { echo printfilter(filter("<div>{$abox}</div>\n")); echo "<div class=spacer> </div>\n"; } } else { //one question only echo printfilter(filter("<div>{$answerbox}</div>\n")); } } echo "</div>"; //end m div echo " "; echo "</div>\n"; //end q div if (!isset($showanswer)) { return $shans; } else { return $showanswer; } }
function scoreq($qnidx, $qidx, $seed, $givenans, $attemptn = 0, $qnpointval = 1) { unset($abstolerance); srand($seed); $GLOBALS['inquestiondisplay'] = false; $query = "SELECT qtype,control,answer FROM imas_questionset WHERE id='{$qidx}'"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); $qdata = mysql_fetch_array($result, MYSQL_ASSOC); if (isset($GLOBALS['lastanswers'])) { foreach ($GLOBALS['lastanswers'] as $iidx => $ar) { $arv = explode('##', $ar); $arv = $arv[count($arv) - 1]; $arv = explode('&', $arv); if (count($arv) == 1) { $arv = $arv[0]; } if (is_array($arv)) { foreach ($arv as $kidx => $arvp) { //if (is_numeric($arvp)) { if ($arvp === '') { $stuanswers[$iidx + 1][$kidx] = null; } else { if (strpos($arvp, '$!$') !== false) { $arvp = explode('$!$', $arvp); $arvp = $arvp[1]; if (is_numeric($arvp)) { $arvp = intval($arvp); } } if (strpos($arvp, '$#$') !== false) { $tmp = explode('$#$', $arvp); $arvp = $tmp[0]; $stuanswersval[$iidx + 1][$kidx] = $tmp[1]; } $stuanswers[$iidx + 1][$kidx] = $arvp; } //} else { // $stuanswers[$iidx+1][$kidx] = preg_replace('/\W+/','',$arvp); //} } } else { //if (is_numeric($arv)) { if ($arv === '' || $arv === 'ReGen') { $stuanswers[$iidx + 1] = null; } else { if (strpos($arv, '$!$') !== false) { $arv = explode('$!$', $arv); $arv = $arv[1]; if (is_numeric($arv)) { $arv = intval($arv); } } if (strpos($arv, '$#$') !== false) { $tmp = explode('$#$', $arv); $arv = $tmp[0]; $stuanswersval[$iidx + 1] = $tmp[1]; } $stuanswers[$iidx + 1] = $arv; } //} else { // $stuanswers[$iidx+1] = preg_replace('/\W+/','',$arv); //} } } } $thisq = $qnidx + 1; unset($stuanswers[$thisq]); //unset old stuanswer for this question if ($qdata['qtype'] == "multipart" || $qdata['qtype'] == 'conditional') { for ($kidx = 0; $kidx < count($_POST); $kidx++) { $partnum = ($qnidx + 1) * 1000 + $kidx; if (isset($_POST["tc{$partnum}"])) { $stuanswers[$qnidx + 1][$kidx] = stripslashes($_POST["tc{$partnum}"]); if ($_POST["qn{$partnum}"] === '') { $stuanswersval[$qnidx + 1][$kidx] = null; $stuanswers[$qnidx + 1][$kidx] = null; } else { if (is_numeric($_POST["qn{$partnum}"])) { $stuanswersval[$qnidx + 1][$kidx] = floatval($_POST["qn{$partnum}"]); } else { if (substr($_POST["qn{$partnum}"], 0, 2) == '[(') { //calcmatrix $stuav = stripslashes(str_replace(array('(', ')', '[', ']'), '', $_POST["qn{$partnum}"])); $stuanswersval[$qnidx + 1][$kidx] = str_replace(',', '|', $stuav); } } } } else { if (isset($_POST["qn{$partnum}"])) { if (isset($_POST["qn{$partnum}-0"])) { //calcmatrix with matrixsize $tmp = array(); $spc = 0; while (isset($_POST["qn{$partnum}-{$spc}"])) { $tmp[] = stripslashes($_POST["qn{$partnum}-{$spc}"]); $spc++; } $stuanswers[$qnidx + 1][$kidx] = implode('|', $tmp); $stuav = stripslashes(str_replace(array('(', ')', '[', ']'), '', $_POST["qn{$partnum}"])); $stuanswersval[$qnidx + 1][$kidx] = str_replace(',', '|', $stuav); } else { $stuanswers[$qnidx + 1][$kidx] = stripslashes_deep($_POST["qn{$partnum}"]); //preg_replace('/\W+/','',stripslashes($_POST["qn$partnum"])); if ($_POST["qn{$partnum}"] === '') { $stuanswersval[$qnidx + 1][$kidx] = null; $stuanswers[$qnidx + 1][$kidx] = null; } else { if (is_numeric($_POST["qn{$partnum}"])) { $stuanswersval[$qnidx + 1][$kidx] = floatval($_POST["qn{$partnum}"]); } } if (isset($_SESSION['choicemap'][$partnum])) { if (is_array($stuanswers[$qnidx + 1][$kidx])) { //multans foreach ($stuanswers[$qnidx + 1][$kidx] as $k => $v) { $stuanswers[$qnidx + 1][$kidx][$k] = $_SESSION['choicemap'][$partnum][$v]; } $stuanswers[$qnidx + 1][$kidx] = implode('|', $stuanswers[$qnidx + 1][$kidx]); } else { $stuanswers[$qnidx + 1][$kidx] = $_SESSION['choicemap'][$partnum][$stuanswers[$qnidx + 1][$kidx]]; if ($stuanswers[$qnidx + 1][$kidx] === null) { $stuanswers[$qnidx + 1][$kidx] = 'NA'; } } } } } else { if (isset($_POST["qn{$partnum}-0"])) { $tmp = array(); $spc = 0; while (isset($_POST["qn{$partnum}-{$spc}"])) { $tmp[] = stripslashes($_POST["qn{$partnum}-{$spc}"]); $spc++; } $stuanswers[$qnidx + 1][$kidx] = implode('|', $tmp); } } } } } else { if (isset($_POST["tc{$qnidx}"])) { $stuanswers[$qnidx + 1] = stripslashes($_POST["tc{$qnidx}"]); if (is_numeric($_POST["qn{$qnidx}"])) { $stuanswersval[$qnidx + 1] = floatval($_POST["qn{$qnidx}"]); } else { if (substr($_POST["qn{$qnidx}"], 0, 2) == '[(') { //calcmatrix $stuav = stripslashes(str_replace(array('(', ')', '[', ']'), '', $_POST["qn{$qnidx}"])); $stuanswersval[$qnidx + 1] = str_replace(',', '|', $stuav); } } } else { if (isset($_POST["qn{$qnidx}"])) { if (isset($_POST["qn{$qnidx}-0"])) { //calcmatrix with matrixsize $tmp = array(); $spc = 0; while (isset($_POST["qn{$qnidx}-{$spc}"])) { $tmp[] = stripslashes($_POST["qn{$qnidx}-{$spc}"]); $spc++; } $stuanswers[$qnidx + 1] = implode('|', $tmp); $stuav = stripslashes(str_replace(array('(', ')', '[', ']'), '', $_POST["qn{$qnidx}"])); $stuanswersval[$qnidx + 1] = str_replace(',', '|', $stuav); } else { $stuanswers[$qnidx + 1] = stripslashes_deep($_POST["qn{$qnidx}"]); //preg_replace('/\W+/','',stripslashes($_POST["qn$qnidx"])); if (is_numeric($_POST["qn{$qnidx}"])) { $stuanswersval[$qnidx + 1] = floatval($_POST["qn{$qnidx}"]); } if (isset($_SESSION['choicemap'][$qnidx])) { if (is_array($stuanswers[$qnidx + 1])) { //multans foreach ($stuanswers[$qnidx + 1] as $k => $v) { $stuanswers[$qnidx + 1][$k] = $_SESSION['choicemap'][$qnidx][$v]; } $stuanswers[$qnidx + 1] = implode('|', $stuanswers[$qnidx + 1]); } else { $stuanswers[$qnidx + 1] = $_SESSION['choicemap'][$qnidx][$stuanswers[$qnidx + 1]]; } } } } else { if (isset($_POST["qn{$qnidx}-0"])) { $tmp = array(); $spc = 0; while (isset($_POST["qn{$qnidx}-{$spc}"])) { $tmp[] = stripslashes($_POST["qn{$qnidx}-{$spc}"]); $spc++; } $stuanswers[$qnidx + 1] = implode('|', $tmp); } } } } eval(interpret('control', $qdata['qtype'], $qdata['control'])); srand($seed + 1); eval(interpret('answer', $qdata['qtype'], $qdata['answer'])); if (isset($choices) && !isset($questions)) { $questions =& $choices; } if (isset($variable) && !isset($variables)) { $variables =& $variable; } if (isset($reqdecimals) && !is_array($reqdecimals) && !isset($abstolerance) && !isset($reltolerance)) { $abstolerance = 0.5 / pow(10, $reqdecimals); } else { if (isset($reqdecimals) && is_array($reqdecimals)) { foreach ($reqdecimals as $kidx => $vval) { if (!isset($abstolerance[$kidx]) && !isset($reltolerance[$kidx])) { $abstolerance[$kidx] = 0.5 / pow(10, $vval); } } } } srand($seed + 2); //pack options from eval if (isset($answer)) { $options['answer'] = $answer; } if (isset($reltolerance)) { $options['reltolerance'] = $reltolerance; } if (isset($abstolerance)) { $options['abstolerance'] = $abstolerance; } if (isset($answerformat)) { $answerformat = str_replace(' ', '', $answerformat); $options['answerformat'] = $answerformat; } if (isset($questions)) { $options['questions'] = $questions; } if (isset($answers)) { $options['answers'] = $answers; } if (isset($answersize)) { $options['answersize'] = $answersize; } if (isset($variables)) { $options['variables'] = $variables; } if (isset($domain)) { $options['domain'] = $domain; } if (isset($requiretimes)) { $options['requiretimes'] = $requiretimes; } if (isset($requiretimeslistpart)) { $options['requiretimeslistpart'] = $requiretimeslistpart; } if (isset($scoremethod)) { $options['scoremethod'] = $scoremethod; } if (isset($strflags)) { $options['strflags'] = $strflags; } if (isset($matchlist)) { $options['matchlist'] = $matchlist; } if (isset($noshuffle)) { $options['noshuffle'] = $noshuffle; } if (isset($reqsigfigs)) { $options['reqsigfigs'] = $reqsigfigs; } if (isset($grid)) { $options['grid'] = $grid; } if (isset($snaptogrid)) { $options['snaptogrid'] = $snaptogrid; } if (isset($partweights)) { $options['partweights'] = $partweights; } if (isset($partialcredit)) { $options['partialcredit'] = $partialcredit; } if (isset($anstypes)) { $options['anstypes'] = $anstypes; } $score = 0; if ($qdata['qtype'] == "multipart") { if (!is_array($anstypes)) { $anstypes = explode(",", $anstypes); } if (in_array('essay', $anstypes) || in_array('file', $anstypes)) { $GLOBALS['questionmanualgrade'] = true; } $partla = array(); if (isset($answeights)) { if (!is_array($answeights)) { $answeights = explode(",", $answeights); } $localsum = array_sum($answeights); if ($localsum == 0) { $localsum = 1; } foreach ($answeights as $kidx => $vval) { $answeights[$kidx] = $vval / $localsum; } } else { if (count($anstypes) > 1) { if ($qnpointval == 0) { $qnpointval = 1; } $answeights = array_fill(0, count($anstypes) - 1, round($qnpointval / count($anstypes), 2)); $answeights[] = $qnpointval - array_sum($answeights); foreach ($answeights as $kidx => $vval) { $answeights[$kidx] = $vval / $qnpointval; } } else { $answeights = array(1); } } $scores = array(); $raw = array(); $accpts = 0; foreach ($anstypes as $kidx => $anstype) { $partnum = ($qnidx + 1) * 1000 + $kidx; $raw[$kidx] = scorepart($anstype, $kidx, $_POST["qn{$partnum}"], $options, $qnidx + 1); if (isset($scoremethod) && $scoremethod == 'acct') { if ($anstype == 'string' && $answer[$kidx] === '') { $scores[$kidx] = $raw[$kidx] - 1; //0 if correct, -1 if wrong } else { $scores[$kidx] = $raw[$kidx]; $accpts++; } } else { $scores[$kidx] = $raw[$kidx] < 0 ? 0 : round($raw[$kidx] * $answeights[$kidx], 4); } $raw[$kidx] = round($raw[$kidx], 2); $partla[$kidx] = $GLOBALS['partlastanswer']; } $partla = str_replace('&', '', $partla); $partla = preg_replace('/#+/', '#', $partla); if ($GLOBALS['lastanswers'][$qnidx] == '') { $GLOBALS['lastanswers'][$qnidx] = implode("&", $partla); } else { $GLOBALS['lastanswers'][$qnidx] .= '##' . implode("&", $partla); } //return array_sum($scores); if (isset($scoremethod) && $scoremethod == "singlescore") { return array(round(array_sum($scores), 3), implode('~', $raw)); } else { if (isset($scoremethod) && $scoremethod == "allornothing") { if (array_sum($scores) < 0.98) { return array(0, implode('~', $raw)); } else { return array(1, implode('~', $raw)); } } else { if (isset($scoremethod) && $scoremethod == "acct") { $sc = round(array_sum($scores) / $accpts, 3); return array($sc, implode('~', $raw)); } else { return array(implode('~', $scores), implode('~', $raw)); } } } } else { if ($qdata['qtype'] == 'essay' || $qdata['qtype'] == 'file') { $GLOBALS['questionmanualgrade'] = true; } $score = scorepart($qdata['qtype'], $qnidx, $givenans, $options, 0); if (isset($scoremethod) && $scoremethod == "allornothing") { if ($score < 0.98) { $score = 0; } } if ($qdata['qtype'] != 'conditional') { $GLOBALS['partlastanswer'] = str_replace('&', '', $GLOBALS['partlastanswer']); $GLOBALS['partlastanswer'] = preg_replace('/#+/', '#', $GLOBALS['partlastanswer']); } if ($GLOBALS['lastanswers'][$qnidx] == '') { $GLOBALS['lastanswers'][$qnidx] = $GLOBALS['partlastanswer']; } else { $GLOBALS['lastanswers'][$qnidx] .= '##' . $GLOBALS['partlastanswer']; } return array(round($score, 3), round($score, 2)); } }
<form name='editor' id='editor' action='punga.php' method='post'> <textarea rows='10' class='span6' name='code' placeholder='enter your code here'><?php echo $_POST['code']; ?> </textarea> <a onClick='document.forms["editor"].submit()' class='btn'>Evaluate</a> </form> </div> <div class="span6"> <?php require_once "parable.php"; $bootstrap = explode("\n", $_POST['code']); foreach ($bootstrap as $src) { if (strlen(trim($src)) > 0) { $s = compile($src, request_slice()); interpret($s); } } if (!empty($errors)) { echo "<div class='alert alert-error'>"; foreach ($errors as $err) { echo "<tt>{$err}</tt><br>\n"; } $errors = array(); echo "</div>"; } $TYPE_NUMBER = 100; $TYPE_STRING = 200; $TYPE_CHARACTER = 300; $TYPE_FUNCTION = 400; $TYPE_FLAG = 500;
function parse_bootstrap($lines) { foreach ($lines as $src) { if (strlen(trim($src)) > 0) { $s = compile($src, request_slice()); interpret($s); } } }
$qsetids[$row[0]] = $row[2]; } if ($dobca) { $qcontrols = array(); $qanswers = array(); $mathfuncs = array("sin", "cos", "tan", "sinh", "cosh", "arcsin", "arccos", "arctan", "arcsinh", "arccosh", "sqrt", "ceil", "floor", "round", "log", "ln", "abs", "max", "min", "count"); $allowedmacros = $mathfuncs; require_once "../assessment/mathphp2.php"; require "../assessment/interpret5.php"; require "../assessment/macros.php"; $qsetidlist = implode(',', $qsetids); $query = "SELECT id,qtype,control,answer FROM imas_questionset WHERE id IN ({$qsetidlist})"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); while ($row = mysql_fetch_row($result)) { $qcontrols[$row[0]] = interpret('control', $row[1], $row[2]); $qanswers[$row[0]] = interpret('answer', $row[1], $row[3]); } } $gb = array(); //create headers $gb[0][0] = "Name"; $gb[1][0] = ""; $qcol = array(); foreach ($itemarr as $k => $q) { $qcol[$q] = 1 + $outcol * $k; $offset = 0; if ($dopts) { $gb[0][1 + $outcol * $k + $offset] = "Question " . $itemnum[$q]; $gb[1][1 + $outcol * $k + $offset] = "Points (" . $qpts[$q] . " possible)"; $offset++; }
function showresults($q, $qtype) { global $qdata, $qsids, $qsdata; eval(interpret('control', $qtype, $qsdata[$qsids[$q]][1])); if ($qtype == 'choices' || $qtype == 'multans' || $qtype == 'multipart') { if (isset($choices) && !isset($questions)) { $questions =& $choices; } if ($qtype == 'multipart') { if (!is_array($anstypes)) { $anstypes = explode(',', $anstypes); } foreach ($anstypes as $i => $type) { if ($type == 'choices' || $type == 'multans') { if (isset($questions[$i])) { $ql = $questions[$i]; } else { $ql = $questions; } if ($type == 'multans') { if (is_array($answers)) { $al = $answers[$i]; } else { $al = $answers; } } else { if ($type == 'choices') { if (is_array($answer)) { $al = $answer[$i]; } else { $al = $answer; } } } disp($q, $type, $i, $al, $ql); } else { if (is_array($answer)) { $al = $answer[$i]; } else { $al = $answer; } disp($q, $type, $i, $al); } } } else { if ($qtype == 'multans') { $al = $answers; } else { if ($qtype == 'choices') { $al = $answer; } } disp($q, $qtype, -1, $al, $questions); } } else { disp($q, $qtype, -1, $answer); } }