LuminaWebsite/includes/contributions.php
Maaxxs 852eee357d Beitragspunkte for members, new Lumina logo
Members can be awarded contribution points for raids, events and other
activities, shown with a golden heart-shaped coin. An award has an occasion,
reason, date and points per member; members are picked with search and
select all/none, and choosing a raid preselects its participants (signed up
in a group and linked to a website account), fills in reason and date and
names participants without an account. The + on a member's card awards to
that member alone, negative points deduct, and awards can be deleted whole
or per member. Balances are always computed from the awards.

The ranking shows members as cards with gold/silver/bronze for the top three
and the viewer's own balance in the header and sidebar. With only
contributions.view it lists the best 25 places plus the viewer's own place;
contributions.view_all or contributions.manage show everyone. The history of
awards is paginated, using pagination now shared with the warehouse.

The sidebar shows the new logo, and the login page and mobile top bar a
wordmark cut from it with soft edges.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 09:23:55 +02:00

152 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* Beitragspunkte: Punkte, die Mitglieder fuer Raids und andere Aktivitaeten bekommen.
*
* Eine Vergabe (contribution_awards) ist ein Anlass mit Grund und Datum, an dem ein oder
* mehrere Mitglieder beteiligt sind (contribution_entries, je Mitglied die Punkte). Der
* Punktestand wird nicht gespeichert, sondern aus der Summe berechnet - so kann er nie von
* der Historie abweichen, und spaeter lassen sich Punkte auch ausgeben (negative Eintraege).
*/
const CONTRIBUTION_CATEGORIES = [
'raid' => 'Raid',
'event' => 'Event',
'other' => 'Sonstiges',
];
// So viele Plaetze der Rangliste sieht jeder mit contributions.view - den Rest nur mit
// contributions.view_all (oder contributions.manage).
const CONTRIBUTION_PUBLIC_RANKS = 25;
// Obergrenze pro Mitglied und Vergabe - schuetzt vor Tippfehlern wie 10000 statt 100.
const CONTRIBUTION_MAX_POINTS = 10000;
function contribution_category_label(string $key): string
{
return CONTRIBUTION_CATEGORIES[$key] ?? $key;
}
/** Das Muenz-Symbol der Beitragspunkte. */
function contribution_coin(int $size = 20): string
{
return '<img class="contribution-coin" src="/assets/images/beitragspunkt.svg" alt="Beitragspunkte" '
. 'width="' . $size . '" height="' . $size . '">';
}
/** Punkte mit Tausenderpunkt, Abzuege mit echtem Minuszeichen. */
function contribution_format(int $points, bool $withSign = false): string
{
$formatted = number_format(abs($points), 0, ',', '.');
if ($points < 0) {
return '−' . $formatted;
}
return ($withSign && $points > 0 ? '+' : '') . $formatted;
}
/**
* Punktestand aller aktiven Mitglieder (auch 0), hoechster zuerst.
*
* @return array<int, array<string, mixed>>
*/
function contribution_balances(): array
{
return db()->query(
"SELECT u.id, u.username, u.display_name, u.avatar_filename,
COALESCE(SUM(e.points), 0) AS points,
COUNT(e.id) AS award_count
FROM users u
LEFT JOIN contribution_entries e ON e.user_id = u.id
WHERE u.status = 'active'
GROUP BY u.id
ORDER BY points DESC, COALESCE(NULLIF(u.display_name, ''), u.username)"
)->fetchAll();
}
function contribution_user_balance(int $userId): int
{
$stmt = db()->prepare('SELECT COALESCE(SUM(points), 0) FROM contribution_entries WHERE user_id = ?');
$stmt->execute([$userId]);
return (int) $stmt->fetchColumn();
}
function contribution_award_count(?int $userId): int
{
if ($userId === null) {
return (int) db()->query('SELECT COUNT(*) FROM contribution_awards')->fetchColumn();
}
$stmt = db()->prepare('SELECT COUNT(*) FROM contribution_entries WHERE user_id = ?');
$stmt->execute([$userId]);
return (int) $stmt->fetchColumn();
}
/**
* Vergaben mit ihren Empfaengern, neueste zuerst. Mit $userId nur die Vergaben, an denen
* dieses Mitglied beteiligt war.
*
* @return array<int, array<string, mixed>>
*/
function contribution_awards(?int $userId, int $limit, int $offset): array
{
$join = $userId !== null ? 'JOIN contribution_entries mine ON mine.award_id = a.id AND mine.user_id = ?' : '';
$stmt = db()->prepare(
"SELECT a.id, a.category, a.title, a.note, a.raid_id, a.awarded_at,
COALESCE(NULLIF(cu.display_name, ''), cu.username) AS author
FROM contribution_awards a
$join
LEFT JOIN users cu ON cu.id = a.created_by
ORDER BY a.awarded_at DESC, a.id DESC
LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset
);
$stmt->execute($userId !== null ? [$userId] : []);
$awards = [];
foreach ($stmt->fetchAll() as $row) {
$row['entries'] = [];
$awards[(int) $row['id']] = $row;
}
if (empty($awards)) {
return [];
}
$ids = array_keys($awards);
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$entryStmt = db()->prepare(
"SELECT e.id, e.award_id, e.user_id, e.points,
COALESCE(NULLIF(u.display_name, ''), u.username) AS name
FROM contribution_entries e
JOIN users u ON u.id = e.user_id
WHERE e.award_id IN ($placeholders)
ORDER BY name"
);
$entryStmt->execute($ids);
foreach ($entryStmt->fetchAll() as $entry) {
$awards[(int) $entry['award_id']]['entries'][] = $entry;
}
return array_values($awards);
}
/** Jetzt in deutscher Zeit fuer ein datetime-local-Feld. */
function contribution_now_input(): string
{
return (new DateTimeImmutable('now', new DateTimeZone('Europe/Berlin')))->format('Y-m-d\TH:i');
}
/** Zeitpunkt aus einem datetime-local-Feld, null bei ungueltiger Eingabe oder weit in der Zukunft. */
function contribution_parse_time(string $value): ?string
{
$tz = new DateTimeZone('Europe/Berlin');
$time = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', trim($value), $tz);
if ($time === false || $time > new DateTimeImmutable('+1 day', $tz)) {
return null;
}
return $time->format('Y-m-d H:i:s');
}
function contribution_format_time(?string $value): string
{
$time = $value ? strtotime($value) : false;
return $time ? date('d.m.Y H:i', $time) : '-';
}