<?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.
 */

/**
 * Holt eine einzelne Frage aus der Datenbank.
 * @param int $id Die Frage-ID
 * @return array Die Fragen-Inhalte als assoziatives Array.
 */
function getQuestionById(int $id): array
{
    $stmt = $GLOBALS['db']->prepare('SELECT * FROM fragen WHERE ID=?');
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $result = $stmt->get_result();

    $question = $result->fetch_array(MYSQLI_ASSOC);

    return $question;
}

/**
 * Holt eine Liste aller Abschnitte im aktuellen Jahr
 * @return array Liste der Themenbereiche.
 */
function getTopics(): array
{
    $nr = 0;
    $description = '';

    $stmt = $GLOBALS['db']->prepare('SELECT `Nr`,`Beschreibung` FROM `abschnitte` WHERE `Jahr` = ? ORDER BY `Nr`');
    $stmt->bind_param('i', $_SESSION['jahr']);
    $stmt->execute();
    $stmt->bind_result($nr, $description);

    $topics = array();
    while ($stmt->fetch()) {
        $topics[$nr] = $description;
    }
    $stmt->close();

    return $topics;
}

/**
 * Kopfzeile mit Informationen über Fragen-Nummer und Lernabschnitt.
 * @param int $id Frage-ID
 * @param int $nr Frage-Nummer
 * @param int $questionCount Anzahl Fragen
 * @return void
 */
function questionHeader(int $id, int $nr, int $questionCount): void
{
    $section = getSingleResult('SELECT Abschnitt FROM fragen WHERE ID=? AND Jahr=?',
        array('ii', $id, $_SESSION['jahr']));

    $sectionName = getSingleResult('SELECT Beschreibung FROM abschnitte WHERE Nr=? AND Jahr=?',
        array('ii', $section, $_SESSION['jahr']));

    $GLOBALS['tpl']->addVars(Array(
            'aktuelleFrage' => $nr,
            'fragenCnt'     => $questionCount,
            'abschnittNr'   => $section,
            'abschnitt'     => $sectionName
    ));

    $GLOBALS['tpl']->parseBlock('content', 'Kopf', 'Content');
}

/**
 * Eine einzelne Frage mit Kopfzeile ausgeben.
 * @param int $id Frage-ID
 * @param int $nr Frage-Nummer
 * @param int $questionCount Anzahl Fragen
 * @return void
 */
function SingleQuestion(int $id, int $nr, int $questionCount): void
{
    questionHeader($id, $nr, $questionCount);
    showQuestion($id);
}

/**
 * Fragengenerator
 *
 * Erzeugt eine Tabelle mit Frage, Antworten und Formularinformationen
 * zur übergebenen Fragen-ID
 * @param int $id Fragen-ID
 * @return void
 */
function showQuestion(int $id): void
{
    // Frage aus DB holen
    $stmt = $GLOBALS['db']->prepare('SELECT * FROM fragen WHERE ID=?');
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $result = $stmt->get_result();

    $question = $result->fetch_array(MYSQLI_ASSOC);

    $GLOBALS['tpl']->addVars(Array(
            'frageID'     => $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', TRUE);
    }

    $result->close();
    $stmt->close();
}

/**
 * Auswertung
 *
 * Erzeugt eine Tabelle mit Frage, Antworten und Markiert richtige/falsche
 * Antworten farblich (grün=richtig, rot=falsch)
 * @param int $id Frage-ID
 * @param array $answer Antwort-Array
 * @param bool $showStatus Status anzeigen
 * @return bool true, falls korrekt beantwortet, sonst false.
 */
function Answer(int $id, array $answer, bool $showStatus = true): bool
{
    // Frage aus DB holen
    $question = getQuestionById($id);

    // Einzelne Antworten aus Flag in Array splitten
    $solution = array( 1 => ( $question['Loesung'] & 0x1),
                      2 => (($question['Loesung'] & 0x2) >> 1),
                      3 => (($question['Loesung'] & 0x4) >> 2));

    $correct = (($solution[1] == (isset($answer[$id][1]) ? $answer[$id][1] : 0))
             && ($solution[2] == (isset($answer[$id][2]) ? $answer[$id][2] : 0))
             && ($solution[3] == (isset($answer[$id][3]) ? $answer[$id][3] : 0)));

    // Aufräumen
    $GLOBALS['tpl']->delHandle('antwortStatus');
    $GLOBALS['tpl']->delHandle('antwort1Loesung');
    $GLOBALS['tpl']->delHandle('antwort2Loesung');
    $GLOBALS['tpl']->delHandle('antwort3Loesung');

    $GLOBALS['tpl']->delBlockHandle('content', 'A1L');
    $GLOBALS['tpl']->delBlockHandle('content', 'A2L');
    $GLOBALS['tpl']->delBlockHandle('content', 'A3L');

    if ($showStatus) {
        if ($correct)
            $GLOBALS['tpl']->parseBlock('content', 'Status', 'Richtig');
        else
            $GLOBALS['tpl']->parseBlock('content', 'Status', 'Falsch');
    } else {
        if ($correct)
            $GLOBALS['tpl']->parseBlock('content', 'BogenStatus', 'Richtig');
        else
            $GLOBALS['tpl']->parseBlock('content', 'BogenStatus', 'Falsch');
    }

    $GLOBALS['tpl']->addVars(Array(
            'abschnittNr'    => $question['Abschnitt'],
            'frageNr'        => $question['Nr'],
            'frageText'      => $question['Frage'],
            'antwort1Status' => $solution[1] ? 'korrekt' : 'falsch',
            'antwort2Status' => $solution[2] ? 'korrekt' : 'falsch',
            'antwort3Status' => $solution[3] ? 'korrekt' : 'falsch',
            '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');
    }

    if (isset($answer[$id][1]) && $answer[$id][1] === '1') $GLOBALS['tpl']->parseBlock('content', 'A1L', 'Haken');
    if (isset($answer[$id][2]) && $answer[$id][2] === '1') $GLOBALS['tpl']->parseBlock('content', 'A2L', 'Haken');
    if (isset($answer[$id][3]) && $answer[$id][3] === '1') $GLOBALS['tpl']->parseBlock('content', 'A3L', 'Haken');

    // Anhängen per default
    $GLOBALS['tpl']->parse('content', 'antwort', TRUE, TRUE);

    return $correct;

}

/**
 * Brotkrumen-Navigation.
 *
 * Fügt einen Link zur Brotkrumen-Navigation hinzu.
 * @param string $page Seiten-Link
 * @param string $title Seiten-Titel
 * @return void
 */
function addBreadcrumb(string $page, string $title): void
{
    $GLOBALS['tpl']->addVars(Array(
            'page' => htmlspecialchars($page),
            'pageTitle' => htmlspecialchars($title)
    ));
    $GLOBALS['tpl']->parseBlock('page-body', 'Breadcrumb', 'Link', TRUE);
}

/**
 * Zeigt die Auflösung an.
 * @param array $frage Referenz auf das assoziative Fragen-Array
 * @return void
 */
function ShowAnswer(array &$frage): void
{


    // Einzelne Antworten aus Flag in Array splitten
    $loesung = array( 1 => ( $frage['Loesung'] & 0x1),
                      2 => (($frage['Loesung'] & 0x2) >> 1),
                      3 => (($frage['Loesung'] & 0x4) >> 2));

    // Aufräumen
    $GLOBALS['tpl']->delHandle('antwortStatus');
    $GLOBALS['tpl']->delHandle('antwort1Loesung');
    $GLOBALS['tpl']->delHandle('antwort2Loesung');
    $GLOBALS['tpl']->delHandle('antwort3Loesung');

    $GLOBALS['tpl']->addVars(Array(
            'abschnittNr'    => $frage['Abschnitt'],
            'frageNr'        => $frage['Nr'],
            'frageText'      => $frage['Frage'],
            'antwort1Status' => $loesung[1]?'korrekt':'falsch',
            'antwort2Status' => $loesung[2]?'korrekt':'falsch',
            'antwort3Status' => $loesung[3]?'korrekt':'falsch',
            'Antwort1'       => $frage['Antwort1'],
            'Antwort2'       => $frage['Antwort2'],
            'Antwort3'       => $frage['Antwort3']
    ));

    $GLOBALS['tpl']->delBlockHandle('content', 'A1L');
    $GLOBALS['tpl']->delBlockHandle('content', 'A2L');
    $GLOBALS['tpl']->delBlockHandle('content', 'A3L');

    if ($loesung[1]) $GLOBALS['tpl']->parseBlock('content', 'A1L', 'Haken');
    if ($loesung[2]) $GLOBALS['tpl']->parseBlock('content', 'A2L', 'Haken');
    if ($loesung[3]) $GLOBALS['tpl']->parseBlock('content', 'A3L', 'Haken');

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