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

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

/**
 * mtcontabilita_manage class
 */
class mtcontabilita_manage {

    /**
     * get_users_listing
     */
    public static function get_users_listing($cid, $sort='name', $dir='ASC', $page=0, $recordsperpage=0,
                               $search='', $firstinitial='', $lastinitial='', $extraselect='',
                               array $extraparams=null, $extracontext = null) {

        global $DB, $CFG;

        $select = "courseid=$cid";
        $params = [];

        if (!empty($search)) {
            $search = trim($search);
            $select .= " AND (". $DB->sql_like($fullname, ':search1', false, false).
                       " OR ". $DB->sql_like('email', ':search2', false, false).
                       " OR username = :search3)";
            $params['search1'] = "%$search%";
            $params['search2'] = "%$search%";
            $params['search3'] = "$search";
        }

        if ($firstinitial) {
            $select .= " AND ". $DB->sql_like('firstname', ':fni', false, false);
            $params['fni'] = "$firstinitial%";
        }
        if ($lastinitial) {
            $select .= " AND ". $DB->sql_like('lastname', ':lni', false, false);
            $params['lni'] = "$lastinitial%";
        }

        if ($extraselect) {
            // The extra WHERE clause may refer to the 'id' column which can now be ambiguous because we
            // changed the query to include joins, so replace any 'id' that is on its own (no alias)
            // with 'u.id'.
            $extraselect = preg_replace('~([ =]|^)id([ =]|$)~', '$1u.id$2', $extraselect);
            $select .= " AND $extraselect";
            $params = $params + (array)$extraparams;
        }

        if ($sort) {
            $sort = " ORDER BY $sort $dir";
        }

        $sql = "SELECT * FROM {mtcontabilita_list} WHERE $select $sort";
        return $DB->get_records_sql($sql, $params, $page, $recordsperpage);
    }

    /**
     * get_users
     */
    public static function get_users($cid, $get=true, $search='', $confirmed=false, array $exceptions=null, $sort='firstname ASC',
                       $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*', $extraselect='', array $extraparams=null) {
        global $DB, $CFG;

        $select = "courseid=$cid";
        $params = [];

        if (!empty($search)) {
            $search = trim($search);
            $select .= " AND (".$DB->sql_like($fullname, ':search1', false)." OR ".$DB->sql_like('email', ':search2', false)." OR username = :search3)";
            $params['search1'] = "%$search%";
            $params['search2'] = "%$search%";
            $params['search3'] = "$search";
        }

        if ($confirmed) {
            $select .= " AND confirmed = 1";
        }

        if ($exceptions) {
            list($exceptions, $eparams) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'ex', false);
            $params = $params + $eparams;
            $select .= " AND id $exceptions";
        }

        if ($firstinitial) {
            $select .= " AND ".$DB->sql_like('firstname', ':fni', false, false);
            $params['fni'] = "$firstinitial%";
        }
        if ($lastinitial) {
            $select .= " AND ".$DB->sql_like('lastname', ':lni', false, false);
            $params['lni'] = "$lastinitial%";
        }

        if ($extraselect) {
            $select .= " AND $extraselect";
            $params = $params + (array)$extraparams;
        }

        if ($get) {
            return $DB->get_records_select('mtcontabilita_list', $select, $params, $sort, $fields, $page, $recordsperpage);
        } else {
            return $DB->count_records_select('mtcontabilita_list', $select, $params);
        }
    }
}
