reset data of an array

Question

I need to do a type of reset in this query for when it detects a certain amount of videos seen from a different IP, send an alert message, the point here is that the condition is met when a user watches for example 4 videos in a certain time from an IP different and sends you the alert message, the problem is that if the user sees another video, he sends the alert again, but that video seen no longer enters the first condition, how can I make it respect it and when the user sees the fifth no longer send the message, could someone help me?
I don’t know much about php

the functions like wp_reset_ṕosdata and wp_reset_query () could serve me?

function qz_log_watch()
{
global $wpdb;

$topic_id = $_REQUEST['topic_id'];
$course_id = learndash_get_course_id($topic_id);
$user_id = get_current_user_id();

// $ban = qz_user_get_ban($user_id);
// if ($ban) {
//     qz_user_ban($user_id, $ban['reason']);
//     wp_send_json_error(qz_user_get_ban_message($banReason));
// }

$wpdb->insert(
    $wpdb->prefix . 'quizard_event_log',
    [
        'event' => 'watch',
        'time' => current_time('mysql'),
        'user_id' => $user_id,
        'ip' => ip2long(get_ip_address()),
        'user_agent' => $_SERVER['HTTP_USER_AGENT'],
        'value' => $topic_id,
        'meta' => json_encode([
            'course_id' => $course_id
        ])
    ]
);

$accountSharing = [
    'hours_ago' => get_field('account_sharing_check_hours_ago', 'option'),
    'unique_video_watches_threshold' => get_field('account_sharing_unique_video_watches_threshold', 'option')
];

$hoursAgo = $accountSharing['hours_ago'];
$unbannedAt = get_user_meta($user_id, 'qz_unbanned_at', true);
$dt = new DateTime();
if ($unbannedAt) {
    $dt->setTimestamp($unbannedAt);
} else {
    $dt->modify('-' . $hoursAgo . ' hours');
}
$fromTime = $dt->format('Y-m-d H:i:s');

$results = $wpdb->get_results(
    "
    SELECT user_id, COUNT(*) as shared_watches FROM
        (
            SELECT user_id, `value`, (count(DISTINCT ip) > 1) as different_ips, COUNT(*) AS watches, ip
            FROM {$wpdb->prefix}quizard_event_log
            WHERE time >= '{$fromTime}'
                AND user_id = $user_id
            GROUP BY user_id, `value`
        ) as t
    where t.watches > 1 and t.different_ips = 1
    group by t.user_id;
    "
);

if (is_array($results) && !empty($results)) {
    $row = $results[0];
    if ($row->shared_watches >= $accountSharing['unique_video_watches_threshold']) {
        $banReason = 'שיתוף';
        qz_user_ban($user_id, $banReason, false);
        //wp_send_json_error(qz_user_get_ban_message($banReason));
    }
}

wp_send_json_success();

}
add_action(‘wp_ajax_log_watch’, ‘qz_log_watch’);

0
Cecilia Duran 2 months 0 Answers 12 views 0

Leave an answer