2012-12-26 14:02:27 +01:00
|
|
|
<?php
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2018-09-25 17:33:31 +02:00
|
|
|
use Carbon\Carbon;
|
2017-01-21 13:58:53 +01:00
|
|
|
use Engelsystem\Database\DB;
|
2018-10-10 03:10:28 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2016-11-18 08:20:17 +01:00
|
|
|
use Engelsystem\ValidationResult;
|
2019-05-31 04:03:19 +02:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2014-09-28 14:50:08 +02:00
|
|
|
|
2014-05-13 15:51:45 +02:00
|
|
|
/**
|
|
|
|
* User model
|
|
|
|
*/
|
|
|
|
|
2017-12-27 12:24:55 +01:00
|
|
|
/**
|
|
|
|
* Returns the tshirt score (number of hours counted for tshirt).
|
|
|
|
* Accounts only ended shifts.
|
2018-01-14 18:09:34 +01:00
|
|
|
*
|
2018-10-09 21:47:31 +02:00
|
|
|
* @param int $userId
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return int
|
2017-12-27 12:24:55 +01:00
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function User_tshirt_score($userId)
|
2018-01-14 18:09:34 +01:00
|
|
|
{
|
2018-09-17 12:33:15 +02:00
|
|
|
$shift_sum_formula = User_get_shifts_sum_query();
|
2018-10-11 01:26:34 +02:00
|
|
|
$result_shifts = DB::selectOne(sprintf('
|
|
|
|
SELECT ROUND((%s) / 3600, 2) AS `tshirt_score`
|
|
|
|
FROM `users` LEFT JOIN `ShiftEntry` ON `users`.`id` = `ShiftEntry`.`UID`
|
2017-12-27 12:24:55 +01:00
|
|
|
LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID`
|
2018-10-11 01:26:34 +02:00
|
|
|
WHERE `users`.`id` = ?
|
2017-12-27 12:24:55 +01:00
|
|
|
AND `Shifts`.`end` < ?
|
2018-10-11 01:26:34 +02:00
|
|
|
GROUP BY `users`.`id`
|
|
|
|
', $shift_sum_formula), [
|
2018-10-09 21:47:31 +02:00
|
|
|
$userId,
|
2017-12-27 12:24:55 +01:00
|
|
|
time()
|
|
|
|
]);
|
2018-08-29 23:10:05 +02:00
|
|
|
if (!isset($result_shifts['tshirt_score'])) {
|
|
|
|
$result_shifts = ['tshirt_score' => 0];
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
$result_worklog = DB::selectOne('
|
|
|
|
SELECT SUM(`work_hours`) AS `tshirt_score`
|
2018-10-11 01:26:34 +02:00
|
|
|
FROM `users`
|
|
|
|
LEFT JOIN `UserWorkLog` ON `users`.`id` = `UserWorkLog`.`user_id`
|
|
|
|
WHERE `users`.`id` = ?
|
2017-12-29 18:57:11 +01:00
|
|
|
AND `UserWorkLog`.`work_timestamp` < ?
|
2018-01-14 18:09:34 +01:00
|
|
|
', [
|
2018-10-09 21:47:31 +02:00
|
|
|
$userId,
|
2017-12-29 18:57:11 +01:00
|
|
|
time()
|
2017-12-29 17:19:27 +01:00
|
|
|
]);
|
2018-08-29 23:10:05 +02:00
|
|
|
if (!isset($result_worklog['tshirt_score'])) {
|
|
|
|
$result_worklog = ['tshirt_score' => 0];
|
|
|
|
}
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
return $result_shifts['tshirt_score'] + $result_worklog['tshirt_score'];
|
2017-12-27 12:24:55 +01:00
|
|
|
}
|
|
|
|
|
2014-08-23 01:55:18 +02:00
|
|
|
/**
|
|
|
|
* Returns true if user is freeloader
|
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return bool
|
2014-08-23 01:55:18 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_is_freeloader($user)
|
|
|
|
{
|
2018-10-10 03:10:28 +02:00
|
|
|
return count(ShiftEntries_freeloaded_by_user($user->id)) >= config('max_freeloadable_shifts');
|
2014-08-23 01:55:18 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 15:51:45 +02:00
|
|
|
/**
|
|
|
|
* Returns all users that are not member of given angeltype.
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $angeltype Angeltype
|
2018-10-11 01:26:34 +02:00
|
|
|
* @return User[]
|
2014-05-13 15:51:45 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Users_by_angeltype_inverted($angeltype)
|
|
|
|
{
|
2018-10-11 01:26:34 +02:00
|
|
|
return User::query()
|
2018-10-17 01:30:10 +02:00
|
|
|
->select('users.*')
|
2018-10-11 01:26:34 +02:00
|
|
|
->leftJoin('UserAngelTypes', function ($query) use ($angeltype) {
|
|
|
|
/** @var JoinClause $query */
|
|
|
|
$query
|
|
|
|
->on('users.id', '=', 'UserAngelTypes.user_id')
|
2018-10-17 01:30:10 +02:00
|
|
|
->where('UserAngelTypes.angeltype_id', '=', $angeltype['id']);
|
2018-10-11 01:26:34 +02:00
|
|
|
})
|
|
|
|
->whereNull('UserAngelTypes.id')
|
|
|
|
->orderBy('users.name')
|
2018-10-17 01:30:10 +02:00
|
|
|
->get();
|
2014-05-13 15:51:45 +02:00
|
|
|
}
|
2013-10-13 00:52:44 +02:00
|
|
|
|
2014-01-05 19:30:06 +01:00
|
|
|
/**
|
|
|
|
* Returns all members of given angeltype.
|
2014-05-13 15:51:45 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $angeltype
|
2018-10-11 01:26:34 +02:00
|
|
|
* @return User[]
|
2014-01-05 19:30:06 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Users_by_angeltype($angeltype)
|
|
|
|
{
|
2018-10-11 01:26:34 +02:00
|
|
|
return User::query()
|
2018-10-17 01:30:10 +02:00
|
|
|
->select('users.*',
|
|
|
|
'UserAngelTypes.id AS user_angeltype_id',
|
|
|
|
'UserAngelTypes.confirm_user_id',
|
|
|
|
'UserAngelTypes.supporter',
|
|
|
|
'UserDriverLicenses.user_id AS wants_to_drive',
|
|
|
|
'UserDriverLicenses.*'
|
|
|
|
)
|
2018-10-11 01:26:34 +02:00
|
|
|
->join('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id')
|
|
|
|
->leftJoin('UserDriverLicenses', 'users.id', '=', 'UserDriverLicenses.user_id')
|
|
|
|
->where('UserAngelTypes.angeltype_id', '=', $angeltype['id'])
|
2018-12-27 14:16:09 +01:00
|
|
|
->orderBy('users.name')
|
2018-10-17 01:30:10 +02:00
|
|
|
->get();
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 19:45:50 +01:00
|
|
|
/**
|
2017-12-01 19:03:15 +01:00
|
|
|
* Strip unwanted characters from a users nick. Allowed are letters, numbers, connecting punctuation and simple space.
|
|
|
|
* Nick is trimmed.
|
2017-12-25 23:12:52 +01:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $nick
|
2019-04-28 14:34:04 +02:00
|
|
|
* @return ValidationResult
|
2013-12-27 19:45:50 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_validate_Nick($nick)
|
|
|
|
{
|
2019-04-28 14:34:04 +02:00
|
|
|
$nick = trim($nick);
|
2019-05-31 04:03:19 +02:00
|
|
|
|
|
|
|
if (strlen($nick) == 0 || strlen($nick) > 23) {
|
2019-04-28 14:34:04 +02:00
|
|
|
return new ValidationResult(false, $nick);
|
|
|
|
}
|
2019-05-31 04:03:19 +02:00
|
|
|
if (preg_match('/([^\p{L}\p{N}\-_. ]+)/ui', $nick)) {
|
2019-04-28 14:34:04 +02:00
|
|
|
return new ValidationResult(false, $nick);
|
|
|
|
}
|
2019-05-31 04:03:19 +02:00
|
|
|
|
2019-04-28 14:34:04 +02:00
|
|
|
return new ValidationResult(true, $nick);
|
2013-12-27 19:45:50 +01:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:00:51 +01:00
|
|
|
/**
|
|
|
|
* Validate user email address.
|
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @param string $mail The email address to validate
|
2016-11-11 17:00:51 +01:00
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_validate_mail($mail)
|
|
|
|
{
|
|
|
|
$mail = strip_item($mail);
|
|
|
|
return new ValidationResult(check_email($mail), $mail);
|
2016-11-11 17:00:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:34:23 +01:00
|
|
|
/**
|
|
|
|
* Validate the planned arrival date
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param int $planned_arrival_date Unix timestamp
|
2016-11-11 16:34:23 +01:00
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_validate_planned_arrival_date($planned_arrival_date)
|
|
|
|
{
|
2018-01-14 17:47:26 +01:00
|
|
|
if (is_null($planned_arrival_date)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// null is not okay
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(false, time());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
$config = config();
|
|
|
|
$buildup = $config->get('buildup_start');
|
|
|
|
$teardown = $config->get('teardown_end');
|
|
|
|
|
|
|
|
/** @var Carbon $buildup */
|
|
|
|
if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_arrival_date))) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Planned arrival can not be before buildup start date
|
2018-09-25 17:33:31 +02:00
|
|
|
return new ValidationResult(false, $buildup->getTimestamp());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
/** @var Carbon $teardown */
|
|
|
|
if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_arrival_date))) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Planned arrival can not be after teardown end date
|
2018-09-25 17:33:31 +02:00
|
|
|
return new ValidationResult(false, $teardown->getTimestamp());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return new ValidationResult(true, $planned_arrival_date);
|
2016-11-11 16:34:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the planned departure date
|
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @param int $planned_arrival_date Unix timestamp
|
|
|
|
* @param int $planned_departure_date Unix timestamp
|
2016-11-11 16:34:23 +01:00
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_validate_planned_departure_date($planned_arrival_date, $planned_departure_date)
|
|
|
|
{
|
2018-01-14 17:47:26 +01:00
|
|
|
if (is_null($planned_departure_date)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// null is okay
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(true, null);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($planned_arrival_date > $planned_departure_date) {
|
|
|
|
// departure cannot be before arrival
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(false, $planned_arrival_date);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
$config = config();
|
|
|
|
$buildup = $config->get('buildup_start');
|
|
|
|
$teardown = $config->get('teardown_end');
|
|
|
|
|
|
|
|
/** @var Carbon $buildup */
|
|
|
|
if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_departure_date))) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Planned arrival can not be before buildup start date
|
2018-09-25 17:33:31 +02:00
|
|
|
return new ValidationResult(false, $buildup->getTimestamp());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
/** @var Carbon $teardown */
|
|
|
|
if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_departure_date))) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Planned arrival can not be after teardown end date
|
2018-09-25 17:33:31 +02:00
|
|
|
return new ValidationResult(false, $teardown->getTimestamp());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return new ValidationResult(true, $planned_departure_date);
|
2016-11-11 16:34:23 +01:00
|
|
|
}
|
|
|
|
|
2013-09-10 14:27:31 +02:00
|
|
|
/**
|
|
|
|
* Generates a new api key for given user.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $user
|
|
|
|
* @param bool $log
|
2013-09-10 14:27:31 +02:00
|
|
|
*/
|
2018-10-10 03:10:28 +02:00
|
|
|
function User_reset_api_key($user, $log = true)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-10-10 03:10:28 +02:00
|
|
|
$user->api_key = md5($user->name . time() . rand());
|
|
|
|
$user->save();
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($log) {
|
2019-05-31 04:03:19 +02:00
|
|
|
engelsystem_log(sprintf('API key resetted (%s).', User_Nick_render($user, true)));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2013-09-10 14:27:31 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return float
|
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function User_get_eligable_voucher_count($user)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-21 23:07:20 +01:00
|
|
|
$voucher_settings = config('voucher_settings');
|
2019-12-25 16:26:59 +01:00
|
|
|
$start = $voucher_settings['voucher_start']
|
|
|
|
? Carbon::createFromFormat('Y-m-d', $voucher_settings['voucher_start'])->setTime(0, 0)
|
|
|
|
: null;
|
|
|
|
$shifts_done = count(ShiftEntries_finished_by_user($user->id, $start));
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
$earned_vouchers = $user->state->got_voucher - $voucher_settings['initial_vouchers'];
|
2017-12-25 23:12:52 +01:00
|
|
|
$eligable_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers;
|
|
|
|
if ($eligable_vouchers < 0) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-25 23:12:52 +01:00
|
|
|
return $eligable_vouchers;
|
2016-02-05 22:57:57 +01:00
|
|
|
}
|
2018-09-17 12:33:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the query to sum night shifts
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function User_get_shifts_sum_query()
|
|
|
|
{
|
|
|
|
$nightShifts = config('night_shifts');
|
|
|
|
if (!$nightShifts['enabled']) {
|
2018-12-27 19:53:48 +01:00
|
|
|
return 'COALESCE(SUM(`end` - `start`), 0)';
|
2018-09-17 12:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('
|
2018-12-27 19:53:48 +01:00
|
|
|
COALESCE(SUM(
|
2018-09-17 12:33:15 +02:00
|
|
|
(1 +
|
|
|
|
(
|
|
|
|
(HOUR(FROM_UNIXTIME(`Shifts`.`end`)) > %1$d AND HOUR(FROM_UNIXTIME(`Shifts`.`end`)) < %2$d)
|
|
|
|
OR (HOUR(FROM_UNIXTIME(`Shifts`.`start`)) > %1$d AND HOUR(FROM_UNIXTIME(`Shifts`.`start`)) < %2$d)
|
|
|
|
OR (HOUR(FROM_UNIXTIME(`Shifts`.`start`)) <= %1$d AND HOUR(FROM_UNIXTIME(`Shifts`.`end`)) >= %2$d)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
* (`Shifts`.`end` - `Shifts`.`start`)
|
|
|
|
* (1 - (%3$d + 1) * `ShiftEntry`.`freeloaded`)
|
2018-12-27 19:53:48 +01:00
|
|
|
), 0)
|
2018-09-17 12:33:15 +02:00
|
|
|
',
|
|
|
|
$nightShifts['start'],
|
|
|
|
$nightShifts['end'],
|
|
|
|
$nightShifts['multiplier']
|
|
|
|
);
|
|
|
|
}
|