tippspiel/pfosten/app/User.php

341 lines
10 KiB
PHP
Raw Permalink Normal View History

2017-06-20 08:43:36 +02:00
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
2017-08-24 17:45:30 +02:00
use Illuminate\Foundation\Auth\User as Authenticatable; // already implements CanResetPassword
use App\Season;
use App\Payment;
use App\Round;
use App\Game;
2017-09-22 17:40:48 +02:00
2017-09-03 09:03:39 +02:00
use Carbon;
use Log;
2017-09-22 17:40:48 +02:00
use DB;
use App\MyClasses\TimeLogger;
2017-06-20 08:43:36 +02:00
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
2020-01-17 09:32:06 +01:00
'name', 'firstname', 'lastname', 'email', 'password', 'gets_reminders', 'iban', 'bankname' , 'blz', 'kontonr', 'mobile'
2017-06-20 08:43:36 +02:00
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
// Only the current season.
2017-11-30 11:07:27 +01:00
public static function is_season_paid($user_id,$seasonId=-1) {
$season=Season::getCurrent();
if ($seasonId!=-1) { $season=Season::where('id','=',$seasonId)->first();}
$retval="Ja";
2017-11-30 11:07:27 +01:00
if (User::payment_date($user_id, $season->id)==null) { $retval="Nein"; }
return $retval;
}
public static function payment_date($user_id,$season_id) {
$season = Season::getCurrent();
if ($season) {
$payment = Payment::where([
['user_id', '=', $user_id],
['season_id', '=', $season->id]
])->first();
if ($payment) {
if ($payment->paid_date) {
return $payment->paid_date;
}
} else {
$payment= Payment::create([
'user_id' => $user_id,
'season_id'=> $season->id,
'paid_date'=> null
]);
$payment->save();
}
}
return null;
}
2017-06-26 17:21:30 +02:00
// Aufgerechnete Summe. wird $maxround_id mit übergeben, dann wird nur
// bis zur entsprechenden Runde aufsummiert. Das setzt voraus, dass die
// Runden in Reihenfolge eingegeben wurden...
public static function getTotalScore($user_id,$maxround_id=1000000) {
$season = Season::getCurrent();
$rounds = Round::where('season_id','=',$season->id)->get();
$totalScore=0;
foreach($rounds as $round) {
2017-06-26 17:21:30 +02:00
if ($round->id <= $maxround_id) {
$totalScore += User::getRoundScore($user_id, $round->id);
}
}
return $totalScore;
}
2017-06-26 17:21:30 +02:00
public static function getRoundScore($user_id, $round_id) {
$games = Game::where('round_id','=',$round_id)->get();
$totalScore=0;
foreach ($games as $game) {
$tipp = Tipp::getTipp($user_id,$game->id);
$score = Tipp::getScore($tipp->id);
$totalScore+=$score;
}
return $totalScore;
}
2017-06-26 17:21:30 +02:00
// This totally does not belong here. But i am too afraid to put it into
// a separate php file that gets smashed by autoregenerate.
2017-06-26 17:21:30 +02:00
public static function toGermanDate($engDate)
{
$values = explode("-", $engDate);
$year = $values[0];
$month = $values[1];
$day = explode(" ", $values[2])[0];
2017-06-26 17:21:30 +02:00
$timeval = explode(" ", $engDate)[1];
$hour = explode(":", $timeval)[0];
$minute = explode(":", $timeval)[1];
return $day.".".$month.".".$year." ".$hour.":".$minute;
}
2017-06-26 17:21:30 +02:00
public static function toDBDate($deDate)
{
2017-09-11 10:38:57 +02:00
if (!$deDate) return "";
2017-06-26 17:21:30 +02:00
$values = explode(" ", $deDate);
$year = explode(".",$values[0])[2];
$month = explode(".",$values[0])[1];
$day = explode(".", $values[0])[0];
Log::info("values[0]=".$values[0]." day=".$day."count=". count($values));
$hour="00";
$minute="00";
if (count($values)>1) {
$hour = explode(":", $values[1])[0];
$minute = explode(":", $values[1])[1];
}
$dbDate=$year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute . ":00";
2018-01-01 18:25:56 +01:00
//Log::info("deDate=".$deDate." converted to:".$dbDate);
2017-06-26 17:21:30 +02:00
return $dbDate;
}
// TODO: implement hasMissingTipps($user_array)
// OPTIMIZED VERSION FOR hasMissingTipp
public static function getMissingTippsAndJokers($users) {
$round = Round::getFirstTippableRound();
2018-12-28 08:54:56 +01:00
2019-01-02 16:59:19 +01:00
2018-12-28 08:54:56 +01:00
if (!$round) {
2019-01-02 16:59:19 +01:00
Log::info("User::getMissingTippsandJokers(users) => no tippable round exists");
2018-12-28 08:54:56 +01:00
$result=array();
$result["MISSING_TIPP"]=array();
$result["MISSING_JOKER"]=array();
return $result;
}
2019-01-02 16:59:19 +01:00
Log::info("User::getMissingTippsandJokers(users) => first tippable round= [".$round->id."] ".$round->name);
$tippClock=new TimeLogger("User::getMissingTippsAndJokers()");
Log::info($tippClock->getStartMessage());
// expecting collection of user db objects like: $users = DB::table('users')->where('deleted',null)->orderBy('lastname')->get();
2019-01-02 16:59:19 +01:00
$usersWithoutJoker=array();
foreach($users as $key => $user) {
2019-01-02 16:59:19 +01:00
//array_push($usersWithoutJoker,$user->id);
$usersWithoutJoker[$user->id]="";
}
$games = Game::where('round_id','=',$round->id)->get();
$gameIds=array();
2019-01-02 16:59:19 +01:00
foreach ($games as $key => $game) { // Vorsicht: $key ist nur der Array index, nicht die game id
array_push($gameIds,$game->id);
Log::info("GAME_ID:".$game->id);
}
$tipps = Tipp::whereIn('game_id', $gameIds)->get();
Log::info("Fetched advanced tipps, checking now for missing tipps and jokers");
$usersWithoutTipps=array();
foreach($tipps as $tipp) {
if ($tipp->score1 == -1 || $tipp->score2 == -1) {
$usersWithoutTipps[$tipp->user_id]="no_tipp";
2019-01-02 16:59:19 +01:00
//Log::info("UserId ".$tipp->user_id." is missing a tipp!");
}
if ($tipp->joker == true) {
2019-01-02 16:59:19 +01:00
Log::info("Removing from usersWithoutJokerlist: ".$tipp->user_id);
unset($usersWithoutJoker[$tipp->user_id]);
}
}
2019-01-02 16:59:19 +01:00
foreach ($usersWithoutJoker as $key => $id) {
Log::info("Jokerless:".$key);
}
$result=array();
$result["MISSING_TIPP"]=$usersWithoutTipps;
$result["MISSING_JOKER"]=$usersWithoutJoker;
foreach(array_keys($result) as $key => $value) {
Log::info("Main key: ".$key.", value=".$value);
}
$mtipps=$result["MISSING_TIPP"];
foreach($mtipps as $key => $value) {
Log::info("MTipp: ".$key." => ".$value);
}
return $result;
}
2017-08-21 16:55:26 +02:00
public static function hasMissingTipp($user_id) {
$round = Round::getFirstTippableRound();
if (!$round) return false;
//$a=new TimeLogger("User::hasMissingTipp()");
//Log::info($a->getStartMessage());
$round_id = $round->id;
$games = Game::where('round_id','=',$round_id)->get();
2017-08-21 16:55:26 +02:00
foreach ($games as $game) {
$tipp = Tipp::where([['game_id','=',$game->id],['user_id','=',$user_id]])->first();
2017-08-21 16:55:26 +02:00
2017-11-13 17:22:28 +01:00
if(!$tipp) {
Log::info("Tipp doesnt exist: game_id=".$game->id.", user_id=".$user_id);
//Log::info($a->getEndMessage());
2017-11-13 17:22:28 +01:00
return 1;
}
2017-08-21 16:55:26 +02:00
if ($tipp->score1 == -1 || $tipp->score2 == -1) {
//Log::info($a->getEndMessage());
2017-08-21 16:55:26 +02:00
return 1;
}
}
//Log::info($a->getEndMessage());
2017-08-21 16:55:26 +02:00
return 0;
}
2017-08-21 16:55:26 +02:00
public static function hasMissingJoker($user_id) {
$round=Round::getFirstTippableRound();
if (!$round) return false;
$round_id = $round->id;
$games = Game::where('round_id','=',$round_id." user_id=".$user_id)->get();
2017-08-21 16:55:26 +02:00
$joker=0;
foreach ($games as $game) {
$tipp = Tipp::where([['game_id','=',$game->id],['user_id','=',$user_id]])->first();
if(!$tipp) {
2017-11-13 17:22:28 +01:00
Log::info("tipp is null! OMG! // Infos: user_id=".$user_id.", game_id=".$game->id.", round_id=".$round_id);
return true;
}
2017-08-21 16:55:26 +02:00
if ($tipp->joker == true) { $joker=1; }
//Log::info("!!! Joker for user_id ".$user_id.", round_id=".$round_id.", game_id=".$game->id." was found.");
2017-08-21 16:55:26 +02:00
}
if ($joker==1) {
return false;
}
//Log::info("!!! Joker is missing for user_id=".$user_id." round_id=".$round_id);
2017-08-21 16:55:26 +02:00
return true;
}
2017-09-03 09:03:39 +02:00
// aktiv = hat die Saison bezahlt.
2017-10-11 11:14:23 +02:00
public static function getActives($seasonId=-1) {
2017-11-13 17:22:28 +01:00
$season=Season::getCurrent();
if (!$season) return array();
if ($seasonId != -1) { $season = Season::where('id','=',$seasonId)->first(); }
$seasonId = $season->id;
2017-10-25 10:42:05 +02:00
2017-10-11 11:14:23 +02:00
$users = User::all();
2017-11-13 17:22:28 +01:00
$payments = Payment::where('season_id','=',$seasonId)->get();
2017-09-03 09:03:39 +02:00
$payref = array();
foreach($payments as $payment) {
2017-09-11 10:38:57 +02:00
if ($payment->paid_date) {
2020-01-17 09:32:06 +01:00
//Log::info("(".$payment->user_id.") PAID DATE: ".$payment->paid_date);
2017-09-11 10:38:57 +02:00
$payref[$payment->user_id] = 'ok';
}
2017-09-03 09:03:39 +02:00
}
$userlist=array();
foreach($users as $user) {
if (array_key_exists($user->id,$payref)) {
if($payref[$user->id]) {
array_push($userlist,$user);
}
}
}
return $userlist;
}
public static function getActivesSortedByName($seasonId=-1) {
$users = User::getActives($seasonId);
usort($users, array("App\User","cmpName"));
return $users;
}
public static function cmpName($user1, $user2) {
return ($user1['lastname']." ".$user1['firstname'] > $user2['lastname']." ".$user2['firstname']);
}
2017-11-30 11:07:27 +01:00
public static function isDeleted($user_id) {
$user=User::where('id','=',$user_id)->first();
if (!$user) return true;
if ($user->deleted) return true;
return false;
}
2017-09-03 09:03:39 +02:00
public static function softDelete($user_id) {
$user = User::where('id','=',$user_id)->first();
if ($user) { $user->deleted = Carbon\Carbon::now();}
$user->save();
2017-11-13 17:22:28 +01:00
$season = Season::getCurrent();
Payment::set_payment_date($user_id,$season->id,null);
2017-09-03 09:03:39 +02:00
}
2017-08-24 17:45:30 +02:00
2017-09-22 17:40:48 +02:00
public static function getNames() {
2017-11-30 11:07:27 +01:00
$users=User::all();
2017-09-22 17:40:48 +02:00
$usernames = array();
foreach($users as $user) {
$usernames[$user->id] = $user->name;
}
2017-09-22 17:40:48 +02:00
return $usernames;
}
2017-06-20 08:43:36 +02:00
}