<?php
$translation        = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname           = "form_contact_list";
$inputname          = "input_id";
$input_page_id      = (isset($_REQUEST["input_page_id"]) ? $_REQUEST["input_page_id"] : "");
$input_component_id = (isset($_REQUEST["input_component_id"]) ? $_REQUEST["input_component_id"] : "");

function load_contact_list($input_id){
    $query = "SELECT * FROM main_contact WHERE active = 1 ORDER BY name ASC";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while($row = @mysqli_fetch_array($result)) {
        $query2 = "SELECT * FROM learning_unit_submit WHERE learning_unit_id = '".$input_id."' AND main_contact_id = ".$row['id']." ORDER BY id DESC LIMIT 1";
        $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
        $row2 = @mysqli_fetch_array($result2);

        $query_mandant = "SELECT * FROM main_mandant WHERE id = ".$row['master_mandant_id'];
        $result_mandant = @mysqli_query($GLOBALS['mysql_con'], $query_mandant);
        $row_mandant = @mysqli_fetch_array($result_mandant);

        $query_active = "SELECT * FROM learning_unit_contact WHERE learning_unit_id= '".$input_id."' AND main_contact_id = ".$row['id'];
        $result_active = @mysqli_query($GLOBALS['mysql_con'], $query_active);
        $row_active = @mysqli_fetch_array($result_active);

        $datetime = "";
        if($row2['datetime']){
            $datetime = new DateTime($row2['datetime']);
            $datetime = $datetime->format('d.m.Y');
        }
        $done = "";
        $active = "";
        $class = "";
        if(isset($row_active) && $row_active['active'] == 1){
            $active = "checked='checked'";
        }
        if($row2['correct'] == 1){
            $done = "checked='checked'";
            $class = "checked";
        }
    ?>
    <tr class='<?= $class ?>'>
        <input type='hidden' name='contact_<?= $row['id']?>'>
        <td><input type='checkbox' class='checkbox' name='done_<?= $row['id']?>' <?= $done?>></td>
        <td><?= $row['name'] ?></td>
        <td><?= $row_mandant['description'] ?></td>
        <td><?= $datetime?></td>
        <td><input type='checkbox' class='checkbox' name='active_<?= $row['id']?>' <?= $active?>></td>
    </tr>
    <?php
    }
}
?>
<h2>Benutzerliste</h2>
<table class='learning_unit_user_list'>
    <tr>
        <th>Erledigt</th>
        <th>Benutzer</th>
        <th>Mandant</th>
        <th>Ausstellungsdatum</th>
        <th>Aktiv</th>
    </tr>
    <?= load_contact_list($input_id);?>
</table>