<?php
$todas_las_categorías = $moodle_db->get('mdl_course_categories');
$todos_los_cursos = $moodle_db->query("SELECT courseid, fullname, shortname, COALESCE(MIN(calculation) <> '', false) as formula, MAX(AGGREGATIONCOEF) > 0 as ponderacion FROM  mdl_grade_items mgi JOIN mdl_course mc ON mc.id = mgi.courseid WHERE itemtype IN ('course', 'category') GROUP BY courseid, fullname, shortname");


function imprimir_curso($category_id = null)
{
    global $todas_las_categorías, $todos_los_cursos, $todos_los_items;
    # obtener subcategorías
    $categories = array_filter($todas_las_categorías, fn ($category) => $category['parent'] == $category_id ?? 0 && count(array_filter($todas_las_categorías, fn ($category) => $category['parent'] == $category_id ?? 0)) > 0);
    # print them as an accordion
    foreach ($categories as $category) : ?>
        <details>
            <summary><?= $category['name'] ?></summary>
            <?php imprimir_curso($category['id']) ?>
        </details>
    <?php endforeach;

    $courses = array_filter($todos_los_cursos, fn ($course) => $course['category'] == $category_id);
    if (count($courses) == 0) return;
    ?>
    <table>
        <tr>
            <th>Curso</th>
            <th>Construcción</th>
            <th>Ponderación</th>
        </tr>
        <tbody>
            <?php foreach ($courses as $course) : ?>
                <tr>
                    <td>
                        <?= $course['fullname'] ?>
                        <small><?= $course['shortname'] ?></small>
                    </td>
                    <td>
                        <?php

                        if (boolval($course['formula'])) : ?>
                            <i class="fas fa-check"></i>
                        <?php else : ?>
                            <i class="fas fa-times"></i>
                        <?php endif ?>
                    </td>
                    <td>
                        <?php
                        if (boolval($course['ponderacion'])) : ?>
                            <i class="fas fa-check"></i>
                        <?php else : ?>
                            <i class="fas fa-times"></i>
                        <?php endif ?>
                    </td>
                </tr>
            <?php endforeach ?>
        </tbody>
    </table>
<?php } ?>



<div class="grid">
    <label for="sin-construcción">
        Profesores que no han construido su curso
        <input type="radio" id="sin-construcción" name="construcción" value="false" checked />
    </label>
    <label for="construcción">
        Profesores que han construido su curso
        <input type="radio" id="construcción" name="construcción" value="true" checked />
    </label>
</div>
<?php
imprimir_curso();
?>
