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

/**
 * Scope base class for aggregated options view.
 *
 * @package mod_booking
 * @copyright 2025 Wunderbyte GmbH <info@wunderbyte.at>
 * @author Georg Maißer, Bernhard Fischer
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace mod_booking\booking_answers;

use local_wunderbyte_table\wunderbyte_table;
use mod_booking\table\manageusers_table;

/**
 * Scope base class for aggregated options view.
 *
 * @package mod_booking
 * @copyright 2025 Wunderbyte GmbH <info@wunderbyte.at>
 * @author Georg Maißer, Bernhard Fischer
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class scope_base_options extends scope_base {
    /**
     * Helper function to get the $selectpart for the return_sql_for_booked_users function.
     * @param string $scope
     * @return string the select part of the sql query
     */
    public function get_selectpart(string $scope): string {
        return
            "SELECT
                bo.id,
                bo.id as optionid,
                ba.waitinglist,
                cm.id AS cmid,
                c.id AS courseid,
                c.fullname AS coursename,
                bo.titleprefix,
                bo.text,
                b.name AS instancename,
                COUNT(ba.id) answerscount,
                SUM(pcnt.presencecount) presencecount,
                '" . $scope . "' AS scope
            FROM {booking_options} bo
            LEFT JOIN {booking_answers} ba ON bo.id = ba.optionid
            LEFT JOIN {user} u ON ba.userid = u.id
            JOIN {course_modules} cm ON bo.bookingid = cm.instance
            JOIN {booking} b ON b.id = bo.bookingid
            JOIN {course} c ON c.id = b.course
            JOIN {modules} m ON m.id = cm.module
            LEFT JOIN (
                SELECT boda.optionid, boda.userid, COUNT(*) AS presencecount
                FROM {booking_optiondates_answers} boda
                WHERE boda.status = :statustocount
                GROUP BY boda.optionid, boda.userid
            ) pcnt
            ON pcnt.optionid = ba.optionid AND pcnt.userid = u.id";
    }

    /**
     * Helper function to get the $endpart for the return_sql_for_booked_users function.
     * @return string the end part of the sql query
     */
    public function get_endpart(): string {
        return
            "GROUP BY cm.id, c.id, c.fullname, bo.id, ba.waitinglist, bo.titleprefix, bo.text, b.name
            ORDER BY bo.titleprefix, bo.text ASC
            LIMIT 1000000";
    }

    /**
     * This functions defines the columns for each scope.
     *
     * @param int $statusparam
     *
     * @return array
     *
     */
    public function return_cols_for_tables(int $statusparam): array {
        $columns = [
            'titleprefix' => get_string('titleprefix', 'mod_booking'),
            'text'  => get_string('bookingoption', 'mod_booking'),
            'answerscount'     => get_string('answerscount', 'mod_booking'),
        ];

        // Only for actual bookings, we need the presence counter.
        if (get_config('booking', 'bookingstrackerpresencecounter') && $statusparam == 0) {
            $columns['presencecount'] = get_string('presencecount', 'mod_booking');
        }
        return $columns;
    }
}
