/**
 * Returns a formatted XHTML form code to handle the specified question.<br>
 * Form fields names are: answer_text, answer_id<br>
 * CSS classes:<ul>
 * <li>div.tcecontentbox</li>
 * <li>div.rowl</li>
 * <li>textarea.answertext</li>
 * </ul>
 * @param $test_id (int) test ID
 * @param $testlog_id (int) test log ID
 * @param $formname (string) form name (form ID)
 * @return string XHTML code
 */
function F_questionForm($test_id, $testlog_id, $formname)
{
    require_once '../config/tce_config.php';
    require_once '../../shared/code/tce_functions_tcecode.php';
    global $db, $l, $examtime, $timeout_logout;
    $test_id = intval($test_id);
    $testlog_id = intval($testlog_id);
    $user_id = intval($_SESSION['session_user_id']);
    $aswkeys = array();
    $str = '';
    if (!isset($test_id) or $test_id == 0) {
        return;
    }
    $testdata = F_getTestData($test_id);
    $noanswer_hidden = '';
    $noanswer_disabled = '';
    if (!F_getBoolean($testdata['test_noanswer_enabled'])) {
        $noanswer_hidden = ' style="visibility:hidden;display:none;"';
        $noanswer_disabled = ' readonly="readonly" disabled="disabled"';
    }
    // select question for the first time
    if (!isset($testlog_id) or $testlog_id == 0) {
        //select first question
        $sql = 'SELECT testlog_id
			FROM ' . K_TABLE_TEST_USER . ', ' . K_TABLE_TESTS_LOGS . '
			WHERE testlog_testuser_id=testuser_id
				AND testuser_test_id=' . $test_id . '
				AND testuser_user_id=' . $user_id . '
			ORDER BY testlog_id
			LIMIT 1';
        if ($r = F_db_query($sql, $db)) {
            if ($m = F_db_fetch_array($r)) {
                $testlog_id = $m['testlog_id'];
            } else {
                return;
            }
        } else {
            F_display_db_error();
        }
    }
    // build selection query for question to display
    $sql = 'SELECT *
			FROM ' . K_TABLE_QUESTIONS . ', ' . K_TABLE_TESTS_LOGS . '
			WHERE question_id=testlog_question_id
				AND testlog_id=' . $testlog_id . '
			LIMIT 1';
    if ($r = F_db_query($sql, $db)) {
        if ($m = F_db_fetch_array($r)) {
            if (F_getBoolean($m['question_fullscreen'])) {
                // hide some section for fullscreen mode
                $str .= '<style>' . K_NEWLINE;
                $str .= '.header{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '.infolink{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= 'h1{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '.pagehelp{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '.userbar{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '.minibutton{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '.navlink{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '.testcomment{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '#terminatetest{visibility:hidden;display:none;}' . K_NEWLINE;
                $str .= '</style>' . K_NEWLINE;
            }
            $str .= '<input type="hidden" name="testid" id="testid" value="' . $test_id . '" />' . K_NEWLINE;
            $str .= '<input type="hidden" name="testlogid" id="testlogid" value="' . $testlog_id . '" />' . K_NEWLINE;
            $str .= '<input type="hidden" name="testuser_id" id="testuser_id" value="' . $m['testlog_testuser_id'] . '" />' . K_NEWLINE;
            // get test data
            $test_data = F_getTestData($test_id);
            // store time information for interactive timer
            $examtime = F_getTestStartTime($m['testlog_testuser_id']) + $test_data['test_duration_time'] * K_SECONDS_IN_MINUTE;
            $str .= '<input type="hidden" name="examtime" id="examtime" value="' . $examtime . '" />' . K_NEWLINE;
            if (F_getBoolean($test_data['test_logout_on_timeout'])) {
                $str .= '<input type="hidden" name="timeout_logout" id="timeout_logout" value="1" />' . K_NEWLINE;
            }
            $str .= '<a name="questionsection" id="questionsection"></a>' . K_NEWLINE;
            $str .= '<div class="tcecontentbox">' . K_NEWLINE;
            //fieldset
            //$str .= '<legend>';
            //$str .= $l['w_question'];
            //$str .= '</legend>'.K_NEWLINE;
            // display question description
            if ($m['question_type'] == 3) {
                $str .= '<label for="answertext">';
            }
            $str .= F_decode_tcecode($m['question_description']) . K_NEWLINE;
            if ($m['question_type'] == 3) {
                $str .= '</label>';
            }
            $str .= '<div class="row">' . K_NEWLINE;
            $str .= '<hr/>' . K_NEWLINE;
            $str .= '</div>' . K_NEWLINE;
            $str .= '<div class="rowl">' . K_NEWLINE;
            if ($m['question_type'] == 3) {
                // TEXT - free text question
                $str .= '<textarea cols="' . K_ANSWER_TEXTAREA_COLS . '" rows="' . K_ANSWER_TEXTAREA_ROWS . '" name="answertext" id="answertext" class="answertext">';
                $str .= $m['testlog_answer_text'];
                $str .= '</textarea>' . K_NEWLINE;
            } else {
                // multiple-choice question
                $checked = false;
                if (F_getBoolean($m['question_inline_answers'])) {
                    // inline display
                    $str .= '<ol class="answer_inline">' . K_NEWLINE;
                } else {
                    $str .= '<ol class="answer">' . K_NEWLINE;
                }
                if ($m['question_type'] == 4) {
                    // get max positions for odering questions
                    $max_position = F_count_rows(K_TABLE_LOG_ANSWER, 'WHERE logansw_testlog_id=' . $testlog_id . '');
                }
                // display answer options
                $sqla = 'SELECT *
					FROM ' . K_TABLE_ANSWERS . ', ' . K_TABLE_LOG_ANSWER . '
					WHERE logansw_answer_id=answer_id
						AND logansw_testlog_id=' . $testlog_id . '
					ORDER BY logansw_order';
                if ($ra = F_db_query($sqla, $db)) {
                    while ($ma = F_db_fetch_array($ra)) {
                        $str .= "<li>";
                        switch ($m['question_type']) {
                            case 1:
                                // MCSA - single-answer question
                                $str .= '<input type="radio" name="answerid" id="answerid_' . $ma['answer_id'] . '" value="' . $ma['answer_id'] . '"';
                                if (intval($ma['logansw_selected']) == 1) {
                                    $str .= ' checked="checked"';
                                    $checked = true;
                                }
                                if (F_getBoolean($m['question_auto_next'])) {
                                    $str .= " onclick=\"var submittime=new Date();document.getElementById('reaction_time').value=submittime.getTime()-document.getElementById('display_time').value;document.getElementById('autonext').value=1;document.getElementById('" . $formname . "').submit();\"";
                                }
                                $str .= ' />&nbsp;';
                                $str .= '<label for="answerid_' . $ma['answer_id'] . '">';
                                $str .= F_decode_tcecode($ma['answer_description']);
                                $str .= '</label>';
                                if ($ma['answer_keyboard_key'] > 0) {
                                    $aswkeys[$ma['answer_keyboard_key']] = 'answerid_' . $ma['answer_id'];
                                }
                                break;
                            case 2:
                                // MCMA - multiple-answer question
                                if (F_getBoolean($testdata['test_mcma_radio'])) {
                                    // radiobuttons
                                    // no-answer option
                                    $str .= '<span style="background-color:#DDDDDD;"' . $noanswer_hidden . '>&nbsp;';
                                    $str .= '<label for="answerid_' . $ma['answer_id'] . 'u" title="' . $l['m_unanswered'] . '">' . $l['w_unanswered_acronym'] . '</label>';
                                    $str .= '<input type="radio"' . $noanswer_disabled . ' name="answerid[' . $ma['answer_id'] . ']" id="answerid_' . $ma['answer_id'] . 'u" value="-1" title="' . $l['m_unanswered'] . '"';
                                    if (intval($ma['logansw_selected']) == -1) {
                                        $str .= ' checked="checked"';
                                    }
                                    $str .= ' />';
                                    $str .= '</span>&nbsp;';
                                    // false option
                                    $str .= '<span style="background-color:#FFBBBB;">&nbsp;';
                                    $str .= '<label for="answerid_' . $ma['answer_id'] . 'f" title="' . $l['w_false'] . '">' . $l['w_false_acronym'] . '</label>';
                                    $str .= '<input type="radio" name="answerid[' . $ma['answer_id'] . ']" id="answerid_' . $ma['answer_id'] . 'f" value="0"';
                                    if (intval($ma['logansw_selected']) == 0) {
                                        $str .= ' checked="checked"';
                                    }
                                    $str .= ' />';
                                    $str .= '</span>&nbsp;';
                                    // true option
                                    $str .= '<span style="background-color:#BBFFBB;">&nbsp;';
                                    $str .= '<label for="answerid_' . $ma['answer_id'] . 't" title="' . $l['w_true'] . '">' . $l['w_true_acronym'] . '</label>';
                                    $str .= '<input type="radio" name="answerid[' . $ma['answer_id'] . ']" id="answerid_' . $ma['answer_id'] . 't" value="1"';
                                    if (intval($ma['logansw_selected']) == 1) {
                                        $str .= ' checked="checked"';
                                    }
                                    $str .= ' />';
                                    $str .= '</span>&nbsp;';
                                    if ($ma['answer_keyboard_key'] > 0) {
                                        $aswkeys[] = array($ma['answer_keyboard_key'] => 'answerid_' . $ma['answer_id'] . 't');
                                    }
                                    $str .= F_decode_tcecode($ma['answer_description']);
                                } else {
                                    // checkbox
                                    $str .= '<input type="checkbox" name="answerid[' . $ma['answer_id'] . ']" id="answerid_' . $ma['answer_id'] . '" value="1"';
                                    if (intval($ma['logansw_selected']) == 1) {
                                        $str .= ' checked="checked"';
                                        $checked = true;
                                    }
                                    $str .= ' />&nbsp;';
                                    $str .= '<label for="answerid_' . $ma['answer_id'] . '">';
                                    $str .= F_decode_tcecode($ma['answer_description']);
                                    $str .= '</label>';
                                }
                                break;
                            case 4:
                                // ORDER - ordering questions
                                $str .= '<select name="answerid[' . $ma['answer_id'] . ']" id="answerid_' . $ma['answer_id'] . '" size="0">' . K_NEWLINE;
                                if (F_getBoolean($testdata['test_noanswer_enabled'])) {
                                    $str .= '<option value="0">&nbsp;</option>' . K_NEWLINE;
                                }
                                for ($pos = 1; $pos <= $max_position; $pos++) {
                                    $str .= '<option value="' . $pos . '"';
                                    if ($pos == $ma['logansw_position']) {
                                        $str .= ' selected="selected"';
                                    }
                                    $str .= '>' . $pos . '</option>' . K_NEWLINE;
                                }
                                $str .= '</select>' . K_NEWLINE;
                                $str .= '<label for="answerid_' . $ma['answer_id'] . '">';
                                $str .= F_decode_tcecode($ma['answer_description']);
                                $str .= '</label>';
                                break;
                        }
                        // end of switch
                        $str .= '</li>' . K_NEWLINE;
                    }
                    // end of while
                } else {
                    F_display_db_error();
                }
                if ($m['question_type'] == 1) {
                    // display default "unanswered" option for MCSA
                    $str .= '<li' . $noanswer_hidden . '>';
                    $str .= '<input type="radio"' . $noanswer_disabled . ' name="answerid" id="answerid_0" value="0"';
                    if (!$checked) {
                        $str .= ' checked="checked"';
                    }
                    $str .= ' />&nbsp;';
                    $str .= '<label for="answerid_0">';
                    $str .= $l['m_unanswered'];
                    $str .= '</label>';
                    $str .= '</li>' . K_NEWLINE;
                }
                $str .= '</ol>' . K_NEWLINE;
            }
            // end multiple answers
            $str .= '</div>' . K_NEWLINE;
            $str .= '</div>' . K_NEWLINE;
            //fieldset
            // script to handle keyboard events
            $str .= '<script type="text/javascript">' . K_NEWLINE;
            $str .= '//<![CDATA[' . K_NEWLINE;
            $str .= 'function actionByChar(e){e=(e)?e:window.event;keynum=(e.keyCode)?e.keyCode:e.which;switch(keynum){' . K_NEWLINE;
            foreach ($aswkeys as $key => $fieldid) {
                $str .= 'case ' . $key . ':{document.getElementById(\'' . $fieldid . '\').checked=true;var submittime=new Date();document.getElementById(\'reaction_time\').value=submittime.getTime()-document.getElementById(\'display_time\').value;document.getElementById(\'autonext\').value=1;document.getElementById(\'' . $formname . '\').submit();break;}' . K_NEWLINE;
            }
            $str .= '}}' . K_NEWLINE;
            $str .= 'if (!document.all) {document.captureEvents(Event.KEYPRESS);}';
            $str .= 'document.onkeypress=actionByChar;' . K_NEWLINE;
            if ($m['question_timer'] > 0) {
                // automatic submit form after specified amount of time
                $str .= "setTimeout('document.getElementById(\\'autonext\\').value=1;document.getElementById(\\'" . $formname . "\\').submit();', " . $m['question_timer'] * 1000 . ");" . K_NEWLINE;
            }
            $str .= '//]]>' . K_NEWLINE;
            $str .= '</script>' . K_NEWLINE;
            // display questions menu
            $str .= F_questionsMenu($testdata, $m['testlog_testuser_id'], $testlog_id, F_getBoolean($m['question_fullscreen']));
        }
        if (empty($m['testlog_display_time'])) {
            // mark test as displayed:
            $sqlu = 'UPDATE ' . K_TABLE_TESTS_LOGS . '
				SET testlog_display_time=\'' . date(K_TIMESTAMP_FORMAT) . '\'
				WHERE testlog_id=' . $testlog_id . '';
            if (!($ru = F_db_query($sqlu, $db))) {
                F_display_db_error();
            }
        }
    } else {
        F_display_db_error();
    }
    return $str;
}
            //check if delete button has been pushed (redundant check)
            $sql = 'DELETE FROM ' . K_TABLE_TEST_USER . '
					WHERE testuser_id=' . $testuser_id . '';
            if (!($r = F_db_query($sql, $db))) {
                F_display_db_error();
            } else {
                $testuser_id = false;
                F_print_error('MESSAGE', $l['m_deleted']);
            }
        }
        break;
    case 'extendtime':
        // extend the test time by 5 minutes
        // this time extension is obtained moving forward the test starting time
        $sqlu = 'UPDATE ' . K_TABLE_TEST_USER . '
			SET testuser_creation_time=\'' . date(K_TIMESTAMP_FORMAT, F_getTestStartTime($testuser_id) + K_EXTEND_TIME_MINUTES * K_SECONDS_IN_MINUTE) . '\'
			WHERE testuser_id=' . $testuser_id . '';
        if (!($ru = F_db_query($sqlu, $db))) {
            F_display_db_error();
        } else {
            F_print_error('MESSAGE', $l['m_updated']);
        }
        break;
    case 'lock':
        // update test mode to 4 = test locked
        $sqlu = 'UPDATE ' . K_TABLE_TEST_USER . '
			SET testuser_status=4
			WHERE testuser_id=' . $testuser_id . '';
        if (!($ru = F_db_query($sqlu, $db))) {
            F_display_db_error();
        } else {