fixes the fetched ampeln in the ampelWidget

This commit is contained in:
SimonGschnell
2024-07-29 13:17:29 +02:00
parent 65b71191a0
commit 2c45478877
4 changed files with 141 additions and 68 deletions
@@ -78,14 +78,14 @@ class Ampeln extends FHCAPI_Controller
*/
public function getNonConfirmedActiveAmpeln()
{
$userAmpeln = array();
// fetch active ampeln
$activeAmpeln = $this->AmpelModel->active();
$activeAmpeln = $this->getDataOrTerminateWithError($activeAmpeln);
foreach($activeAmpeln as $ampel){
// check if ampel is confirmed by user
@@ -94,22 +94,40 @@ class Ampeln extends FHCAPI_Controller
// only include non confirmed active ampeln in the result
if(!$confirmedByUser){
$userUID_array = $this->AmpelModel->execBenutzerSelect($ampel->benutzer_select);
$userUID_array = $this->getDataOrTerminateWithError($userUID_array);
// check if the user was assigned to the ampel
$zugeteilt = $this->AmpelModel->isZugeteilt($this->uid, $ampel->benutzer_select);
if(isError($zugeteilt)){
$this->addError(getError($zugeteilt));
$zugeteilt = false;
}else{
$zugeteilt = $this->getDataOrTerminateWithError($zugeteilt);
}
if($zugeteilt) $userAmpeln[] = $this->translateAmpel($ampel);
// old way to check if an ampel was assigned to the user
/* $userUID_array = $this->AmpelModel->execBenutzerSelect($ampel->benutzer_select);
if(isError($userUID_array)){
$this->addError(getError($userUID_array));
}
$userUID_array = $this->getDataOrTerminateWithError($userUID_array);
// check if user is assigned to the ampel
foreach($userUID_array as $user_obj){
// property is called uid for students and mitarbeiter_uid for employees
$user_uid = property_exists($user_obj,"uid") ? $user_obj->uid : $user_obj->mitarbeiter_uid;
if($user_uid === $this->uid){
$userAmpeln[] = $this->translateAmpel($ampel);
}
}
}
}
} */
}
}
$this->terminateWithSuccess($userAmpeln);
}
@@ -121,23 +139,27 @@ class Ampeln extends FHCAPI_Controller
public function getAllActiveAmpeln()
{
$userAmpeln = array();
$ampel_result = $this->AmpelModel->active();
$ampel_result = $this->getDataOrTerminateWithError($ampel_result);
foreach($ampel_result as $ampel){
$confirmedByUser = $this->AmpelModel->isConfirmed($ampel->ampel_id,$this->uid);
$ampel->bestaetigt = $confirmedByUser;
$userUID_array = $this->AmpelModel->execBenutzerSelect($ampel->benutzer_select);
$userUID_array = $this->getDataOrTerminateWithError($userUID_array);
foreach($userUID_array as $user_obj){
$user_uid = property_exists($user_obj,"uid") ? $user_obj->uid : $user_obj->mitarbeiter_uid;
if($user_uid === $this->uid){
$userAmpeln[] = $this->translateAmpel($ampel);
}
// check if the ampel was assigned to the user
$zugeteilt = $this->AmpelModel->isZugeteilt($this->uid, $ampel->benutzer_select);
if(isError($zugeteilt)){
$this->addError(getError($zugeteilt));
$zugeteilt = false;
}else{
$zugeteilt = $this->getDataOrTerminateWithError($zugeteilt);
}
if($zugeteilt) $userAmpeln[] = $this->translateAmpel($ampel);
}
$this->terminateWithSuccess($userAmpeln);
@@ -157,8 +179,13 @@ class Ampeln extends FHCAPI_Controller
$alle_ampeln = $this->getDataOrTerminateWithError($alle_ampeln);
// translate ampeln
array_map(function($ampel){ return $this->translateAmpel($ampel);}, $alle_ampeln);
$alle_ampeln = array_map(function($ampel){
// check if ampel is confirmed by user
$confirmedByUser = $this->AmpelModel->isConfirmed($ampel->ampel_id,$this->uid);
$ampel->bestaetigt = $confirmedByUser;
// translate ampeln
return $this->translateAmpel($ampel);
}, $alle_ampeln);
$this->terminateWithSuccess($alle_ampeln);
@@ -183,8 +210,8 @@ class Ampeln extends FHCAPI_Controller
$userLanguage = $this->getDataOrTerminateWithError($userLanguage)[0]->index - 1; // why does the index start at 1?
// translate the ampel description and button text
$ampel->beschreibung = $ampel->beschreibung[$userLanguage];
$ampel->buttontext = $ampel->buttontext[$userLanguage];
if(isset($ampel->beschreibung) && count($ampel->beschreibung)>=($userLanguage+1)) $ampel->beschreibung = $ampel->beschreibung[$userLanguage];
if(isset($ampel->buttontext) && count($ampel->buttontext)>=($userLanguage+1)) $ampel->buttontext = $ampel->buttontext[$userLanguage];
return $ampel;
@@ -28,6 +28,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
*/
function _createReturnObject($code, $error, $retval)
{
$returnObject = new stdClass();
$returnObject->code = $code;
$returnObject->error = $error;
@@ -39,7 +40,7 @@ function _createReturnObject($code, $error, $retval)
/**
* Success
*
* @return array
* @return stdClass
*/
function success($retval = null, $code = null)
{
@@ -49,7 +50,7 @@ function success($retval = null, $code = null)
/**
* Error
*
* @return array
* @return stdClass
*/
function error($retval = null, $code = null)
{
+70 -18
View File
@@ -33,10 +33,16 @@ class Ampel_model extends DB_Model
}
$query .= '(
(NOW()<(deadline+(COALESCE(verfallszeit,0) || \' days\')::interval)::date)
OR (verfallszeit IS NULL)
AND (NOW()>(deadline-(COALESCE(vorlaufzeit,0) || \' days\')::interval)::date)
OR (vorlaufzeit IS NULL AND NOW() < deadline))';
(
(NOW()<(deadline+(COALESCE(verfallszeit,0) || \' days\')::interval)::date)
OR (verfallszeit IS NULL)
)
AND
(
(NOW()>(deadline-(COALESCE(vorlaufzeit,0) || \' days\')::interval)::date)
OR (vorlaufzeit IS NULL AND NOW() < deadline)
)
)';
$query .= ' ORDER BY deadline DESC';
@@ -92,6 +98,12 @@ class Ampel_model extends DB_Model
}
/**
* confirms Ampel by the user.
* @param int $ampel_id Ampel-ID
* @param string $uid UID
* @return bool insert into result
*/
public function confirmAmpel($ampel_id, $uid)
{
if(isset($ampel_id) && isset($uid)){
@@ -105,6 +117,33 @@ class Ampel_model extends DB_Model
}
/**
* checks if a user is assigned to an ampel
* @param string $uid userID
* @param string $benutzer_select the select query which gets all the user that are assigned to an ampel
* @return stdClass
*/
public function isZugeteilt($uid, $benutzer_select){
$zugeteilt = $this->execReadOnlyQuery("
SELECT
CASE WHEN ? IN (".$benutzer_select.")
THEN true
ELSE false
END as zugeteilt
", [$uid]);
if(isError($zugeteilt)){
return $zugeteilt;
}
$zugeteilt = getData($zugeteilt);
return success(current($zugeteilt)->zugeteilt);
}
function alleAmpeln($uid){
@@ -125,30 +164,43 @@ class Ampel_model extends DB_Model
if(isError($allAmpeln)) return error(getError($allAmpeln));
$allAmpeln = getData($allAmpeln);
foreach($allAmpeln as $ampel){
// check if the ampel is assigned to the user
$zugeteilt = $this->execReadOnlyQuery("
SELECT
CASE WHEN ? IN (?)
CASE WHEN ? IN (".$ampel->benutzer_select.")
THEN true
ELSE false
END as zugeteilt
", [$uid, $ampel->benutzer_select]);
if(isError($zugeteilt)) return error(getError($zugeteilt));
", [$uid]);
if($zugeteilt
// datum der Ampel liegt nicht vor der Vorlaufzeit
&& (isset($ampel->vorlaufzeit) && !($now < strtotime('-' . $ampel->vorlaufzeit . ' day', $datum->mktime_fromdate($ampel->deadline)) ))
// verfallszeit nicht vor dem start Datum des Benutzers abgelaufen
&& isset($ampel->verfallszeit) && $benutzerStartDate < strtotime('+' . $ampel->verfallszeit . ' day', $datum->mktime_fromdate($ampel->deadline))
if(isError($zugeteilt)) return error(getError($zugeteilt));
// verfallszeit ist abgelaufen
//&& $now > strtotime('+' . $ampel->verfallszeit . ' day', $ampel->deadline)
$zugeteilt = current(getData($zugeteilt))->zugeteilt;
// abgelaufen check
// $now > strtotime('+' . $ampel->verfallszeit . ' day', $ampel->deadline)
if(
// aktuelles datum liegt vor der Vorlaufzeit der Ampel
(isset($ampel->vorlaufzeit) && $now < strtotime('-' . $ampel->vorlaufzeit . ' day', $datum->mktime_fromdate($ampel->deadline)))
||
// ampel ist vor Arbeitsstart abgelaufen
(isset($ampel->verfallszeit) && $benutzerStartDate > strtotime('+' . $ampel->verfallszeit . ' day', $datum->mktime_fromdate($ampel->deadline)))
||
// ampel ist vor Arbeitsstart abgelaufen (verfallszeit nicht vorhanden)
($benutzerStartDate > strtotime('+' . $ampel->verfallszeit . ' day', $datum->mktime_fromdate($ampel->deadline)))
){
$zugeteile_ampeln[] = $ampel;
// continue iteration if ampel is expired before work start or shouldn't be visible yet
continue;
}
$ampel->zugeteilt = $zugeteilt;
if($zugeteilt) $zugeteile_ampeln[] = $ampel;
}
return success($zugeteile_ampeln);
+18 -25
View File
@@ -52,18 +52,7 @@ export default {
default: return data;
}
},
isExpiredAmpel(ampel){
let deadline = new Date(ampel.deadline);
if(ampel.verfallszeit instanceof Number) deadline.setDate(deadline.getDate() + ampel.verfallszeit);
deadline.setHours(0, 0, 0, 0);
let today = new Date();
today.setHours(0, 0, 0, 0);
return deadline < today ? true : false;
},
toggleFilter(value){
this.filter === value ? this.filter = '' : this.filter = value;
@@ -87,27 +76,31 @@ export default {
closeOffcanvas(){
this.filter = '';
},
async fetchActiveAmpeln(){
async fetchNonConfirmedActiveAmpeln(){
await this.$fhcApi.factory.ampeln.getNonConfirmedActiveAmpeln().then(res=>{
this.activeAmpeln = res.data.sort((a,b) => new Date(b.deadline) - new Date(a.deadline));
});
},
async fetchAllAmpeln(){
async fetchAllActiveAmpeln(){
await this.$fhcApi.factory.ampeln.alleAmpeln().then(res=>{
await this.$fhcApi.factory.ampeln.getAllActiveAmpeln().then(res=>{
this.allAmpeln = res.data.sort((a,b) => new Date(b.deadline) - new Date(a.deadline));
});
},
async confirm(ampelId){
await this.$fhcApi.factory.ampeln.confirmAmpel(ampelId).then(res =>{
res.data ? this.$toast.success('Ampel bestätigt') : this.$toast.error('Fehler beim Bestätigen der Ampel');
this.$fhcApi.factory.ampeln.confirmAmpel(ampelId).then(res =>{
// response of the enpoint when confirming an ampel (true if confirmed and false if not confirmed)
if(res.data){
console.log("ampel was successfully confirmed");
}else{
console.error("ampel was not successfully confirmed");
}
});
// fetch all the ampeln after an ampel has been confirmed
await this.fetchActiveAmpeln();
await this.fetchAllAmpeln();
// update the ampeln by refetching them
this.fetchNonConfirmedActiveAmpeln();
this.fetchAllActiveAmpeln();
},
validateBtnTxt(buttontext){
@@ -124,8 +117,8 @@ export default {
},
async mounted() {
await this.fetchActiveAmpeln();
await this.fetchAllAmpeln();
await this.fetchNonConfirmedActiveAmpeln();
await this.fetchAllActiveAmpeln();
},
template: /*html*/`
<div class="widgets-ampel w-100 h-100">
@@ -197,7 +190,7 @@ export default {
</div>
<div v-for="ampel in ampelnComputed" :key="ampel.ampel_id" class="mt-2">
<ul class="list-group">
<li :style="{...(isExpiredAmpel(ampel)? {'background':'#E2E3E5'} : {})}" class="list-group-item small">
<li class="list-group-item small">
<div class="position-relative"><!-- prevents streched-link from stretching outside this parent element -->
<div class="d-flex">
<span class="small text-muted me-auto"><small>Deadline: {{ getDate(ampel.deadline) }}</small></span>
@@ -205,11 +198,11 @@ export default {
<div v-if="ampel.verpflichtend"><span class="badge bg-warning ms-1"><i class="fa fa-solid fa-triangle-exclamation"></span></div>
<div v-if="ampel.bestaetigt"><span class="badge bg-success ms-1"><i class="fa fa-solid fa-circle-check"></i></span></div>
</div>
<a :class="{...(isExpiredAmpel(ampel)? {'text-reset':true, 'text-decoration-none':true} : {})}" :href="'#ampelCollapse_' + ampel.ampel_id" data-bs-toggle="collapse" class="stretched-link">{{ ampel.kurzbz }}</a><br>
<a :href="'#ampelCollapse_' + ampel.ampel_id" data-bs-toggle="collapse" class="stretched-link">{{ ampel.kurzbz }}</a><br>
</div>
<div class="collapse my-3" :id="'ampelCollapse_' + ampel.ampel_id" :ref="'ampelCollapse_' + ampel.ampel_id">
<div v-html="ampel.beschreibung"></div>
<div v-if="!isExpiredAmpel(ampel)" class="d-flex justify-content-end mt-3">
<div v-if="!ampel.bestaetigt " class="d-flex justify-content-end mt-3">
<button class="btn btn-sm btn-primary" :class="{disabled: ampel.bestaetigt}" @click="confirm(ampel.ampel_id)">{{ validateBtnTxt(ampel.buttontext) }}</button>
</div>
</div>