/**
  * Handle sending responses to the response database (if used)
  *
  * Triggered by checkTokenCompletion
  *
  * @param int $userId The id of the gems user
  */
 protected function toResponseDatabase($userId)
 {
     $db = $this->project->getResponseDatabase();
     // WHY EXPLANATION!!
     //
     // For some reason mysql prepared parameters do nothing with a \Zend_Db_Expr
     // object and that causes an error when using CURRENT_TIMESTAMP
     $current = \MUtil_Date::now()->toString(\Gems_Tracker::DB_DATETIME_FORMAT);
     $rValues = array('gdr_id_token' => $this->_tokenId, 'gdr_changed' => $current, 'gdr_changed_by' => $userId, 'gdr_created' => $current, 'gdr_created_by' => $userId);
     $responses = $this->getRawAnswers();
     unset($responses['token'], $responses['id'], $responses['lastpage'], $responses['startlanguage'], $responses['submitdate'], $responses['startdate'], $responses['datestamp']);
     // first read current responses to differentiate between insert and update
     $responseSelect = $db->select()->from('gemsdata__responses', array('gdr_answer_id', 'gdr_response'))->where('gdr_id_token = ?', $this->_tokenId);
     $currentResponses = $db->fetchPairs($responseSelect);
     if (!$currentResponses) {
         $currentResponses = array();
     }
     // \MUtil_Echo::track($currentResponses, $responses);
     // Prepare sql
     $sql = "UPDATE gemsdata__responses\n            SET `gdr_response` = ?, `gdr_changed` = ?, `gdr_changed_by` = ?\n            WHERE gdr_id_token = ? AND gdr_answer_id = ? AND gdr_answer_row = 1";
     $stmt = $db->prepare($sql);
     $inserts = array();
     foreach ($responses as $fieldName => $response) {
         $rValues['gdr_answer_id'] = $fieldName;
         if (is_array($response)) {
             $response = join('|', $response);
         }
         $rValues['gdr_response'] = $response;
         if (array_key_exists($fieldName, $currentResponses)) {
             // Already exists, do update
             // But only if value changed
             if ($currentResponses[$fieldName] != $response) {
                 try {
                     // \MUtil_Echo::track($sql, $rValues['gdr_id_token'], $fieldName, $response);
                     $stmt->execute(array($response, $rValues['gdr_changed'], $rValues['gdr_changed_by'], $rValues['gdr_id_token'], $fieldName));
                 } catch (\Zend_Db_Statement_Exception $e) {
                     error_log($e->getMessage());
                     \Gems_Log::getLogger()->logError($e);
                 }
             }
         } else {
             // We add the inserts together in the next prepared statement to improve speed
             $inserts[$fieldName] = $rValues;
         }
     }
     if (count($inserts) > 0) {
         // \MUtil_Echo::track($inserts);
         try {
             $fields = array_keys(reset($inserts));
             $fields = array_map(array($db, 'quoteIdentifier'), $fields);
             $sql = 'INSERT INTO gemsdata__responses (' . implode(', ', $fields) . ') VALUES (' . implode(', ', array_fill(1, count($fields), '?')) . ')';
             // \MUtil_Echo::track($sql);
             $stmt = $db->prepare($sql);
             foreach ($inserts as $insert) {
                 // \MUtil_Echo::track($insert);
                 $stmt->execute($insert);
             }
         } catch (\Zend_Db_Statement_Exception $e) {
             error_log($e->getMessage());
             \Gems_Log::getLogger()->logError($e);
         }
     }
 }