<commit_before><?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
    // just to be sure, we verify that they really are admins of the team.
    if ($arguments["roleid"] != ROLE_ADMIN) {
        header("Status: 300", true, 300);
        die();
    }

    $data = json_decode(file_get_contents('php://input'), true);

    $db->autocommit(FALSE); // ensure all changes go through.

    foreach ($data as $id => $change) {
        $db->query("UPDATE klandring
                    SET verdict=$change[verdict], paid=$change[paid]
                    WHERE id=$id");
    }

    if (!$db->commit()) {
        header("Status: 500", true, 500);
    }
    echo "all is well";
    die();
}

$sql = "SELECT * FROM `klandring` WHERE team = $arguments[id]";
$result = $db->query($sql);

$klandringer = [];
$ids = [];
$users = [];

while ($row = $result->fetch_assoc()) {
    $klandringer[] = $row;
}
$result->free();

if (count($klandringer)) {
    foreach ($klandringer as $klandring) {
        $ids[] = intval($klandring["from"]);
        $ids[] = intval($klandring["to"]);
    }

    // todo: solve in one go, instead of 3.
    $ids = array_values(array_unique($ids));
    sort($ids);

    $users = array_combine($ids, get_user_infos_arr($ids));
}

$base = "var unchanged={";

function a_checkbox($row, $users, $checked) {
    $name = $users[$row["from"]]["name"];
    if ($checked) {
        return "<input type=\"checkbox\" id=\"va-$row[id]\" checked> $name";
    } else {
        return "<input type=\"checkbox\" id=\"va-$row[id]\"> $name";
    }
}
function b_checkbox($row, $users, $checked) {
    $name = $users[$row["to"]]["name"];
    if ($checked) {
        return "<input type=\"checkbox\" id=\"vb-$row[id]\" checked> $name";
    } else {
        return "<input type=\"checkbox\" id=\"vb-$row[id]\"> $name";
    }
}
?>
<div class="panel panel-default">
    <div class="panel-heading">Håndtering af <?= $arguments["name"] ?></div>
<?php if(count($klandringer) > 0) : ?>
    <table class='table table-hover'>
        <thead>
            <tr>
                <th>ID</th>
                <th>Dato</th>
                <th>Klandrer</th>
                <th>Klandret</th>
                <th>Betalt</th>
            </tr>
        </thead>
        <tbody id="klandringer">
<?php foreach ($klandringer as $row) : ?>
            <tr id="r-<?= $row["id"] ?>">
                <td><?= $row["id"]?></td>
                <td><div id="d-<?= $row["id"] /* TODO: edit options */?>"><?= $row["verdictdate"] ?></div></td>
<?php switch($row["verdict"]): ?>
<?php default: ?>
<?php case 0: ?>
                <td><?= a_checkbox($row, $users, false) ?></td>
                <td><?= b_checkbox($row, $users, false) ?></td>
<?php break; ?>
<?php case 1: ?><commit_msg>added a button for admin-klandringer<commit_after><?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
    // just to be sure, we verify that they really are admins of the team.
    if ($arguments["roleid"] != ROLE_ADMIN) {
        header("Status: 300", true, 300);
        die();
    }

    $data = json_decode(file_get_contents('php://input'), true);

    $db->autocommit(FALSE); // ensure all changes go through.

    foreach ($data as $id => $change) {
        $db->query("UPDATE klandring
                    SET verdict=$change[verdict], paid=$change[paid]
                    WHERE id=$id");
    }

    if (!$db->commit()) {
        header("Status: 500", true, 500);
    }
    echo "all is well";
    die();
}

$sql = "SELECT * FROM `klandring` WHERE team = $arguments[id]";
$result = $db->query($sql);

$klandringer = [];
$ids = [];
$users = [];

while ($row = $result->fetch_assoc()) {
    $klandringer[] = $row;
}
$result->free();

if (count($klandringer)) {
    foreach ($klandringer as $klandring) {
        $ids[] = intval($klandring["from"]);
        $ids[] = intval($klandring["to"]);
    }

    // todo: solve in one go, instead of 3.
    $ids = array_values(array_unique($ids));
    sort($ids);

    $users = array_combine($ids, get_user_infos_arr($ids));
}

$base = "var unchanged={";

function a_checkbox($row, $users, $checked) {
    $name = $users[$row["from"]]["name"];
    if ($checked) {
        return "<input type=\"checkbox\" id=\"va-$row[id]\" checked> $name";
    } else {
        return "<input type=\"checkbox\" id=\"va-$row[id]\"> $name";
    }
}
function b_checkbox($row, $users, $checked) {
    $name = $users[$row["to"]]["name"];
    if ($checked) {
        return "<input type=\"checkbox\" id=\"vb-$row[id]\" checked> $name";
    } else {
        return "<input type=\"checkbox\" id=\"vb-$row[id]\"> $name";
    }
}
?>
<div class="panel panel-default">
    <div class="panel-heading clearfix">
        <div class="pull-left panel-title" style="padding-top:7.5px;">Håndtering af <?= $arguments["name"] ?></div>
        <div class="btn-group pull-right">
        <?php if($arguments["roleid"] == ROLE_ADMIN) {
            echo "<a href=\"admin-klandring\" class=\"btn btn-default\" style=\"\"><i class=\"glyphicon glyphicon-pencil\"> </i> Tilføj klandring</a>";
        }?>
        </div>
    </div>
<?php if(count($klandringer) > 0) : ?>
    <table class='table table-hover'>
        <thead>
            <tr>
                <th>ID</th>
                <th>Dato</th>
                <th>Klandrer</th>
                <th>Klandret</th>
                <th>Betalt</th>
            </tr>
        </thead>
        <tbody id="klandringer">
<?php foreach ($klandringer as $row) : ?>
            <tr id="r-<?= $row["id"] ?>">
                <td><?= $row["id"]?></td>
                <td><div id="d-<?= $row["id"] /* TODO: edit options */?>"><?= $row["verdictdate"] ?></div></td>
<?php switch($row["verdict"]): ?>
<?php default: ?>
<?php case 0: ?>
                <td><?= a_checkbox($row, $users, false) ?></td>
                <td><?= b_checkbox($row, $users, false) ?></td>
<?php break; ?>
<?php case 1: ?>
{'fraction_non_alphanumeric': 0.22375361579036923, 'fraction_numerical': 0.005785264590777607, 'mean_word_length': 2.5282112845138056, 'pattern_counts': {'":': 0, '<': 110, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974723', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1978, 'n_words': 442}