<?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>
 */

require_once(__DIR__ . '/../../../config.php');
require_login();
$cmid = 0;
$attemptid = 0;
$num = 0;
if ($data = data_submitted()) {
    $cmid      = (int)$data->cmid;
    $attemptid = (int)$data->attemptid;
    $num       = isset($data->num) ? (int)$data->num : 0;

    $adesso = time();

    foreach ($data->scheme as $key => $val) {
        $itemdetid = $key;
        $scheme = strtolower($val);
        $da = strtolower($data->da[$key]);
        // Confronto.
        $sql = "SELECT d.itemtype, l.scheme, d.score
                FROM {mtcontabilita_itemdet} d, {mtcontabilita_list} l
                WHERE d.listid=l.id
                AND d.id=?";
        $record = $DB->get_record_sql($sql, [$itemdetid]);
        if ($record && strtolower($record->scheme) === $scheme && strtolower($record->itemtype) === $da/* && $record->mc === $mc*/) {
            $response[$itemdetid] = 'ok';
        } else {
            $response[$itemdetid] = 'ko';
        }

        $pars = ['attemptid' => $data->attemptid, 'mtcontabilitaid' => $data->mtcontabilitaid, 'itemdetid' => $key, 'bitem' => 'i'];
        if (!$rec = $DB->get_record('mtcontabilita_attempt_items', $pars)) {
            $new = new \stdClass();
            $new->attemptid = $data->attemptid;
            $new->mtcontabilitaid = $data->mtcontabilitaid;
            $new->itemdetid = $key;
            $new->scheme = $val;
            $new->da = $data->da[$key];
            $new->macroclasse = $data->mc[$key];
            $new->timemodified = $adesso;
            $new->timecreated = $adesso;
            $new->bitem = 'i';
            $new->itemnum = 0;
            $new->listid = 0;
            $new->bcheck = 1;
            $new->score = $response[$itemdetid] == 'ok' ? $record->score : 0;

            $DB->insert_record('mtcontabilita_attempt_items', $new);
        } else {
            $rec->timemodified = $adesso;
            $rec->scheme = $val;
            $rec->da = $data->da[$key];
            $rec->macroclasse = $data->mc[$key];

            $rec->bcheck = 1;
            $rec->score = $response[$itemdetid] == 'ok' ? $record->score : 0;

            $DB->update_record('mtcontabilita_attempt_items', $rec);
        }
    }
}

$params = ['cmid' => $cmid, 'num' => $num, 'attemptid' => $attemptid];
redirect(new moodle_url('/mod/mtcontabilita/player/attempt.php', $params));
