/** * RPC Routine to add a response to the survey responses collection. * Returns the id of the inserted survey response * * @access public * @param string $sSessionKey Auth credentials * @param int $iSurveyID Id of the Survey to insert responses * @param struct $aResponseData The actual response * @return int The response ID */ public function add_response($sSessionKey, $iSurveyID, $aResponseData) { if (!$this->_checkSessionKey($sSessionKey)) { return array('status' => 'Invalid session key'); } $oSurvey = Survey::model()->findByPk($iSurveyID); if (is_null($oSurvey)) { return array('status' => 'Error: Invalid survey ID'); } if (hasSurveyPermission($iSurveyID, 'responses', 'create')) { if (!Yii::app()->db->schema->getTable('{{survey_' . $iSurveyID . '}}')) { return array('status' => 'No survey response table'); } //set required values if not set // @todo: Some of this is part of the validation and should be done in the model instead if (!isset($aResponseData['submitdate'])) { $aResponseData['submitdate'] = date("Y-m-d H:i:s"); } if (!isset($aResponseData['startlanguage'])) { $aResponseData['startlanguage'] = getBaseLanguageFromSurveyID($iSurveyID); } if ($oSurvey->datestamp == 'Y') { if (!isset($aResponseData['datestamp'])) { $aResponseData['datestamp'] = date("Y-m-d H:i:s"); } if (!isset($aResponseData['startdate'])) { $aResponseData['startdate'] = date("Y-m-d H:i:s"); } } Survey_dynamic::sid($iSurveyID); $survey_dynamic = new Survey_dynamic(); $result = $survey_dynamic->insert($aResponseData); if ($result) { return $survey_dynamic->primaryKey; } else { return array('status' => 'Unable to add response'); } } else { return array('status' => 'No permission'); } }