/admin/classes/model/fine.php

https://bitbucket.org/dmcnerney/hockey-registration-system · PHP · 194 lines · 159 code · 33 blank · 2 comment · 17 complexity · 6e241d425a1c8f1ac03103fccedad10a MD5 · raw file

  1. <?php
  2. //----------------------------------------------------------------------
  3. class Model_Fine extends \Orm\Model
  4. {
  5. protected static $_properties = array(
  6. 'id',
  7. 'date',
  8. 'type',
  9. 'club_id',
  10. 'matchcard_id',
  11. 'detail',
  12. 'resolved',
  13. );
  14. protected static $_belongs_to = array('club');
  15. protected static $_has_one = array(
  16. 'card'=>array(
  17. 'key_to'=>'id',
  18. 'key_from'=>'matchcard_id',
  19. 'model_to'=>'Model_Matchcard',
  20. )
  21. );
  22. protected static $_table_name = 'incident';
  23. protected static $_conditions = array(
  24. 'order_by' => array('date' => 'desc'),
  25. 'where' => array(
  26. array('type', '=', 'Missing'), // Missing is actually the type for fine (Late=Warning)
  27. ),
  28. );
  29. public static function generate() {
  30. try {
  31. $fines = array();
  32. foreach (get_class_methods('Model_Fine') as $method) {
  33. if (!preg_match('/^generate.+$/', $method)) continue;
  34. $result = call_user_func(array('Model_Fine', $method));
  35. $fines = array_merge($fines, $result);
  36. }
  37. $results = \DB::query("select club_id, matchcard_id, detail from incident i
  38. left join matchcard m on i.matchcard_id = m.id
  39. where i.resolved = 0
  40. and club_id is not null and club_id > 0
  41. and i.type = 'Missing'
  42. and (m.id is null or m.open < 60) order by club_id, matchcard_id, detail")->execute();
  43. $openFines = array();
  44. foreach ($results as $result) {
  45. if ($result['matchcard_id']) {
  46. $openFines[] = $result['club_id']." ".$result['matchcard_id'];
  47. } else {
  48. $json = json_decode($result['detail']);
  49. $openFines[] = $result['club_id']." ".$json->fixtureId;
  50. }
  51. }
  52. foreach ($fines as $fine) {
  53. if (!$fine) continue;
  54. if ($fine['matchcard_id'] !== null) {
  55. $key = $fine['club']['id']." ".$fine['matchcard_id'];
  56. } else {
  57. $json = json_decode($fine['detail']);
  58. $key = $fine['club']['id']." ".$json->fixtureId;
  59. }
  60. if (in_array($key, $openFines)) continue;
  61. try {
  62. $fine->save();
  63. } catch (Exception $e) {
  64. echo "Exception saving fine: ".$e->getMessage()."\n";
  65. }
  66. echo " * ".$fine->detail."\n";
  67. }
  68. } catch (Exception $e) {
  69. echo "Exception: ".$e->getMessage()."\n";
  70. }
  71. }
  72. private static function generateForNoPlayersAtStartOfMatch() {
  73. echo "Matchcard must have players at the start of the match\n";
  74. $delay = 0;
  75. $list = \DB::query("select m.id, m.fixture_id, t.club_id, c.name club
  76. from (select id, fixture_id, open, date, home_id team_id from matchcard
  77. union select id, fixture_id, open, date, away_id from matchcard) m
  78. left join team t on t.id = m.team_id
  79. left join incident i on i.matchcard_id = m.id
  80. and type = 'Played'
  81. and i.club_id = t.club_id
  82. left join club c on t.club_id = c.id
  83. where m.fixture_id is not null
  84. and i.id is null
  85. and m.open < 60
  86. and m.date < now()
  87. order by m.date")->execute();
  88. $fines = array();
  89. foreach ($list as $card) {
  90. $fixture = Model_Fixture::get($card['fixture_id']);
  91. if ($fixture['cover'] !== 'CHA') continue;
  92. $fixtureName = $fixture['competition'].":".$fixture['home']." v ${fixture['away']} (${fixture['fixtureID']})";
  93. $fines[] = self::createFine($card['club'], $card['id'], null, null,
  94. "No players listed at start of the match (7 required)");
  95. }
  96. return $fines;
  97. }
  98. private static function generateForLessThan7PlayersAtStartOfMatch() {
  99. echo "Matchcard must have 7 players before start of match\n";
  100. $playerCount = 7;
  101. $delay = 0;
  102. $list = \DB::query("select m.id, m.fixture_id, c.name club, count(i.id) playerCount
  103. from matchcard m
  104. left join incident i on i.matchcard_id = m.id
  105. and type = 'Played'
  106. and i.date between m.date and date_add(m.date, interval $delay minute)
  107. left join club c on i.club_id = c.id
  108. where m.fixture_id is not null
  109. and m.open < 60
  110. and m.date < now()
  111. group by m.id, m.fixture_id, i.club_id
  112. having count(i.id) < $playerCount
  113. order by m.date")->execute();
  114. $fines = array();
  115. foreach ($list as $card) {
  116. $fixture = Model_Fixture::get($card['fixture_id']);
  117. if ($fixture['cover'] !== 'CHA') continue;
  118. $fixtureName = $fixture['competition'].":".$fixture['home']." v ".$fixture['away'];
  119. $fines[] = self::createFine($card['club'], $card['id'], null, null,
  120. "Only ${card['playerCount']} players listed at the start of the match ($playerCount required)");
  121. }
  122. return $fines;
  123. }
  124. private static function generateCardNotSubmittedBeforeMidnight() {
  125. echo "Matchcard must be submitted before midnight\n";
  126. $nowDate = Date::forge();
  127. foreach (Model_Fixture::getAll() as $fixture) {
  128. if ($fixture['datetime'] > $nowDate) continue;
  129. $fixtureIds[] = $fixture['fixtureID'];
  130. }
  131. // Find the fixtures of all cards and eliminate them from missing cards
  132. $matchcardFixtures = Model_Matchcard::query()->where('fixture_id','!=','null')->select('fixture_id')->get();
  133. $matchcardFixtures = array_map(function($a) { return $a->fixture_id; }, $matchcardFixtures);
  134. $fines = array();
  135. foreach (array_diff($fixtureIds, $matchcardFixtures) as $fixtureId) {
  136. $fixture = Model_Fixture::get($fixtureId);
  137. if ($fixture['cover'] !== 'CHA') continue;
  138. if (!isset($fixture['home_club']) || !isset($fixture['away_club'])) continue;
  139. $fixtureName = $fixture['competition'].":".$fixture['home']." v ".$fixture['away'];
  140. $fines[] = self::createFine($fixture['home_club'], null, null, $fixtureId,
  141. "Matchcard not submitted by midnight");
  142. $fines[] = self::createFine($fixture['away_club'], null, null, $fixtureId,
  143. "Matchcard not submitted by midnight");
  144. }
  145. return $fines;
  146. }
  147. protected static function createFine($club, $matchcardId, $value, $fixtureId, $detail) {
  148. $club = Model_Club::find_by_name($club);
  149. if ($club == null) return null;
  150. if ($value === null) $value = 20;
  151. $f = new Model_Fine();
  152. $f->type = 'Missing';
  153. $f->club = $club;
  154. $f->matchcard_id = $matchcardId;
  155. $f->resolved = false;
  156. $detail = '"fine":'.$value.',"msg":"'.$detail.'"';
  157. if ($fixtureId) $detail .= ',"fixtureId":'.$fixtureId;
  158. $f->detail = '{'.$detail.'}';
  159. return $f;
  160. }
  161. }