Last active
December 23, 2015 08:59
-
-
Save gsf/6611005 to your computer and use it in GitHub Desktop.
updateUserEvents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ... | |
| /** | |
| * Updates UserEvents that are in the past and if the selection strategy of | |
| * first is used will update, but not mark as incomplete, encounters that are | |
| * ongoing. This method does not normally need to be modified. | |
| */ | |
| protected function updateUserEvents() | |
| { | |
| //$this->updateExpiredUserEvents(); | |
| // | |
| //if ($this->getApiParam('selection_strategy', 'first') === 'first') | |
| //{ | |
| // $this->updateActiveUserEvents(); | |
| //} | |
| $strategy = $this->getApiParam('selection_strategy', 'first'); | |
| $field = $this->getApiParam('selection_field', $this->fields[0]); | |
| // Create a query for incomplete user events without a form submission | |
| $q = Doctrine::getTable('UserEvent') | |
| ->createQuery('ue') | |
| ->where('ue.completed IS NULL') | |
| ->andWhere('ue.form_submission_id IS NULL'); | |
| // If selection_strategy is first, only start_date must be in the past | |
| if ($strategy === 'first') | |
| { | |
| $q->andWhere('ue.start_date <= ?', aDate::mysql(time())); | |
| } | |
| // Else end_date plus latency must be in the past | |
| // Since we need the window to have ended to accurately calculate other strategies | |
| else | |
| { | |
| $q->andWhere('ue.end_date < ?', aDate::mysql(time() - $this->latency)); | |
| } | |
| // Get events and loop over them | |
| $userEvents = $q->execute(); | |
| foreach ($userEvents as $userEvent) | |
| { | |
| // For each event, get form submissions that fall within the event window | |
| $q = $this->createQueryFormSubmissionsForUserEvent($this->source, $userEvent); | |
| // Grab a form submission based on selection_strategy | |
| Doctrine::getTable('aFormSubmission')->queryBySelectionStrategy($q, $field, $strategy); | |
| $aFormSubmission = $q->fetchOne(); | |
| // If we have a form submission, link the two together and complete the event | |
| if ($aFormSubmission) | |
| { | |
| $userEvent->complete($aFormSubmission->created_at); | |
| $userEvent->form_submission_id = $aFormSubmission->id; | |
| $userEvent->save(); | |
| } | |
| else | |
| { | |
| // XXX Will this keep an event from being linked if a submission comes in later? | |
| $userEvent->complete(null, false); | |
| } | |
| } | |
| } | |
| // ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment