2017-12-29 17:19:27 +01:00
< ? php
use Engelsystem\Database\Db ;
/**
* Load a single work log entry .
2017-12-29 18:57:11 +01:00
*
* @ param int $user_worklog_id
2017-12-29 17:19:27 +01:00
*/
2017-12-29 18:57:11 +01:00
function UserWorkLog ( $user_worklog_id )
{
return Db :: selectOne ( " SELECT * FROM `UserWorkLog` WHERE `id`=? " , [
$user_worklog_id
]);
2017-12-29 17:19:27 +01:00
}
/**
* Returns all work log entries for a user .
2017-12-29 18:57:11 +01:00
*
* @ param User $user
2017-12-29 17:19:27 +01:00
*/
2017-12-29 18:57:11 +01:00
function UserWorkLogsForUser ( $user )
{
return Db :: select ( " SELECT * FROM `UserWorkLog` WHERE `user_id`=? ORDER BY `created_timestamp` " , [
$user [ 'UID' ]
]);
2017-12-29 17:19:27 +01:00
}
/**
* Delete a work log entry .
2017-12-29 18:57:11 +01:00
*
* @ param UserWorkLog $userWorkLog
2017-12-29 17:19:27 +01:00
*/
function UserWorkLog_delete ( $userWorkLog )
{
2017-12-29 17:30:54 +01:00
$user_source = User ( $userWorkLog [ 'user_id' ]);
2017-12-29 17:19:27 +01:00
$result = Db :: delete ( " DELETE FROM `UserWorkLog` WHERE `id`=? " , [
$userWorkLog [ 'id' ]
]);
2017-12-29 18:57:11 +01:00
engelsystem_log ( sprintf ( 'Delete work log for %s, %s hours, %s' , User_Nick_render ( $user_source ), $userWorkLog [ 'work_hours' ], $userWorkLog [ 'comment' ]));
2017-12-29 17:19:27 +01:00
return $result ;
}
/**
* Update work log entry ( only work hours and comment )
2017-12-29 18:57:11 +01:00
*
* @ param UserWorkLog $userWorkLog
2017-12-29 17:19:27 +01:00
*/
function UserWorkLog_update ( $userWorkLog )
{
2017-12-29 17:30:54 +01:00
$user_source = User ( $userWorkLog [ 'user_id' ]);
2017-12-29 18:57:11 +01:00
2017-12-29 17:19:27 +01:00
$result = Db :: update ( " UPDATE `UserWorkLog` SET
2017-12-29 18:57:11 +01:00
`work_timestamp` = ? ,
2017-12-29 17:19:27 +01:00
`work_hours` = ? ,
`comment` = ?
WHERE `id` = ? " , [
2017-12-29 18:57:11 +01:00
$userWorkLog [ 'work_timestamp' ],
2017-12-29 17:19:27 +01:00
$userWorkLog [ 'work_hours' ],
2017-12-29 18:57:11 +01:00
$userWorkLog [ 'comment' ],
$userWorkLog [ 'id' ]
]);
engelsystem_log ( sprintf ( 'Updated work log for %s, %s hours, %s' , User_Nick_render ( $user_source ), $userWorkLog [ 'work_hours' ], $userWorkLog [ 'comment' ]));
2017-12-29 17:19:27 +01:00
return $result ;
}
/**
* Create a new work log entry
2017-12-29 18:57:11 +01:00
*
* @ param UserWorkLog $userWorkLog
2017-12-29 17:19:27 +01:00
*/
function UserWorkLog_create ( $userWorkLog )
{
global $user ;
2017-12-29 17:30:54 +01:00
$user_source = User ( $userWorkLog [ 'user_id' ]);
2017-12-29 17:19:27 +01:00
$result = Db :: insert ( " INSERT INTO `UserWorkLog` (
`user_id` ,
2017-12-29 18:57:11 +01:00
`work_timestamp` ,
2017-12-29 17:19:27 +01:00
`work_hours` ,
`comment` ,
`created_user_id` ,
`created_timestamp`
)
2017-12-29 18:57:11 +01:00
VALUES ( ? , ? , ? , ? , ? , ? ) " , [
$userWorkLog [ 'user_id' ],
$userWorkLog [ 'work_timestamp' ],
2017-12-29 17:19:27 +01:00
$userWorkLog [ 'work_hours' ],
2017-12-29 18:57:11 +01:00
$userWorkLog [ 'comment' ],
$user [ 'UID' ],
time ()
]);
engelsystem_log ( sprintf ( 'Added work log entry for %s, %s hours, %s' , User_Nick_render ( $user_source ), $userWorkLog [ 'work_hours' ], $userWorkLog [ 'comment' ]));
2017-12-29 17:19:27 +01:00
return $result ;
}
/**
* New user work log entry
*
* @ param array [] $user
*/
function UserWorkLog_new ( $user )
{
2017-12-29 18:57:11 +01:00
$work_date = parse_date ( 'Y-m-d H:i' , date ( 'Y-m-d 00:00' , time ()));
$event_config = EventConfig ();
if ( ! empty ( $event_config [ 'buildup_start_date' ])) {
$work_date = parse_date ( 'Y-m-d H:i' , date ( 'Y-m-d 00:00' , $event_config [ 'buildup_start_date' ]));
}
2017-12-29 17:19:27 +01:00
return [
'user_id' => $user [ 'UID' ],
2017-12-29 18:57:11 +01:00
'work_timestamp' => $work_date ,
2017-12-29 17:19:27 +01:00
'work_hours' => 0 ,
'comment' => ''
];
}
?>