<?php

/*
 *  Copyright (C) 2001 Kai Blaschke <webmaster@thw-theorie.de>
 *
 *  The included 'THW Thema' templates, logos and the Q&A catalog are protected
 *  by copyright laws, and must not be used without the written permission
 *  of the
 *
 *  Bundesanstalt Technisches Hilfswerk
 *  Provinzialstraße 93
 *  D-53127 Bonn
 *  Germany
 *  E-Mail: redaktion@thw.de
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
*
* Vollständiger Prüfungsbogen mit zufälligen Fragen
*
*/

$GLOBALS['tpl']->parseBlock('page-body', 'NavBogen', 'Sublinks');
$GLOBALS['tpl']->addVars('navBogen', 'current');

if (isset($_REQUEST['create']) && $_REQUEST['create'] == '1') {
    // Neuen Bogen erstellen
    unset($_SESSION['bogen']);

    // Werte initialisieren
    $_SESSION['bogen']['StartTime'] = time();
    $_SESSION['bogen']['Fragen'] = array();

    // Anz. Abschnitte und Fragen aus der DB holen
    $sectionCount = getSingleResult('SELECT COUNT(*) FROM abschnitte WHERE Jahr = ?', array('i', $_SESSION['jahr']));

    $questionId = 0;
    $questionSection = 0;

    $stmt = $GLOBALS['db']->prepare('SELECT ID, Abschnitt FROM fragen WHERE Jahr = ? ORDER BY Abschnitt, Nr');
    $stmt->bind_param('i', $_SESSION['jahr']);
    $stmt->execute();
    $stmt->bind_result($questionId, $questionSection);

    // Fragen in Array übertragen
    while ($stmt->fetch()) {
        $fragen[$questionId] = $questionSection;
    }
    $stmt->close();

    // Gesamtanzahl Fragen
    $count_ab = array_count_values($fragen);

    // Eine Frage pro Themengebiet aus der DB holen
    for ($i = 1; $i <= $sectionCount; $i++) {
        $nr = rand(1, $count_ab[$i]);
        $id = getSingleResult('SELECT ID FROM fragen WHERE Nr = ? AND Abschnitt = ? AND Jahr = ?',
            array('iii', $nr, $i, $_SESSION['jahr']));

        array_push($_SESSION['bogen']['Fragen'], (int)$id);

        // Frage entfernen
        unset($fragen[$id]);
    }

    // Restliche Fragen zufällig auffüllen.
    // Dazu verbliebene Keys von $fragen als Values in neues Array $fragen2 kopieren,
    // da shuffle() die Keys verwirft!
    $fragen2 = array();
    foreach ($fragen as $key => $value) {
        array_push($fragen2, $key);
    }

    shuffle($fragen2);
    for ($i = 0; $i < 40 - $sectionCount; $i++) {
        array_push($_SESSION['bogen']['Fragen'], array_shift($fragen2));
    }

    // Fragen erneut mischen
    shuffle($_SESSION['bogen']['Fragen']);

}

if (isset($_SESSION['bogen'])) {
    if (isset($_POST['antwort'])) {
        // Fragen beantwortet
        $richtig = 0;
        $falsch = 0;
        $fragen_cnt = count($_SESSION['bogen']['Fragen']);
        $zeit = time() - $_SESSION['bogen']['StartTime'];

        for ($i = 0; $i < $fragen_cnt; $i++) {
            $id = $_SESSION['bogen']['Fragen'][$i];
            $solutionBitmask = getSingleResult('SELECT `Loesung` FROM `fragen` WHERE `ID` = ?',
                array('i', $id));

            $loesung = array(1 => ($solutionBitmask & 0x1),
                2 => (($solutionBitmask & 0x2) >> 1),
                3 => (($solutionBitmask & 0x4) >> 2));

            if (isset($_POST['antwort'][$id])) {
                $korrekt = (($loesung[1] == (isset($_POST['antwort'][$id][1]) ? $_POST['antwort'][$id][1] : 0))
                    && ($loesung[2] == (isset($_POST['antwort'][$id][2]) ? $_POST['antwort'][$id][2] : 0))
                    && ($loesung[3] == (isset($_POST['antwort'][$id][3]) ? $_POST['antwort'][$id][3] : 0)));
            } else {
                $korrekt = false;
            }

            if ($korrekt) {
                $_SESSION["stats"]['Fragen_Richtig']++;
                $richtig++;
            } else {
                $_SESSION["stats"]['Fragen_Falsch']++;
                $falsch++;
            }
            $_SESSION["stats"]['Fragen_Bisher']++;
        }

        $_SESSION["stats"]['Boegen_Bisher']++;

        if (($richtig / $fragen_cnt) >= 0.8)
            $_SESSION["stats"]['Boegen_Bestanden']++;
        else
            $_SESSION["stats"]['Boegen_Durchgefallen']++;

        $GLOBALS['db']->query('UPDATE statistik SET fragen=fragen+' . $fragen_cnt
            . ',richtig=richtig+' . $richtig
            . ',falsch=falsch+' . $falsch
            . ',boegen=boegen+1'
            . ',bestanden=bestanden+' . (($richtig / $fragen_cnt) >= 0.8 ? 1 : 0)
            . ',durchgefallen=durchgefallen+' . (($richtig / $fragen_cnt) < 0.8 ? 1 : 0));

        $GLOBALS['tpl']->addTemplates('content', 'bogen-ende');

        $GLOBALS['tpl']->addVars(array(
            'anzFragen' => count($_SESSION['bogen']['Fragen']),
            'zeit' => sprintf('%02d:%02d\'%02d', $zeit / 3600, ($zeit / 60) % 60, $zeit % 60),
            'fragenRichtig' => $richtig,
            'fragenRichtigQuote' => sprintf('%.2f %%', $richtig / $fragen_cnt * 100),
            'fragenFalsch' => $falsch,
            'fragenFalschQuote' => sprintf('%.2f %%', $falsch / $fragen_cnt * 100)
        ));

        if (($richtig / $fragen_cnt) >= 0.8) {
            $GLOBALS['tpl']->parseBlock('content', 'Bestanden', 'Ja');
        }
        else {
            $GLOBALS['tpl']->parseBlock('content', 'Bestanden', 'Nein');
        }

        for ($i = 0; $i < count($_SESSION['bogen']['Fragen']); $i++) {
            if ($i > 0 && $i % 10 == 0) {
                // 'Nach oben'-Zeile an Handle anhängen
                $GLOBALS['tpl']->parseBlock('content', 'Aufloesung', 'Topline', TRUE);
            }
            Answer($_SESSION['bogen']['Fragen'][$i], $_POST['antwort'], FALSE);
            $GLOBALS['tpl']->parseBlock('content', 'Aufloesung', 'Antwort', TRUE, TRUE);
        }

        unset($_SESSION['bogen']);
    } else {
        // Fragen anzeigen
        $GLOBALS['tpl']->addTemplates(array(
            'content' => 'bogen-fragen'
        ));

        $GLOBALS['tpl']->addVars(array(
            'zeit' => date('G:i', $_SESSION['bogen']['StartTime'])
        ));

        for ($i = 0; $i < count($_SESSION['bogen']['Fragen']); $i++) {
            if ($i > 0 && $i % 10 == 0) {
                $GLOBALS['tpl']->parseBlock('content', 'Bogen', 'Topline', TRUE);
            }

            // Frage aus DB holen
            $question = getQuestionById($_SESSION['bogen']['Fragen'][$i]);

            $GLOBALS['tpl']->addVars(array(
                'frageIndex' => $i + 1,
                'frageID' => $question['ID'],
                'frageNr' => $question['Nr'],
                'abschnittNr' => $question['Abschnitt'],
                'frageText' => $question['Frage'],
                'Antwort1' => $question['Antwort1'],
                'Antwort2' => $question['Antwort2'],
                'Antwort3' => $question['Antwort3']
            ));

            if ($question['Antwort3'] == '') {
                $GLOBALS['tpl']->addVars('rowCnt', '2');
                $GLOBALS['tpl']->delBlockHandle('content', 'ThreeRows');
            } else {
                $GLOBALS['tpl']->addVars('rowCnt', '3');
                $GLOBALS['tpl']->parseBlock('content', 'ThreeRows', 'Row');
            }

            $GLOBALS['tpl']->parseBlock('content', 'Bogen', 'Frage', TRUE, TRUE);
        }

    }
} else {
    $GLOBALS['tpl']->addVars('navBogenNeu', 'current');
    $GLOBALS['tpl']->addTemplates(array('content' => 'bogen-start'));
}