Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 85 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| OlzZielsprint | |
0.00% |
0 / 85 |
|
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 1 |
| getHtml | |
0.00% |
0 / 85 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Components\OlzZielsprint; |
| 4 | |
| 5 | use Olz\Components\Common\OlzComponent; |
| 6 | use Olz\Components\Common\OlzEditableText\OlzEditableText; |
| 7 | |
| 8 | /** @extends OlzComponent<array<string, mixed>> */ |
| 9 | class OlzZielsprint extends OlzComponent { |
| 10 | public function getHtml(mixed $args): string { |
| 11 | $out = ''; |
| 12 | |
| 13 | $db = $this->dbUtils()->getDb(); |
| 14 | |
| 15 | $out .= "<h2>OLZ-Zielsprint-Challenge 2020</h2>"; |
| 16 | |
| 17 | // $out .= "<div style='color:rgb(180,0,0); font-weight:bold; text-align:center; font-size:14px;'>In Bearbeitung</div>"; |
| 18 | $out .= OlzEditableText::render(['snippet_id' => 9]); |
| 19 | |
| 20 | $sql = " |
| 21 | SELECT solv_uid, name, date |
| 22 | FROM solv_events |
| 23 | WHERE |
| 24 | date>'2020-03-13' |
| 25 | AND date<'2021-01-01' |
| 26 | AND kind='foot' |
| 27 | ORDER BY date ASC"; |
| 28 | $res_events = $db->query($sql); |
| 29 | $points_by_person = []; |
| 30 | // @phpstan-ignore-next-line |
| 31 | while ($row_event = $res_events->fetch_assoc()) { |
| 32 | // $out .= "<h3>".json_encode($row_event)."</h3>"; |
| 33 | $event_id = intval($row_event['solv_uid']); |
| 34 | $sql = " |
| 35 | SELECT person, finish_split |
| 36 | FROM solv_results |
| 37 | WHERE |
| 38 | event='{$event_id}' |
| 39 | AND finish_split > '0' |
| 40 | ORDER BY finish_split ASC"; |
| 41 | $res_results = $db->query($sql); |
| 42 | $last_finish_split = null; |
| 43 | $last_actual_points = null; |
| 44 | // @phpstan-ignore-next-line |
| 45 | for ($points = $res_results->num_rows; $points > 0; $points--) { |
| 46 | // @phpstan-ignore-next-line |
| 47 | $row_results = $res_results->fetch_assoc(); |
| 48 | // @phpstan-ignore-next-line |
| 49 | $person_id = intval($row_results['person']); |
| 50 | // @phpstan-ignore-next-line |
| 51 | $finish_split = $row_results['finish_split']; |
| 52 | $actual_points = ($last_finish_split === $finish_split) |
| 53 | ? $last_actual_points |
| 54 | : $points; |
| 55 | $person_points = $points_by_person[$person_id] |
| 56 | ?? ['points' => 0, 'calculation' => []]; |
| 57 | $points_by_person[$person_id]['points'] = $person_points['points'] + $actual_points; |
| 58 | $points_by_person[$person_id]['calculation'][] = [ |
| 59 | 'event_name' => $row_event['name'], |
| 60 | 'points' => $actual_points, |
| 61 | 'finish_split' => $finish_split, |
| 62 | // @phpstan-ignore-next-line |
| 63 | 'max_points' => $res_results->num_rows, |
| 64 | ]; |
| 65 | $last_finish_split = $finish_split; |
| 66 | $last_actual_points = $actual_points; |
| 67 | // $out .= "<div>".json_encode($row_results)."</div>"; |
| 68 | } |
| 69 | } |
| 70 | $ranking = []; |
| 71 | foreach ($points_by_person as $person_id => $points) { |
| 72 | $ranking[] = [ |
| 73 | 'person_id' => $person_id, |
| 74 | 'points' => $points['points'], |
| 75 | 'calculation' => $points['calculation'], |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | // @phpstan-ignore-next-line |
| 80 | usort($ranking, function ($a, $b) { |
| 81 | return $b['points'] - $a['points']; |
| 82 | }); |
| 83 | |
| 84 | $out .= "<table>"; |
| 85 | $out .= "<tr>"; |
| 86 | $out .= "<th style='border-bottom: 1px solid black; text-align: right;'>Rang </th>"; |
| 87 | $out .= "<th style='border-bottom: 1px solid black;'>Name</th>"; |
| 88 | $out .= "<th style='border-bottom: 1px solid black; text-align: right;'>Punkte</th>"; |
| 89 | $out .= "</tr>"; |
| 90 | $last_points = null; |
| 91 | $last_actual_rank = 1; |
| 92 | for ($index = 0; $index < count($ranking); $index++) { |
| 93 | $rank = $index + 1; |
| 94 | $ranking_entry = $ranking[$index]; |
| 95 | $person_id = intval($ranking_entry['person_id']); |
| 96 | $points = intval($ranking_entry['points']); |
| 97 | $actual_rank = ($last_points === $points) |
| 98 | ? $last_actual_rank |
| 99 | : $rank; |
| 100 | $sql = " |
| 101 | SELECT name |
| 102 | FROM solv_people |
| 103 | WHERE id='{$person_id}'"; |
| 104 | $res_person = $db->query($sql); |
| 105 | // @phpstan-ignore-next-line |
| 106 | $row_person = $res_person->fetch_assoc(); |
| 107 | // @phpstan-ignore-next-line |
| 108 | $person_name = $row_person['name']; |
| 109 | $calculation = "{$person_name}\\n---\\n"; |
| 110 | foreach ($ranking_entry['calculation'] as $event_calculation) { |
| 111 | $event_name = $event_calculation['event_name']; |
| 112 | $event_points = $event_calculation['points']; |
| 113 | $event_max_points = $event_calculation['max_points']; |
| 114 | $finish_split = $event_calculation['finish_split']; |
| 115 | $finish_minutes = floor(intval($finish_split) / 60); |
| 116 | $finish_seconds = str_pad(strval(intval($finish_split) % 60), 2, '0'); |
| 117 | $pretty_finish_split = "{$finish_minutes}:{$finish_seconds}"; |
| 118 | $calculation .= "{$event_name}: {$event_points} / {$event_max_points} ({$pretty_finish_split})\\n"; |
| 119 | } |
| 120 | $bgcolor = ($index % 2 === 0) ? 'rgba(0,0,0,0.1)' : 'rgba(0,0,0,0)'; |
| 121 | $out .= "<tr style='background-color:{$bgcolor}; cursor:pointer;' onclick='alert("{$calculation}")'>"; |
| 122 | $out .= "<td style='text-align: right;'>{$actual_rank}. </td>"; |
| 123 | $out .= "<td>{$person_name}</td>"; |
| 124 | $out .= "<td style='text-align: right;'>{$points}</td>"; |
| 125 | $out .= "</tr>"; |
| 126 | $last_points = $points; |
| 127 | $last_actual_rank = $actual_rank; |
| 128 | } |
| 129 | $out .= "</table>"; |
| 130 | |
| 131 | return $out; |
| 132 | } |
| 133 | } |