<?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 une évaluation — 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\eval_repository;

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

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

$content = eval_repository::build_csv_template($eval, $studentid ?: null);
$filename = clean_param('evaluation_' . $eval->titre . ($studentid ? '_eleve' : '') . '.csv', PARAM_FILE);
csv_import_service::send($content, $filename);
