function pbmailpage($options, $content)
{
    if (!is_user_logged_in()) {
        return __t("You must login to view the mail page.");
    }
    $v = validate();
    if ($v[0] != 'success') {
        return $v[1];
    }
    // error message
    extract($v[1]);
    // $student, $problem, $focus, $sid
    $name = nicefiedUsername($sid, FALSE);
    $r = '';
    global $wpdb;
    $students = getStudents();
    $cstudents = count($students);
    $r .= reselector($students, $cstudents);
    $r .= '<hr style="width:80%;align:center;">';
    if ($problem !== NULL) {
        $finished = $wpdb->get_var($wpdb->prepare("SELECT time FROM " . $wpdb->prefix . "pb_completed WHERE userid = %d AND problem = %s", $sid, $problem['slug']));
        $r .= '<div class="history-note">' . sprintf(__t('Mail about %1$s [%3$s] for %2$s'), $problem['publicname'], userString($sid), '<a href="' . $problem['url'] . '">' . __t('link to original page') . '</a>');
        $r .= '</div>';
        if ($finished !== NULL) {
            $r .= "<img title='" . $student->user_login . __t(" has completed this problem.") . "' src='" . UFILES . "checked.png' class='pycheck'/>";
        } else {
            $r .= "<img title='" . $student->user_login . __t(" has not completed this problem.") . "' src='" . UFILES . "icon.png' class='pycheck'/>";
        }
        if ($finished !== NULL) {
            $r .= "<div class='history-prenote'>" . sprintf(__t('Note: this student completed the problem at %s'), $finished) . "</div>";
        }
        $r .= '<i>' . __t('Click on a message title to toggle the message open or closed.') . '</i>';
        global $mailcond;
        $messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "pb_mail WHERE ustudent = %d AND problem = %s AND {$mailcond} ORDER BY ID desc", $sid, $problem['slug']), ARRAY_A);
        foreach ($messages as $i => $message) {
            $c = $message['ID'] == $focus ? " showing" : " hiding";
            $idp = $message['ID'] == $focus ? " id='m' " : '';
            $r .= "<div {$idp} class='collapseContain{$c}' style='border-radius: 5px;'>";
            $title = __t("From") . " " . nicefiedUsername($message['ufrom'], FALSE) . ' ' . __t('to') . ' ' . nicefiedUsername($message['uto'], FALSE) . ', ' . $message['time'];
            if (count($messages) > 1 && $i == 0) {
                $title .= " " . __t("(newest)");
            }
            if (count($messages) > 1 && $i == count($messages) - 1) {
                $title .= " " . __t("(oldest)");
            }
            $r .= "<div class='collapseHead'><span class='icon'></span>{$title}</div>";
            $r .= "<div class='collapseBody'><span class='quoth'>" . __t("Quote/<br/>Reply") . "</span>" . preBox($message['body'], -1, 10000, "font-size:12px; line-height:14px; white-space: pre-wrap;") . '</div>';
            $r .= '</div>';
        }
        $to = "";
        if (getUserID() == $sid) {
            $guru_login = get_the_author_meta('pbguru', get_current_user_id());
            $guru = get_user_by('login', $guru_login);
            // FALSE if does not exist
            $to .= '<div style="text-align: center">';
            $to .= __t('Send a question by e-mail to: ');
            if ($guru !== FALSE) {
                $to .= "<select class='recipient'>\n<option value='1'>" . __t("My guru") . " ({$guru_login})</option>\n<option value='-1'>" . __t("CS Circles Assistant") . "</option>\n</select></div>";
            } else {
                $to .= "<select class='recipient'>\n<option value='-1'>" . __t("CS Circles Assistant") . "</option>\n<option value='0'>" . __t("(No guru is specified)") . "</option>\n</select>";
                $to .= '<br/></div>';
            }
        }
        $r .= '<div class="pybox fixsresize mailform" id="mailform">
<div id="bodyarea" class="pyboxTextwrap">
<textarea name="body" class="resizy" placeholder="' . __t('Type here to send a reply about this problem') . '" style="width:100%; white-space: pre-wrap; font-size: 11px; line-height:13px" rows=12></textarea>
</div>
' . $to;
        if (getUserID() != $sid) {
            $r .= '<input style="position:relative; top:2px" type="checkbox" id="noreply" onclick="toggleVisibility(\'bodyarea\')">' . ' <label style="font-size:75%" for="noreply">' . __t('Just mark as read without replying') . '</label><br>';
        }
        $r .= '<button onclick="mailReply(' . $sid . ',\'' . $problem['slug'] . '\');">' . __t('Send this message!') . '</button>
</div>';
        $problemname = $problem['publicname'];
        $r .= '<hr style="width:80%;align:center;">';
        if (getUserID() != $sid) {
            $r .= "<div class='history-note'><a href='" . cscurl('progress') . '?user='******'>" . sprintf(__t("%s's progress page (new window)"), $name) . "</a></div>";
        }
        $r .= "\n<div class='collapseContain hiding'>\n<div class='collapseHead'><span class='icon'></span>" . __t("Problem description for") . " " . $problem['publicname'] . "</div>\n<div class='collapseBody'>" . apply_filters('the_content', pyBoxHandler(json_decode($problem['shortcodeArgs'], TRUE), $problem['content'])) . "</div>\n</div>";
        if (getUserID() != $sid) {
            $r .= niceFlex('us', sprintf(__t('%1$s\'s submissions for %2$s'), $name, $problemname), 'problem-history', 'dbProblemHistory', array('user' => $sid, 'p' => $problem['slug']));
        }
        $r .= niceFlex('ms', sprintf(__t("My previous submissions for %s"), $problemname), 'problem-history', 'dbProblemHistory', array('p' => $problem['slug']));
        if (getUserID() != $sid) {
            $r .= niceFlex('omp', sprintf(__t("My other messages about %s"), $problemname), 'mail', 'dbMail', array('what' => $problem['slug'], 'xwho' => $sid));
        }
        $r .= niceFlex('oms', getUserID() == $sid ? __t("My messages for other programs") : sprintf(__t("Messages to/from %s for other problems"), $name), 'mail', 'dbMail', array('who' => $sid, 'xwhat' => $problem['slug']));
    }
    if ($cstudents > 0 || userIsAssistant()) {
        $r .= niceFlex('allstu', sprintf(__t("All messages ever about %s's work"), $name), 'mail', 'dbMail', array('who' => $sid));
    }
    $r .= niceFlex('allme', __t("All messages ever to or from me"), 'mail', 'dbMail', array());
    return $r;
}
function dbMail($limit, $sortname, $sortorder, $req = NULL)
{
    global $db_query_info;
    $db_query_info = array();
    $who = getSoft($req === NULL ? $_REQUEST : $req, "who", "");
    $xwho = getSoft($req === NULL ? $_REQUEST : $req, "xwho", "");
    $what = getSoft($req === NULL ? $_REQUEST : $req, "what", "");
    $xwhat = getSoft($req === NULL ? $_REQUEST : $req, "xwhat", "");
    $unans = getSoft($req === NULL ? $_REQUEST : $req, "unans", "");
    $db_query_info['type'] = 'mail-history';
    $db_query_info['who'] = $who;
    $db_query_info['xwho'] = $xwho;
    $db_query_info['what'] = $what;
    $db_query_info['xwhat'] = $xwhat;
    $db_query_info['unans'] = $unans;
    if (!is_user_logged_in()) {
        return __t("You must log in to view past mail.");
    }
    $where = 'WHERE 1';
    if (userIsAdmin()) {
        $where .= ' AND (uto = ' . getUserID() . ' OR uto = 0 OR ufrom = ' . getUserID() . ' OR ufrom = 0)';
    } else {
        $students = getStudents();
        $students[] = getUserID();
        $where .= ' AND (ustudent IN (' . implode(',', $students) . ') OR uto = ' . getUserID() . ' OR ufrom = ' . getUserID() . ' )';
    }
    if ($who != '') {
        if (!is_numeric($who)) {
            return sprintf(__t("%s must be numeric."), "'who'");
        }
        $who = (int) $who;
        if (userIsAdmin() || getUserID() == $who || getUserID() == guruIDID($who) || userIsAssistant()) {
            $where .= ' AND ustudent = ' . $who;
        } else {
            return __t("Access denied.");
        }
    } else {
        if ($xwho != '') {
            if (!is_numeric($xwho)) {
                return sprintf(__t("%s must be numeric."), "'xwho'");
            }
            $xwho = (int) $xwho;
            $where .= ' AND ustudent != ' . $xwho;
        }
    }
    if ($unans != '') {
        if (!is_numeric($unans)) {
            return sprintf(__t("%s must be numeric."), "'unans'");
        }
        $unans = (int) $unans;
        $where .= ' AND unanswered = ' . $unans;
    }
    global $wpdb;
    if ($what != '') {
        $where .= $wpdb->prepare(' AND problem = %s', $what);
    }
    if ($xwhat != '') {
        $where .= $wpdb->prepare(' AND problem != %s', $xwhat);
    }
    $table_name = $wpdb->prefix . "pb_mail";
    $knownFields = array(__t("from") => "ufrom", __t("to") => "uto", __t("when") => "time", __t("message") => "body", __t("problem") => "problem", __t("replied?") => "unanswered");
    $sortString = array_key_exists($sortname, $knownFields) ? $knownFields[$sortname] . " " . $sortorder . ", " : "";
    $count = $wpdb->get_var("SELECT COUNT(1) from {$table_name} {$where}");
    $prep = "SELECT * from {$table_name} {$where} ORDER BY {$sortString} ID DESC" . $limit;
    //   pyboxlog($prep);
    $flexirows = array();
    foreach ($wpdb->get_results($prep, ARRAY_A) as $r) {
        $cell = array();
        $cell[__t('from')] = nicefiedUsername($r['ufrom']);
        $cell[__t('to')] = nicefiedUsername($r['uto']);
        $url = cscurl('mail') . "?who=" . $r['ustudent'] . "&what=" . $r['problem'] . "&which=" . $r['ID'] . "#m";
        $cell[__t('when')] = str_replace(' ', '<br>', $r['time']);
        if ($what == '') {
            $cell[__t('problem')] = $r['problem'];
        }
        if ($unans == '') {
            $cell[__t('replied?')] = $r['unanswered'] == 1 ? __t('no') : __t('yes');
        }
        $cell[__t('message')] = "<a href='{$url}'>" . preBox($r['body']) . "</a>";
        $flexirows[] = array('id' => $r['ID'], 'cell' => $cell);
    }
    return array('total' => $count, 'rows' => $flexirows);
}