summaryrefslogtreecommitdiff
path: root/includes/model
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model')
-rw-r--r--includes/model/Shifts_model.php32
-rw-r--r--includes/model/UserAngelTypes_model.php13
-rw-r--r--includes/model/UserWorkLog_model.php7
-rw-r--r--includes/model/User_model.php46
4 files changed, 38 insertions, 60 deletions
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index e03ca0bf..9e803efd 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -1,6 +1,7 @@
<?php
use Engelsystem\Database\DB;
+use Engelsystem\Models\User\User;
use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftSignupState;
@@ -301,7 +302,7 @@ function Shift_free_entries($needed_angeltype, $shift_entries)
/**
* Check if shift signup is allowed from the end users point of view (no admin like privileges)
*
- * @param array $user
+ * @param User $user
* @param array $shift The shift
* @param array $angeltype The angeltype to which the user wants to sign up
* @param array|null $user_angeltype
@@ -321,12 +322,12 @@ function Shift_signup_allowed_angel(
) {
$free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
- if (config('signup_requires_arrival') && !$user['Gekommen']) {
+ if (config('signup_requires_arrival') && !$user->state->arrived) {
return new ShiftSignupState(ShiftSignupState::NOT_ARRIVED, $free_entries);
}
if (empty($user_shifts)) {
- $user_shifts = Shifts_by_user($user['UID']);
+ $user_shifts = Shifts_by_user($user->id);
}
$signed_up = false;
@@ -352,7 +353,7 @@ function Shift_signup_allowed_angel(
}
if (empty($user_angeltype)) {
- $user_angeltype = UserAngelType_by_User_and_AngelType($user['UID'], $angeltype);
+ $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype);
}
if (
@@ -415,14 +416,15 @@ function Shift_signup_allowed_admin($needed_angeltype, $shift_entries)
/**
* Check if an angel can signout from a shift.
*
- * @param array $shift The shift
- * @param array $angeltype The angeltype
- * @param array $signout_user The user that was signed up for the shift
+ * @param array $shift The shift
+ * @param array $angeltype The angeltype
+ * @param int $signout_user_id The user that was signed up for the shift
* @return bool
*/
-function Shift_signout_allowed($shift, $angeltype, $signout_user)
+function Shift_signout_allowed($shift, $angeltype, $signout_user_id)
{
- global $user, $privileges;
+ global $privileges;
+ $user = auth()->user();
// user shifts admin can sign out any user at any time
if (in_array('user_shifts_admin', $privileges)) {
@@ -437,7 +439,7 @@ function Shift_signout_allowed($shift, $angeltype, $signout_user)
return true;
}
- if ($signout_user['UID'] == $user['UID'] && $shift['start'] > time() + config('last_unsubscribe') * 3600) {
+ if ($signout_user_id == $user->id && $shift['start'] > time() + config('last_unsubscribe') * 3600) {
return true;
}
@@ -447,7 +449,7 @@ function Shift_signout_allowed($shift, $angeltype, $signout_user)
/**
* Check if an angel can sign up for given shift.
*
- * @param array $signup_user
+ * @param User $signup_user
* @param array $shift The shift
* @param array $angeltype The angeltype to which the user wants to sign up
* @param array|null $user_angeltype
@@ -465,7 +467,7 @@ function Shift_signup_allowed(
$needed_angeltype,
$shift_entries
) {
- global $user, $privileges;
+ global $privileges;
if (in_array('user_shifts_admin', $privileges)) {
return Shift_signup_allowed_admin($needed_angeltype, $shift_entries);
@@ -473,7 +475,7 @@ function Shift_signup_allowed(
if (
in_array('shiftentry_edit_angeltype_supporter', $privileges)
- && User_is_AngelType_supporter($user, $angeltype)
+ && User_is_AngelType_supporter(auth()->user(), $angeltype)
) {
return Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entries);
}
@@ -613,8 +615,8 @@ function Shift_create($shift)
/**
* Return users shifts.
*
- * @param int $userId
- * @param bool $include_freeload_comments
+ * @param int $userId
+ * @param bool $include_freeload_comments
* @return array[]
*/
function Shifts_by_user($userId, $include_freeload_comments = false)
diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php
index 943dbb5b..e18eaf1b 100644
--- a/includes/model/UserAngelTypes_model.php
+++ b/includes/model/UserAngelTypes_model.php
@@ -1,6 +1,7 @@
<?php
use Engelsystem\Database\DB;
+use Engelsystem\Models\User\User;
/**
* User angeltypes model
@@ -67,15 +68,13 @@ function User_unconfirmed_AngelTypes($userId)
/**
* Returns true if user is angeltype supporter or has privilege admin_user_angeltypes.
*
- * @param array $user
+ * @param User $user
* @param array $angeltype
* @return bool
*/
-function User_is_AngelType_supporter(&$user, $angeltype)
+function User_is_AngelType_supporter($user, $angeltype)
{
- if (!isset($user['privileges'])) {
- $user['privileges'] = privileges_for_user($user['UID']);
- }
+ $privileges = privileges_for_user($user->id);
return (count(DB::select('
SELECT `id`
@@ -86,11 +85,11 @@ function User_is_AngelType_supporter(&$user, $angeltype)
LIMIT 1
',
[
- $user['UID'],
+ $user->id,
$angeltype['id']
]
)) > 0)
- || in_array('admin_user_angeltypes', $user['privileges']);
+ || in_array('admin_user_angeltypes', $privileges);
}
/**
diff --git a/includes/model/UserWorkLog_model.php b/includes/model/UserWorkLog_model.php
index c7de3f9e..11f60901 100644
--- a/includes/model/UserWorkLog_model.php
+++ b/includes/model/UserWorkLog_model.php
@@ -2,6 +2,7 @@
use Carbon\Carbon;
use Engelsystem\Database\Db;
+use Engelsystem\Models\User\User;
/**
* Load a single work log entry.
@@ -39,7 +40,7 @@ function UserWorkLogsForUser($userId)
*/
function UserWorkLog_delete($userWorkLog)
{
- $user_source = User($userWorkLog['user_id']);
+ $user_source = User::find($userWorkLog['user_id']);
$result = Db::delete("DELETE FROM `UserWorkLog` WHERE `id`=?", [
$userWorkLog['id']
]);
@@ -62,7 +63,7 @@ function UserWorkLog_delete($userWorkLog)
*/
function UserWorkLog_update($userWorkLog)
{
- $user_source = User($userWorkLog['user_id']);
+ $user_source = User::find($userWorkLog['user_id']);
$result = Db::update("UPDATE `UserWorkLog` SET
`work_timestamp`=?,
@@ -95,7 +96,7 @@ function UserWorkLog_create($userWorkLog)
{
$user = Auth()->user();
- $user_source = User($userWorkLog['user_id']);
+ $user_source = User::find($userWorkLog['user_id']);
$result = Db::insert("INSERT INTO `UserWorkLog` (
`user_id`,
diff --git a/includes/model/User_model.php b/includes/model/User_model.php
index bed6bf47..298ac18d 100644
--- a/includes/model/User_model.php
+++ b/includes/model/User_model.php
@@ -2,6 +2,7 @@
use Carbon\Carbon;
use Engelsystem\Database\DB;
+use Engelsystem\Models\User\User;
use Engelsystem\ValidationResult;
/**
@@ -238,14 +239,12 @@ function Users($order_by = 'Nick')
/**
* Returns true if user is freeloader
*
- * @param array $user
+ * @param User $user
* @return bool
*/
function User_is_freeloader($user)
{
- global $user;
-
- return count(ShiftEntries_freeloaded_by_user($user['UID'])) >= config('max_freeloadable_shifts');
+ return count(ShiftEntries_freeloaded_by_user($user->id)) >= config('max_freeloadable_shifts');
}
/**
@@ -419,19 +418,6 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de
}
/**
- * Returns user by id.
- *
- * @param int $user_id UID
- * @return array|null
- */
-function User($user_id)
-{
- $user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]);
-
- return empty($user) ? null : $user;
-}
-
-/**
* Returns User by api_key.
*
* @param string $api_key User api key
@@ -473,23 +459,13 @@ function User_by_password_recovery_token($token)
/**
* Generates a new api key for given user.
*
- * @param array $user
- * @param bool $log
+ * @param User $user
+ * @param bool $log
*/
-function User_reset_api_key(&$user, $log = true)
+function User_reset_api_key($user, $log = true)
{
- $user['api_key'] = md5($user['Nick'] . time() . rand());
- DB::update('
- UPDATE `User`
- SET `api_key`=?
- WHERE `UID`=?
- LIMIT 1
- ',
- [
- $user['api_key'],
- $user['UID']
- ]
- );
+ $user->api_key = md5($user->name . time() . rand());
+ $user->save();
if ($log) {
engelsystem_log(sprintf('API key resetted (%s).', User_Nick_render($user)));
@@ -523,15 +499,15 @@ function User_generate_password_recovery_token(&$user)
}
/**
- * @param array $user
+ * @param User $user
* @return float
*/
function User_get_eligable_voucher_count($user)
{
$voucher_settings = config('voucher_settings');
- $shifts_done = count(ShiftEntries_finished_by_user($user['UID']));
+ $shifts_done = count(ShiftEntries_finished_by_user($user->id));
- $earned_vouchers = $user['got_voucher'] - $voucher_settings['initial_vouchers'];
+ $earned_vouchers = $user->state->got_voucher - $voucher_settings['initial_vouchers'];
$eligable_vouchers = $shifts_done / $voucher_settings['shifts_per_voucher'] - $earned_vouchers;
if ($eligable_vouchers < 0) {
return 0;