<?php
/*
 * Copyright (C) 2021 Eichehome
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

class Datenbank extends DBClass
{
    /**
     * Diese Methode prüft, ob das Loginpasswort und -name mit den Werten in der Datenbank übereinstimmen
     * @param string $name Loginname
     * @param string $passwort Loginpasswort
     * @return boolean
     */
    public function checkLogin(string $name, string $passwort)
    {
        $sql = 'SELECT * FROM Test_v2 WHERE Name=:name AND Code=:passwort';

        $params = array(
            'name' => strtolower($name),
            'passwort' => $passwort
        );

        $result = $this->select($sql, $params);

        if (count($result) == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Diese Methode prüft, ob ein Nutzer sich schon eingetragen hat
     * @param string $name
     * @return boolean
     */
    public function hasAlredySelected(string $name)
    {
        $sql = 'SELECT * FROM Test_v2 WHERE Name=:name AND Kurs IS NOT NULL AND Kurs2 IS NOT NULL AND Kurs3 IS NOT NULL ';

        $params = array(
            'name' => $name
        );

        $result = $this->select($sql, $params);

        if (count($result) == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Diese Methode gibt alle verfügbaren Kurse aus
     * @return Countable|array
     */
    public function selectKurse()
    {
        $sql = 'SELECT name, stunde FROM Kurse';

        return $this->select($sql);
    }

    /**
     * Diese Methode trägt die Auswahl eines Benutzers in die Datenbank ein
     * @param string Loginname
     * @param array[strings] Die ausgewählten Kurse
     * @return boolean
     */
    public function insertSelection(string $name, array $kurse)
    {
        if (count($kurse) == 3) {
            $sql = 'UPDATE Test_v2 SET Kurs=:kurs1, Kurs2=:kurs2, Kurs3=:kurs3 WHERE name =:name';

            $params = array(
                'name' => $name,
                'kurs1' => $kurse[0],
                'kurs2' => $kurse[1],
                'kurs3' => $kurse[2]
            );

            return $this->query($sql, $params);
        } else {
            return false;
        }
    }

    /**
     * Nicht mehr nötig
     * @param string $name
     * @return boolean
     */
    /*public function updateHasAlredySelected(string $name)
    {
        return true;
    }*/
}