diff --git a/application/helpers/common_helper.php b/application/helpers/common_helper.php --- a/application/helpers/common_helper.php +++ b/application/helpers/common_helper.php @@ -1000,16 +1000,24 @@ function getExtendedAnswer($iSurveyID, $sFieldCode, $sValue, $sLanguage, $ques if ($sValue == null || $sValue == '') { return ''; } - $survey = Survey::model()->findByPk($iSurveyID); - $rawQuestions = Question::model()->findAll("sid = :sid", [":sid" => $iSurveyID]); - $found = false; - foreach ($rawQuestions as $rawQuestion) { - $found = $found || (strpos($sFieldCode, "Q{$rawQuestion->qid}") === 0); + // createFieldMap() is cached per survey/language in the user session, but + // Survey::model()->findByPk() is not - cache it per request (keyed off the + // current Yii app instance, which is recreated fresh for every request) so + // that rendering many cells of the same survey (e.g. the responses grid) + // doesn't re-fetch the survey row for every single cell. + static $surveyCache = []; + $appId = spl_object_id(Yii::app()); + if (!isset($surveyCache[$appId])) { + $surveyCache = [$appId => []]; } + if (!array_key_exists($iSurveyID, $surveyCache[$appId])) { + $surveyCache[$appId][$iSurveyID] = Survey::model()->findByPk($iSurveyID); + } + $survey = $surveyCache[$appId][$iSurveyID]; + //Fieldcode used to determine question, $sValue used to match against answer code //Returns NULL if question type does not suit - if ($found) { - //Only check if it looks like a real fieldcode + { $fieldmap = createFieldMap($survey, 'short', false, false, $sLanguage); if (isset($fieldmap[$sFieldCode])) { $fields = $fieldmap[$sFieldCode];