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

namespace mod_mtcontabilita\output;

use context_module;
use renderer_base;
use core_reportbuilder\system_report_factory;
use mod_mtcontabilita\reportbuilder\local\systemreports\sessions;

/**
 * mtintems class.
 */
class mtitems_view implements \templatable, \renderable {
    /**
     * @var \stdClass $mtcontabilita
     */
    private $mtcontabilita;

    /**
     * @var int $signupsessionid
     */
    private $signupsessionid;

    /**
     * Constructor
     *
     * @param \stdClass $mtcontabilita
     * @param int $signupsessionid Session to show sign up modal on page load.
     */
    public function __construct(\stdClass $mtcontabilita, int $signupsessionid = 0) {
        $this->mtcontabilita = $mtcontabilita;
        $this->signupsessionid = $signupsessionid;
    }

    /**
     * Exports for template.
     *
     * @param renderer_base $output
     * @return array
     */
    public function export_for_template(renderer_base $output): array {
        $cm = get_coursemodule_from_instance('mtcontabilita', $this->mtcontabilita->id, $this->mtcontabilita->course);
        $contextmodule = context_module::instance($cm->id);

        // Create our report instance in the course module context.
        $report = system_report_factory::create(sessions::class, $contextmodule, '', '', 0, [
            'mtcontabilitaid' => $this->mtcontabilita->id,
        ]);

        $context = [
            'sessionslisttable' => $report->output(),
            'signupsessionid' => $this->signupsessionid,
        ];

        if ($this->signupsessionid) {
            $session = mtcontabilita_get_session($this->signupsessionid);
            $context['cansignup'] = \mod_mtcontabilita\permission::can_signup($session, \context_module::instance($cm->id));
        }

        if (\mod_mtcontabilita\permission::can_edit_sessions(\context_module::instance($cm->id))) {
            $context['actions'] = $cm->id;

        }

        return $context;
    }
}
