<?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.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Simulatore contabilità
 *
 * @package    mod_mtcontabilita
 * @copyright  2026 Mediatouch 2000 Srl <https://www.mediatouch.it>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Marco Cappuccio <m.cappuccio@mediatouch.it>
 */
use mod_mtcontabilita\mtcontabilita_attempt;

require_once(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/mod/mtcontabilita/locallib.php');
require_once($CFG->dirroot.'/mod/mtcontabilita/player/locallib.php');

$id  = required_param('id', PARAM_INT);  // Course module ID.
$aid = required_param('aid', PARAM_INT); // Attempt ID.

// Ottiene corso e modulo.
list($course, $cm) = get_course_and_cm_from_cmid($id, 'mtcontabilita');
$context = context_module::instance($cm->id);

require_course_login($course, true, $cm);
\mod_mtcontabilita\permission::require_can_view_mtcontabilita($context);

$PAGE->set_url(new moodle_url('/mod/mtcontabilita/player/endattempt.php', ['id' => $id, 'aid' => $aid]));
$PAGE->set_cacheable(false);
$PAGE->set_heading($course->fullname);
$PAGE->set_title(get_string('endattempt', 'mod_mtcontabilita'));

// Recupera i dati del tentativo se vuoi mostrarli.
$attempt = $DB->get_record('mtcontabilita_attempts', ['id' => $aid, 'userid' => $USER->id], '*', MUST_EXIST);

// Prepara link di ritorno.
$returnurl = new moodle_url('/mod/mtcontabilita/view.php', ['id' => $cm->id]);

echo $OUTPUT->header();

// ✅ Messaggio di ringraziamento personalizzato.
echo html_writer::tag('h3', get_string('thankyouattempt', 'mod_mtcontabilita'));

echo html_writer::start_div('mtcontabilita-endattempt');
echo html_writer::tag('p', get_string('attemptcompleted', 'mod_mtcontabilita',
    userdate($attempt->timefinish))
);

// Se vuoi mostrare punteggio o altri dati:
if (!empty($attempt->score)) {
    echo html_writer::tag('p', get_string('attemptscore', 'mod_mtcontabilita', $attempt->score));
}

// Pulsante per tornare alla pagina principale.
echo html_writer::div(
    $OUTPUT->single_button($returnurl, get_string('backtocourse', 'mod_mtcontabilita'), 'get'),
    'mtcontabilita-backbtn'
);

echo html_writer::end_div();

echo $OUTPUT->footer();
