<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

/**
 * Annuaire des élèves — recherche + abécédaire A-Z (façon liste de
 * participants Moodle), lecture seule, filtrée et paginée, sur les élèves
 * actifs rattachés à une promotion KID. Aucune écriture : {@see
 * \local_kid\local\promotion_repository::diff_and_apply_membership()} est
 * le seul chemin qui alimente {local_kid_student} (déjà lecture seule sur
 * les cohortes Moodle natives).
 *
 * @package local_kid
 */

require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/local/kid/lib.php');

use local_kid\local\access_helper;
use local_kid\local\nav_helper;

require_login();
$context = context_system::instance();
access_helper::require_capability_or_teacher('local/kid:viewannuaire', $context);

$filters = [
    'sifirst' => optional_param('sifirst', 'all', PARAM_NOTAGS),
    'silast' => optional_param('silast', 'all', PARAM_NOTAGS),
    'studentquery' => optional_param('studentquery', '', PARAM_TEXT),
    'promotionid' => optional_param('promotionid', 0, PARAM_INT),
];

$sort = optional_param('sort', 'lastname', PARAM_ALPHA);
$dir = optional_param('dir', 'ASC', PARAM_ALPHA);
$page = optional_param('page', 0, PARAM_INT);

$PAGE->set_url('/local/kid/pages/annuaire.php', array_filter($filters + ['sort' => $sort, 'dir' => $dir, 'page' => $page]));
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('page_annuaire', 'local_kid'));
local_kid_setup_page($PAGE);

$renderer = $PAGE->get_renderer('local_kid');
$inner = $renderer->render_annuaire_page(new \local_kid\output\annuaire_page($filters, $sort, $dir, $page));
$shell = $renderer->render_admin_shell(get_string('page_annuaire', 'local_kid'), '', [], $inner);

echo $OUTPUT->header();
echo $renderer->render_shell(nav_helper::get_sidebar_items('annuaire'), $shell);
echo $OUTPUT->footer();
