Пример #1
0
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();
    }
}
function readTestQuestion($i_questionno, $i_questionid)
{
    global $g_db, $G_SESSION, $lngstr, $srv_settings, $g_vars;
    $i_rSet2 = $g_db->Execute("SELECT * FROM " . $srv_settings['table_prefix'] . "questions WHERE questionid=" . $i_questionid);
    if (!$i_rSet2) {
        showDBError('readTestQuestion', 1);
    } else {
        if (!$i_rSet2->EOF) {
            $g_vars['page']['question'][$i_questionno]['text'] = $i_rSet2->fields['question_text'];
            $g_vars['page']['question'][$i_questionno]['type'] = $i_rSet2->fields['question_type'];
            $g_vars['page']['question'][$i_questionno]['shuffle'] = $i_rSet2->fields['question_shufflea'];
        } else {
            $g_vars['page']['errors'] = sprintf($lngstr['err_no_question_id_in_db'], $i_questionid);
            $g_vars['page']['errors_fatal'] = true;
            return false;
        }
        $i_rSet2->Close();
    }
    $i_answer_text = array();
    $i_answer_feedback = array();
    $g_vars['page']['question'][$i_questionno]['answer_count'] = 0;
    $g_vars['page']['question'][$i_questionno]['correct_answer_count'] = 0;
    $i_rSet3 = $g_db->Execute("SELECT answer_text, answer_feedback, answer_correct FROM " . $srv_settings['table_prefix'] . "answers WHERE questionid=" . $i_questionid . " ORDER BY answerid");
    if (!$i_rSet3) {
        showDBError('readTestQuestion', 2);
    } else {
        while (!$i_rSet3->EOF) {
            $g_vars['page']['question'][$i_questionno]['answer_count']++;
            $i_answer_text[$g_vars['page']['question'][$i_questionno]['answer_count']] = $i_rSet3->fields['answer_text'];
            $i_answer_feedback[$g_vars['page']['question'][$i_questionno]['answer_count']] = $i_rSet3->fields['answer_feedback'];
            $g_vars['page']['question'][$i_questionno]['correct_answer_count'] += $i_rSet3->fields['answer_correct'] ? 1 : 0;
            $i_rSet3->MoveNext();
        }
        $i_rSet3->Close();
        switch ($g_vars['page']['question'][$i_questionno]['type']) {
            case QUESTION_TYPE_MULTIPLECHOICE:
            case QUESTION_TYPE_TRUEFALSE:
            case QUESTION_TYPE_MULTIPLEANSWER:
                if ($g_vars['page']['question'][$i_questionno]['answer_count']) {
                    if (!isset($G_SESSION['yt_answers'][$i_questionno])) {
                        $i_answer_order = array();
                        for ($i = 0; $i < $g_vars['page']['question'][$i_questionno]['answer_count']; $i++) {
                            $i_answer_order[$i] = $i + 1;
                        }
                        if ($g_vars['page']['question'][$i_questionno]['answer_count'] > 1 && $g_vars['page']['question'][$i_questionno]['type'] != QUESTION_TYPE_TRUEFALSE) {
                            if ($G_SESSION['yt_shufflea'] && $g_vars['page']['question'][$i_questionno]['shuffle'] == IGT_SHUFFLE_ANSWERS_INHERIT || $g_vars['page']['question'][$i_questionno]['shuffle'] == IGT_SHUFFLE_ANSWERS_DO) {
                                $i_answer_order = getShuffledArray($i_answer_order);
                            } else {
                                if ($g_vars['page']['question'][$i_questionno]['shuffle'] == IGT_SHUFFLE_ANSWERS_EXCEPT_FIRST) {
                                    array_shift($i_answer_order);
                                    $i_answer_order = getShuffledArray($i_answer_order);
                                    array_unshift($i_answer_order, 1);
                                } else {
                                    if ($g_vars['page']['question'][$i_questionno]['shuffle'] == IGT_SHUFFLE_ANSWERS_EXCEPT_LAST) {
                                        array_pop($i_answer_order);
                                        $i_answer_order = getShuffledArray($i_answer_order);
                                        array_push($i_answer_order, $g_vars['page']['question'][$i_questionno]['answer_count']);
                                    }
                                }
                            }
                        }
                        $G_SESSION['yt_answers'][$i_questionno] = $i_answer_order;
                    }
                    foreach ($G_SESSION['yt_answers'][$i_questionno] as $i_id => $i_no) {
                        $g_vars['page']['question'][$i_questionno]['answer'][$i_id + 1]['text'] = $i_answer_text[$i_no];
                        $g_vars['page']['question'][$i_questionno]['answer'][$i_id + 1]['feedback'] = $i_answer_feedback[$i_no];
                        if (!$g_vars['page']['has_feedback']) {
                            $g_vars['page']['has_feedback'] = strlen($i_answer_feedback[$i_no]) > 0;
                        }
                        switch ($g_vars['page']['question'][$i_questionno]['type']) {
                            case QUESTION_TYPE_MULTIPLECHOICE:
                            case QUESTION_TYPE_TRUEFALSE:
                                if ($G_SESSION['yt_state'] == TEST_STATE_QREVIEW) {
                                    $nSelectedAnswer = (int) getRecordItem($srv_settings['table_prefix'] . 'results_answers', 'result_answer_text', 'resultid=' . $G_SESSION['resultid'] . ' AND questionid=' . $i_questionid);
                                    $bIsSelected = $nSelectedAnswer == $i_no;
                                } else {
                                    $nSelectedAnswer = isset($_POST['answer']) && isset($_POST['answer'][$i_questionno]) ? $_POST['answer'][$i_questionno] : 0;
                                    $bIsSelected = $nSelectedAnswer == $i_id + 1;
                                }
                                break;
                            case QUESTION_TYPE_MULTIPLEANSWER:
                                if ($G_SESSION['yt_state'] == TEST_STATE_QREVIEW) {
                                    $nSelectedAnswers = explode(QUESTION_TYPE_MULTIPLEANSWER_BREAK, getRecordItem($srv_settings['table_prefix'] . 'results_answers', 'result_answer_text', 'resultid=' . $G_SESSION['resultid'] . ' AND questionid=' . $i_questionid));
                                    $bIsSelected = is_array($nSelectedAnswers) && in_array($i_no, $nSelectedAnswers);
                                } else {
                                    $nSelectedAnswers = isset($_POST['answer']) && isset($_POST['answer'][$i_questionno]) ? $_POST['answer'][$i_questionno] : array();
                                    $bIsSelected = is_array($nSelectedAnswers) && in_array($i_id + 1, $nSelectedAnswers);
                                }
                                break;
                        }
                        $g_vars['page']['question'][$i_questionno]['answer'][$i_id + 1]['selected'] = $bIsSelected;
                    }
                } else {
                    $g_vars['page']['errors'] = sprintf($lngstr['err_no_answers_in_question'], $i_questionid);
                    $g_vars['page']['errors_fatal'] = true;
                    return false;
                }
                break;
            case QUESTION_TYPE_FILLINTHEBLANK:
            case QUESTION_TYPE_ESSAY:
                $g_vars['page']['question'][$i_questionno]['answer_count'] = 1;
                $g_vars['page']['question'][$i_questionno]['answer'][1]['feedback'] = '';
                if ($G_SESSION['yt_state'] == TEST_STATE_QREVIEW) {
                    $g_vars['page']['question'][$i_questionno]['answer'][1]['text'] = getRecordItem($srv_settings['table_prefix'] . 'results_answers', 'result_answer_text', 'resultid=' . $G_SESSION['resultid'] . ' AND questionid=' . $i_questionid);
                } else {
                    $g_vars['page']['question'][$i_questionno]['answer'][1]['text'] = '';
                }
                break;
        }
    }
    return true;
}