<?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\local\helpers;

use stdClass;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot. '/mod/mtcontabilita/lib.php');

/**
 * Class mtcontabilita_format
 *
 * @package    mod_mtcontabilita
 * @author     2019 David Matamoros <davidmc@moodle.com>
 * @copyright  2019 Moodle Pty Ltd <support@moodle.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class mtcontabilita_format {

    /**
     * Returns Session date and times
     *
     * @param string $value
     * @param stdClass $row
     * @return string
     */
    public static function sessiondatetime(string $value, stdClass $row): string {
        $formatdate = get_string('strftimedaydate', 'langconfig');
        $formattime = get_string('strftimetime', 'langconfig');
        $dateobj = [];
        $dateobj['itemdate'] = userdate($row->itemdate, $formatdate);

        return get_string('sessionitemdateandtimewithouttimezone', 'mod_mtcontabilita', $dateobj);
    }

    /**
     * returns session status
     *
     * @param string|null $value
     * @param stdClass $row
     * @return string
     */
    public static function sessionstatus(?string $value, stdClass $row): string {
        if (!isset($row->sessionid)) {
            return '';
        }
        $session = mtcontabilita_get_session($row->sessionid);
        return mtcontabilita_get_session_info($session)['status'];
    }

    /**
     * Returns formatted description
     *
     * @param string|null $value
     * @param stdClass $row
     * @return string
     */
    public static function description(?string $value, stdClass $row): string {
        if (empty($row->id)) {
            return '';
        }
        $contextid = \context_module::instance($row->id)->id;
        $description = file_rewrite_pluginfile_urls($row->description, 'pluginfile.php', $contextid,
            'mod_mtcontabilita', 'session', $row->mtcontabilitaid);
        return format_text($description, $row->descriptionformat);
    }
}
