<?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.

/**
 * Vue élève — dossier transverse d'un élève (compétences, évaluations,
 * stages, CAPP). Un élève ne voit toujours que son propre dossier PUBLIÉ
 * (student_page::export_for_template() force publishedonly=true). Le staff
 * détenant local/kid:viewany peut prévisualiser un autre élève via
 * ?studentid= (local_kid_student.id, même convention que tous les écrans
 * Évaluations/Stages/CAPP) et y voit alors la fiche exhaustive (tout statut
 * de workflow/publication, chacun marqué individuellement) — un simple
 * élève ne peut jamais activer ce mode, le paramètre n'est honoré que si le
 * compte courant détient réellement local/kid:viewany, sinon il est
 * silencieusement ignoré et l'élève résolu reste toujours celui de $USER,
 * quoi qu'ait demandé l'URL.
 *
 * @package local_kid
 */

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

use local_kid\event\dossier_consulted;
use local_kid\local\access_helper;
use local_kid\local\nav_helper;
use local_kid\local\promotion_repository;
use local_kid\local\promotion_selector;
use local_kid\output\student_page;

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

global $DB;

$canpreview = access_helper::has_capability_or_teacher('local/kid:viewany', $context);
$requestedstudentid = optional_param('studentid', 0, PARAM_INT);

$ownpromotion = promotion_repository::get_active_promotion_for_user((int) $USER->id);
$ownstudentrecord = $ownpromotion
    ? promotion_repository::get_active_student_record((int) $ownpromotion->id, (int) $USER->id)
    : null;

$studentid = $ownstudentrecord ? (int) $ownstudentrecord->id : 0;
$promotionid = $ownpromotion ? (int) $ownpromotion->id : 0;
$ispreview = false;

if ($canpreview && $requestedstudentid && $requestedstudentid !== $studentid) {
    $targetrecord = $DB->get_record('local_kid_student', ['id' => $requestedstudentid], '*', IGNORE_MISSING);
    if ($targetrecord) {
        $ispreview = true;
        $studentid = (int) $targetrecord->id;
        $promotionid = (int) $targetrecord->promotionid;

        dossier_consulted::create([
            'context' => $context,
            'userid' => (int) $USER->id,
            'other' => ['studentid' => $studentid, 'promotionid' => $promotionid],
        ])->trigger();
    }
}

$previewoptions = [];
if ($canpreview) {
    $selectedpromotionid = promotion_selector::get_selected((int) $USER->id);
    if ($selectedpromotionid) {
        $previewoptions = promotion_repository::get_active_students($selectedpromotionid);
    }
}

$candownload = $ispreview ? has_capability('local/kid:view', $context) : true;

$PAGE->set_url('/local/kid/pages/student.php', $requestedstudentid ? ['studentid' => $requestedstudentid] : []);
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('page_student', 'local_kid'));
local_kid_setup_page($PAGE);
$PAGE->requires->js_call_amd('local_kid/student_accordions', 'init');

$renderer = $PAGE->get_renderer('local_kid');
$page = new student_page($studentid, $promotionid, $ispreview, $candownload, $previewoptions);

echo $OUTPUT->header();
echo $renderer->render_shell(
    nav_helper::get_sidebar_items('student'),
    $OUTPUT->heading(get_string('page_student', 'local_kid'), 2) . $renderer->render_student_page($page)
);
echo $OUTPUT->footer();
