Untitled

                Never    
PHP
       
<?php

// Initialize the session
session_start();
// Include config file
require_once "u2_config.php";

// Check if the user is already logged in, if yes then redirect him to logged page
if((!(isset($_SESSION["type"])) || ($_SESSION['type'] != admin))){
    header("location: u2_index.php");
    exit;
}

if(isset($_POST['sumbit_body'])){
    $tim=$_POST['tim'];
    $predmet=$_POST['predmet'];
    $rok=$_POST['rok'];
    $body=$_POST['body'];

    $sql = "UPDATE uloha2_timy SET body_tim='$body' WHERE predmet='$predmet' AND rok='$rok' AND tim='$tim'";

    $result = mysqli_query($conn, $sql);
}

if(isset($_POST['sumbit_body_admin'])){
    $tim=$_POST['tim'];
    $predmet=$_POST['predmet'];
    $rok=$_POST['rok'];

    $sql = "UPDATE uloha2_timy SET suhlas_admin=1 WHERE predmet='$predmet' AND rok='$rok' AND tim='$tim'";

    $result = mysqli_query($conn, $sql);
}

//zobrazovani timov vybraneho roku a predmetu
if(isset($_GET['show'])){
    $rok=$_GET['schoolYear'];
    $predmet = $_GET['predmetName'];

    echo "<h2>".$predmet." ".$rok."</h2>";

    $sql="SELECT * FROM uloha2
          INNER JOIN uloha2_timy ON uloha2_timy.tim=uloha2.tim
          INNER JOIN uloha2_predmety ON uloha2_predmety.predmet = uloha2_timy.predmet
          WHERE uloha2_timy.rok='$rok' AND uloha2_timy.predmet='$predmet' AND uloha2_predmety.rok=uloha2_timy.rok AND uloha2.id=uloha2_predmety.id  
          ORDER BY uloha2_timy.tim ASC";


    //premenne na zistenie ci je uz iny tim
    $actualTeam=null;
    $previousTeam=null;

    //premenna do ktorej sa uklada vypis
    $toPrint="";

    //vypisanie timov daneho predmetu, strasne nepekna vec, nic lepsi mna nenapadlo
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {   //Creates a loop to loop through results
            $classUdelitBody="btn btn-primary";
            $classSuhlasBody="btn btn-warning";
            $actualTeam= $row['tim'];
            $meno=$row['meno'];
            $email=$row['email'];
            $body=$row['body'];
            $suhlas = $row['suhlas'];
            $body_tim = $row['body_tim'];
            $suhlas_admin=$row['suhlas_admin'];

            if($body_tim!=null){
                $classUdelitBody="btn btn-primary disabled";
            }

            $sql2 = "SELECT uloha2_predmety.suhlas, uloha2_timy.suhlas_admin FROM uloha2 
                INNER JOIN uloha2_timy ON uloha2_timy.tim=uloha2.tim 
                INNER JOIN uloha2_predmety ON uloha2_predmety.predmet = uloha2_timy.predmet 
                WHERE uloha2_timy.rok='$rok' AND uloha2_timy.predmet='$predmet' AND uloha2_timy.tim='$actualTeam' AND uloha2_predmety.rok=uloha2_timy.rok AND uloha2.id=uloha2_predmety.id 
                ORDER BY uloha2_timy.tim ASC";

            $result2 = $conn->query($sql2);
            if ($result2->num_rows > 0) {
                while ($row = $result2->fetch_assoc()) {
                    $suhlasyClenov = $row['suhlas'];
                    $suhlasAdmina = $row['suhlas_admin'];

                    if($suhlasyClenov==0 ){
                        $classSuhlasBody="btn btn-warning disabled";
                        $text = "Odsúhlasiť body tímu";
                    }

                    if($suhlasAdmina==1){
                        $classSuhlasBody="btn btn-warning disabled";
                        $text = "Odsúhlasené";
                    }


                }
            }

            if($previousTeam!=$actualTeam){

                //suhlas clenov timov --> suhlas admina povoleny
                $sql2 = "SELECT uloha2_predmety.suhlas, uloha2_timy.suhlas_admin FROM uloha2 
                INNER JOIN uloha2_timy ON uloha2_timy.tim=uloha2.tim 
                INNER JOIN uloha2_predmety ON uloha2_predmety.predmet = uloha2_timy.predmet 
                WHERE uloha2_timy.rok='$rok' AND uloha2_timy.predmet='$predmet' AND uloha2_timy.tim='$actualTeam' AND uloha2_predmety.rok=uloha2_timy.rok AND uloha2.id=uloha2_predmety.id 
                ORDER BY uloha2_timy.tim ASC";



                $toPrint=$toPrint."</table><br><br><label>Tím ".$actualTeam." <br>
                Body tímu: $body_tim</label>
            <form action='".$_SERVER['REQUEST_URI']."' method='post' enctype='multipart/form-data'>
                <input type='number' name='body' placeholder='Body pre tím' min='0'>
                <input type='text' name='tim' value='$actualTeam' hidden>
                <input type='text' name='rok' value='$rok' hidden>
                <input type='text' name='predmet' value='$predmet' hidden>
                <button type='submit' name='sumbit_body' class='$classUdelitBody' >Prideliť body tímu</button>
            </form>
              
            <form action='".$_SERVER['REQUEST_URI']."' method='post' enctype='multipart/form-data'>
                <input type='text' name='tim' value='$actualTeam' hidden>
                <input type='text' name='rok' value='$rok' hidden>
                <input type='text' name='predmet' value='$predmet' hidden>
                <button type='submit' name='sumbit_body_admin' class='$classSuhlasBody' >$text</button>
            </form>

            
            <table class='table table-bordered'>
                <thead class='thead-dark'>
                    <tr>
                        <th scope='col' class=\"col-md-3\">Email</th>
                        <th scope='col' class=\"col-md-4\">Meno</th>
                        <th scope='col' class=\"col-md-2\">Body</th>
                        <th scope='col' class=\"col-md-2\">Súhlas</th>
                    </tr>
                </thead>";
                    $previousTeam=$actualTeam;
            }
            $toPrint=$toPrint."
            <tr>
                <td>".$email."</td>
                <td>".$meno."</td>
                <td>".$body."</td>
                <td>".$suhlas."</td>
            </tr>";
        }
        if(isset($toPrint)){
            $toPrint=$toPrint."</table>";
            echo $toPrint;
        }
    }
}


//importovani
$predmetNameErr="";
if (isset($_POST["import"])) {
    $fileName=$_FILES['file']['tmp_name'];

    if($_POST['predmetName']==""){
        $predmetNameErr="Pole názov predmetu musí byť vyplnené";
    }

    $predmet=$_POST['predmetName'];
    $rok=$_POST['schoolYear'];

    if ($_FILES["file"]["size"] > 0 && ($_POST['predmetName']!=null)) {

        $file = fopen($fileName, "r");
        fgetcsv($file, 10000, $_POST['separator']); //skip one line

        //ulozenie predmetu
        $sql = "INSERT INTO predmet (nazov,rok)
                   values ('". $predmet ."','". $rok ."')";

        if (mysqli_query($conn, $sql) === TRUE) {
            $id_predmetu_ins = $conn->insert_id;
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        while (($column = fgetcsv($file, 10000, $_POST['separator'])) !== FALSE) {
            $heslo=password_hash($column[3], PASSWORD_DEFAULT);

            //ulozenie prihlas. udajov
            $sql = "INSERT INTO student (id,meno,email,heslo)
                   values ('" . $column[0] . "','". $column[1] ."','". $column[2] ."','". $heslo ."')";

            if (mysqli_query($conn, $sql) === TRUE) {
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }


            //ulozenie timov

            $sql="SELECT * FROM tim 
                  WHERE cislo=$column[4] AND id_predmet=$id_predmetu_ins";
            $result = $conn->query($sql);

            if ($result->num_rows == 0) {
                $sql = "INSERT into tim (cislo, id_predmet)
                        values ('". $column[4] ."','". $id_predmetu_ins ."')";

                $result = mysqli_query($conn, $sql);
                $id_timu_ins = $conn->insert_id;
            }


            $sql = "INSERT INTO zaznam (id_predmet,id_tim,id_student)
                    values ('" . $id_predmetu_ins . "','". $id_timu_ins ."','". $column[0] ."')";
            if (mysqli_query($conn, $sql) === TRUE) {
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
<!--    upload udajov-->
    <div class="wrapper">
        <h2>Import výsledkov</h2>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label>Školský rok</label>
                <select name='schoolYear' class="form-control">
                    <!--
                        tu este nie je jasne ci to chcu takto napevno roky alebo ci to bude nejako v DB a
                        odtial sa to bude tahat
                     -->
                    <option value="ZS 2019/2020">ZS 2019/2020</option>
                    <option value="LS 2019/2020">LS 2019/2020</option>
                    <option value="ZS 2020/2021">ZS 2020/2021</option>
                    <option value="LS 2020/2021">LS 2020/2021</option>
                    <option value="ZS 2021/2022">ZS 2021/2022</option>
                    <option value="LS 2021/2022">LS 2021/2022</option>
                </select>
            </div>
            <div class="form-group" <?php echo (!empty($predmetNameErr)) ? 'has-error' : ''; ?>>
                <label>Názov predmetu</label>
                <input type="text" name="predmetName" class="form-control">
                <span class="help-block"><?php echo $predmetNameErr; ?></span>
            </div>
            <div class="form-group">
                <div class="custom-file">
                    <label class="btn btn-default btn-file">
                        <input type="file" name="file" class="custom-file-input" accept=".csv">
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label>Oddeľovač stĺpcov</label>
                <select class="form-control" name="separator">
                    <option value=";"> ; </option>
                    <option value=","> , </option>
                </select>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Import" name="import">
            </div>
            <!--<p>Don't have an account? <a href="register.php">Sign up now</a>.</p> Ak by bolo potrebne aj registráciu tak pridám-->
            <a href="u2_logout.php" class="btn btn-danger">Sign Out of Your Account</a>
        </form>
    </div>

<!--    zobrazenie timov-->
    <div class="wrapper">
        <h2>Zobrazenie výsledkov</h2>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="get">
            <select name='schoolYear' id="schoolYear" class="form-control" onChange="getPredmet(this.value);">
                <option value="">Vyber rok</option>
                <option value="ZS 2019/2020">ZS 2019/2020</option>
                <option value="LS 2019/2020">LS 2019/2020</option>
                <option value="ZS 2020/2021">ZS 2020/2021</option>
                <option value="LS 2020/2021">LS 2020/2021</option>
                <option value="ZS 2021/2022">ZS 2021/2022</option>
                <option value="LS 2021/2022">LS 2021/2022</option>
            </select>

            <select name='predmetName' id="predmetName" class="form-control">
                <option value="">Vyber rok</option>
            </select>

            <div class="form-group">
                <input type="submit" class="btn btn-primary" name="show" value="Show">
            </div>
        </form>
    </div>

<!--    jQuery -> $_POST na "select option"-->
    <script>
        function getPredmet(val) {
            $.ajax({
                type: "POST",
                url: "u2_selectOptions.php",
                data: 'rok=' + val,
                success: function (data) {
                    $("#predmetName").html(data);
                }
            });
        }
    </script>
</body>
</html>