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

/**
 * Bouton "Synchroniser les feedbacks" (section 10.2 du cahier des charges) —
 * synchronise tous les dossiers d'une évaluation "Devoir Moodle" en un clic,
 * même chemin de code que la tâche planifiée
 * (`eval_repository::sync_feedback()`), jamais de logique dupliquée.
 *
 * @package local_kid
 */

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

use local_kid\local\access_helper;
use local_kid\local\eval_repository;

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

$id = required_param('id', PARAM_INT);
$promotionid = required_param('promotionid', PARAM_INT);

$eval = eval_repository::get($id);
if (!$eval || $eval->sourcetype !== 'moodle_devoir') {
    throw new \moodle_exception('invalidrecord', 'error');
}

$entries = eval_repository::get_entries_for_eval($id);
foreach ($entries as $entry) {
    eval_repository::sync_feedback((int) $entry->id);
}

$returnurl = new moodle_url('/local/kid/pages/admin.php', ['tab' => 'evaluations', 'promotionid' => $promotionid]);
redirect($returnurl, get_string('eval_sync_result', 'local_kid', count($entries)), null, \core\output\notification::NOTIFY_SUCCESS);
