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

use moodle_exception;
use stdClass;

/**
 * Class permission to perform permission checks.
 */
class permission {

    /**
     * session book
     */
    public static function is_session_bookable(\stdClass $session, \context_module $contextmodule): bool {
        return true;
    }

    /**
     * If user can signup to given session.
     *
     * @param stdClass $session
     * @param \context_module $contextmodule
     * @return bool
     */
    public static function can_signup(\stdClass $session, \context_module $contextmodule): bool {
        try {
            self::require_can_signup($session, $contextmodule);
        } catch (\moodle_exception $e) {
            return false;
        }
        return true;
    }

    /**
     * If user can cancel the given session.
     *
     * @param stdClass $session
     * @param \context_module $contextmodule
     * @return bool
     */
    public static function can_cancel_signup(\stdClass $session, \context_module $contextmodule): bool {
        try {
            self::require_can_cancel_signup($session, $contextmodule);
        } catch (\moodle_exception $e) {
            return false;
        }
        return true;
    }

    /**
     * Make sure user can signup to given session.
     *
     * @param stdClass $session
     * @param \context_module $contextmodule
     * @throws \moodle_exception
     */
    public static function require_can_signup(\stdClass $session, \context_module $contextmodule) {
        global $USER;
        // Check access to the module and capability to sign up.
        require_capability('mod/mtcontabilita:view', $contextmodule);

        // This is equivalent to self::is_session_bookable() but with various exception messages.
        $timenow = time();
        if (mtcontabilita_has_session_started($session, $timenow)) {
            if (mtcontabilita_is_session_in_progress($session, $timenow)) {
                throw new \moodle_exception('cannotsignupsessioninprogress', 'mod_mtcontabilita');
            } else {
                throw new \moodle_exception('cannotsignupsessionover', 'mod_mtcontabilita');
            }
        }

        // Check if user has already signed up.
        if (mtcontabilita_get_user_submissions($session->mtcontabilita, $USER->id)) {
            throw new \moodle_exception('alreadysignedup', 'mod_mtcontabilita');
        }
    }

    /**
     * Make sure user can cancel signup in any session on given module context.
     *
     * @param stdClass $session
     * @param \context_module $contextmodule
     * @throws moodle_exception
     */
    public static function require_can_cancel_signup(\stdClass $session, \context_module $contextmodule) {
        require_capability('mod/mtcontabilita:view', $contextmodule);

        if (mtcontabilita_check_signup($session->mtcontabilitaid) != $session->id) {
            throw new moodle_exception('notsignedup', 'mod_mtcontabilita');
        }

        if (mtcontabilita_has_session_started($session, time()) ) {
            throw new moodle_exception('error:eventoccurred', 'mod_mtcontabilita');
        }
    }

    /**
     * User can add mtcontabilita instance.
     *
     * @param \context_course $context
     * @return bool
     */
    public static function can_add_instance(\context_course $context): bool {
        return has_capability('mod/mtcontabilita:addinstance', $context);
    }

    /**
     * Make sure user can add mtcontabilita instance.
     *
     * @param \context_course $context
     */
    public static function require_can_add_instance(\context_course $context) {
        require_capability('mod/mtcontabilita:addinstance', $context);
    }

    /**
     * User can view attendees
     *
     * @param \context_module $context
     * @return bool
     */
    public static function can_view_attendees(\context_module $context): bool {
        return has_capability('mod/mtcontabilita:view', $context);
    }

    /**
     * User can view mtcontabilita.
     *
     * @param \context_module $context
     * @return bool
     */
    public static function can_view_mtcontabilita(\context_module $context): bool {
        return has_capability('mod/mtcontabilita:view', $context);
    }

    /**
     * User can edit sessions
     *
     * @param \context_module $context
     * @return bool
     */
    public static function can_edit_sessions(\context_module $context): bool {
        return has_capability('mod/mtcontabilita:view', $context);
    }

    /**
     * Require user to be able to view attendees
     *
     * @param \context_module $context
     */
    public static function require_can_view_attendees(\context_module $context) {
        require_capability('mod/mtcontabilita:view', $context,
            null, true, 'errorcannotviewattendees', 'mod_mtcontabilita');
    }

    /**
     * Require user to be able to view mtcontabilita
     *
     * @param \context_module $context
     */
    public static function require_can_view_mtcontabilita(\context_module $context) {
        require_capability('mod/mtcontabilita:view', $context,
            null, true, 'errorcannotviewmtcontabilita', 'mod_mtcontabilita');
    }
}
