<?php
ini_set('include_path', '../_class/');
include('class_sql.php');
include('class_log.php');
/*
foreach ($_POST as $key => $value) {
    trigger_error($_SERVER['HTTP_HOST'], E_USER_NOTICE);
}
*/
if ($_SERVER['HTTP_HOST'] == 'cegledcoach.hu') $sql = new sql('bcd_mysql','root','','badminton_coach');
else $sql = new sql('localhost','livingsp_coach','R186er012qw5','livingsp_badminton');

$check_query = "SELECT * FROM presence WHERE pr_training_tr_id = " . $_POST['tr_id'] . " AND pr_user_kid_uk_id = " . $_POST['user_id'] . ";";

if (!$sql->num_of_rows($check_query) && $_POST['checked'] == "false") {
    $sql->insert_into('presence', array('pr_user_kid_uk_id' => $_POST['user_id'], 'pr_training_tr_id' => $_POST['tr_id']));
    log::register('new_presence', 'Edzés: ' . $_POST['tr_id'] . ', gyerek: ' . $_POST['user_id'], null, $_POST['admin_id']);

    if ($_POST['credited'] == 'true') {
        $sql->update_table('user_kid', array('uk_credit' => $_POST['credit'] + 1), array('uk_id' => $_POST['user_id']));
    }
}
elseif ($sql->num_of_rows($check_query) && $_POST['checked'] == "true") {
    $sql->execute_query('delete from presence where pr_user_kid_uk_id = ' . $_POST['user_id'] . ' AND pr_training_tr_id = ' . $_POST['tr_id']);
    log::register('delete_presence', 'Edzés: ' . $_POST['tr_id'] . ', gyerek: ' . $_POST['user_id'], null, $_POST['admin_id']);

    if ($_POST['credit'] > 0) {
        $sql->update_table('user_kid', array('uk_credit' => $_POST['credit'] - 1), array('uk_id' => $_POST['user_id']));
    }
}

?>
