mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-10-22 19:59:34 +00:00
138 lines
4.2 KiB
PHP
138 lines
4.2 KiB
PHP
<?php
|
|
class CoodleSurveyParticipant_model extends DB_Model
|
|
{
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->dbTable = 'campus.tbl_coodle_survey_participants';
|
|
}
|
|
|
|
public function getParticipants($surveyId)
|
|
{
|
|
$query = "
|
|
SELECT
|
|
surveyParticipant.participant_uid as uid,
|
|
surveyParticipant.selection as selection,
|
|
person.vorname || ' ' || person.nachname AS name
|
|
FROM $this->dbTable surveyParticipant
|
|
JOIN public.tbl_benutzer benutzer ON(benutzer.uid = surveyParticipant.participant_uid)
|
|
JOIN public.tbl_person person ON(person.person_id = benutzer.person_id)
|
|
WHERE survey_id = $surveyId
|
|
";
|
|
return $this->execQuery($query);
|
|
}
|
|
|
|
public function getParticipantEntriesByUid($uid)
|
|
{
|
|
$query = "SELECT * FROM $this->dbTable WHERE participant_uid = '$uid'";
|
|
return $this->execQuery($query);
|
|
}
|
|
|
|
public function getActiveParticipantEntriesByUid($uid)
|
|
{
|
|
$query = "SELECT
|
|
surveyParticipant.survey_id as survey_id,
|
|
surveyParticipant.participant_uid as participant_uid,
|
|
surveyParticipant.selection as selection
|
|
FROM $this->dbTable surveyParticipant
|
|
JOIN campus.tbl_coodle_surveys survey ON(survey.id = surveyParticipant.survey_id)
|
|
WHERE surveyParticipant.participant_uid = '$uid'
|
|
AND survey.completed_at IS NULL
|
|
AND survey.canceled_at IS NULL";
|
|
return $this->execQuery($query);
|
|
}
|
|
|
|
public function updateParticipants($surveyId, $participants, $timeslots)
|
|
{
|
|
$participantUids = array_map(function ($participant) {
|
|
return $participant["uid"];
|
|
}, $participants);
|
|
|
|
$existingParticipantsQuery = "SELECT * FROM " . $this->dbTable . " WHERE survey_id = " . $surveyId;
|
|
$existingParticipants = getData($this->execQuery($existingParticipantsQuery));
|
|
$existingParticipantUids = array_map(
|
|
function ($existingParticipant) {
|
|
return $existingParticipant->participant_uid;
|
|
},
|
|
$existingParticipants
|
|
);
|
|
|
|
$newParticipantUids = array_filter(
|
|
$participantUids,
|
|
function ($participantUid) use ($existingParticipantUids) {
|
|
return !in_array($participantUid, $existingParticipantUids);
|
|
}
|
|
);
|
|
$redundantParticipantUids = array_filter(
|
|
$existingParticipantUids,
|
|
function ($existingParticipantUid) use ($participantUids) {
|
|
return !in_array($existingParticipantUid, $participantUids);
|
|
}
|
|
);
|
|
|
|
if (count($newParticipantUids)) {
|
|
$insertQuery = "INSERT INTO $this->dbTable VALUES ";
|
|
$newParticipantInsertValues = array_map(
|
|
function ($newParticipantUid) use ($surveyId) {
|
|
return "('$surveyId', '$newParticipantUid', NULL)";
|
|
},
|
|
$newParticipantUids
|
|
);
|
|
$newParticipantInsertValues = implode(", ", $newParticipantInsertValues);
|
|
$insertQuery .= $newParticipantInsertValues;
|
|
$insertQuery .= ";";
|
|
|
|
$this->execQuery($insertQuery);
|
|
}
|
|
|
|
if (count($redundantParticipantUids)) {
|
|
$deleteQuery = "DELETE FROM $this->dbTable WHERE survey_id = $surveyId AND participant_uid IN ('" . implode("', '", $redundantParticipantUids) . "');";
|
|
$this->execQuery($deleteQuery);
|
|
}
|
|
|
|
$timeslotIds = array_map(
|
|
function ($timeslot) {
|
|
return $timeslot->id;
|
|
},
|
|
$timeslots
|
|
);
|
|
|
|
$updatedExistingParticipants = getData($this->execQuery($existingParticipantsQuery));
|
|
foreach ($updatedExistingParticipants as $participant) {
|
|
if (!$participant->selection)
|
|
continue;
|
|
|
|
$selection = json_decode($participant->selection, true);
|
|
if (!count($selection))
|
|
continue;
|
|
|
|
$obsoleteSelectedTimeslotIds = array_diff($selection, $timeslotIds);
|
|
if (!count($obsoleteSelectedTimeslotIds))
|
|
continue;
|
|
|
|
$updatedSelection = null;
|
|
if (count($obsoleteSelectedTimeslotIds) !== count($selection)) {
|
|
$updatedSelection = array_filter(
|
|
$selection,
|
|
function ($timeslotId) use ($obsoleteSelectedTimeslotIds) {
|
|
return !in_array($timeslotId, $obsoleteSelectedTimeslotIds);
|
|
}
|
|
);
|
|
}
|
|
|
|
$this->updateSelection($surveyId, $participant->participant_uid, $updatedSelection);
|
|
}
|
|
}
|
|
|
|
public function updateSelection($surveyId, $participantUid, $selection)
|
|
{
|
|
$selection = $selection !== null ? "'" . json_encode($selection) . "'" : "NULL";
|
|
$selectionUpdateQuery = "UPDATE $this->dbTable SET selection = $selection WHERE survey_id = $surveyId AND participant_uid = '$participantUid'";
|
|
$this->execQuery($selectionUpdateQuery);
|
|
}
|
|
}
|