Пример #1
0
<?php

require '../include/core/common.php';
if ($_SESSION['login']['id'] > 0) {
    $new_info['login']['lastaction'] = 0;
    login_save_user_data($_SESSION['login']['id'], $new_info);
}
if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time() - 42000, '/');
}
session_destroy();
jscript_go_back();
Пример #2
0
function tests_calculate_score($options)
{
    $options['source'] = isset($options['source']) ? $options['source'] : $_POST;
    if (!isset($options['handle'])) {
        tests_index(array('error_message' => 'Inget handle skickat till tests_calculate_score()!'));
        return;
    }
    $answers = array();
    $answers_to_fetch = array();
    foreach ($_POST as $post_variable_key => $post_variable) {
        if (preg_match('/^tests_save_answer_([0-9]+)_([0-9r]+)$/', $post_variable_key, $matches)) {
            // $matches[1] = question id
            $answers[$matches[1]][] = $post_variable;
            //$answers[question_id][x] = answer
            $unique_questions_answered[$matches[1]] = $matches[1];
        }
    }
    $answers_points = array();
    $answers_to_fetch_sql = '';
    foreach ($answers_to_fetch as $answer) {
        $answers_to_fetch_sql .= ' OR q.question_id = ' . $answer;
    }
    if (count($answers) > 0) {
        $query = 'SELECT id, custom_css, custom_score_phrases';
        $query .= ' FROM tests';
        $query .= ' WHERE handle = "' . $options['handle'] . '"';
        $result = mysql_query($query) or report_sql_error($query, __FILE__, __LINE__);
        if (mysql_num_rows($result) != 1) {
            tests_index(array('error_message' => 'Testet du postade till finns inte eller fanns två gånger!'));
            return;
        }
        $data = mysql_fetch_assoc($result);
        $test_custom_css = $data['custom_css'];
        $test_custom_score_phrases = $data['custom_score_phrases'];
        $test_id = $data['id'];
        $query = 'SELECT q.answers, q.answer_type, q.question_id';
        $query .= ' FROM tests_questions AS q, tests AS t';
        $query .= ' WHERE t.handle = "' . $options['handle'] . '" AND t.id = q.test_id';
        $result = mysql_query($query) or report_sql_error($query, __FILE__, __LINE__);
        $total_score = 0;
        $max_score = 0;
        $answers_to_save_sql = array();
        while ($data = mysql_fetch_assoc($result)) {
            $question_answers = unserialize(stripslashes($data['answers']));
            $this_question_max_score = 0;
            foreach ($question_answers as $question_answer_key => $question_answer) {
                /* question_answer[0] = answer, question_answer[1] = points/score (<--That's what we're going to use!). */
                if (in_array($question_answer_key, $answers[$data['question_id']])) {
                    if (login_checklogin()) {
                        // %TESTID% is replaced with the tests ID below...
                        // (user_id, test_id, question_id, answer_id)
                        $answers_to_save_sql[] = '(' . $_SESSION['login']['id'] . ', %TESTID%, ' . $data['question_id'] . ', ' . $question_answer_key . ')';
                    }
                    $total_score = $total_score + $question_answer[1];
                }
                if ($data['answer_type'] == 'single_answer') {
                    if ($this_question_max_score < $question_answer[1]) {
                        $this_question_max_score = $question_answer[1];
                    }
                } elseif ($data['answer_type'] == 'multiple_answers' && $question_answer[1] > 0) {
                    $this_question_max_score = $this_question_max_score + $question_answer[1];
                }
            }
            $max_score = $max_score + $this_question_max_score;
        }
        if ($max_score > 0 && $total_score > 0) {
            $score_percents_right = round($total_score / $max_score * 100);
            if ($max_score == $total_score) {
                $score_text_index = 'all';
            } else {
                $score_text_index = round($total_score / $max_score * 4) + 1;
                // 1-5
            }
            $score_color_index = round($total_score / $max_score * 4) + 1;
            // 1-5
        } else {
            $percents_right = 0;
            $score_color_index = 1;
            $score_text_index = 'zero';
            /*if($total_score < 0)
            		{
            			$score_text_index = 'below_zero';
            		}*/
        }
        $score_texts = array();
        //$score_texts['below_zero'] = 'Men du, det här gick inte så bra alls! Minuspoäng! %TOTAL_SCORE% av %MAX_SCORE%.';
        $score_texts['zero'] = 'Men du... Du fick noll poäng, dags att öva?';
        $score_texts['all'] = 'Gratulerar! Du fick alla rätt! (%MAX_SCORE% var max).';
        $score_texts[1] = 'Nä, det här var du inte så bra på. Du fick bara %TOTAL_SCORE% av %MAX_SCORE% poäng.';
        $score_texts[2] = 'Det gick väl sådär, du fick %TOTAL_SCORE% poäng av %MAX_SCORE% möjliga.';
        $score_texts[3] = 'Bättre kan du! Av %MAX_SCORE% fick du %TOTAL_SCORE%, alltså ungefär hälften.';
        $score_texts[4] = 'Du fick hyfast bra poäng, men ändå inte jättebra. Om du övar lite så kanske dina %TOTAL_SCORE% poäng blir maxpoäng (%MAX_SCORE%p).';
        $score_texts[5] = 'Gratulerar! Du fick %TOTAL_SCORE% poäng, vilket är nästan alla rätt. På det här testet kunde man som mest få %MAX_SCORE%.';
        if (!empty($custom_score_phrases)) {
            $score_texts = unserialize(stripslashes($custom_score_phrases));
        }
        $score_text = str_replace(array('%TOTAL_SCORE%', '%MAX_SCORE%'), array($total_score, $max_score), $score_texts[$score_text_index]);
        global $ui_options;
        $ui_options['stylesheets'][] = 'tests.css';
        $ui_options['stylesheets'][] = 'comments.css';
        $ui_options['javascripts'][] = 'comments.js';
        if ($custom_css == 'yes') {
            $ui_options['stylesheets'][] = $options['handle'];
        }
        ui_top($ui_options);
        //echo '<h1>Resultat av test</h1>';
        echo '<h2>' . $score_text . '</h2>';
        echo '<div class="score_meter"><div class="color_' . $score_color_index . '" style="width: ' . $score_percents_right * 3 . 'px">&nbsp;</div></div>';
        echo '<div class="score_meter_right">Din poäng: ' . $total_score . ' (' . $score_percents_right . '% av max)<br />Maxpoäng: ' . $max_score . '</div>';
        echo '<br style="clear: both" />';
        if (login_checklogin()) {
            if (count($answers_to_save_sql) > 0) {
                $query = 'SELECT id FROM tests WHERE handle = "' . $options['handle'] . '" LIMIT 1';
                $result = mysql_query($query) or report_sql_error($query);
                $data = mysql_fetch_assoc($result);
                $test_id = $data['id'];
                $query = 'INSERT INTO tests_user_score (user_id, test_id, score, timestamp) VALUES';
                $query .= ' (' . $_SESSION['login']['id'] . ', ' . $test_id . ', ' . $total_score . ', ' . time() . ')';
                $result = @mysql_query($query);
                // ...
                if ($result) {
                    $query = 'INSERT INTO tests_user_answers (user_id, test_id, question_id, answer_id) VALUES';
                    $query .= ' ' . str_replace('%TESTID%', $test_id, implode(', ', $answers_to_save_sql));
                } else {
                    echo 'Poängen sparades inte eftersom att du redan gjort testet en gång.';
                }
            } else {
                echo 'Du måste svara på någon av frågorna!';
            }
        } else {
            echo 'Om du loggar in så kan vi spara dina poäng också!';
        }
        echo '</p><br /><br />' . "\n";
        echo '<h2>Folk som gjort testet</h2>';
        echo rounded_corners_top();
        echo tests_get_last_test_completers(array('test_id' => $test_id, 'limit' => 4));
        echo '<br style="clear: both" />';
        echo rounded_corners_bottom();
        echo rounded_corners_top();
        echo 'Kommentera testet: ' . comments_input_draw($test_id, 'tests');
        echo comments_list($test_id, 'tests');
        echo rounded_corners_bottom();
        echo '<p>';
        echo '<a href="/tests/">Tillbaka till testernas förstasida.</a>';
        echo '</p>';
    } else {
        global $ui_options;
        ui_top($ui_options);
        jscript_go_back();
    }
}