function deleteUser($i_id)
{
    global $g_db, $srv_settings;
    if ($i_id > SYSTEM_USER_MAX_INDEX) {
        $g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "tests_attempts WHERE id=" . $i_id);
        $i_rSet1 = $g_db->Execute("SELECT resultid FROM " . $srv_settings['table_prefix'] . "results WHERE id=" . $i_id);
        if (!$i_rSet1) {
            showDBError(__FILE__, 1);
        } else {
            while (!$i_rSet1->EOF) {
                deleteResultRecord($i_rSet1->fields["resultid"]);
                $i_rSet1->MoveNext();
            }
            $i_rSet1->Close();
        }
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "results WHERE id=" . $i_id) === false) {
            showDBError(__FILE__, 2);
        }
        //9917//9917
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "groups_users WHERE id=" . $i_id) === false) {
            showDBError(__FILE__, 3);
        }
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "users WHERE id=" . $i_id) === false) {
            showDBError(__FILE__, 4);
        }
    }
}
function deleteVisitor($i_visitorid)
{
    global $g_db, $srv_settings;
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "visitors WHERE visitorid={$i_visitorid}") === false) {
        showDBError(__FILE__, 1);
    }
}
function deleteETemplate($i_etemplateid)
{
    global $g_db, $srv_settings;
    if ($i_etemplateid > SYSTEM_ETEMPLATES_MAX_INDEX) {
        if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "tests SET result_etemplateid=0 WHERE result_etemplateid={$i_etemplateid}") === false) {
            showDBError(__FILE__, 1);
        }
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "etemplates WHERE etemplateid={$i_etemplateid}") === false) {
            showDBError(__FILE__, 2);
        }
    }
}
function deleteGrade($i_gscaleid)
{
    global $g_db, $srv_settings;
    if ($i_gscaleid > SYSTEM_GRADES_MAX_INDEX) {
        if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "tests SET gscaleid=1 WHERE gscaleid=" . $i_gscaleid) === false) {
            showDBError(__FILE__, 1);
        }
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "gscales_grades WHERE gscaleid={$i_gscaleid}") === false) {
            showDBError(__FILE__, 2);
        }
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "gscales WHERE gscaleid={$i_gscaleid}") === false) {
            showDBError(__FILE__, 3);
        }
    }
}
function deletesubject($i_subjectid)
{
    global $g_db, $srv_settings;
    if ($i_subjectid > SYSTEM_SUBJECTS_MAX_INDEX) {
        if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "tests SET subjectid=1 WHERE subjectid=" . $i_subjectid) === false) {
            showDBError(__FILE__, 1);
        }
        if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "questions SET subjectid=1 WHERE subjectid=" . $i_subjectid) === false) {
            showDBError(__FILE__, 2);
        }
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "subjects WHERE subjectid={$i_subjectid}") === false) {
            showDBError(__FILE__, 3);
        }
    }
}
function deleteTest($i_testid)
{
    global $g_db, $srv_settings;
    $g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "tests_attempts WHERE testid=" . $i_testid);
    //9917//9917
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "groups_tests WHERE testid=" . $i_testid) === false) {
        showDBError(__FILE__, 1);
    }
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "tests_questions WHERE testid=" . $i_testid) === false) {
        showDBError(__FILE__, 2);
    }
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "tests WHERE testid=" . $i_testid) === false) {
        showDBError(__FILE__, 3);
    }
}
function deleteGroup($i_groupid)
{
    global $g_db, $srv_settings;
    if ($i_groupid > SYSTEM_GROUP_MAX_INDEX) {
        //9917//9917
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "groups_users WHERE groupid={$i_groupid}") === false) {
            showDBError(__FILE__, 1);
        }
        //9917//9917
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "groups_tests WHERE groupid={$i_groupid}") === false) {
            showDBError(__FILE__, 2);
        }
        //9917
        if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "groups WHERE groupid={$i_groupid}") === false) {
            showDBError(__FILE__, 3);
        }
    }
}
function deleteQuestion($i_questionid)
{
    global $g_db, $srv_settings;
    $i_rSet1 = $g_db->Execute("SELECT test_questionid, testid FROM " . $srv_settings['table_prefix'] . "tests_questions WHERE questionid={$i_questionid} ORDER BY test_questionid DESC");
    if (!$i_rSet1) {
        showDBError(__FILE__, 1);
    } else {
        while (!$i_rSet1->EOF) {
            deleteQuestionLink($i_rSet1->fields["testid"], $i_rSet1->fields["test_questionid"]);
            $i_rSet1->MoveNext();
        }
        $i_rSet1->Close();
    }
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "answers WHERE questionid={$i_questionid}") === false) {
        showDBError(__FILE__, 2);
    }
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "questions WHERE questionid={$i_questionid}") === false) {
        showDBError(__FILE__, 3);
    }
}
function deleteScaleGrade($gscaleid, $gscale_gradeid)
{
    global $g_db, $srv_settings;
    if ($g_db->Execute("DELETE FROM " . $srv_settings['table_prefix'] . "gscales_grades WHERE gscaleid=" . $gscaleid . " AND gscale_gradeid=" . $gscale_gradeid) === false) {
        showDBError(__FILE__, 1);
    }
    $i_rSet2 = $g_db->Execute("SELECT gscale_gradeid FROM " . $srv_settings['table_prefix'] . "gscales_grades WHERE gscaleid=" . $gscaleid . " AND gscale_gradeid>" . $gscale_gradeid . " ORDER BY gscale_gradeid");
    if (!$i_rSet2) {
        showDBError(__FILE__, 2);
    } else {
        $i_counter = $gscale_gradeid - 1;
        while (!$i_rSet2->EOF) {
            $i_counter++;
            if ($i_rSet2->fields["gscale_gradeid"] != $i_counter) {
                $g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "gscales_grades SET gscale_gradeid=" . $i_counter . " WHERE gscaleid=" . $gscaleid . " AND gscale_gradeid=" . $i_rSet2->fields["gscale_gradeid"]);
            }
            $i_rSet2->MoveNext();
        }
        $i_rSet2->Close();
    }
}
function getAdvancedReportPDF($resultid)
{
    global $g_db, $G_SESSION, $DOCUMENT_FPDF, $lngstr, $srv_settings, $i_rSet2, $i_rSet3, $i_rSet4, $i_rSet5, $i_rSet6;
    ob_start();
    include_once $DOCUMENT_FPDF . 'html2fpdf.php';
    $i_pdf = false;
    $i_can_access = false;
    if ($G_SESSION['access_reportsmanager'] > 1) {
        $i_can_access = true;
    } else {
        $i_rSet1 = $g_db->Execute("SELECT resultid FROM " . $srv_settings['table_prefix'] . "results WHERE id=" . $G_SESSION['id'] . " AND resultid=" . $resultid);
        if (!$i_rSet1) {
            showDBError(__FILE__, 1);
        } else {
            $i_can_access = $i_rSet1->RecordCount() > 0;
        }
    }
    $i_isok = $i_can_access;
    if ($i_isok) {
        $i_isok = $i_rSet2 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "results WHERE resultid=" . $resultid, 1);
    }
    if ($i_isok) {
        $i_isok = !$i_rSet2->EOF;
    }
    if ($i_isok) {
        $i_isok = $i_rSet3 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "tests WHERE testid=" . $i_rSet2->fields['testid'], 1);
    }
    if ($i_isok) {
        $i_isok = !$i_rSet3->EOF;
    }
    if ($i_isok) {
        $i_isok = $i_rSet3->fields['rtemplateid'] > 0 && ($i_rSet3->fields['test_reportgradecondition'] == 0 || $i_rSet3->fields['test_reportgradecondition'] >= $i_rSet2->fields['gscale_gradeid']);
    }
    if ($i_isok) {
        $i_isok = $i_rSet6 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "users WHERE id=" . $i_rSet2->fields['id'], 1);
    }
    if ($i_isok) {
        $i_isok = !$i_rSet6->EOF;
    }
    $i_result_detailed_1_items = array();
    $i_result_detailed_2_items = array();
    $i_result_detailed_3_items = array();
    $i_result_detailed_4_items = array();
    if ($i_isok) {
        $i_isok = $i_rSet7 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "results_answers, " . $srv_settings['table_prefix'] . "questions WHERE resultid=" . $resultid . " AND " . $srv_settings['table_prefix'] . "results_answers.questionid=" . $srv_settings['table_prefix'] . "questions.questionid ORDER BY result_answerid");
    }
    if ($i_isok) {
        $i_questionno = 0;
        while (!$i_rSet7->EOF) {
            $i_questionno++;
            $i_answers = array();
            $i_rSet5 = $g_db->Execute("SELECT answer_text, answer_correct FROM " . $srv_settings['table_prefix'] . "answers WHERE questionid=" . $i_rSet7->fields['questionid'] . " ORDER BY answerid");
            if (!$i_rSet5) {
                showDBError(__FILE__, 5);
            } else {
                $i_answerno = 1;
                while (!$i_rSet5->EOF) {
                    $i_answers[$i_answerno] = $i_rSet5->fields['answer_text'];
                    $i_answers_correct[$i_answerno] = $i_rSet5->fields['answer_correct'];
                    $i_answerno++;
                    $i_rSet5->MoveNext();
                }
                $i_rSet5->Close();
            }
            $i_detailed_correct = $i_rSet7->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_CORRECT;
            $i_result_detailed_3_items[$i_questionno] = $i_questionno . '. ' . getTruncatedHTML($i_rSet7->fields['question_text'], 0);
            $i_result_detailed_1_items[$i_questionno] = $i_result_detailed_3_items[$i_questionno] . '<br />';
            $i_result_detailed_1_items[$i_questionno] .= $lngstr['email_answer_iscorrect'] . ($i_rSet7->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_UNDEFINED ? $lngstr['label_undefined'] : ($i_rSet7->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_CORRECT ? $lngstr['label_yes'] : ($i_rSet7->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_PARTIALLYCORRECT ? $lngstr['label_partially'] : $lngstr['label_no']))) . '<br />';
            $i_result_detailed_1_items[$i_questionno] .= $lngstr['email_answer_points'] . $i_rSet7->fields['result_answer_points'] . '<br />';
            if (!$i_detailed_correct) {
                $i_result_detailed_2_items[$i_questionno] = $i_result_detailed_1_items[$i_questionno];
            }
            $i_result_detailed_3_items[$i_questionno] = '<tr><td>' . $i_result_detailed_3_items[$i_questionno] . '</td></tr>';
            for ($i = 1; $i < $i_answerno; $i++) {
                switch ($i_rSet7->fields['question_type']) {
                    case QUESTION_TYPE_MULTIPLECHOICE:
                    case QUESTION_TYPE_TRUEFALSE:
                        $i_answers_given = (int) $i_rSet7->fields['result_answer_text'];
                        $i_result_detailed_3_items[$i_questionno] .= '<tr><td><img src="images/button-checkbox-' . ($i_answers_correct[$i] ? '2' : ($i == $i_answers_given ? '4' : '0')) . '.gif" width=13 height=13> &nbsp; ' . $i_answers[$i] . '</tr>';
                        break;
                    case QUESTION_TYPE_MULTIPLEANSWER:
                        $i_answers_given = explode(QUESTION_TYPE_MULTIPLEANSWER_BREAK, $i_rSet7->fields['result_answer_text']);
                        $i_result_detailed_3_items[$i_questionno] .= '<tr><td><img src="images/button-checkbox-' . ($i_answers_correct[$i] ? '2' : (in_array($i, $i_answers_given) ? '4' : '0')) . '.gif" width=13 height=13> &nbsp; ' . $i_answers[$i] . '</tr>';
                        break;
                    case QUESTION_TYPE_FILLINTHEBLANK:
                        $i_result_detailed_3_items[$i_questionno] .= '<tr><td>' . nl2br($i_answers[$i]) . '</td></tr>';
                        break;
                }
            }
            if ($i_rSet7->fields['question_type'] == QUESTION_TYPE_ESSAY) {
                $i_result_detailed_3_items[$i_questionno] .= '<tr><td>' . nl2br($i_rSet7->fields['result_answer_text']) . '</td></tr>';
            }
            $i_result_detailed_3_items[$i_questionno] .= '<tr><td>' . $lngstr['email_answer_points'] . $i_rSet7->fields['result_answer_points'] . '</td></tr>';
            if (!$i_detailed_correct) {
                $i_result_detailed_4_items[$i_questionno] = $i_result_detailed_3_items[$i_questionno];
            }
            $i_rSet7->MoveNext();
        }
        $i_rSet7->Close();
        $i_result_detailed_1_text = implode('<br />', $i_result_detailed_1_items);
        $i_result_detailed_2_text = implode('<br />', $i_result_detailed_2_items);
        $i_result_detailed_3_text = '<table cellpadding=0 cellspacing=0 border=0 width="100%">' . implode("\n", $i_result_detailed_3_items) . '</table>';
        $i_result_detailed_4_text = '<table cellpadding=0 cellspacing=0 border=0 width="100%">' . implode("\n", $i_result_detailed_4_items) . '</table>';
        $i_result_detailed_5_text = '';
        $i_result_detailed_6_text = '';
        $i_pdf = eventOnQueryCustomReportPDF();
        if (empty($i_pdf)) {
            $i_pdf = new HTML2FPDF();
            $i_pdf->AddPage();
            $i_pdf->SetFont('Arial', '', 11);
            $i_pdf->SetTextColor(0, 0, 0);
            $g_vars['page']['grade'] = getGradeData($i_rSet2->fields['gscaleid'], $i_rSet2->fields['gscale_gradeid']);
            $g_vars['page']['subjects'] = getTestResultSubjectsData($resultid);
            $i_template_body = getReportTemplate(array('rtemplateid' => $i_rSet3->fields['rtemplateid'], 'username' => $i_rSet6->fields['username'], 'email' => $i_rSet6->fields['email'], 'title' => $i_rSet6->fields['user_title'], 'firstname' => $i_rSet6->fields['user_firstname'], 'lastname' => $i_rSet6->fields['user_lastname'], 'middlename' => $i_rSet6->fields['user_middlename'], 'address' => $i_rSet6->fields['user_address'], 'city' => $i_rSet6->fields['user_city'], 'state' => $i_rSet6->fields['user_state'], 'zip' => $i_rSet6->fields['user_zip'], 'country' => $i_rSet6->fields['user_country'], 'phone' => $i_rSet6->fields['user_phone'], 'fax' => $i_rSet6->fields['user_fax'], 'mobile' => $i_rSet6->fields['user_mobile'], 'pager' => $i_rSet6->fields['user_pager'], 'ipphone' => $i_rSet6->fields['user_ipphone'], 'webpage' => $i_rSet6->fields['user_webpage'], 'icq' => $i_rSet6->fields['user_icq'], 'msn' => $i_rSet6->fields['user_msn'], 'aol' => $i_rSet6->fields['user_aol'], 'gender' => $i_rSet6->fields['user_gender'], 'birthday' => $i_rSet6->fields['user_birthday'], 'husbandwife' => $i_rSet6->fields['user_husbandwife'], 'children' => $i_rSet6->fields['user_children'], 'trainer' => $i_rSet6->fields['user_trainer'], 'photo' => $i_rSet6->fields['user_photo'], 'company' => $i_rSet6->fields['user_company'], 'cposition' => $i_rSet6->fields['user_cposition'], 'department' => $i_rSet6->fields['user_department'], 'coffice' => $i_rSet6->fields['user_coffice'], 'caddress' => $i_rSet6->fields['user_caddress'], 'ccity' => $i_rSet6->fields['user_ccity'], 'cstate' => $i_rSet6->fields['user_cstate'], 'czip' => $i_rSet6->fields['user_czip'], 'ccountry' => $i_rSet6->fields['user_ccountry'], 'cphone' => $i_rSet6->fields['user_cphone'], 'cfax' => $i_rSet6->fields['user_cfax'], 'cmobile' => $i_rSet6->fields['user_cmobile'], 'cpager' => $i_rSet6->fields['user_cpager'], 'cipphone' => $i_rSet6->fields['user_cipphone'], 'cwebpage' => $i_rSet6->fields['user_cwebpage'], 'cphoto' => $i_rSet6->fields['user_cphoto'], 'ufield1' => $i_rSet6->fields['user_ufield1'], 'ufield2' => $i_rSet6->fields['user_ufield2'], 'ufield3' => $i_rSet6->fields['user_ufield3'], 'ufield4' => $i_rSet6->fields['user_ufield4'], 'ufield5' => $i_rSet6->fields['user_ufield5'], 'ufield6' => $i_rSet6->fields['user_ufield6'], 'ufield7' => $i_rSet6->fields['user_ufield7'], 'ufield8' => $i_rSet6->fields['user_ufield8'], 'ufield9' => $i_rSet6->fields['user_ufield9'], 'ufield10' => $i_rSet6->fields['user_ufield10'], 'test_name' => $i_rSet3->fields['test_name'], 'result_id' => $resultid, 'result_date' => getDateLocal($lngstr['language']['date_format_full'], $i_rSet2->fields['result_datestart']), 'result_time_spent' => getTimeFormatted($i_rSet2->fields['result_timespent']), 'result_time_exceeded' => $i_rSet2->fields['result_timeexceeded'] > 0 ? $lngstr['label_yes'] : $lngstr['label_no'], 'result_points_scored' => $i_rSet2->fields['result_points'], 'result_points_possible' => $i_rSet2->fields['result_pointsmax'], 'result_percents' => $i_rSet2->fields['result_pointsmax'] > 0 ? round($i_rSet2->fields['result_points'] * 100 / $i_rSet2->fields['result_pointsmax']) : 0, 'result_grade' => $g_vars['page']['grade']['name'], 'result_grade_feedback' => $g_vars['page']['grade']['feedback'], 'result_subjects' => $g_vars['page']['subjects'], 'result_detailed_1' => $i_result_detailed_1_text, 'result_detailed_2' => $i_result_detailed_2_text, 'result_detailed_3' => $i_result_detailed_3_text, 'result_detailed_4' => $i_result_detailed_4_text, 'result_detailed_5' => $i_result_detailed_5_text, 'result_detailed_6' => $i_result_detailed_6_text));
            $i_pdf->WriteHTML($i_template_body);
        }
    } else {
        $i_pdf = false;
    }
    ob_end_clean();
    return $i_pdf;
}
     showDBError(__FILE__, 4);
 } else {
     if (!$i_rSet4->EOF) {
         $i_questiontext = $i_rSet4->fields["question_text"];
         $i_questiontype = $i_rSet4->fields["question_type"];
         $i_questionpoints = $i_rSet4->fields["question_points"];
     }
     $i_rSet4->Close();
 }
 echo '<h2>' . $lngstr['page_reportsmanager']['correct_answer'] . '</h2>';
 echo '<p><table cellpadding=0 cellspacing=5 border=0 width="100%">';
 echo '<tr><td colspan=2>' . $i_questiontext . '</td></tr>';
 $i_answers = array();
 $i_rSet5 = $g_db->Execute("SELECT answer_text, answer_correct FROM " . $srv_settings['table_prefix'] . "answers WHERE questionid=" . $i_questionid . " ORDER BY answerid");
 if (!$i_rSet5) {
     showDBError(__FILE__, 5);
 } else {
     $i_answerno = 1;
     while (!$i_rSet5->EOF) {
         $i_answers[$i_answerno] = $i_rSet5->fields["answer_text"];
         $i_answers_correct[$i_answerno] = $i_rSet5->fields["answer_correct"];
         $i_answerno++;
         $i_rSet5->MoveNext();
     }
     $i_rSet5->Close();
 }
 for ($i = 1; $i < $i_answerno; $i++) {
     echo '<tr>';
     switch ($i_questiontype) {
         case QUESTION_TYPE_MULTIPLECHOICE:
         case QUESTION_TYPE_TRUEFALSE:
function writeResultStats($i_resultid)
{
    global $g_db, $lngstr, $srv_settings, $g_vars;
    $g_vars['page']['resultid'] = $i_resultid;
    $i_testid = 0;
    $i_rSet6 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "results WHERE resultid=" . $i_resultid, 1);
    if (!$i_rSet6) {
        showDBError(__FILE__, 6);
    } else {
        if (!$i_rSet6->EOF) {
            $i_testid = $i_rSet6->fields['testid'];
            $g_vars['page']['result_date'] = getDateLocal($lngstr['language']['date_format_full'], $i_rSet6->fields['result_datestart']);
            $g_vars['page']['time_spent'] = getTimeFormatted($i_rSet6->fields['result_timespent']);
            $g_vars['page']['got_points'] = $i_rSet6->fields['result_points'];
            $g_vars['page']['points_max'] = $i_rSet6->fields['result_pointsmax'];
            $g_vars['page']['score'] = $g_vars['page']['points_max'] != 0 ? round($g_vars['page']['got_points'] * 100 / $g_vars['page']['points_max']) : 100;
            $g_vars['page']['grade'] = getGradeData($i_rSet6->fields['gscaleid'], $i_rSet6->fields['gscale_gradeid']);
            $i_id = $i_rSet6->fields['id'];
            $i_rSet7 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "users WHERE id=" . $i_id, 1);
            if (!$i_rSet7) {
                showDBError(__FILE__, 7);
            } else {
                if (!$i_rSet7->EOF) {
                    $g_vars['page']['username'] = $i_rSet7->fields['user_firstname'] . ' ' . $i_rSet7->fields['user_lastname'];
                }
                $i_rSet7->Close();
            }
        }
        $i_rSet6->Close();
    }
    $g_vars['page']['test_name'] = '';
    $i_gscaleid = 0;
    $i_rSet1 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "tests WHERE testid=" . $i_testid, 1);
    if (!$i_rSet1) {
        showDBError(__FILE__, 1);
    } else {
        if (!$i_rSet1->EOF) {
            $g_vars['page']['test_name'] = $i_rSet1->fields['test_name'];
            $i_gscaleid = $i_rSet1->fields['gscaleid'];
        }
        $i_rSet1->Close();
    }
    $g_vars['page']['grades_times_total'] = 0;
    $i_rSet2 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "gscales_grades WHERE gscaleid=" . $i_gscaleid . " ORDER BY gscale_gradeid");
    if (!$i_rSet2) {
        showDBError(__FILE__, 2);
    } else {
        while (!$i_rSet2->EOF) {
            $i_gradeid = $i_rSet2->fields['gscale_gradeid'];
            $g_vars['page']['grades'][$i_gradeid]['name'] = $i_rSet2->fields['grade_name'];
            $g_vars['page']['grades'][$i_gradeid]['times'] = getRecordCount($srv_settings['table_prefix'] . 'results', 'testid=' . $i_testid . ' AND gscaleid=' . $i_gscaleid . ' AND gscale_gradeid=' . $i_gradeid);
            $g_vars['page']['grades_times_total'] += $g_vars['page']['grades'][$i_gradeid]['times'];
            $i_rSet2->MoveNext();
        }
        $i_rSet2->Close();
    }
    foreach ($g_vars['page']['grades'] as $i_gradeid => $i_grade) {
        $g_vars['page']['grades'][$i_gradeid]['times_percents'] = $g_vars['page']['grades_times_total'] != 0 ? $g_vars['page']['grades'][$i_gradeid]['times'] * 100 / $g_vars['page']['grades_times_total'] : 0;
    }
    $g_vars['page']['test']['participant_count'] = 0;
    $i_points_all = array();
    $g_vars['page']['test']['points_max'] = 0;
    $g_vars['page']['test']['points_low'] = IGT_MAX_FLOAT;
    $g_vars['page']['test']['points_high'] = 0;
    $i_points_total = 0;
    $g_vars['page']['test']['points_total_max'] = 0;
    $i_rSet3 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "results WHERE testid=" . $i_testid . " AND gscale_gradeid > 0");
    if (!$i_rSet3) {
        showDBError(__FILE__, 3);
    } else {
        while (!$i_rSet3->EOF) {
            $g_vars['page']['test']['participant_count']++;
            $i_points = $i_rSet3->fields['result_points'];
            array_push($i_points_all, $i_points);
            $g_vars['page']['test']['points_max'] = $i_rSet3->fields['result_pointsmax'];
            if ($i_points > $g_vars['page']['test']['points_high']) {
                $g_vars['page']['test']['points_high'] = $i_points;
            }
            if ($i_points < $g_vars['page']['test']['points_low']) {
                $g_vars['page']['test']['points_low'] = $i_points;
            }
            $i_points_total += $i_points;
            $g_vars['page']['test']['points_total_max'] += $g_vars['page']['test']['points_max'];
            $i_rSet3->MoveNext();
        }
        $i_rSet3->Close();
    }
    $g_vars['page']['test']['points_average'] = $i_points_total / $g_vars['page']['test']['participant_count'];
    $g_vars['page']['test']['points_average_percents'] = $i_points_total * 100 / $g_vars['page']['test']['points_total_max'];
    $g_vars['page']['test']['points_high_percents'] = $g_vars['page']['test']['points_high'] * 100 / $g_vars['page']['test']['points_max'];
    $g_vars['page']['test']['points_low_percents'] = $g_vars['page']['test']['points_low'] * 100 / $g_vars['page']['test']['points_max'];
    $g_vars['page']['test']['variance'] = 0;
    if ($g_vars['page']['test']['participant_count'] > 1) {
        foreach ($i_points_all as $i_points) {
            $g_vars['page']['test']['variance'] += ($i_points - $g_vars['page']['test']['points_average']) * ($i_points - $g_vars['page']['test']['points_average']);
        }
        $g_vars['page']['test']['variance'] = $g_vars['page']['test']['variance'] / ($g_vars['page']['test']['participant_count'] - 1);
    }
    $g_vars['page']['test']['std_deviation'] = sqrt($g_vars['page']['test']['variance']);
    $g_vars['page']['questions'] = array();
    $i_rSet4 = $g_db->Execute("SELECT questionid, result_answer_iscorrect FROM " . $srv_settings['table_prefix'] . "results_answers WHERE resultid=" . $i_resultid);
    if (!$i_rSet4) {
        showDBError(__FILE__, 4);
    } else {
        while (!$i_rSet4->EOF) {
            $i_questionid = $i_rSet4->fields['questionid'];
            $g_vars['page']['questions'][$i_questionid]['iscorrect'] = $i_rSet4->fields['result_answer_iscorrect'];
            $i_rSet4->MoveNext();
        }
        $i_rSet4->Close();
    }
    $g_vars['page']['subjects'] = array();
    foreach ($g_vars['page']['questions'] as $i_questionid => $i_question) {
        $i_rSet5 = $g_db->SelectLimit("SELECT " . $srv_settings['table_prefix'] . "subjects.subjectid, " . $srv_settings['table_prefix'] . "subjects.subject_name FROM " . $srv_settings['table_prefix'] . "questions, " . $srv_settings['table_prefix'] . "subjects WHERE questionid=" . $i_questionid . " AND " . $srv_settings['table_prefix'] . "questions.subjectid=" . $srv_settings['table_prefix'] . "subjects.subjectid", 1);
        if (!$i_rSet5) {
            showDBError(__FILE__, 5);
        } else {
            if (!$i_rSet5->EOF) {
                $i_subjectid = $i_rSet5->fields['subjectid'];
                $g_vars['page']['subjects'][$i_subjectid]['question_count'] = getRecordCount($srv_settings['table_prefix'] . 'questions', 'subjectid=' . $i_subjectid);
                if (!isset($g_vars['page']['subjects'][$i_subjectid]['question_got'])) {
                    $g_vars['page']['subjects'][$i_subjectid]['name'] = $i_rSet5->fields['subject_name'];
                    $g_vars['page']['subjects'][$i_subjectid]['question_got'] = 0;
                    $g_vars['page']['subjects'][$i_subjectid]['question_correct'] = 0;
                }
                $g_vars['page']['subjects'][$i_subjectid]['question_got']++;
                if ($i_question['iscorrect'] == IGT_ANSWER_IS_CORRECT) {
                    $g_vars['page']['subjects'][$i_subjectid]['question_correct']++;
                }
            }
            $i_rSet5->Close();
        }
    }
    foreach ($g_vars['page']['subjects'] as $i_subjectid => $i_grade) {
        $g_vars['page']['subjects'][$i_subjectid]['question_correct_percents'] = $g_vars['page']['subjects'][$i_subjectid]['question_correct'] * 100 / $g_vars['page']['subjects'][$i_subjectid]['question_got'];
    }
}
        echo '<td width=32><a href="groups.php?pageno=' . $i_pageno_count . $i_url_pages_addon . '"><img src="images/button-last-big.gif" border=0 title="' . $lngstr['button_last_page'] . '"></a></td>';
    } else {
        echo '<td width=32><img src="images/button-next-big-inactive.gif" border=0 title="' . $lngstr['button_next_page'] . '"></td>';
        echo '<td width=32><img src="images/button-last-big-inactive.gif" border=0 title="' . $lngstr['button_last_page'] . '"></td>';
    }
}
echo '<td width=2><img src="images/toolbar-right.gif" width=2 height=32></td></tr></table>';
echo '</td></tr><tr><td>';
echo '<table class=rowtable2 cellpadding=5 cellspacing=1 border=0 width="100%">';
echo '<tr vAlign=top><td class=rowhdr1 title="' . $lngstr['label_hdr_select_hint'] . '" width=22><input type=checkbox name=toggleAll onclick="toggleCBs(this);"></td>';
writeQryTableHeaders('groups.php?action=' . $i_url_limit_addon, $i_tablefields, $i_order_no, $i_direction);
echo '<td class=rowhdr1 colspan=2>' . $lngstr['label_hdr_action'] . '</td></tr>';
//9917
$i_rSet1 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "groups" . $i_sql_order_addon, $i_limitcount, $i_limitfrom);
if (!$i_rSet1) {
    showDBError(__FILE__, 1);
} else {
    $i_counter = 0;
    while (!$i_rSet1->EOF) {
        $rowname = $i_counter % 2 ? "rowone" : "rowtwo";
        echo '<tr id=tr_' . $i_pagewide_id . ' class=' . $rowname . ' onmouseover="rollTR(' . $i_pagewide_id . ',1);" onmouseout="rollTR(' . $i_pagewide_id . ',0);"><td align=center width=22' . ($i_rSet1->fields["groupid"] > SYSTEM_GROUP_MAX_INDEX ? '' : ' class=system') . '><input id=cb_' . $i_pagewide_id . ' type=checkbox name=box_groups[] value="' . $i_rSet1->fields["groupid"] . '" onclick="toggleCB(this);"></td><td align=right>' . $i_rSet1->fields["groupid"] . '</td><td>' . getTruncatedHTML($i_rSet1->fields["group_name"]) . '</td><td>' . $i_rSet1->fields["group_description"] . '</td>';
        echo '<td align=center width=22><a href="groups.php?groupid=' . $i_rSet1->fields["groupid"] . $i_order_addon . $i_url_limit_addon . '&action=edit"><img width=20 height=20 border=0 src="images/button-edit.gif" title="' . $lngstr['label_action_group_edit'] . '"></a></td><td align=center width=22>' . ($i_rSet1->fields["groupid"] > SYSTEM_GROUP_MAX_INDEX ? '<a href="groups.php?groupid=' . $i_rSet1->fields["groupid"] . $i_order_addon . $i_url_limit_addon . '&action=delete" onclick="return confirmMessage(this, \'' . $lngstr['qst_delete_group'] . '\')"><img width=20 height=20 border=0 src="images/button-cross.gif" title="' . $lngstr['label_action_group_delete'] . '"></a>' : '<img width=20 height=20 border=0 src="images/button-cross-inactive.gif">') . '</td></tr>';
        $i_counter++;
        $i_pagewide_id++;
        $i_rSet1->MoveNext();
    }
    $i_rSet1->Close();
}
echo '</table>';
echo '</td></tr></table></form>';
displayTemplate('_footer');
Beispiel #14
0
	<link rel="stylesheet" type="text/css" href="cache.php?css=theme,default" />
	<link rel="SHORTCUT ICON" href="favicon.ico" />
	<link rel="stylesheet" type="text/css" href="cache.php?css=menu,treeview,results,context,alerts" />
	<script type="text/javascript" language="javascript" src="cache.php?script=jquery,mobile,<?php 
echo $DB->name();
?>
"></script>
	<!--[if lt IE 8]>
		<script type="text/javascript" language="javascript" src="cache.php?script=json2"></script>
	<![endif]-->
</head>
<body class="mainbody">
<?php 
if (!$DB->connect(DB_HOST, DB_USER, DB_PASS, getDbName())) {
    include BASE_PATH . "/modules/splash.php";
    die(getSplashScreen(showDBError()));
}
if (Session::get('session', 'init') != '1') {
    // session just started, so we load information here
    Session::set('db', 'user', $DB->getCurrentUser(), true);
    Session::set('session', 'init', 1);
}
$KEY_CODES = getKeyCodes();
?>
	<div id="editToolbar">
		<div class="tb-header ui-widget-header"><span class="fname"></span></div>
		<div class="tb-row">Type: <span class="ftype"></span></div>
		<div class="tb-row">[ <?php 
echo str_replace('{{KEY}}', $KEY_CODES['KEYCODE_SETNULL'][1], __('Press {{KEY}} to set NULL'));
?>
 ]</div>
}
if ($i_questiontype == QUESTION_TYPE_ESSAY) {
    if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "results_answers SET result_answer_points=" . $f_points . ", result_answer_iscorrect=" . ($f_notdefined ? IGT_ANSWER_IS_UNDEFINED : ($f_points >= $i_questionpoints ? IGT_ANSWER_IS_CORRECT : ($f_points > 0 ? IGT_ANSWER_IS_PARTIALLYCORRECT : IGT_ANSWER_IS_INCORRECT))) . ", result_answer_feedback=" . $f_feedback . " WHERE resultid=" . $f_resultid . " AND result_answerid=" . $f_answerid) === false) {
        showDBError(__FILE__, 5);
    }
    $i_testpoints_max = 0;
    $i_rSet6 = $g_db->Execute("SELECT result_pointsmax FROM " . $srv_settings['table_prefix'] . "results WHERE resultid=" . $f_resultid);
    if (!$i_rSet6) {
        showDBError(__FILE__, 6);
    } else {
        if (!$i_rSet6->EOF) {
            $i_testpoints_max = $i_rSet6->fields["result_pointsmax"];
        }
        $i_rSet6->Close();
    }
    $i_rSet7 = $g_db->Execute("SELECT SUM(result_answer_points) as testpoints FROM " . $srv_settings['table_prefix'] . "results_answers WHERE resultid=" . $f_resultid);
    if (!$i_rSet7) {
        showDBError(__FILE__, 7);
    } else {
        if (!$i_rSet7->EOF) {
            $i_testpoints_new = $i_rSet7->fields['testpoints'];
        }
        $i_rSet7->Close();
    }
    $i_percents = $i_testpoints_max != 0 ? round($i_testpoints_new / $i_testpoints_max * 100) : 100;
    $i_grade = calcResultGrade($i_gscaleid, $i_percents);
    if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "results SET result_points=" . $i_testpoints_new . ", gscaleid=" . $i_gscaleid . ", gscale_gradeid=" . $i_grade['gradeid'] . " WHERE resultid=" . $f_resultid) === false) {
        showDBError(__FILE__, 9);
    }
}
gotoLocation('reports-manager.php?resultid=' . $f_resultid . '&action=viewq');
function initATest($testid, $existing_resultid = 0, $existing_answered_questionids = array())
{
    global $g_db, $G_SESSION, $lngstr, $srv_settings, $g_vars;
    $i_now = time();
    $i_rSet1 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "tests WHERE testid=" . $testid . " AND test_enabled=1 AND test_datestart<=" . $i_now . " AND test_dateend>" . $i_now, 1);
    if (!$i_rSet1) {
        showDBError(__FILE__, 1);
    } else {
        if (!$i_rSet1->EOF) {
            $i_isallowed = $i_rSet1->fields['test_forall'];
            if (!$i_isallowed) {
                //9917//9917
                $i_isallowed = getRecordCount($srv_settings['table_prefix'] . 'groups_users, ' . $srv_settings['table_prefix'] . 'groups_tests', "(" . $srv_settings['table_prefix'] . "groups_tests.testid=" . $i_rSet1->fields['testid'] . " AND " . $srv_settings['table_prefix'] . "groups_tests.groupid=" . $srv_settings['table_prefix'] . "groups_users.groupid AND " . $srv_settings['table_prefix'] . "groups_users.id=" . $G_SESSION['id'] . ")") > 0;
            }
            if ($i_isallowed) {
                $i_isallowed = $i_rSet1->fields['test_price'] <= 0 || getRecordCount($srv_settings['table_prefix'] . 'tests_own', 'testid=' . $testid . ' AND id=' . $G_SESSION['id']) > 0;
            }
            if ($i_isallowed) {
                $yt_questionids = array();
                $i_rSet2 = $g_db->Execute("SELECT " . $srv_settings['table_prefix'] . "tests_questions.questionid, " . $srv_settings['table_prefix'] . "tests_questions.test_sectionid, " . $srv_settings['table_prefix'] . "questions.subjectid, " . $srv_settings['table_prefix'] . "questions.question_points, " . $srv_settings['table_prefix'] . "questions.question_type FROM " . $srv_settings['table_prefix'] . "tests_questions, " . $srv_settings['table_prefix'] . "questions WHERE testid=" . $testid . " AND " . $srv_settings['table_prefix'] . "tests_questions.questionid=" . $srv_settings['table_prefix'] . "questions.questionid ORDER BY " . $srv_settings['table_prefix'] . "tests_questions.test_questionid");
                if (!$i_rSet2) {
                    showDBError(__FILE__, 2);
                } else {
                    $i_questioncounter = 0;
                    $yt_pointsmax = 0;
                    while (!$i_rSet2->EOF) {
                        if ($i_rSet2->fields['question_type'] != QUESTION_TYPE_RANDOM) {
                            $yt_questionids[$i_questioncounter + 1] = $i_rSet2->fields['questionid'];
                            $yt_pointsmax += $i_rSet2->fields['question_points'];
                            $i_questioncounter++;
                        } else {
                            $i_questioncount_by_subjectid = getRecordCount($srv_settings['table_prefix'] . 'questions', "subjectid=" . $i_rSet2->fields['subjectid'] . " AND question_type NOT IN (" . QUESTION_TYPE_RANDOM . ")" . ($yt_questionids ? " AND questionid NOT IN (" . implode(',', $yt_questionids) . ")" : ""));
                            if ($i_questioncount_by_subjectid > 0) {
                                srand((double) microtime() * 10000000);
                                $i_rSet3 = $g_db->SelectLimit("SELECT questionid, question_points FROM " . $srv_settings['table_prefix'] . "questions WHERE subjectid=" . $i_rSet2->fields['subjectid'] . " AND question_type NOT IN (" . QUESTION_TYPE_RANDOM . ")" . ($yt_questionids ? " AND questionid NOT IN (" . implode(',', $yt_questionids) . ")" : ""), 1, rand(0, $i_questioncount_by_subjectid - 1));
                                if (!$i_rSet3) {
                                    showDBError(__FILE__, 3);
                                } else {
                                    if (!$i_rSet3->EOF) {
                                        $yt_questionids[$i_questioncounter + 1] = $i_rSet3->fields['questionid'];
                                        $yt_pointsmax += $i_rSet3->fields['question_points'];
                                        $i_questioncounter++;
                                    }
                                    $i_rSet3->Close();
                                }
                            } else {
                                showError(__FILE__, $lngstr['err_no_questions_left_in_bank']);
                            }
                        }
                        $i_rSet2->MoveNext();
                    }
                    $i_rSet2->Close();
                }
                $yt_questioncount = $i_questioncounter;
                if ($yt_questioncount > 0) {
                    for ($i = 0; $i < $yt_questioncount; $i++) {
                        $yt_questions[$i] = $i + 1;
                    }
                    if ($i_rSet1->fields['test_shuffleq']) {
                        $yt_questions_2 = array();
                        if (!empty($existing_answered_questionids)) {
                            foreach ($existing_answered_questionids as $key => $val) {
                                foreach ($yt_questionids as $key2 => $val2) {
                                    if ($val2 == $val) {
                                        $i_questionno = $key2;
                                        break;
                                    }
                                }
                                $yt_questions_2[$key] = $i_questionno;
                                unset($yt_questions[$i_questionno - 1]);
                            }
                        }
                        $yt_questions = array_merge($yt_questions_2, getShuffledArray($yt_questions));
                    }
                    $yt_shufflea = (bool) $i_rSet1->fields['test_shufflea'];
                    $i_testtime = (int) $i_rSet1->fields['test_time'];
                    $yt_test_timeforceout = (bool) $i_rSet1->fields['test_timeforceout'];
                    if ($i_testtime < 0) {
                        $i_testtime = 0;
                    }
                    $G_SESSION['testid'] = $testid;
                    if ($existing_resultid > 0) {
                        $G_SESSION['resultid'] = $existing_resultid;
                    } else {
                        if ($g_db->Execute("INSERT INTO " . $srv_settings['table_prefix'] . "results (testid,id,result_datestart) VALUES (" . $testid . "," . $G_SESSION['id'] . "," . $i_now . ")") === false) {
                            showDBError(__FILE__, 5);
                        }
                        $G_SESSION['resultid'] = $g_db->Insert_ID($srv_settings['table_prefix'] . 'results', 'resultid');
                    }
                    $G_SESSION['yt_name'] = $i_rSet1->fields['test_name'];
                    $G_SESSION['yt_result_etemplateid'] = $i_rSet1->fields['result_etemplateid'];
                    if ($i_rSet1->fields['result_etemplateid'] > 0) {
                        $G_SESSION['yt_result_email'] = trim($i_rSet1->fields['test_result_email']);
                        $G_SESSION['yt_result_emailtouser'] = $i_rSet1->fields['test_result_emailtouser'];
                    }
                    $G_SESSION['yt_teststart'] = $i_now;
                    $G_SESSION['yt_testtime'] = $i_testtime;
                    $G_SESSION['yt_timeforceout'] = $yt_test_timeforceout;
                    $G_SESSION['yt_attempts'] = $i_rSet1->fields['test_attempts'];
                    $G_SESSION['yt_pointsmax'] = $yt_pointsmax;
                    $G_SESSION['yt_questioncount'] = $yt_questioncount;
                    $G_SESSION['yt_questions'] = $yt_questions;
                    $G_SESSION['yt_questionids'] = $yt_questionids;
                    $G_SESSION['yt_answers'] = array();
                    $G_SESSION['yt_shufflea'] = $yt_shufflea;
                    $G_SESSION['yt_test_qsperpage'] = $i_rSet1->fields['test_qsperpage'];
                    $G_SESSION['yt_test_showqfeedback'] = (bool) $i_rSet1->fields['test_showqfeedback'];
                    $G_SESSION['yt_result_showanswers'] = (bool) $i_rSet1->fields['test_result_showanswers'];
                    $G_SESSION['yt_result_showpoints'] = (bool) $i_rSet1->fields['test_result_showpoints'];
                    $G_SESSION['yt_result_showgrade'] = (bool) $i_rSet1->fields['test_result_showgrade'];
                    $G_SESSION['yt_result_showgradefeedback'] = (bool) $i_rSet1->fields['test_result_showgradefeedback'];
                    $G_SESSION['yt_result_showhtml'] = (bool) $i_rSet1->fields['test_result_showhtml'] && $i_rSet1->fields['rtemplateid'] > 0;
                    $G_SESSION['yt_result_showpdf'] = (bool) $i_rSet1->fields['test_result_showpdf'] && $i_rSet1->fields['rtemplateid'] > 0;
                    $G_SESSION['yt_result_rtemplateid'] = $i_rSet1->fields['test_result_rtemplateid'];
                    $G_SESSION['yt_reportgradecondition'] = $i_rSet1->fields['test_reportgradecondition'];
                    $G_SESSION['yt_gscaleid'] = (int) $i_rSet1->fields['gscaleid'];
                    $G_SESSION['yt_prevtestid'] = (int) $i_rSet1->fields['test_prevtestid'];
                    $G_SESSION['yt_nexttestid'] = (int) $i_rSet1->fields['test_nexttestid'];
                    $G_SESSION['yt_other_repeatuntilcorrect'] = (bool) $i_rSet1->fields['test_other_repeatuntilcorrect'];
                    $G_SESSION['yt_contentprotection'] = (int) $i_rSet1->fields['test_contentprotection'];
                    $G_SESSION['yt_canreview'] = (int) $i_rSet1->fields['test_canreview'];
                    $G_SESSION['yt_subjects'] = array();
                    $G_SESSION['yt_questionno'] = 1;
                    $G_SESSION['yt_got_answers'] = 0;
                    $G_SESSION['yt_got_points'] = 0;
                    $G_SESSION['yt_points_pending'] = 0;
                    if (!empty($i_rSet1->fields['test_instructions'])) {
                        $G_SESSION['yt_state'] = TEST_STATE_TESTINTRO;
                    } else {
                        $G_SESSION['yt_state'] = TEST_STATE_QSHOW;
                    }
                    eventOnBeforeTestStart();
                    return true;
                } else {
                    $g_vars['page']['errors'] = $lngstr['err_no_questions'];
                    return false;
                }
            } else {
                $g_vars['page']['notifications'] = $lngstr['inf_cant_passtest'];
                return false;
            }
        } else {
            $g_vars['page']['errors'] = $lngstr['err_no_tests'];
            return false;
        }
        $i_rSet1->Close();
    }
}
     $i_email_tos_text .= SYSTEM_ARRAY_ITEM_SEPARATOR . $i_rSet2->fields['email'];
 }
 $i_email_tos = explode(SYSTEM_ARRAY_ITEM_SEPARATOR, $i_email_tos_text);
 $i_result_detailed_1_items = array();
 $i_result_detailed_2_items = array();
 $i_result_detailed_3_items = array();
 $i_result_detailed_4_items = array();
 $i_isok = $i_isok && ($i_rSet3 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "results_answers, " . $srv_settings['table_prefix'] . "questions WHERE resultid=" . $G_SESSION['resultid'] . " AND " . $srv_settings['table_prefix'] . "results_answers.questionid=" . $srv_settings['table_prefix'] . "questions.questionid ORDER BY result_answerid"));
 if ($i_isok) {
     $i_questionno = 0;
     while (!$i_rSet3->EOF) {
         $i_questionno++;
         $i_answers = array();
         $i_rSet4 = $g_db->Execute("SELECT answer_text, answer_correct FROM " . $srv_settings['table_prefix'] . "answers WHERE questionid=" . $i_rSet3->fields['questionid'] . " ORDER BY answerid");
         if (!$i_rSet4) {
             showDBError(__FILE__, 4);
         } else {
             $i_answerno = 1;
             while (!$i_rSet4->EOF) {
                 $i_answers[$i_answerno] = $i_rSet4->fields['answer_text'];
                 $i_answers_correct[$i_answerno] = $i_rSet4->fields['answer_correct'];
                 $i_answerno++;
                 $i_rSet4->MoveNext();
             }
             $i_rSet4->Close();
         }
         $i_detailed_correct = $i_rSet3->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_CORRECT;
         $i_result_detailed_3_items[$i_questionno] = $i_questionno . '. ' . trim(getTruncatedHTML($i_rSet3->fields['question_text'], 0)) . "\n";
         $i_result_detailed_1_items[$i_questionno] = $i_result_detailed_3_items[$i_questionno];
         $i_result_detailed_1_items[$i_questionno] .= $lngstr['email_answer_iscorrect'] . ($i_rSet3->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_UNDEFINED ? $lngstr['label_undefined'] : ($i_rSet3->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_CORRECT ? $lngstr['label_yes'] : ($i_rSet3->fields['result_answer_iscorrect'] == IGT_ANSWER_IS_PARTIALLYCORRECT ? $lngstr['label_partially'] : $lngstr['label_no']))) . "\n";
         $i_result_detailed_1_items[$i_questionno] .= $lngstr['email_answer_points'] . $i_rSet3->fields['result_answer_points'] . "\n";
writeErrorsWarningsBar();
$i_rSet1 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "gscales WHERE gscaleid={$f_gscaleid}");
if (!$i_rSet1) {
    showDBError(__FILE__, 1);
} else {
    if (!$i_rSet1->EOF) {
        echo '<p><form method=post action="grades.php?gscaleid=' . $f_gscaleid . '&action=settings">';
        echo '<table class=rowtable2 cellpadding=5 cellspacing=1 border=0 width="100%">';
        $i_rowno = 0;
        writeTR2($lngstr['page_grades_gscaleid'], $i_rSet1->fields["gscaleid"]);
        writeTR2($lngstr['page_grades_gradename'], getInputElement('gscale_name', $i_rSet1->fields["gscale_name"]));
        writeTR2($lngstr['page_grades_gradedescription'], getTextArea('gscale_description', $i_rSet1->fields["gscale_description"]));
        $i_scale_text = "";
        $i_rSet3 = $g_db->Execute("SELECT gscale_gradeid, grade_name, grade_from, grade_to FROM " . $srv_settings['table_prefix'] . "gscales_grades WHERE gscaleid=" . $f_gscaleid);
        if (!$i_rSet3) {
            showDBError(__FILE__, 3);
        } else {
            while (!$i_rSet3->EOF) {
                $i_scale_text .= sprintf("%.1f", $i_rSet3->fields['grade_from']) . '% - ' . sprintf("%.1f", $i_rSet3->fields['grade_to']) . '% <b>' . $i_rSet3->fields['grade_name'] . '</b> [<a href="grades.php?action=edits&gscaleid=' . $f_gscaleid . '&gscale_gradeid=' . $i_rSet3->fields['gscale_gradeid'] . '">' . $lngstr['page_grades']['edit_grade'] . '</a>]<br>';
                $i_rSet3->MoveNext();
            }
            $i_rSet3->Close();
        }
        if ($i_scale_text) {
            $i_scale_text .= '<br>';
        }
        writeTR2($lngstr['page_grades_gradescale'], $i_scale_text . '<a href="grades.php?action=edit&gscaleid=' . $i_rSet1->fields["gscaleid"] . '">' . $lngstr['page_grades_gradescale_text'] . '</a>');
        echo '</table>';
        echo '<p class=center><input class=btn type=submit name=bsubmit value=" ' . $lngstr['button_update'] . ' "> <input class=btn type=submit name=bcancel value=" ' . $lngstr['button_cancel'] . ' "></form>';
    }
    $i_rSet1->Close();
function writeTestStats($i_testid)
{
    global $g_db, $lngstr, $srv_settings;
    $i_testid = (int) $i_testid;
    $i_test_name = '';
    $i_gscaleid = 0;
    $i_rSet1 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "tests WHERE testid=" . $i_testid, 1);
    if (!$i_rSet1) {
        showDBError(__FILE__, 1);
    } else {
        if (!$i_rSet1->EOF) {
            $i_test_name = $i_rSet1->fields['test_name'];
            $i_gscaleid = $i_rSet1->fields['gscaleid'];
        }
        $i_rSet1->Close();
    }
    $i_gradecount = 0;
    $i_grades_times_total = 0;
    $i_rSet2 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "gscales_grades WHERE gscaleid=" . $i_gscaleid . " ORDER BY gscale_gradeid");
    if (!$i_rSet2) {
        showDBError(__FILE__, 2);
    } else {
        while (!$i_rSet2->EOF) {
            $i_gradecount++;
            $i_gradeid = $i_rSet2->fields['gscale_gradeid'];
            $i_grades_names[$i_gradeid] = $i_rSet2->fields['grade_name'];
            $i_grades_times[$i_gradeid] = getRecordCount($srv_settings['table_prefix'] . 'results', 'testid=' . $i_testid . ' AND gscaleid=' . $i_gscaleid . ' AND gscale_gradeid=' . $i_gradeid);
            $i_grades_times_total += $i_grades_times[$i_gradeid];
            $i_rSet2->MoveNext();
        }
        $i_rSet2->Close();
    }
    $i_resultcount = 0;
    $i_points_max = 0;
    $i_points_low = IGT_MAX_FLOAT;
    $i_points_high = 0;
    $i_points_total = 0;
    $i_points_total_max = 0;
    $i_rSet3 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "results WHERE testid=" . $i_testid . " AND gscale_gradeid > 0");
    if (!$i_rSet3) {
        showDBError(__FILE__, 3);
    } else {
        while (!$i_rSet3->EOF) {
            $i_resultcount++;
            $i_points = $i_rSet3->fields['result_points'];
            $i_points_max = $i_rSet3->fields['result_pointsmax'];
            if ($i_points > $i_points_high) {
                $i_points_high = $i_points;
            }
            if ($i_points < $i_points_low) {
                $i_points_low = $i_points;
            }
            $i_points_total += $i_points;
            $i_points_total_max += $i_points_max;
            $i_rSet3->MoveNext();
        }
        $i_rSet3->Close();
    }
    echo '<tr class=rowone>';
    echo '<td rowspan=' . ($i_gradecount + 8) . ' align=right>' . $i_testid . '</td>';
    echo '<td colspan=4><b>' . $i_test_name . '</b></td>';
    echo '</tr>';
    echo '<tr class=rowone><td colspan=4><img src="images/1x1.gif" width=1 height=5></td></tr>';
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_testmanager_stats']['test_average_score'] . '</td>';
    if ($i_points_total_max > 0 && $i_resultcount > 0) {
        $i_points_average = $i_points_total / $i_resultcount;
        $i_points_average_percentage = $i_points_total * 100 / $i_points_total_max;
        echo '<td>' . getGauge($i_points_average_percentage, 'yellow') . '</td>';
        echo '<td align=right>' . sprintf("%.0f", $i_points_average) . '</td>';
        echo '<td align=right>' . sprintf("%.2f", $i_points_average_percentage) . '%</td>';
    } else {
        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
    }
    echo '</tr>';
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_testmanager_stats']['test_high_score'] . '</td>';
    if ($i_points_max > 0) {
        $i_points_high_percentage = $i_points_high * 100 / $i_points_max;
        echo '<td>' . getGauge($i_points_high_percentage, 'green') . '</td>';
        echo '<td align=right>' . $i_points_high . '</td>';
        echo '<td align=right>' . sprintf("%.2f", $i_points_high_percentage) . '%</td>';
    } else {
        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
    }
    echo '</tr>';
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_testmanager_stats']['test_low_score'] . '</td>';
    if ($i_points_max > 0) {
        $i_points_low_percentage = $i_points_low * 100 / $i_points_max;
        echo '<td>' . getGauge($i_points_low_percentage, 'red') . '</td>';
        echo '<td align=right>' . $i_points_low . '</td>';
        echo '<td align=right>' . sprintf("%.2f", $i_points_low_percentage) . '%</td>';
    } else {
        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
    }
    echo '</tr>';
    echo '<tr class=rowone><td colspan=4><img src="images/1x1.gif" width=1 height=5></td></tr>';
    echo '<tr class=rowone><td colspan=2 class=rowhdr1 title="' . $lngstr['page_testmanager_stats']['hdr_grade_data_hint'] . '">' . $lngstr['page_testmanager_stats']['hdr_grade_data'] . '</td><td class=rowhdr1 title="' . $lngstr['page_testmanager_stats']['hdr_grade_responses_hint'] . '">' . $lngstr['page_testmanager_stats']['hdr_grade_responses_hint'] . '</td><td class=rowhdr1 title="' . $lngstr['page_testmanager_stats']['hdr_percents_hint'] . '">' . $lngstr['page_testmanager_stats']['hdr_percents'] . '</td></tr>';
    foreach ($i_grades_names as $i_gradeid => $i_grade_name) {
        echo '<tr class=rowone>';
        echo '<td>' . $i_grade_name . '</td>';
        if ($i_grades_times_total > 0) {
            $i_grade_times_percentage = $i_grades_times[$i_gradeid] * 100 / $i_grades_times_total;
            echo '<td>' . getGauge($i_grade_times_percentage) . '</td>';
            echo '<td align=right>' . $i_grades_times[$i_gradeid] . '</td>';
            echo '<td align=right>' . sprintf("%.2f", $i_grade_times_percentage) . '%</td>';
        } else {
            echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
        }
        echo '</tr>';
    }
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_testmanager_stats']['total_responses'] . '</td>';
    echo '<td></td>';
    echo '<td align=right>' . $i_resultcount . '</td>';
    echo '<td align=right></td>';
    echo '</tr>';
}
function writeQuestionStats($i_questionid)
{
    global $g_db, $i_counter, $lngstr, $srv_settings;
    $i_questionid = (int) $i_questionid;
    $i_rSet1 = $g_db->SelectLimit("SELECT * FROM " . $srv_settings['table_prefix'] . "questions WHERE questionid=" . $i_questionid, 1);
    if (!$i_rSet1) {
        showDBError(__FILE__, 1);
    } else {
        if (!$i_rSet1->EOF) {
            $i_question_text = $i_rSet1->fields["question_text"];
            $i_question_type = $i_rSet1->fields["question_type"];
        }
        $i_rSet1->Close();
    }
    $i_answers_text = array();
    $i_answercount = 0;
    $i_answers_correct = array();
    $answers_clicks = array();
    $answers_clicks_total = 0;
    $question_views_total = 0;
    $question_correct = 0;
    $question_incorrect = 0;
    $question_partially = 0;
    $question_undefined = 0;
    $i_rSet2 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "answers WHERE questionid=" . $i_questionid . " ORDER BY answerid");
    if (!$i_rSet2) {
        showDBError(__FILE__, 2);
    } else {
        while (!$i_rSet2->EOF) {
            $i_answercount++;
            $i_answers_text[$i_answercount] = $i_rSet2->fields["answer_text"];
            $i_answers_correct[$i_answercount] = $i_rSet2->fields["answer_correct"];
            $answers_clicks[$i_answercount] = 0;
            $i_rSet2->MoveNext();
        }
        $i_rSet2->Close();
    }
    $i_rSet3 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "results_answers WHERE questionid=" . $i_questionid . " ORDER BY result_answerid");
    if (!$i_rSet2) {
        showDBError(__FILE__, 3);
    } else {
        switch ($i_question_type) {
            case QUESTION_TYPE_MULTIPLECHOICE:
            case QUESTION_TYPE_TRUEFALSE:
                while (!$i_rSet3->EOF) {
                    $answers_clicks[(int) $i_rSet3->fields["result_answer_text"]]++;
                    switch ($i_rSet3->fields["result_answer_iscorrect"]) {
                        case IGT_ANSWER_IS_INCORRECT:
                            $question_incorrect++;
                            break;
                        case IGT_ANSWER_IS_PARTIALLYCORRECT:
                            $question_partially++;
                            break;
                        case IGT_ANSWER_IS_CORRECT:
                            $question_correct++;
                            break;
                    }
                    $answers_clicks_total++;
                    $question_views_total++;
                    $i_rSet3->MoveNext();
                }
                break;
            case QUESTION_TYPE_MULTIPLEANSWER:
                while (!$i_rSet3->EOF) {
                    if ($i_rSet3->fields["result_answer_text"]) {
                        $i_answers_given = explode(QUESTION_TYPE_MULTIPLEANSWER_BREAK, $i_rSet3->fields["result_answer_text"]);
                        foreach ($i_answers_given as $i_answer_given) {
                            $answers_clicks[(int) $i_answer_given]++;
                            $answers_clicks_total++;
                        }
                    }
                    switch ($i_rSet3->fields["result_answer_iscorrect"]) {
                        case IGT_ANSWER_IS_INCORRECT:
                            $question_incorrect++;
                            break;
                        case IGT_ANSWER_IS_PARTIALLYCORRECT:
                            $question_partially++;
                            break;
                        case IGT_ANSWER_IS_CORRECT:
                            $question_correct++;
                            break;
                    }
                    $question_views_total++;
                    $i_rSet3->MoveNext();
                }
                break;
            case QUESTION_TYPE_FILLINTHEBLANK:
                $i_fillintheblank_text = array();
                $i_fillintheblank_clicks = array();
                $i_fillintheblank_count = 0;
                while (!$i_rSet3->EOF) {
                    $i_found = array_search($i_rSet3->fields["result_answer_text"], $i_fillintheblank_text);
                    if (!$i_found) {
                        $i_fillintheblank_count++;
                        $i_fillintheblank_text[$i_fillintheblank_count] = $i_rSet3->fields["result_answer_text"];
                        $i_fillintheblank_clicks[$i_fillintheblank_count] = 1;
                    } else {
                        $i_fillintheblank_clicks[$i_found]++;
                    }
                    switch ($i_rSet3->fields["result_answer_iscorrect"]) {
                        case IGT_ANSWER_IS_INCORRECT:
                            $question_incorrect++;
                            break;
                        case IGT_ANSWER_IS_PARTIALLYCORRECT:
                            $question_partially++;
                            break;
                        case IGT_ANSWER_IS_CORRECT:
                            $question_correct++;
                            break;
                    }
                    $answers_clicks_total++;
                    $question_views_total++;
                    $i_rSet3->MoveNext();
                }
                break;
            case QUESTION_TYPE_ESSAY:
                while (!$i_rSet3->EOF) {
                    switch ($i_rSet3->fields["result_answer_iscorrect"]) {
                        case IGT_ANSWER_IS_INCORRECT:
                            $question_incorrect++;
                            break;
                        case IGT_ANSWER_IS_PARTIALLYCORRECT:
                            $question_partially++;
                            break;
                        case IGT_ANSWER_IS_CORRECT:
                            $question_correct++;
                            break;
                        case IGT_ANSWER_IS_UNDEFINED:
                            $question_undefined++;
                            break;
                    }
                    $answers_clicks_total++;
                    $question_views_total++;
                    $i_rSet3->MoveNext();
                }
                break;
        }
        $i_rSet3->Close();
    }
    switch ($i_question_type) {
        case QUESTION_TYPE_MULTIPLECHOICE:
        case QUESTION_TYPE_TRUEFALSE:
        case QUESTION_TYPE_MULTIPLEANSWER:
        case QUESTION_TYPE_ESSAY:
            echo '<tr id=tr_' . $i_counter . ' class=rowone>';
            echo '<td rowspan=' . ($i_answercount + 5 + ($i_question_type == QUESTION_TYPE_ESSAY ? 1 : 0)) . ' align=right>' . $i_questionid . '</td>';
            echo '<td colspan=4><b>' . getTruncatedHTML($i_question_text, SYSTEM_TRUNCATED_LENGTH_LONG) . '</b></td>';
            echo '<td rowspan=' . ($i_answercount + 5 + ($i_question_type == QUESTION_TYPE_ESSAY ? 1 : 0)) . ' align=center><a href="question-bank.php?questionid=' . $i_questionid . '&action=editq"><img width=20 height=20 border=0 src="images/button-edit.gif" title="' . $lngstr['label_action_question_edit'] . '"></a></td>';
            echo '</tr>';
            foreach ($i_answers_text as $key => $val) {
                echo '<tr class=rowone>';
                echo '<td>' . getTruncatedHTML($val, SYSTEM_TRUNCATED_LENGTH_LONG) . '</td>';
                if ($answers_clicks_total != 0) {
                    $i_answers_clicks_percentage = $answers_clicks[(int) $key] * 100 / $answers_clicks_total;
                    echo '<td>' . getGauge($i_answers_clicks_percentage) . '</td>';
                    echo '<td align=right>' . $answers_clicks[(int) $key] . '</td>';
                    echo '<td align=right>' . sprintf("%.2f", $i_answers_clicks_percentage) . '%</td>';
                } else {
                    echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
                }
                echo '</tr>';
            }
            break;
        case QUESTION_TYPE_FILLINTHEBLANK:
            $i_sort_order_1 = SORT_DESC;
            $i_sort_type_1 = SORT_NUMERIC;
            $i_sort_order_2 = SORT_ASC;
            $i_sort_type_2 = SORT_STRING;
            array_multisort($i_fillintheblank_clicks, $i_sort_order_1, $i_sort_type_1, $i_fillintheblank_text, $i_sort_order_2, $i_sort_type_2);
            $i_answercount = min(sizeof($i_fillintheblank_clicks), MAX_STATS_ANSWERS_FILLINTHEBLANK) + 1;
            echo '<tr id=tr_' . $i_counter . ' class=rowone>';
            echo '<td rowspan=' . ($i_answercount + 5) . ' align=right>' . $i_questionid . '</td>';
            echo '<td colspan=4><b>' . getTruncatedHTML($i_question_text, SYSTEM_TRUNCATED_LENGTH_LONG) . '</b></td>';
            echo '<td rowspan=' . ($i_answercount + 5) . ' align=center><a href="question-bank.php?questionid=' . $i_questionid . '&action=editq"><img width=20 height=20 border=0 src="images/button-edit.gif" title="' . $lngstr['label_action_question_edit'] . '"></a></td>';
            echo '</tr>';
            $i = 0;
            $i_others_clicks = 0;
            $i_others_count = 0;
            foreach ($i_fillintheblank_clicks as $key => $val) {
                $i++;
                if ($i <= MAX_STATS_ANSWERS_FILLINTHEBLANK) {
                    echo '<tr class=rowone>';
                    echo '<td>' . getTruncatedHTML($i_fillintheblank_text[$key], SYSTEM_TRUNCATED_LENGTH_LONG) . '</td>';
                    if ($answers_clicks_total != 0) {
                        $i_answers_clicks_percentage = $val * 100 / $answers_clicks_total;
                        echo '<td>' . getGauge($i_answers_clicks_percentage) . '</td>';
                        echo '<td align=right>' . $val . '</td>';
                        echo '<td align=right>' . sprintf("%.2f", $i_answers_clicks_percentage) . '%</td>';
                    } else {
                        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
                    }
                    echo '</tr>';
                } else {
                    $i_others_clicks += $val;
                    $i_others_count++;
                }
            }
            echo '<tr class=rowone>';
            echo '<td>' . sprintf($lngstr['page_questionstats_fillintheblank_others'], $i_others_count) . '</td>';
            if ($answers_clicks_total != 0) {
                $i_answers_clicks_percentage = $i_others_clicks * 100 / $answers_clicks_total;
                echo '<td>' . getGauge($i_answers_clicks_percentage) . '</td>';
                echo '<td align=right>' . $i_others_clicks . '</td>';
                echo '<td align=right>' . sprintf("%.2f", $i_answers_clicks_percentage) . '%</td>';
            } else {
                echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
            }
            echo '</tr>';
            break;
        case QUESTION_TYPE_RANDOM:
            echo '<tr id=tr_' . $i_counter . ' class=rowone>';
            echo '<td rowspan=5 align=right>' . $i_questionid . '</td>';
            echo '<td colspan=4><b></b></td>';
            echo '<td rowspan=5 align=center><a href="question-bank.php?questionid=' . $i_questionid . '&action=editq"><img width=20 height=20 border=0 src="images/button-edit.gif" title="' . $lngstr['label_action_question_edit'] . '"></a></td>';
            echo '</tr>';
            break;
    }
    echo '<tr class=rowone><td colspan=4><img src="images/1x1.gif" width=1 height=5></td></tr>';
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_questionstats_correct_count'] . '</td>';
    if ($question_views_total != 0) {
        $question_correct_percentage = $question_correct * 100 / $question_views_total;
        echo '<td>' . getGauge($question_correct_percentage, 'green') . '</td>';
        echo '<td align=right>' . $question_correct . '</td>';
        echo '<td align=right>' . sprintf("%.2f", $question_correct_percentage) . '%</td>';
    } else {
        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
    }
    echo '</tr>';
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_questionstats_partially_count'] . '</td>';
    if ($question_views_total != 0) {
        $question_partially_percentage = $question_partially * 100 / $question_views_total;
        echo '<td>' . getGauge($question_partially_percentage, 'yellow') . '</td>';
        echo '<td align=right>' . $question_partially . '</td>';
        echo '<td align=right>' . sprintf("%.2f", $question_partially_percentage) . '%</td>';
    } else {
        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
    }
    echo '</tr>';
    echo '<tr class=rowone>';
    echo '<td>' . $lngstr['page_questionstats_incorrect_count'] . '</td>';
    if ($question_views_total != 0) {
        $question_incorrect_percentage = $question_incorrect * 100 / $question_views_total;
        echo '<td>' . getGauge($question_incorrect_percentage, 'red') . '</td>';
        echo '<td align=right>' . $question_incorrect . '</td>';
        echo '<td align=right>' . sprintf("%.2f", $question_incorrect_percentage) . '%</td>';
    } else {
        echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
    }
    echo '</tr>';
    if ($i_question_type == QUESTION_TYPE_ESSAY) {
        echo '<tr class=rowone>';
        echo '<td>' . $lngstr['page_questionstats_undefined_count'] . '</td>';
        if ($question_views_total != 0) {
            $question_undefined_percentage = $question_undefined * 100 / $question_views_total;
            echo '<td>' . getGauge($question_undefined_percentage, 'gray') . '</td>';
            echo '<td align=right>' . $question_undefined . '</td>';
            echo '<td align=right>' . sprintf("%.2f", $question_undefined_percentage) . '%</td>';
        } else {
            echo '<td colspan=3 align=center class=gray>' . $lngstr['label_notapplicable'] . '</td>';
        }
        echo '</tr>';
    }
}
function insertQuestion()
{
    global $g_db, $f_question_time_donotuse, $f_question_time, $f_answercount, $f_subjectid, $f_questionid, $f_question_type, $f_question_type2, $f_question_text, $f_answer_correct, $f_answer_percents, $f_answer_text, $f_question_points, $i_nAnswerCount1, $f_testid, $DOCUMENT_PAGES, $srv_settings;
    $f_question_time_donotuse = true;
    $f_question_time = 0;
    $f_answercount = $i_nAnswerCount1;
    if (!isset($f_question_type)) {
        $f_question_type = QUESTION_TYPE_MULTIPLECHOICE;
    }
    if (!isset($f_question_type2)) {
        $f_question_type2 = QUESTION_TYPE_MULTIPLECHOICE;
    }
    switch ($f_question_type) {
        case QUESTION_TYPE_MULTIPLECHOICE:
        case QUESTION_TYPE_MULTIPLEANSWER:
            for ($i = 1; $i <= $f_answercount; $i++) {
                if (!isset($f_answer_correct[$i])) {
                    $f_answer_correct[$i] = '';
                }
                if (!isset($f_answer_percents[$i])) {
                    $f_answer_percents[$i] = '';
                }
            }
            break;
    }
    if (!isset($f_question_points)) {
        $f_question_points = 1;
    }
    $f_question_shufflea = IGT_SHUFFLE_ANSWERS_INHERIT;
    if (empty($f_subjectid)) {
        $f_subjectid = 0;
        $i_rSet1 = $g_db->Execute("SELECT subjectid FROM " . $srv_settings['table_prefix'] . "tests WHERE testid=" . $f_testid);
        if (!$i_rSet1) {
            showDBError(__FILE__, 1);
        } else {
            if (!$i_rSet1->EOF) {
                $f_subjectid = (int) $i_rSet1->fields["subjectid"];
            }
            $i_rSet1->Close();
        }
    }
    if ($g_db->Execute("INSERT INTO " . $srv_settings['table_prefix'] . "questions (subjectid, question_pre, question_post, question_text, question_solution) VALUES(" . $f_subjectid . ", '', '', '', '')") === false) {
        showDBError(__FILE__, 2);
    }
    $f_questionid = (int) $g_db->Insert_ID($srv_settings['table_prefix'] . 'questions', 'questionid');
    createQuestionLink($f_testid, $f_questionid);
    include $DOCUMENT_PAGES . "edit_questions-3-int.inc.php";
    unset($f_question_time_donotuse);
    unset($f_question_time);
    unset($f_answercount);
    unset($f_subjectid);
    unset($f_questionid);
    unset($f_question_type);
    unset($f_question_type2);
    unset($f_question_text);
    if (is_array($f_answer_correct)) {
        foreach ($f_answer_correct as $key => $val) {
            unset($f_answer_correct[$key]);
        }
    }
    unset($f_answer_correct);
    if (is_array($f_answer_percents)) {
        foreach ($f_answer_percents as $key => $val) {
            unset($f_answer_percents[$key]);
        }
    }
    unset($f_answer_percents);
    foreach ($f_answer_text as $key => $val) {
        unset($f_answer_text[$key]);
    }
    unset($f_answer_text);
    unset($f_question_points);
}
<?php

$f_subjectid = (int) readGetVar('subjectid');
$f_subject_parent_subjectid = (int) readPostVar('subject_parent_subjectid');
$f_subject_name = readPostVar('subject_name');
$f_subject_name = $g_db->qstr($f_subject_name, get_magic_quotes_gpc());
$f_subject_description = readPostVar('subject_description');
$f_subject_description = $g_db->qstr($f_subject_description, get_magic_quotes_gpc());
if ($g_vars['page']['errors']) {
    include_once $DOCUMENT_PAGES . "subjects-2.inc.php";
} else {
    if ($g_db->Execute("UPDATE " . $srv_settings['table_prefix'] . "subjects SET subject_parent_subjectid={$f_subject_parent_subjectid}, subject_name={$f_subject_name}, subject_description={$f_subject_description} WHERE subjectid={$f_subjectid}") === false) {
        showDBError(__FILE__, 2);
    }
    gotoLocation('subjects.php');
}
function createGrade($i_gscaleid)
{
    global $g_db, $srv_settings;
    $i_gradecount = 0;
    $i_gradecount = getRecordCount($srv_settings['table_prefix'] . 'gscales_grades', 'gscaleid=' . $i_gscaleid);
    $i_gradecount++;
    if ($g_db->Execute("INSERT INTO " . $srv_settings['table_prefix'] . "gscales_grades (gscale_gradeid, gscaleid, grade_feedback) VALUES(" . $i_gradecount . ", " . $i_gscaleid . ", '')") === false) {
        showDBError(__FILE__, 2);
    }
    return $i_gradecount;
}