diff --git a/application/controllers/api/frontend/v1/Ampeln.php b/application/controllers/api/frontend/v1/Ampeln.php index 21fa83c4d..2e64ba8b6 100644 --- a/application/controllers/api/frontend/v1/Ampeln.php +++ b/application/controllers/api/frontend/v1/Ampeln.php @@ -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; diff --git a/application/helpers/hlp_return_object_helper.php b/application/helpers/hlp_return_object_helper.php index cc896856d..d9004d993 100755 --- a/application/helpers/hlp_return_object_helper.php +++ b/application/helpers/hlp_return_object_helper.php @@ -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) { diff --git a/application/models/content/Ampel_model.php b/application/models/content/Ampel_model.php index 136d4b38d..8f89ff3e9 100755 --- a/application/models/content/Ampel_model.php +++ b/application/models/content/Ampel_model.php @@ -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); diff --git a/public/js/components/DashboardWidget/Ampel.js b/public/js/components/DashboardWidget/Ampel.js index 271aa2326..1f1154a3b 100755 --- a/public/js/components/DashboardWidget/Ampel.js +++ b/public/js/components/DashboardWidget/Ampel.js @@ -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*/`