tippspiel/pfosten/app/Season.php

121 lines
3.3 KiB
PHP
Raw Normal View History

2017-06-20 08:43:36 +02:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use \App\Setting;
2017-06-26 17:21:30 +02:00
use Log;
2017-10-29 07:13:52 +01:00
use \App\Leagues\League;
2017-06-20 08:43:36 +02:00
class Season extends Model
{
protected $fillable = [
2020-01-17 09:32:06 +01:00
'name','rounds',"max_identical_jokertipps"
2017-06-20 08:43:36 +02:00
];
/*
public function isActive() {
return $this->hasOne('App\Setting','season_id');
}
*/
public static function getCurrent() {
2017-06-26 17:21:30 +02:00
$setting = Setting::all()->first();
if (!$setting) {
//Log::info('Season.getCurrent(): No current season set');
error_log('Season.getCurrent(): No current season set');
2017-06-26 17:21:30 +02:00
return null;
}
//Log::info("current_season_id=" . $setting->current_season_id);
$currentSeason = Season::where('id',$setting->current_season_id)->first();
return $currentSeason;
}
public static function setCurrent($season_id) {
$setting = Setting::all()->first();
if (!$setting) {
$setting = Setting::create([
'current_season_id' => $season_id
]);
}
$setting->current_season_id=$season_id;
$setting->save();
2017-06-20 08:43:36 +02:00
}
2017-10-11 11:14:23 +02:00
2017-10-16 11:19:57 +02:00
public static function getPrevious() {
2017-10-11 11:14:23 +02:00
$seasons=Season::where('id','>',0)->orderBy('id','DESC')->get();
if (sizeof($seasons)<2) { return null; }
return $seasons[1];
}
2018-01-08 10:09:44 +01:00
public static function get($seasonId) {
if ($seasonId==-1) return Season::getCurrent();
$season=Season::where('id','=',$seasonId)->first();
return $season;
}
public static function hasQualifications($seasonId=-1) {
return Season::hasLeagueType($seasonId,League::RANK_QUALIFICATION);
}
public static function hasStandardLeague($seasonId=-1) {
return Season::hasLeagueType($seasonId,League::RANK_LEAGUE1);
}
public static function hasLeagueType($seasonId=-1, $ltype) {
$season=null;
if ($seasonId==-1) {
$season = Season::getCurrent();
if (!$season) return false;
$seasonId=$season->id;
}
$leagues = League::where('season_id', '=', $seasonId)->get();
foreach ($leagues as $league) {
if ($league->rank == $ltype) return true;
2017-10-29 07:13:52 +01:00
}
return false;
}
public static function hasGreenShirtGame($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season = Season::getCurrent();
if (!$season) return false;
$seasonId=$season->id;
}
2018-01-26 10:09:20 +01:00
$quota=Quota::getQuota("gshirt");
return $quota>0;
}
2020-01-17 09:32:06 +01:00
// implemented 2020-01-08 because of needed first round_id of the season
public static function getFirstRoundId($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season = Season::getCurrent();
} else {
$season = Season::where('id','=',$seasonId)->first();
}
if (!$season) return -1;
$seasonId=$season->id;
$firstRound = Round::where('season_id','=',$seasonId)->orderBy('id')->first();
return $firstRound->id;
}
public static function maxIdenticalJokertipps($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season = Season::getCurrent();
} else {
$season = Season::where('id','=',$seasonId)->first();
}
return $season->max_identical_jokertipps;
}
2017-06-20 08:43:36 +02:00
}