<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    cns_forum
 */

/**
 * Hook class.
 */
class Hook_task_export_voting_power
{
    /**
     * Run the task hook.
     *
     * @param  float $base The root base to use for this calculation
     * @param  float $multiplier The multiplier to use
     * @param  integer $offset The offset to use
     * @param  ?integer $ceiling The ceiling to use (null: no (infinite) ceiling)
     * @param  ?string $file_type The file type to export with (null: default)
     * @return ?array A tuple of at least 2: Return mime-type, content (either Tempcode, or a string, or a filename and file-path pair to a temporary file), map of HTTP headers if transferring immediately, map of ini_set commands if transferring immediately (null: show standard success message)
     */
    public function run(float $base, float $multiplier, int $offset, ?int $ceiling, ?string $file_type = null) : ?array
    {
        if (!addon_installed('cns_forum') || !addon_installed('points') || get_forum_type() != 'cns') {
            return null;
        }

        require_code('cns_polls_action2');
        require_lang('cns_polls');
        require_code('points');

        require_code('files_spreadsheets_write');
        if ($file_type === null) {
            $file_type = spreadsheet_write_default();
        }

        $filename = 'voting_power_calculator_' . strval(time()) . '.' . $file_type;
        $outfile_path = null;
        $sheet_writer = spreadsheet_open_write($outfile_path, $filename);

        $member_id = null;
        $data = [];
        $total_power = 0.0;
        do {
            $rows = $GLOBALS['FORUM_DRIVER']->get_next_members($member_id, 100);
            foreach ($rows as $member) {
                $member_id = $GLOBALS['FORUM_DRIVER']->mrow_member_id($member);
                $username = $GLOBALS['FORUM_DRIVER']->get_username($member_id, false, USERNAME_DEFAULT_NULL);
                if ($username === null) {
                    $username = do_lang('UNKNOWN');
                }

                // Calculate voting power
                $points = points_rank($member_id);
                $voting_power = cns_points_to_voting_power($points);

                $data[] = [
                    'member' => $member_id,
                    'username' => $username,
                    'points' => $points,
                    'voting_power' => $voting_power,
                ];

                $total_power += $voting_power;
            }
        } while (count($rows) > 0);

        sort_maps_by($data, '!voting_power,!points');

        foreach ($data as $datum) {
            $data_point = [
                do_lang('MEMBER') => $datum['member'],
                do_lang('USERNAME') => $datum['username'],
                do_lang('POINTS') => $datum['points'],
                do_lang('VOTING_POWER') => $datum['voting_power'],
                do_lang('VOTING_POWER_CONTROL_PERCENTAGE') => (($datum['voting_power'] / $total_power) * 100),
            ];
            $sheet_writer->write_row($data_point);
        }
        $sheet_writer->close();

        $headers = [];
        $headers['Content-Type'] = $sheet_writer->get_mime_type();
        $headers['Content-Disposition'] = 'attachment; filename="' . escape_header($filename) . '"';

        $ini_set = [];
        $ini_set['ocproducts.xss_detect'] = '0';

        return [$sheet_writer->get_mime_type(), [$filename, $outfile_path], $headers, $ini_set];
    }
}
