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

/**
 * Télécharge le modèle CSV d'import pour un stage — global ou pour un seul
 * élève, au même format que l'import CSV pour permettre un aller-retour
 * direct.
 *
 * @package local_kid
 */

require(__DIR__ . '/../../../config.php');

use local_kid\local\access_helper;
use local_kid\local\csv_import_service;
use local_kid\local\stage_repository;

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

$stageid = required_param('stageid', PARAM_INT);
$studentid = optional_param('studentid', 0, PARAM_INT);
$stage = stage_repository::get($stageid);
if (!$stage) {
    throw new \moodle_exception('invalidrecord', 'error');
}

$content = stage_repository::build_csv_template($stage, $studentid ?: null);
$filename = clean_param('stage_' . $stage->libelle . ($studentid ? '_eleve' : '') . '.csv', PARAM_FILE);
csv_import_service::send($content, $filename);
