PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Create the auction_penalties table CREATE TABLE auction_penalties ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, created_at DATETIME NOT NULL, penalty_type VARCHAR(50) ); -- Insert some sample entries INSERT INTO auction_penalties (user_id, created_at, penalty_type) VALUES (1, '2024-05-01 10:00:00', 'Late Payment'), (1, '2024-06-01 10:00:00', 'Bid Retraction'), (1, '2024-07-01 10:00:00', 'Non-Delivery'), (1, '2024-08-01 10:00:00', 'Late Payment'), (2, '2024-06-01 10:00:00', 'Bid Retraction'), (2, '2024-07-01 10:00:00', 'Bid Retraction'), (2, '2024-08-01 10:00:00', 'Non-Delivery'), (3, '2024-07-01 10:00:00', 'Bid Retraction'), (3, '2024-08-01 10:00:00', 'Non-Delivery'), (4, '2024-05-01 10:00:00', 'Non-Delivery'), (4, '2024-06-01 10:00:00', 'Non-Delivery');
Copy Clear
Copy Format Clear
<?php // Suggested Values for $penalty_time_frame and Their Impact // 30 days: Focuses on recent behavior. This setting is strict, targeting users who have accumulated penalties in the last month. // 90 days: Provides a balance between short-term and long-term behavior, penalizing users who consistently misbehave over several months. // 180 days: Tracks half a year's worth of penalties, useful for identifying persistent problematic behavior over a longer period. // GETTERS // The time frame for penalties (e.g., 90 days) // Suggested Values and Their Impact: // 30 days: Focuses on recent behavior. This setting is strict, targeting users who have accumulated penalties in the last month. // 90 days: Provides a balance between short-term and long-term behavior, penalizing users who consistently misbehave over several months. // 180 days: Tracks half a year's worth of penalties, useful for identifying persistent problematic behavior over a longer period. function getPenaltyTimeFrame() { //$PENALTY_TIME_FRAME = 90; $PENALTY_TIME_FRAME = 180; return $PENALTY_TIME_FRAME; } function getMinimumPenaltyCounts() { $MIN_PENALTY_COUNTS = [ //'many' => 10, // Many penalties //'moderate' => 5, // Moderate penalties //'few' => 0 // Few penalties 'many' => 1, // Many penalties 'moderate' => 0, // Moderate penalties 'few' => 0 // Few penalties ]; return $MIN_PENALTY_COUNTS; } function getPenaltyCountGroupName($penalty_count) { $group_name = ''; $penalty_count = max($penalty_count, 0); $min_penalty_counts = getMinimumPenaltyCounts(); foreach ($min_penalty_counts as $name => $min_count) { if ($penalty_count < $min_count) { continue; } if (empty($group_name)) { $group_name = $name; } else if ($min_penalty_counts[$group_name] < $min_count) { $group_name = $name; } } return $group_name; } // // Retrieve all entries from the auction_penalties table // for a specific user within the last $penalty_time_frame days. function getEntriesForAuctionPenalty($user_id) { global $mysqli; // Initialize an empty array to store penalty entries $entries = []; // Get the time frame for penalties $penalty_time_frame = getPenaltyTimeFrame(); // SQL query to fetch penalties for the user within the specified time frame $sql = "SELECT *, NOW() AS present_timestamp FROM auction_penalties WHERE user_id = $user_id AND created_at >= NOW() - INTERVAL $penalty_time_frame DAY"; // Execute the query $result = $mysqli->query($sql); // Check if the result contains any rows if ($result->num_rows > 0) { // Fetch rows and add them to the $entries array while ($row = $result->fetch_assoc()) { $entries[] = $row; } } // Return the fetched penalty entries return $entries; } // // AUCTION PENALTY SYSTEM /* * This system manages user participation in an auction platform based on their penalty history. * Users can accumulate penalties for violating auction rules, and these penalties decay over time. * The system calculates a penalty score for each user based on their recent penalties, with a decay * rate that adjusts according to the number of penalties. * * The penalty score is used to assign the user to a penalty tier: * - Tier 1: Mild restrictions (full participation allowed). * - Tier 2: Moderate restrictions (bidding allowed, other actions restricted). * - Tier 3: Strict restrictions (participation denied). * * The `getParticipation_info` function evaluates a user's eligibility to participate in specific * auction actions (e.g., creating an auction, placing a bid) based on their penalty tier, and * provides feedback or restricts participation accordingly. * * This system ensures that users with more infractions face stricter consequences, while allowing * penalties to diminish in impact over time, encouraging better behavior. */ // Determine the user's participation eligibility based on their penalty tier. // The function checks what action the user is trying to take (e.g., $control = 'place-bid' for placing a bid) and restricts or allows it accordingly. function getParticipation_info($user_id, $control) { // Fetch the user's penalties within the defined time frame $penalties = getEntriesForAuctionPenalty(user_id: $user_id); // Calculate the user's penalty score based on decayed penalties $score = getAuctionPenaltyScore(penalties: $penalties); // Assign a penalty tier based on the calculated score $tier = getAuctionPenaltyTier(score: $score); // Initialize the response array with default values $info = [ 'can_participate' => true, // Default to allowing full participation 'warning_message' => "" // Default to no warning message ]; // Determine participation restrictions based on the penalty tier switch ($tier) { case 'Tier 1': // Tier 1: Allow full participation break; case 'Tier 2': // Tier 2: Only allow bidding if ($control != 'place-bid') { $info['can_participate'] = false; $info['warning_message'] = "Participation restricted to bidding only."; } else { $info['warning_message'] = "You have restricted participation due to penalties."; } break; case 'Tier 3': // Tier 3: Deny participation entirely $info['can_participate'] = false; $info['warning_message'] = "Participation denied due to excessive penalties."; break; } // Return the participation information array return $info; } // Calculate the penalty score for a user based on decayed penalties. // Penalties decay over time, and the rate of decay is determined by the number of penalties. function getAuctionPenaltyScore($penalties = [], $benchmark = false) { // Initialize the penalty score $score = 0; if ($benchmark) { $timestamp_now = date('Y-m-d H:i:s'); $timestamp_some_days_ago = date('Y-m-d H:i:s', strtotime('-30 days')); $penalties = [ [ 'present_timestamp' => $timestamp_now, 'created_at' => $timestamp_some_days_ago ] ]; } // Get the total count of penalties $penalty_count = count($penalties); // Calculate the decay rate based on the number of penalties $decay_rate = calculateDecayRate(penalty_count: $penalty_count); // Loop through each penalty to calculate its decayed value foreach ($penalties as $penalty) { // Calculate the number of days since the penalty was created $daysAgo = (strtotime($penalty['present_timestamp']) - strtotime($penalty['created_at'])) / (60 * 60 * 24); // Calculate the decay factor using an exponential decay formula $decayFactor = exp(-$daysAgo / $decay_rate); // Accumulate the decayed penalty score $score += $decayFactor; } // Return the total decayed penalty score return $score; } // Suggested Values for $decay_rate and Their Impact // 10: Fast decay, where penalties lose half their value in about 7 days. This is suitable for a forgiving system. // 30: Moderate decay, where penalties lose half their value in about 21 days. It offers a balanced approach. // 60: Slow decay, where penalties lose half their value in about 42 days. This setting is stricter, keeping penalties relevant for a longer period. // Determine the decay rate based on the number of penalties. // More penalties result in a slower decay, meaning penalties retain their impact longer. function calculateDecayRate($penalty_count) { $group_name = getPenaltyCountGroupName(penalty_count: $penalty_count); $decay_rate = 0; switch ($group_name) { // Many penalties case 'many': $decay_rate = 60; // Slow decay for many penalties break; // Moderate penalties case 'moderate': $decay_rate = 30; // Moderate decay for a moderate number of penalties break; // Few penalties case 'few': $decay_rate = 10; // Fast decay for few penalties break; } return $decay_rate; } // Assign a penalty tier based on the calculated penalty score. // The higher the score, the stricter the restrictions. function getAuctionPenaltyTier($score) { $benchmark_score = getAuctionPenaltyScore(benchmark: true); if ($score > $benchmark_score) { return 'Tier 3'; // Strict restrictions } else if ($score > 0.75 * $benchmark_score) { return 'Tier 2'; // Moderate restrictions } else { return 'Tier 1'; // Mild restrictions } } // // $penalty_time_frame = getPenaltyTimeFrame(); // $penalties = getEntriesForAuctionPenalty(user_id: 1); // print_r($penalties); // $penalty_count = 2; // printf( getPenaltyCountGroupName($penalty_count) ); // The target user $user_id = 4; // Fetch the user's penalties within the defined time frame $penalties = getEntriesForAuctionPenalty(user_id: $user_id); // Calculate the user's penalty score based on decayed penalties $score = getAuctionPenaltyScore(penalties: $penalties); printf("Score: $score" . PHP_EOL); // Assign a penalty tier based on the calculated score // $tier = getAuctionPenaltyTier(score: $score); $benchmark_score = getAuctionPenaltyScore(benchmark: true); printf("Benchmark score: $benchmark_score" . PHP_EOL); // Check if the user can participate in a specific action (e.g., place a bid) // $user_id = 4; $control = 'place-bid'; $participation_info = getParticipation_info($user_id, $control); $can_participate = $participation_info['can_participate']; $warning_message = $participation_info['warning_message']; print_r($participation_info);
Show:  
Copy Clear