function printJSTranslation() { // if language is english, define __t as doing nothing if (currLang2() != 'en') { echo sprintf("var translationArray = %s;", jsonTranslationArray()); } else { echo "var translationArray = null;"; } }
function showPrevNext() { global $wpdb, $post; $table_name = $wpdb->prefix . "pb_lessons"; if (!isset($post)) { return ''; } //not in a single page $here = $post->ID; $thisrow = $wpdb->get_row("SELECT * FROM {$table_name} WHERE id = {$here}"); if ($thisrow == NULL) { return ''; } // not a numbered lesson $lo = $thisrow->ordering - 1; $hi = $thisrow->ordering + 2; $results = $wpdb->get_results("SELECT * FROM {$table_name} WHERE " . "ordering >= {$lo} AND ordering <= {$hi} AND lang = '" . currLang2() . "' " . "ORDER BY ordering ASC"); echo '<div class="locator">'; echo '<table class="locator"><tr>'; //echo '<td style="text-align: center;">Navigation</td>'; echo '<td class="locator">'; foreach ($results as $row) { if ($row->ordering < $thisrow->ordering) { $s = 'l'; } elseif ($row->ordering > $thisrow->ordering) { $s = 'r'; } else { $s = 'c'; } $factor = $row->ordering - $thisrow->ordering; if ($factor > 0) { $factor = $factor - 1; } $factor = abs($factor); $factor = 100 - 10 * $factor; $longname = $row->number . ': ' . $row->title; echo '<a style="font-size:' . $factor . '%" ' . 'class="open-same-window locator locator-' . $s . '" '; if ($s != 'c') { echo ' title="' . $longname . '" href="' . get_page_link($row->id) . '">'; } else { echo ' title="' . $longname . ' ' . __t('(goes to top of current page)') . '" onclick="scrollToTop()">'; } echo "<span class='buttn'>"; if ($thisrow->ordering == $row->ordering - 1) { echo "<span class='nextlesson'>" . __t("Next") . "</span> "; } echo $longname; echo '</span></a>'; } echo '</td>'; echo '</tr></table></div>'; }
function dbEntireHistory($limit, $sortname, $sortorder, $req = NULL) { global $db_query_info; $db_query_info = array(); if ($req == NULL) { $req = $_REQUEST; } $db_query_info['type'] = 'entire-history'; $user = getSoft($req, "user", ""); $problem = getSoft($req, "problemhash", ""); $resultdesc = array('y' => __t('Did not crash.'), 'Y' => __t('Correct!'), 'N' => __t('Incorrect.'), 'E' => __t('Internal error.'), 'S' => __t('Saved.'), 's' => __t('Saved.')); global $current_user; get_currentuserinfo(); global $wpdb; if (!is_user_logged_in()) { return __t("You must log in to view past submissions."); } if ($user == "all") { $u = "all"; } elseif ($user == "") { $u = $current_user; } elseif (userIsAdmin() || userIsAssistant()) { $u = get_userdata($user); if ($u === false) { return __t("User number not found."); } } else { $u = get_userdata($user); if ($u === false) { return __t("User number not found."); } if (strcasecmp(get_user_meta($user, 'pbguru', true), $current_user->user_login) != 0) { return sprintf(__t("User %s does not have you as their guru."), $user); } } if ($user != "") { $db_query_info['viewuser'] = $user; } // make an associative array indexed by slug $problemTable = $wpdb->get_results("SELECT slug, publicname, url FROM " . $wpdb->prefix . "pb_problems WHERE slug IS NOT NULL AND lang = '" . currLang2() . "'", OBJECT_K); $whereProblem = "1"; if ($problem != '') { if (!array_key_exists($problem, $problemTable)) { return sprintf(__t("Problem %s is unknown."), $problem); } $whereProblem = $wpdb->prepare("problem = %s", $problem); } $db_query_info['problem'] = $problem; $knownFields = array(__t("userid") => "userid", __t("time & ID") => "beginstamp", __t("problem") => "problem", __t("user code") => "usercode", __t("user input") => "userinput", __t("result") => "result"); if (array_key_exists($sortname, $knownFields)) { $sortString = $knownFields[$sortname] . " " . $sortorder . ", "; } else { $sortString = ""; } $whereStudent = NULL; if ($u == "all") { $whereStudent = userIsAdmin() ? "1" : "userid in " . getStudentList(); } else { $uid = $u->ID; $whereStudent = $wpdb->prepare("userid = %d", $uid); } $count = $wpdb->get_var("\nSELECT COUNT(1)\nFROM " . $wpdb->prefix . "pb_submissions \nWHERE {$whereStudent} AND {$whereProblem}"); if ($count == 0) { return __t("We do not have record of any submissions."); } $prep = "\nSELECT userid, ID, beginstamp, usercode, userinput, result, problem\nFROM " . $wpdb->prefix . "pb_submissions \nWHERE {$whereStudent} AND {$whereProblem}\nORDER BY {$sortString} ID DESC " . $limit; $flexirows = array(); foreach ($wpdb->get_results($prep, ARRAY_A) as $r) { $cell = array(); if ($u == "all") { $cell[__t('userid')] = str_replace(' ', "<br>", userString($r['userid'], true)); } $p = $r['problem']; if (array_key_exists($p, $problemTable)) { $cell[__t('problem')] = '<a class="open-same-window" href="' . $problemTable[$p]->url . '">' . $problemTable[$p]->publicname . '</a>'; } else { $cell[__t('problem')] = $p; } $cell[__t('user code')] = preBox($r['usercode'], -1, -1); $cell[__t('user input')] = $r['userinput'] == NULL ? '<i>' . __t('n/a') . '</i>' : preBox($r['userinput'], -1, 100000); if ($p != 'visualizer' && $p != 'visualizer-iframe') { $cell[__t('result')] = getSoft($resultdesc, $r['result'], '???'); } else { $cell[__t('result')] = '<i>n/a</i>'; } $cell[__t('time & ID')] = str_replace(' ', '<br/>', $r['beginstamp']) . '<br/>#' . $r['ID']; $flexirows[] = array('id' => $r['ID'], 'cell' => $cell); } return array('total' => $count, 'rows' => $flexirows); }
function pyMultiScrambleHandler($options, $content) { $id = generateId(); $answer = $options['answer']; if (substr($answer, 0, 6) == '@file:') { $answer = trim(softSafeDereference($answer)); } $answer = explode("\n", $answer); for ($i = 0; $i < count($answer); $i++) { $answer[$i] = array($i, $answer[$i]); } shuffle($answer); $r = ''; $slug = getSoft($options, 'slug', 'NULL'); $r .= "<div class='pybox modeNeutral multiscramble' id='pybox{$id}'>\n"; registerPybox($id, $slug, "multichoice scramble", FALSE, getSoft($options, 'title', NULL), $content, $options); if (isMakingDatabases()) { return do_short_and_sweetcode($content); } // faster db generation with accurate count $r .= checkbox($slug); if (!array_key_exists('slug', $options)) { $r .= slugwarn(); } $r .= heading(__t('Scramble Exercise'), $options); // $r .= "<b>Note (Dec 13)</b>: scramble exercises are temporarily broken — sorry!<br>"; $r .= $content; $r .= '<ul class="pyscramble" name="pyscramble" id="pyscramble' . $id . '">' . "\n"; foreach ($answer as $a) { $r .= '<li id="pyli' . $id . '_' . $a[0] . '" class="pyscramble" >' . $a[1] . '</li>' . "\n"; } $r .= '</ul>' . "\n"; $r .= '<div class="pyboxbuttons">'; $r .= "<input type='button' value='" . __t("Check answer") . "' onclick='pbMultiscrambleCheck({$id})'/>\n"; $r .= '<input type="hidden" name="lang" value="' . currLang4() . '"/>'; $r .= '<input type="hidden" name="slug" value="' . $slug . '"/>' . "\n"; $r .= '</div>'; $r .= '<div id="pbresults' . $id . '" class="pbresults"></div>'; $r .= '<div class="epilogue">' . getSoft($options, "epilogue", __t("Correct!")) . '</div>'; $r .= problemSourceWidget(array('slug' => $slug, 'lang' => currLang2())); $r .= '</div>'; return $r; }
function cscircles_makedb_page() { echo "<div class='wrap'>\n<h2>Rebuild CS Circles Databases</h2>\n<div>This page will rebuild the lesson database and the problem database. \n(If you're writing your own lessons, it assumes a structure like 99X-lessontitle\non the lesson slugs; contact us for help if needed.)</div>"; if (!array_key_exists('submitted', $_REQUEST)) { echo "<form method='get' action='admin.php'>\n <input type='hidden' name='page' value='cscircles-makedb'>\n <input type='hidden' name='submitted' value='true'>\n <button class='button-primary' id='submit'>Rebuild Databases</button></form>"; } else { $out = get_pages(); $lessons = array(); foreach ($out as $page) { if ($page->post_status != 'publish') { continue; } $s = $page->post_title; $m = preg_match('/^([0-9]+)([A-Za-z]?)\\: (.*)$/', $s, $matches); if (class_exists('PLL_Base')) { global $polylang; $lang = $polylang->get_post_language($page->ID); if ($lang != NULL) { $lang = $lang->slug; } else { $lang = substr(get_bloginfo("language"), 0, 2); } } else { // use default language $lang = currLang2(); } if ($m >= 1) { $lessons[] = array('number' => $matches[1] . $matches[2], 'title' => $matches[3], 'major' => $matches[1], 'minor' => $matches[2], 'id' => $page->ID, 'lang' => $lang); } elseif (class_exists('PLL_Base') && get_page_by_path('console')->ID == pll_get_post($page->ID, 'en') || get_page_by_path('console')->ID == $page->ID) { $lessons[] = array('id' => $page->ID, 'number' => NULL, 'lang' => $lang); } // go through the console page too, mainly to set up the right url in history grids, // it does not get added to pb_lessons but its contents do get added to pb_problems } function cmp($l1, $l2) { $c = strcmp($l1['lang'], $l2['lang']); if ($c != 0) { if ($l1['lang'] == 'en') { return -1; } // put english first if ($l2['lang'] == 'en') { return 1; } return $c; } if ($l1['number'] == NULL ^ $l2['number'] == NULL) { return $l1['number'] == NULL ? 1 : -1; } $c = $l1['major'] - $l2['major']; if ($c != 0) { return $c; } return strcmp($l1['minor'], $l2['minor']); } usort($lessons, 'cmp'); pyboxlog('[makedb] Rebuilding problem and lesson db', TRUE); global $wpdb, $SKIP_DB_REBUILD; $SKIP_DB_REBUILD = false; // set to true to be diagnostic $lesson_table_name = $wpdb->prefix . "pb_lessons"; $problem_table_name = $wpdb->prefix . "pb_problems"; if (!$GLOBALS['SKIP_DB_REBUILD']) { echo '<b>Truncating tables: '; $wpdb->query("TRUNCATE TABLE {$problem_table_name};"); $wpdb->query("TRUNCATE TABLE {$lesson_table_name};"); echo 'done</b>'; } $currlang = 'xx'; $i = -1; $gets = array(); foreach ($lessons as $l) { echo '<pre>'; if ($currlang != $l['lang']) { $currlang = $l['lang']; $i = 0; } if ($l['number'] == NULL) { $index = -1; } else { $index = $i; $i++; } $l['ordering'] = $index; if ($l['number'] != NULL) { echo 'About to insert lesson: ' . rowSummary($l); //. json_encode($l); if (!$GLOBALS['SKIP_DB_REBUILD']) { echo $wpdb->insert($lesson_table_name, $l) != 1 ? '<br>insert bad' : ' insert ok'; } } global $lesson_reg_info, $pyRenderCount, $post; $lesson_reg_info = array('index' => $index, 'lang' => $currlang, 'fullnumber' => $l['number'], 'url' => str_replace('/dev', '', get_page_link($l['id'])), 'id' => $l['id']); // pyboxlog($currlang . ' ' . $lesson_reg_info['url'], FALSE); // was used Apr. 7 to help transl. // render! the following line is not just cosmetic as it registers the problems. echo '<br>Snippet: ' . htmlspecialchars(substr(do_sweetcode(get_page($l['id'])->post_content), 0, 50)); $pyRenderCount = 0; // to get problem links working echo '</pre>'; } $lesson_reg_info = 1; } echo '</div>'; }
function pyUser($options, $content) { if (!is_user_logged_in()) { return __t("You must login to view your user page."); } global $wpdb; $user = wp_get_current_user(); $uid = $user->ID; $students = getStudents(); $cstudents = count($students); $problem_table = $wpdb->prefix . "pb_problems"; $problems = $wpdb->get_results("SELECT * FROM {$problem_table} WHERE facultative = 0 AND lang = '" . currLang2() . "' AND lesson IS NOT NULL ORDER BY lesson ASC, boxid ASC", ARRAY_A); $problemsByNumber = array(); foreach ($problems as $prow) { $problemsByNumber[$prow['slug']] = $prow; } $gp = getSoft($_GET, "problem", ""); if ($gp != "" && $gp != "console" && !array_key_exists($gp, $problemsByNumber)) { echo sprintf(__t("Problem %s not found (at least in current language)"), $gp); return; } if (userIsAdmin() || userIsAssistant() || $cstudents > 0) { $preamble = "<div class='progress-selector'>\n <form method='get'><table style='border:none'><tr><td>" . sprintf(__t("View one of your students? (you have %s)"), $cstudents) . '</td><td>'; $options = array(); $options[''] = __t('Show only me'); $options['all'] = __t('Summary of all my students'); if (!userIsAdmin()) { foreach ($students as $student) { $info = get_userdata($student); $options[$info->ID] = userString($info->ID); } } if (userIsAdmin()) { $preamble .= 'blank: you; "all": all; id#: user (<a href="' . cscurl('allusers') . '">list</a>) <input style = "padding:0px;width:60px" type="text" name="user" value="' . getSoft($_REQUEST, 'user', '') . '">'; } else { $preamble .= optionsHelper($options, 'user'); } $preamble .= '</td></tr><tr><td>'; $preamble .= __t("Just show submissions for one problem?"); $options = array(); $options[''] = __t('Show all'); $options['console'] = __t('Console'); foreach ($problems as $problem) { if ($problem['type'] == 'code') { $options[$problem['slug']] = $problem['publicname']; } } $preamble .= '</td><td>'; $preamble .= optionsHelper($options, 'problem'); $preamble .= "</td></tr><tr><td colspan='2' style='text-align:center'><input style='width: 25%' type='submit' value='" . __t('Submit') . "'/></tr></td></table></form></div>"; echo $preamble; } $allStudents = isSoft($_GET, 'user', 'all'); $viewingAsStudent = '' == getSoft($_GET, 'user', ''); $allProblems = $gp == ""; if (!$viewingAsStudent) { if ($allProblems) { $problem_html = "all problems"; } else { if ($gp == 'console') { $problem_html = "Console"; } else { $problem_html = "<a href='" . $problemsByNumber[$gp]['url'] . "'>" . $problemsByNumber[$gp]['publicname'] . "</a>"; } } } if (!$allStudents && array_key_exists('user', $_GET) && $_GET['user'] != '') { if (!is_numeric($_GET['user'])) { return __t("User id must be numeric."); } $getuid = (int) $_GET['user']; if (userIsAdmin() || userIsAssistant()) { if (get_userdata($getuid) === FALSE) { return __t("Invalid user id."); } } else { if (!in_array($getuid, $students)) { return __t("Invalid user id."); } } $uid = $getuid; $user = get_userdata($uid); echo "<div class='history-prenote'>" . sprintf(__t("Now viewing %s for "), $problem_html) . userString($uid) . '</div>'; } if ($allStudents) { echo "<div class='history-prenote'>" . sprintf(__t("Now viewing %s for all of your students"), $problem_html) . "</div>"; } /***************** end of header ***************/ $flexigrids = ""; $completed_table = $wpdb->prefix . "pb_completed"; if ($allStudents && !$allProblems && $gp != "console") { $flexigrids .= niceFlex('perstudent', sprintf(__t("Solutions by my students for %s"), $problemsByNumber[$_GET['problem']]['publicname']), 'problem-summary', 'dbProblemSummary', array('p' => $_GET['problem'])); } $dbparams = array(); if (getSoft($_GET, 'user', '') != '') { $dbparams['user'] = $_GET['user']; } if (getSoft($_GET, 'problem', '') != '') { $dbparams['problemhash'] = $_GET['problem']; } $flexigrids .= niceFlex('submittedcode', $allProblems ? __t("Submitted code") : sprintf(__t("Submitted code for %s"), $_GET['problem'] == 'console' ? 'Console' : $problemsByNumber[$_GET['problem']]['publicname']), 'entire-history', 'dbEntireHistory', $dbparams); $recent = ""; if (!$allStudents) { // queries more than 6 in order to fill out progress table of all problems $completed = $wpdb->get_results("SELECT * FROM {$completed_table} WHERE userid = {$uid} ORDER BY time DESC", ARRAY_A); $recent .= '<div class="recent"><span class="latest-title">' . __t("Latest problems completed") . ":</span>"; // but for now we only use 6 entries for "most recently completed" section for ($i = 0; $i < count($completed) && $i < 6; $i++) { $p = getSoft($problemsByNumber, $completed[$i]['problem'], FALSE); if ($p !== FALSE) { if (getSoft($_GET, 'user', '') != '') { if ($problemsByNumber[$p['slug']]['type'] == 'code') { $url = '.?user='******'user'] . '&problem=' . $p['slug']; } else { $url = null; } } else { $url = $p['url']; } $recent .= ' <a class="open-same-window problem-completed" '; if ($url != null) { $recent .= ' href="' . $url . '" '; } $recent .= ' title="' . $completed[$i]['time'] . '">' . $p['publicname'] . '</a>'; } else { $recent .= '[' . $completed[$i]['problem'] . ']'; } } $recent .= '</div>'; } $submissions_table = $wpdb->prefix . "pb_submissions"; $studentTable = ''; if ($allStudents && !userIsAdmin()) { $studentList = getStudentList(); $where = "WHERE userid in {$studentList}"; if (!$allProblems) { $where .= $wpdb->prepare("and problem LIKE %s", $gp); } // show number of problems each student completed $scompleted = $wpdb->get_results("SELECT userid, count(1) as comps from {$completed_table} {$where} GROUP BY userid", OBJECT_K); // show number of submissions by each student for this problem $ssubmissions = $wpdb->get_results("SELECT userid, count(1) as subs from {$submissions_table} {$where} GROUP BY userid", OBJECT_K); $studentTable .= '<div class="history-note">Student listing (click name to drill down)</div>'; $studentTable .= '<table>'; foreach (getStudents() as $stu) { $studentTable .= '<tr>'; $studentTable .= '<td>'; $studentTable .= '<a class="open-same-window" href="?user='******'&problem=' . $gp . '">'; $studentTable .= userString($stu); $studentTable .= '</a></td>'; $studentTable .= '<td>'; if ($allProblems) { $studentTable .= (array_key_exists($stu, $scompleted) ? $scompleted[$stu]->comps : 0) . ' completed'; } else { $studentTable .= '<img src="' . UFILES . (array_key_exists($stu, $scompleted) ? 'checked' : 'icon') . '.png"/>'; } $studentTable .= '</td>'; $studentTable .= '<td>'; $studentTable .= (array_key_exists($stu, $ssubmissions) ? $ssubmissions[$stu]->subs : 0) . ' submissions'; $studentTable .= '</td>'; $studentTable .= '</tr>'; } $studentTable .= '</table>'; } $lessons_table = $wpdb->prefix . "pb_lessons"; $lessons = $wpdb->get_results("SELECT * FROM {$lessons_table} WHERE lang = '" . currLang2() . "'", ARRAY_A); $lessonsByNumber = array(); foreach ($lessons as $lrow) { $lessonsByNumber[$lrow['ordering']] = $lrow; } $overview = ''; if ($allProblems || !$allStudents) { $overview = '<h2 style="margin-top:5px;text-align:center">' . __t('List of all problems') . ' ' . ($allStudents ? __t('(with #completed)') : __t('(with #submissions)')) . '</h2>'; if (!$viewingAsStudent) { $overview .= "<div style='text-align:center'>Click on the <img style='height:1em,width:1em' src='" . UFILES . "/icon.png'> to drill down.</div>"; } $checkIt = array(); //array from slug to boolean, whether to check the icon $showNum = array(); //array from slug to number, number to display beside each if ($allStudents) { if (userIsAdmin() || userIsAssistant()) { $completed = $wpdb->get_results("SELECT count(userid), problem from {$completed_table} GROUP BY problem", ARRAY_A); } else { $studentList = getStudentList(); $completed = $wpdb->get_results("SELECT count(userid), problem from {$completed_table} WHERE userid in {$studentList} GROUP BY problem", ARRAY_A); } foreach ($completed as $crow) { $showNum[$crow['problem']] = $crow['count(userid)']; } } else { $submissions = $wpdb->get_results("SELECT count(1), problem from {$submissions_table} WHERE userid = {$uid} GROUP BY problem", ARRAY_A); foreach ($submissions as $srow) { $showNum[$srow['problem']] = $srow['count(1)']; } foreach ($completed as $crow) { // this was queried earlier $checkIt[$crow['problem']] = TRUE; } } $overview .= '<table style="width:auto;border:none;margin:0px auto;">'; $lesson = -1; $lrow = NULL; $llink = ""; $firstloop = true; foreach ($problems as $prow) { if ($prow['lesson'] != $lesson) { if (!$firstloop) { $overview .= "</td></tr>\n"; } $firstloop = false; $overview .= "<tr><td class='lessoninfo'>"; $lesson = $prow['lesson']; $lrow = $lessonsByNumber[$lesson]; $overview .= '<a class="open-same-window" href="'; $llink = get_page_link($lrow['id']); $overview .= $llink; $overview .= '">'; $overview .= $lrow['number'] . ": " . $lrow['title']; $overview .= '</a></td><td>'; } if (!$viewingAsStudent) { // drill-down link $url = '.?user='******'user'] . '&problem=' . $prow['slug']; } else { $url = $prow['url']; } $overview .= '<a class="open-same-window" '; if ($url != null) { $overview .= ' href="' . $url . '" '; } $overview .= '>'; $overview .= '<table class="history-tablette" ><tr class="history-tablette-top"><td>'; $overview .= '<img style="margin:-10px 0px" title="' . $prow['publicname'] . '" src="' . UFILES . (isSoft($checkIt, $prow['slug'], TRUE) ? 'checked' : 'icon') . '.png"/>'; $overview .= '</a></td></tr><tr class="history-tablette-bottom"><td>'; /* $overview .= '<a class="open-same-window" '; if ($url != null) $overview .= ' href="' . $url . '" '; $overview .= '>';*/ $overview .= array_key_exists($prow['slug'], $showNum) ? $showNum[$prow['slug']] : ' '; $overview .= '</td></tr></table></a>'; } $overview .= '</table>'; } return "<div class='userpage'>{$flexigrids} {$recent} {$studentTable} {$overview}</div>"; }