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

/**
 * Hook class.
 */
class Hook_cron_points_posts
{
    /**
     * Get info from this hook.
     *
     * @param  ?TIME $last_run Last time run (null: never)
     * @param  ?boolean $calculate_num_queued Calculate the number of items queued, if possible (null: the hook may decide / low priority)
     * @return ?array Return a map of info about the hook (null: disabled)
     */
    public function info(?int $last_run, ?bool $calculate_num_queued) : ?array
    {
        if ((!addon_installed('points')) || (get_forum_type() == 'cns')) { // Conversr awards points instantly
            return null;
        }

        return [
            'label' => 'Credit forum post points',
            'num_queued' => null,
            'minutes_between_runs' => (60 * 24),
            'enabled_by_default' => true,
        ];
    }

    /**
     * Run function for system scheduler hooks. Searches for things to do. ->info(..., true) must be called before this method.
     *
     * @param  ?TIME $last_run Last time run (null: never)
     */
    public function run(?int $last_run)
    {
        if ((!addon_installed('points')) || (!addon_installed('cns_forum')) || (get_forum_type() == 'cns')) { // Conversr awards points instantly
            return;
        }

        $points_per_post = intval(get_option('points_posting'));

        require_code('points2');

        $member_id = null;
        do {
            $rows = $GLOBALS['FORUM_DRIVER']->get_next_members($member_id, 100);
            foreach ($rows as $row) {
                $member_id = $GLOBALS['FORUM_DRIVER']->mrow_member_id($row);

                $_prev_count = get_value('points_post__' . strval($member_id), '0', true);
                $cur_count = $GLOBALS['FORUM_DRIVER']->get_post_count($member_id);
                $diff = @intval($cur_count - @intval($_prev_count));

                if ($points_per_post > 0) {
                    if ($diff > 0) {
                        points_credit_member($member_id, do_lang('COUNT_POSTSTODAY'), ($diff * $points_per_post), 0, null, 0, 'post', 'add', '');
                    } elseif ($diff < 0) {
                        // We probably do not want to remove points for a negative change as this will make the ledger messy without context
                        // points_debit_member($member_id, do_lang('COUNT_POSTSTODAY'), (abs($diff) * @intval($points_per_post)), 0, 0, true, 0, 'post', 'delete', '');
                    }
                }

                set_value('points_post__' . strval($member_id), strval($cur_count), true);
            }
        } while (count($rows) > 0);
    }
}
