2012-12-26 14:02:27 +01:00
|
|
|
<?php
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
use Engelsystem\Database\DB;
|
2016-11-18 08:20:17 +01:00
|
|
|
use Engelsystem\ValidationResult;
|
2014-09-28 14:50:08 +02:00
|
|
|
|
2014-05-13 15:51:45 +02:00
|
|
|
/**
|
|
|
|
* User model
|
|
|
|
*/
|
|
|
|
|
2015-12-30 15:48:41 +01:00
|
|
|
/**
|
|
|
|
* Delete a user
|
2016-09-29 11:18:17 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param int $user_id
|
2015-12-30 15:48:41 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_delete($user_id)
|
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::delete('DELETE FROM `User` WHERE `UID`=?', [$user_id]);
|
2015-12-30 15:48:41 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2017-12-27 12:24:55 +01:00
|
|
|
* @param array[] $user
|
2018-01-14 18:09:34 +01:00
|
|
|
* @return int
|
2017-12-27 12:24:55 +01:00
|
|
|
*/
|
2018-01-14 18:09:34 +01:00
|
|
|
function User_tshirt_score($user)
|
|
|
|
{
|
2017-12-27 12:24:55 +01:00
|
|
|
$shift_sum_formula = config('shift_sum_formula');
|
2018-01-14 18:09:34 +01:00
|
|
|
|
2017-12-29 17:19:27 +01:00
|
|
|
$result_shifts = DB::selectOne('
|
2017-12-27 12:24:55 +01:00
|
|
|
SELECT ROUND((' . $shift_sum_formula . ') / 3600, 2) AS `tshirt_score`
|
|
|
|
FROM `User` LEFT JOIN `ShiftEntry` ON `User`.`UID` = `ShiftEntry`.`UID`
|
|
|
|
LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID`
|
|
|
|
WHERE `User`.`UID` = ?
|
|
|
|
AND `Shifts`.`end` < ?
|
|
|
|
GROUP BY `User`.`UID`
|
2018-01-14 18:09:34 +01:00
|
|
|
', [
|
2017-12-27 12:24:55 +01:00
|
|
|
$user['UID'],
|
|
|
|
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`
|
|
|
|
FROM `User`
|
|
|
|
LEFT JOIN `UserWorkLog` ON `User`.`UID` = `UserWorkLog`.`user_id`
|
|
|
|
WHERE `User`.`UID` = ?
|
2017-12-29 18:57:11 +01:00
|
|
|
AND `UserWorkLog`.`work_timestamp` < ?
|
2018-01-14 18:09:34 +01:00
|
|
|
', [
|
2017-12-29 18:57:11 +01:00
|
|
|
$user['UID'],
|
|
|
|
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-12-26 01:49:59 +01:00
|
|
|
/**
|
|
|
|
* Update user.
|
2014-12-26 19:26:53 +01:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $user
|
2014-12-26 01:49:59 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_update($user)
|
|
|
|
{
|
2017-07-23 11:46:54 +02:00
|
|
|
DB::update('
|
2017-01-21 13:58:53 +01:00
|
|
|
UPDATE `User` SET
|
|
|
|
`Nick`=?,
|
|
|
|
`Name`=?,
|
|
|
|
`Vorname`=?,
|
|
|
|
`Alter`=?,
|
|
|
|
`Telefon`=?,
|
|
|
|
`DECT`=?,
|
|
|
|
`Handy`=?,
|
|
|
|
`email`=?,
|
|
|
|
`email_shiftinfo`=?,
|
|
|
|
`email_by_human_allowed`=?,
|
|
|
|
`jabber`=?,
|
|
|
|
`Size`=?,
|
|
|
|
`Gekommen`=?,
|
|
|
|
`Aktiv`=?,
|
|
|
|
`force_active`=?,
|
|
|
|
`Tshirt`=?,
|
|
|
|
`color`=?,
|
|
|
|
`Sprache`=?,
|
|
|
|
`Hometown`=?,
|
|
|
|
`got_voucher`=?,
|
|
|
|
`arrival_date`=?,
|
|
|
|
`planned_arrival_date`=?,
|
|
|
|
`planned_departure_date`=?
|
|
|
|
WHERE `UID`=?
|
2017-01-21 19:37:42 +01:00
|
|
|
',
|
2017-01-21 13:58:53 +01:00
|
|
|
[
|
|
|
|
$user['Nick'],
|
|
|
|
$user['Name'],
|
|
|
|
$user['Vorname'],
|
|
|
|
$user['Alter'],
|
|
|
|
$user['Telefon'],
|
|
|
|
$user['DECT'],
|
|
|
|
$user['Handy'],
|
|
|
|
$user['email'],
|
2017-11-19 13:17:34 +01:00
|
|
|
(int)$user['email_shiftinfo'],
|
|
|
|
(int)$user['email_by_human_allowed'],
|
2017-01-21 13:58:53 +01:00
|
|
|
$user['jabber'],
|
|
|
|
$user['Size'],
|
|
|
|
$user['Gekommen'],
|
|
|
|
$user['Aktiv'],
|
2017-11-19 13:17:34 +01:00
|
|
|
(int)$user['force_active'],
|
2017-01-21 13:58:53 +01:00
|
|
|
$user['Tshirt'],
|
|
|
|
$user['color'],
|
|
|
|
$user['Sprache'],
|
|
|
|
$user['Hometown'],
|
|
|
|
$user['got_voucher'],
|
|
|
|
$user['arrival_date'],
|
|
|
|
$user['planned_arrival_date'],
|
|
|
|
$user['planned_departure_date'],
|
|
|
|
$user['UID'],
|
|
|
|
]
|
|
|
|
);
|
2014-12-26 01:49:59 +01:00
|
|
|
}
|
|
|
|
|
2014-09-28 15:01:02 +02:00
|
|
|
/**
|
|
|
|
* Counts all forced active users.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return int
|
2014-09-28 15:01:02 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_force_active_count()
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1');
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (empty($result)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)array_shift($result);
|
2014-09-28 15:01:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return int
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_active_count()
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1');
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (empty($result)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)array_shift($result);
|
2014-09-28 15:01:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return int
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_got_voucher_count()
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('SELECT SUM(`got_voucher`) FROM `User`');
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (empty($result)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)array_shift($result);
|
2014-12-26 01:49:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return int
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_arrived_count()
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1');
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (empty($result)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)array_shift($result);
|
2014-09-28 15:01:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-01-21 13:58:53 +01:00
|
|
|
* @return int
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_tshirts_count()
|
|
|
|
{
|
2017-07-28 20:11:09 +02:00
|
|
|
$result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1');
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (empty($result)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)array_shift($result);
|
2014-09-28 15:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all column names for sorting in an array.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2014-09-28 15:01:02 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_sortable_columns()
|
|
|
|
{
|
|
|
|
return [
|
2017-01-02 15:43:36 +01:00
|
|
|
'Nick',
|
|
|
|
'Name',
|
|
|
|
'Vorname',
|
|
|
|
'Alter',
|
|
|
|
'DECT',
|
|
|
|
'email',
|
|
|
|
'Size',
|
|
|
|
'Gekommen',
|
|
|
|
'Aktiv',
|
|
|
|
'force_active',
|
|
|
|
'Tshirt',
|
|
|
|
'lastLogIn'
|
|
|
|
];
|
2014-09-28 15:01:02 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 14:50:08 +02:00
|
|
|
/**
|
|
|
|
* Get all users, ordered by Nick by default or by given param.
|
2014-09-28 15:01:02 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $order_by
|
2017-07-28 19:15:52 +02:00
|
|
|
* @return array
|
2014-09-28 14:50:08 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Users($order_by = 'Nick')
|
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select(sprintf('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT *
|
|
|
|
FROM `User`
|
|
|
|
ORDER BY `%s` ASC
|
|
|
|
',
|
|
|
|
trim(DB::getPdo()->quote($order_by), '\'')
|
|
|
|
));
|
2014-09-28 14:50:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-23 01:55:18 +02:00
|
|
|
/**
|
|
|
|
* Returns true if user is freeloader
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $user
|
|
|
|
* @return bool
|
2014-08-23 01:55:18 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_is_freeloader($user)
|
|
|
|
{
|
2017-01-21 23:07:20 +01:00
|
|
|
global $user;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 23:07:20 +01:00
|
|
|
return count(ShiftEntries_freeloaded_by_user($user)) >= 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
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2014-05-13 15:51:45 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Users_by_angeltype_inverted($angeltype)
|
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT `User`.*
|
|
|
|
FROM `User`
|
|
|
|
LEFT JOIN `UserAngelTypes`
|
|
|
|
ON (`User`.`UID`=`UserAngelTypes`.`user_id` AND `angeltype_id`=?)
|
|
|
|
WHERE `UserAngelTypes`.`id` IS NULL
|
|
|
|
ORDER BY `Nick`
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$angeltype['id']
|
|
|
|
]
|
|
|
|
);
|
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
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2014-01-05 19:30:06 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function Users_by_angeltype($angeltype)
|
|
|
|
{
|
2017-07-28 19:15:52 +02:00
|
|
|
return DB::select('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT
|
|
|
|
`User`.*,
|
|
|
|
`UserAngelTypes`.`id` AS `user_angeltype_id`,
|
|
|
|
`UserAngelTypes`.`confirm_user_id`,
|
|
|
|
`UserAngelTypes`.`supporter`,
|
2017-08-30 00:07:01 +02:00
|
|
|
(`UserDriverLicenses`.`user_id` IS NOT NULL) AS `wants_to_drive`,
|
2017-01-21 13:58:53 +01:00
|
|
|
`UserDriverLicenses`.*
|
|
|
|
FROM `User`
|
|
|
|
JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id`
|
|
|
|
LEFT JOIN `UserDriverLicenses` ON `User`.`UID`=`UserDriverLicenses`.`user_id`
|
|
|
|
WHERE `UserAngelTypes`.`angeltype_id`=?
|
|
|
|
ORDER BY `Nick`
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$angeltype['id']
|
|
|
|
]
|
|
|
|
);
|
2014-01-05 19:30:06 +01:00
|
|
|
}
|
|
|
|
|
2013-12-29 15:08:21 +01:00
|
|
|
/**
|
|
|
|
* Returns User id array
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return array[]
|
2013-12-29 15:08:21 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_ids()
|
|
|
|
{
|
2017-01-21 13:58:53 +01:00
|
|
|
return DB::select('SELECT `UID` FROM `User`');
|
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
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2013-12-27 19:45:50 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_validate_Nick($nick)
|
|
|
|
{
|
2017-12-15 00:06:34 +01:00
|
|
|
return preg_replace('/([^\p{L}\p{N}-_. ]+)/ui', '', trim($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-14 19:10:29 +01:00
|
|
|
/**
|
|
|
|
* Validate user jabber address
|
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @param string $jabber Jabber-ID to validate
|
2016-11-14 19:10:29 +01:00
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_validate_jabber($jabber)
|
|
|
|
{
|
|
|
|
$jabber = strip_item($jabber);
|
|
|
|
if ($jabber == '') {
|
|
|
|
// Empty is ok
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(true, '');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return new ValidationResult(check_email($jabber), $jabber);
|
2016-11-14 19:10:29 +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
|
|
|
}
|
|
|
|
$event_config = EventConfig();
|
2018-01-14 17:47:26 +01:00
|
|
|
if (empty($event_config)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Nothing to validate against
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(true, $planned_arrival_date);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) {
|
|
|
|
// Planned arrival can not be before buildup start date
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(false, $event_config['buildup_start_date']);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) {
|
|
|
|
// Planned arrival can not be after teardown end date
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(false, $event_config['teardown_end_date']);
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
$event_config = EventConfig();
|
2018-01-14 18:09:34 +01:00
|
|
|
if (empty($event_config)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Nothing to validate against
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(true, $planned_departure_date);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) {
|
|
|
|
// Planned arrival can not be before buildup start date
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(false, $event_config['buildup_start_date']);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) {
|
|
|
|
// Planned arrival can not be after teardown end date
|
2017-01-02 15:43:36 +01:00
|
|
|
return new ValidationResult(false, $event_config['teardown_end_date']);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return new ValidationResult(true, $planned_departure_date);
|
2016-11-11 16:34:23 +01:00
|
|
|
}
|
|
|
|
|
2012-12-26 14:02:27 +01:00
|
|
|
/**
|
|
|
|
* Returns user by id.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param int $user_id UID
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return array|null
|
2012-12-26 14:02:27 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User($user_id)
|
|
|
|
{
|
2018-01-14 23:07:34 +01:00
|
|
|
$user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]);
|
|
|
|
|
|
|
|
return empty($user) ? null : $user;
|
2012-12-26 14:02:27 +01:00
|
|
|
}
|
|
|
|
|
2013-09-10 14:27:31 +02:00
|
|
|
/**
|
|
|
|
* Returns User by api_key.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @param string $api_key User api key
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return array|null Matching user, null if not found
|
2013-09-10 14:27:31 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_by_api_key($api_key)
|
|
|
|
{
|
2018-01-14 23:07:34 +01:00
|
|
|
$user = DB::selectOne('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]);
|
|
|
|
|
|
|
|
return empty($user) ? null : $user;
|
2013-09-10 14:27:31 +02:00
|
|
|
}
|
|
|
|
|
2013-12-26 13:34:48 +01:00
|
|
|
/**
|
|
|
|
* Returns User by email.
|
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $email
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return array|null Matching user, null when not found
|
2013-12-26 13:34:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_by_email($email)
|
|
|
|
{
|
2018-01-14 23:07:34 +01:00
|
|
|
$user = DB::selectOne('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]);
|
|
|
|
|
|
|
|
return empty($user) ? null : $user;
|
2013-12-26 13:34:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns User by password token.
|
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $token
|
2018-01-14 23:07:34 +01:00
|
|
|
* @return array|null Matching user, null when not found
|
2013-12-26 13:34:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_by_password_recovery_token($token)
|
|
|
|
{
|
2018-01-14 23:07:34 +01:00
|
|
|
$user = DB::selectOne('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]);
|
|
|
|
|
|
|
|
return empty($user) ? null : $user;
|
2013-12-26 13:34:48 +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
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $user
|
|
|
|
* @param bool $log
|
2013-09-10 14:27:31 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_reset_api_key(&$user, $log = true)
|
|
|
|
{
|
|
|
|
$user['api_key'] = md5($user['Nick'] . time() . rand());
|
2017-01-22 01:02:52 +01:00
|
|
|
DB::update('
|
2017-01-21 13:58:53 +01:00
|
|
|
UPDATE `User`
|
|
|
|
SET `api_key`=?
|
|
|
|
WHERE `UID`=?
|
|
|
|
LIMIT 1
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$user['api_key'],
|
|
|
|
$user['UID']
|
|
|
|
]
|
|
|
|
);
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($log) {
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_log(sprintf('API key resetted (%s).', User_Nick_render($user)));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2013-09-10 14:27:31 +02:00
|
|
|
}
|
|
|
|
|
2013-12-26 13:34:48 +01:00
|
|
|
/**
|
|
|
|
* Generates a new password recovery token for given user.
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $user
|
|
|
|
* @return string
|
2013-12-26 13:34:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_generate_password_recovery_token(&$user)
|
|
|
|
{
|
|
|
|
$user['password_recovery_token'] = md5($user['Nick'] . time() . rand());
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::update('
|
|
|
|
UPDATE `User`
|
|
|
|
SET `password_recovery_token`=?
|
|
|
|
WHERE `UID`=?
|
|
|
|
LIMIT 1
|
|
|
|
',
|
|
|
|
[
|
|
|
|
$user['password_recovery_token'],
|
|
|
|
$user['UID'],
|
|
|
|
]
|
|
|
|
);
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_log('Password recovery for ' . User_Nick_render($user) . ' started.');
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $user['password_recovery_token'];
|
2013-12-26 13:34:48 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param array $user
|
|
|
|
* @return float
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function User_get_eligable_voucher_count(&$user)
|
|
|
|
{
|
2017-01-21 23:07:20 +01:00
|
|
|
$voucher_settings = config('voucher_settings');
|
2017-01-02 03:57:23 +01:00
|
|
|
$shifts_done = count(ShiftEntries_finished_by_user($user));
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$earned_vouchers = $user['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
|
|
|
}
|