Auto commit: 2026-02-18T23:43:39.506Z
This commit is contained in:
parent
656a7fadcd
commit
4197c9a786
@ -78,6 +78,40 @@ $top_chatters = $db->query("
|
|||||||
LIMIT 10
|
LIMIT 10
|
||||||
")->fetchAll();
|
")->fetchAll();
|
||||||
|
|
||||||
|
// 6. Award Fan Points to Top 3 Chatters (Locking to prevent double rewards)
|
||||||
|
$rewards = [50, 30, 20];
|
||||||
|
$rewarded_users = [];
|
||||||
|
$today = date('Y-m-d');
|
||||||
|
|
||||||
|
$stmt_lock = $db->prepare("SELECT last_run FROM automation_logs WHERE task_name = 'weekly_report_rewards' AND DATE(last_run) = ?");
|
||||||
|
$stmt_lock->execute([$today]);
|
||||||
|
|
||||||
|
if (!$stmt_lock->fetch()) {
|
||||||
|
foreach ($top_chatters as $i => $c) {
|
||||||
|
if ($i >= 3) break;
|
||||||
|
$username = $c['username'];
|
||||||
|
$points_to_add = $rewards[$i];
|
||||||
|
|
||||||
|
$stmt_fan = $db->prepare("SELECT id FROM fans WHERE name = ?");
|
||||||
|
$stmt_fan->execute([$username]);
|
||||||
|
$fan = $stmt_fan->fetch();
|
||||||
|
|
||||||
|
if ($fan) {
|
||||||
|
$db->prepare("UPDATE fans SET points = points + ? WHERE id = ?")->execute([$points_to_add, $fan['id']]);
|
||||||
|
} else {
|
||||||
|
$db->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$username, $points_to_add]);
|
||||||
|
}
|
||||||
|
$rewarded_users[$username] = $points_to_add;
|
||||||
|
}
|
||||||
|
$db->prepare("INSERT INTO automation_logs (task_name, last_run) VALUES ('weekly_report_rewards', NOW()) ON DUPLICATE KEY UPDATE last_run = NOW()")->execute();
|
||||||
|
} else {
|
||||||
|
// If already rewarded today, we still want to show it in the email being generated
|
||||||
|
foreach ($top_chatters as $i => $c) {
|
||||||
|
if ($i >= 3) break;
|
||||||
|
$rewarded_users[$c['username']] = $rewards[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$report_date = date('d/m/Y');
|
$report_date = date('d/m/Y');
|
||||||
$week_start = date('d/m/Y', strtotime('-7 days'));
|
$week_start = date('d/m/Y', strtotime('-7 days'));
|
||||||
|
|
||||||
@ -99,6 +133,7 @@ $html_content = "
|
|||||||
th { background: #f4f4f4; text-align: left; padding: 10px; border-bottom: 2px solid #ddd; }
|
th { background: #f4f4f4; text-align: left; padding: 10px; border-bottom: 2px solid #ddd; }
|
||||||
td { padding: 10px; border-bottom: 1px solid #eee; }
|
td { padding: 10px; border-bottom: 1px solid #eee; }
|
||||||
.crown { color: #ffca28; }
|
.crown { color: #ffca28; }
|
||||||
|
.reward-badge { background: #fff9c4; color: #f57f17; padding: 2px 6px; border-radius: 4px; font-size: 0.75rem; font-weight: bold; border: 1px solid #fbc02d; }
|
||||||
.footer { text-align: center; font-size: 0.8rem; color: #999; margin-top: 40px; border-top: 1px solid #eee; padding-top: 20px; }
|
.footer { text-align: center; font-size: 0.8rem; color: #999; margin-top: 40px; border-top: 1px solid #eee; padding-top: 20px; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@ -139,12 +174,16 @@ $html_content .= "</tbody></table>
|
|||||||
<h2>💬 Top 10 Interactores del Chat</h2>
|
<h2>💬 Top 10 Interactores del Chat</h2>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr><th>#</th><th>Usuario</th><th>Mensajes</th></tr>
|
<tr><th>#</th><th>Usuario</th><th>Mensajes</th><th>Recompensa</th></tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>";
|
<tbody>";
|
||||||
foreach ($top_chatters as $i => $c) {
|
foreach ($top_chatters as $i => $c) {
|
||||||
$emoji = ($i === 0) ? "👑 " : "💬 ";
|
$emoji = ($i === 0) ? "👑 " : "💬 ";
|
||||||
$html_content .= "<tr><td>" . ($i + 1) . "</td><td>$emoji" . htmlspecialchars($c['username']) . "</td><td><b>" . $c['count'] . "</b></td></tr>";
|
$reward_html = "";
|
||||||
|
if (isset($rewarded_users[$c['username']])) {
|
||||||
|
$reward_html = "<span class='reward-badge'>+{$rewarded_users[$c['username']]} Fan Points</span>";
|
||||||
|
}
|
||||||
|
$html_content .= "<tr><td>" . ($i + 1) . "</td><td>$emoji" . htmlspecialchars($c['username']) . "</td><td><b>" . $c['count'] . "</b></td><td>$reward_html</td></tr>";
|
||||||
}
|
}
|
||||||
$html_content .= "</tbody></table>
|
$html_content .= "</tbody></table>
|
||||||
|
|
||||||
|
|||||||
@ -84,6 +84,7 @@ $html_content = "
|
|||||||
th { background: #f4f4f4; text-align: left; padding: 10px; border-bottom: 2px solid #ddd; }
|
th { background: #f4f4f4; text-align: left; padding: 10px; border-bottom: 2px solid #ddd; }
|
||||||
td { padding: 10px; border-bottom: 1px solid #eee; }
|
td { padding: 10px; border-bottom: 1px solid #eee; }
|
||||||
.crown { color: #ffca28; }
|
.crown { color: #ffca28; }
|
||||||
|
.reward-badge { background: #fff9c4; color: #f57f17; padding: 2px 6px; border-radius: 4px; font-size: 0.75rem; font-weight: bold; border: 1px solid #fbc02d; }
|
||||||
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 0.8rem; background: #eee; }
|
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 0.8rem; background: #eee; }
|
||||||
.badge-wa { background: #e8f5e9; color: #2e7d32; }
|
.badge-wa { background: #e8f5e9; color: #2e7d32; }
|
||||||
.badge-web { background: #e3f2fd; color: #1565c0; }
|
.badge-web { background: #e3f2fd; color: #1565c0; }
|
||||||
@ -142,16 +143,23 @@ $html_content .= "
|
|||||||
<th>#</th>
|
<th>#</th>
|
||||||
<th>Usuario</th>
|
<th>Usuario</th>
|
||||||
<th>Mensajes</th>
|
<th>Mensajes</th>
|
||||||
|
<th>Recompensa Semanal</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>";
|
<tbody>";
|
||||||
foreach ($top_chatters as $i => $c) {
|
foreach ($top_chatters as $i => $c) {
|
||||||
$emoji = ($i === 0) ? "👑 " : "💬 ";
|
$emoji = ($i === 0) ? "👑 " : "💬 ";
|
||||||
|
$reward = "";
|
||||||
|
if ($i == 0) $reward = "<span class='reward-badge'>+50 Fan Points</span>";
|
||||||
|
elseif ($i == 1) $reward = "<span class='reward-badge'>+30 Fan Points</span>";
|
||||||
|
elseif ($i == 2) $reward = "<span class='reward-badge'>+20 Fan Points</span>";
|
||||||
|
|
||||||
$html_content .= "
|
$html_content .= "
|
||||||
<tr>
|
<tr>
|
||||||
<td style='width: 40px;'>" . ($i + 1) . "</td>
|
<td style='width: 40px;'>" . ($i + 1) . "</td>
|
||||||
<td>$emoji" . htmlspecialchars($c['username']) . "</td>
|
<td>$emoji" . htmlspecialchars($c['username']) . "</td>
|
||||||
<td><b>" . $c['count'] . "</b></td>
|
<td><b>" . $c['count'] . "</b></td>
|
||||||
|
<td>$reward</td>
|
||||||
</tr>";
|
</tr>";
|
||||||
}
|
}
|
||||||
$html_content .= "
|
$html_content .= "
|
||||||
@ -210,6 +218,35 @@ $html_content .= "
|
|||||||
";
|
";
|
||||||
|
|
||||||
if ($action === 'send') {
|
if ($action === 'send') {
|
||||||
|
// Award points if not already awarded this week (optional safety, but let's keep it simple for now as requested for the automatic part)
|
||||||
|
// Actually, let's keep the reward logic in the cron as the "official" automated trigger.
|
||||||
|
// However, to be consistent, if they send it manually, we could also trigger it.
|
||||||
|
// Let's add the reward logic here too, but ONLY if it hasn't been run today.
|
||||||
|
|
||||||
|
$today = date('Y-m-d');
|
||||||
|
$stmt = $db->prepare("SELECT last_run FROM automation_logs WHERE task_name = 'weekly_report_rewards' AND DATE(last_run) = ?");
|
||||||
|
$stmt->execute([$today]);
|
||||||
|
|
||||||
|
if (!$stmt->fetch()) {
|
||||||
|
$rewards = [50, 30, 20];
|
||||||
|
foreach ($top_chatters as $i => $c) {
|
||||||
|
if ($i >= 3) break;
|
||||||
|
$username = $c['username'];
|
||||||
|
$points_to_add = $rewards[$i];
|
||||||
|
|
||||||
|
$stmt_fan = $db->prepare("SELECT id FROM fans WHERE name = ?");
|
||||||
|
$stmt_fan->execute([$username]);
|
||||||
|
$fan = $stmt_fan->fetch();
|
||||||
|
|
||||||
|
if ($fan) {
|
||||||
|
$db->prepare("UPDATE fans SET points = points + ? WHERE id = ?")->execute([$points_to_add, $fan['id']]);
|
||||||
|
} else {
|
||||||
|
$db->prepare("INSERT INTO fans (name, points) VALUES (?, ?)")->execute([$username, $points_to_add]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$db->prepare("INSERT INTO automation_logs (task_name, last_run) VALUES ('weekly_report_rewards', NOW()) ON DUPLICATE KEY UPDATE last_run = NOW()")->execute();
|
||||||
|
}
|
||||||
|
|
||||||
$subject = "📊 Reporte de Actividad Semanal - Lili Records Radio ($report_date)";
|
$subject = "📊 Reporte de Actividad Semanal - Lili Records Radio ($report_date)";
|
||||||
$res = MailService::sendMail(null, $subject, $html_content);
|
$res = MailService::sendMail(null, $subject, $html_content);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user