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

/**
 * Récupère les niveaux (jamais les commentaires) de la session CAPP
 * précédente, pour un élève ou toute la promotion.
 *
 * @package    local_kid
 */

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

use local_kid\local\access_helper;
use local_kid\local\capp_repository;

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

$sessionid = required_param('sessionid', PARAM_INT);
$studentid = optional_param('studentid', 0, PARAM_INT);

if ($studentid) {
    $copied = capp_repository::copy_previous_niveaux($sessionid, $studentid);
    redirect(new moodle_url('/local/kid/pages/capp.php', ['sessionid' => $sessionid, 'studentid' => $studentid]),
        get_string('capp_recover_result', 'local_kid', $copied), null, \core\output\notification::NOTIFY_SUCCESS);
}

$result = capp_repository::copy_previous_niveaux_bulk($sessionid, $USER->id);

redirect(new moodle_url('/local/kid/pages/capp.php', ['sessionid' => $sessionid]),
    get_string('capp_recover_bulk_result', 'local_kid', (object) $result), null, \core\output\notification::NOTIFY_SUCCESS);
