diff --git a/application/controllers/api/frontend/v1/coodle/CoodleSurvey.php b/application/controllers/api/frontend/v1/coodle/CoodleSurvey.php index a88a75805..5ec1d76a0 100644 --- a/application/controllers/api/frontend/v1/coodle/CoodleSurvey.php +++ b/application/controllers/api/frontend/v1/coodle/CoodleSurvey.php @@ -265,7 +265,6 @@ class CoodleSurvey extends FHCAPI_Controller } $participants = $this->CoodleSurveyParticipantModel->getParticipants($surveyId); - $this->addMeta("particips", $participants); $participantUids = array_map( function ($participant) { return $participant->uid; @@ -292,8 +291,6 @@ class CoodleSurvey extends FHCAPI_Controller ); $selection = array_slice($selection, 0, $survey->max_selections); - $this->addMeta("selection", $selection); - $this->addMeta("timeslotIds", $timeslotIds); } $this->CoodleSurveyParticipantModel->updateSelection($surveyId, getAuthUID(), json_encode($selection)); @@ -310,6 +307,8 @@ class CoodleSurvey extends FHCAPI_Controller $this->terminateWithError("Survey not found!"); } else if ($survey->creator_uid !== getAuthUID()) { $this->terminateWithError("You are not authorized to modify this survey!"); + } else if ($survey->canceled_at) { + $this->terminateWithError("This survey has already been canceled!"); } else if ($survey->completed_at) { $this->terminateWithError("This survey has already been completed!"); } @@ -335,6 +334,8 @@ class CoodleSurvey extends FHCAPI_Controller $this->terminateWithError("You are not authorized to modify this survey!"); } else if ($survey->canceled_at) { $this->terminateWithError("This survey has already been canceled!"); + } else if ($survey->completed_at) { + $this->terminateWithError("This survey has already been completed!"); } if ($selectedTimeslotId) { @@ -357,6 +358,35 @@ class CoodleSurvey extends FHCAPI_Controller } } + public function sendVotingReminders() + { + $surveyId = $this->input->post("surveyId"); + $survey = $this->CoodleSurveyModel->getSurvey($surveyId); + if (!$survey) { + $this->terminateWithError("Survey not found!"); + } else if ($survey->creator_uid !== getAuthUID()) { + $this->terminateWithError("You do not own this survey!"); + } else if ($survey->canceled_at || $survey->completed_at) { + $this->terminateWithError("This survey is no longer active!"); + } + + $participants = $this->CoodleSurveyParticipantModel->getParticipants($surveyId); + $participantsWithoutVote = array_filter( + $participants, + function ($participant) { + return $participant->selection === null; + } + ); + + if (!count($participantsWithoutVote)) { + $this->terminateWithError("All participants have already voted!"); + } + + // todo: send reminder emails + + $this->terminateWithSuccess(); + } + // ----------------------------------------------------------------------------------------------------------------- // Private methods diff --git a/public/js/api/factory/coodle.js b/public/js/api/factory/coodle.js index ff4f2cf2c..8170f373a 100644 --- a/public/js/api/factory/coodle.js +++ b/public/js/api/factory/coodle.js @@ -98,4 +98,13 @@ export default { }, }; }, + sendReminders(surveyId) { + return { + method: "post", + url: "/api/frontend/v1/coodle/CoodleSurvey/sendVotingReminders", + params: { + surveyId, + }, + }; + } }; diff --git a/public/js/components/Cis/Coodle/Components/CoodleSurvey/CoodleSurveyHeader.js b/public/js/components/Cis/Coodle/Components/CoodleSurvey/CoodleSurveyHeader.js index bef391e91..05161b670 100644 --- a/public/js/components/Cis/Coodle/Components/CoodleSurvey/CoodleSurveyHeader.js +++ b/public/js/components/Cis/Coodle/Components/CoodleSurvey/CoodleSurveyHeader.js @@ -41,7 +41,7 @@ export default { this.$props.survey?.title, ); const cancellationConfirmation = await this.$fhcAlert.confirm({ - header: "Cancellation confirmation!", + header: "Cancellation confirmation", message: cancellationConfirmationMessage, acceptLabel: "Yes", rejectLabel: "No", @@ -66,6 +66,37 @@ export default { this.$emit("surveyCanceled"); } }, + async sendReminders() { + if (!this.$props.survey?.id) return; + + const numberOfParticipantsWithoutVote = + this.$props.survey.participants.filter( + (participant) => participant.selection === null, + ).length; + + if (!numberOfParticipantsWithoutVote) { + this.$fhcAlert.alertError( + "All participants have already voted!", + ); + return; + } + + const reminderConfirmation = await this.$fhcAlert.confirm({ + header: "Reminder confirmation", + message: + "Are you sure you want to remind participants to vote?", + acceptLabel: "Yes", + rejectLabel: "No", + }); + if (!reminderConfirmation) return; + + const remindersResponse = this.$api.call( + CoodleApi.sendReminders(this.$props.survey.id), + ); + if (remindersResponse.meta.status === "success") { + this.$fhcAlert.alertSuccess("Reminders successfully sent!"); + } + }, }, template: /*html*/ `
@@ -84,6 +115,15 @@ export default {