~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/util/agMessageResponseHandler.class.php

  • Committer: Chad Heuschober
  • Date: 2011-05-20 22:47:55 UTC
  • mto: (1.26.1 push-trunk) (7.1.1 mayon)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: chad.heuschober@mail.cuny.edu-20110520224755-d2nhq8q962ywvj2l
Nearly buttoned up agMessageResponseHandler->setEventStaffStatus() but can't test it until we have responses coming back. There is an unknown in how timestamps will be formatted that will need to be worked through for comparison purposes. A todo has been sunk in the relevant place for a quick find later on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 */
22
22
class agMessageResponseHandler extends agImportNormalization
23
23
{
24
 
  
25
24
  public $agEventAvailableStaffStatus;
26
25
  public $agEventUnAvailableStaffStatus;
27
26
  /**
31
30
   */
32
31
  public function __construct($tempTable = NULL, $logEventLevel = NULL)
33
32
  {
34
 
    if (is_null($tempTable)) { $tempTable = 'temp_staff_import'; }
 
33
    if (is_null($tempTable)) { $tempTable = 'temp_message_import'; }
35
34
 
36
35
    // DO NOT REMOVE
37
36
    parent::__construct($tempTable, $logEventLevel);
185
184
   */
186
185
  protected function setEventStaffStatus($throwOnError, Doctrine_Connection $conn)
187
186
  {
188
 
    
189
187
    // always start with any data maps we'll need so they're explicit
190
188
    $responses = array();
191
 
    
 
189
 
192
190
    // loop through our raw data and build our person language data
 
191
    $this->logInfo('Looping throw the message reponses and retrieving the most recent.');
193
192
    foreach ($this->importData as $rowId => $rowData)
194
193
    {
195
194
      $rd = $rowData['_rawData'];
196
195
      if (array_key_exists('resonse', $rd)) {
197
 
        $responses[$rd['unique_id']] = $rd['response'];
198
 
      }
199
 
    }
200
 
    
201
 
    // @todo foreach $responses doctrine goodness
202
 
    
 
196
        $rVals = array(self::mapResponse($rd['response']), strtotime($rd['time_stamp']));
 
197
        $responses[$rd['unique_id']] = $rVals;
 
198
      }
 
199
    }
 
200
 
 
201
    // this step is necessary to avoid index constraints
 
202
    $coll = agDoctrineQuery::create()
 
203
      ->select('ess.*')
 
204
        ->from('agEventStaffStatus ess')
 
205
        ->whereIn(array_keys($responses))
 
206
          ->andWhere('EXISTS (SELECT s.id ' .
 
207
              'FROM agEventStaffStatus AS s ' .
 
208
              'WHERE s.event_staff_id = ess.event_staff_id ' .
 
209
              'HAVING MAX(s.time_stamp) = ess.time_stamp)')
 
210
        ->execute();
 
211
 
 
212
    // loop through all of our event staff and insert for those who already have a status
 
213
    foreach ($coll as $collId => &$rec)
 
214
    {
 
215
      // grab that particular response record
 
216
      $rVals = $responses[$rec['event_staff_id']];
 
217
 
 
218
      // @todo Check the timestamp output... this might need to be strtotime'd
 
219
      if ($rec['time_stamp'] == $rVals[1])
 
220
      {
 
221
        // if the timestamps were the same, update the response
 
222
        $eventMsg = 'Updating existing response for event staff id {' . $rec['event_staff_id'] .
 
223
          '}.';
 
224
        $this->logDebug($eventMsg);
 
225
        $rec['staff_allocation_status_id'] = $rVals[0];
 
226
      }
 
227
      else if ($rec['time_stamp'] < $rVals[1])
 
228
      {
 
229
        $eventMsg = 'Creating new status record for event staff id {' . $rec['event_staff_id'] .
 
230
          '}.';
 
231
        $this->logDebug($eventMsg);
 
232
 
 
233
        // if the db timestamp is older than the import one, make a new record and add it
 
234
        $nRec = new agEventStaffStatus();
 
235
        $nRec['event_staff_id'] = $rec['event_staff_id'];
 
236
        $nRec['time_stamp'] = $rVals[1];
 
237
        $nRec['staff_allocation_status_id'] = $rVals[0];
 
238
        $coll->add($nRec);
 
239
      }
 
240
      else
 
241
      {
 
242
        $eventMsg = 'Import timestamp {' . $rVals[1] . '} is older than the current timestamp in' .
 
243
          'the database {' . $rec['time_stamp'] . '}. Skipping insertion of older timestamp.';
 
244
        $this->logWarn($eventMsg);
 
245
      }
 
246
 
 
247
      // either way, we can be safely done this response
 
248
      unset($responses[$rec['event_staff_id']]);
 
249
    }
 
250
 
 
251
    // as a safety measure, always unset a referenced array value as soon as the loop is over
 
252
    unset($rec);
 
253
   
 
254
    // If we still have members in $responses, they were not event staff as of this execution
 
255
    // NOTE: Theoretically, an event staff person could have an an event staff record but no
 
256
    // event staff status record (the source for the collection), however, operationally, this
 
257
    // case should not occur since all newly generated staff pool members are given a default
 
258
    // status.
 
259
 
 
260
    // Either way, we should warn the user that these records will not be updated
 
261
    $eventMsg = 'Event staff with IDs {' . implode(',', array_keys($responses)) . '} are no ' .
 
262
      'longer valid members of this event. Skipping response updates.';
 
263
    $this->logWarn($eventMsg);
 
264
 
 
265
    // here's the big to-do; let's save!
 
266
    $coll->save();
 
267
  }
 
268
 
 
269
  /**
 
270
   * Method to map a message response to a database-understood status type.
 
271
   * @param string $response
 
272
   */
 
273
  protected static function mapResponse( $response )
 
274
  {
 
275
    //@todo make this do something
 
276
    return $response;
203
277
  }
204
278
}
 
 
b'\\ No newline at end of file'