From 4fddeb50953d0842f7b16c0298b346ddbe9f2c57 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Thu, 29 Oct 2015 09:39:17 -0500 Subject: [PATCH] Fix survey already answered by anonymous users - res BT#10490 --- main/survey/fillsurvey.php | 12 ++++++++-- main/survey/survey.lib.php | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/main/survey/fillsurvey.php b/main/survey/fillsurvey.php index 10ca6b92e4..eb301be6c8 100755 --- a/main/survey/fillsurvey.php +++ b/main/survey/fillsurvey.php @@ -141,8 +141,13 @@ if (Database::num_rows($result) < 1) { $survey_invitation = Database::fetch_array($result, 'ASSOC'); // Now we check if the user already filled the survey -if ( !isset($_POST['finish_survey']) && - ($isAnonymous && isset($_SESSION['surveyuser'])) || +if ( + !isset($_POST['finish_survey']) && + ( + $isAnonymous && + isset($_SESSION['surveyuser']) && + SurveyUtil::isSurveyAnsweredFlagged($survey_invitation['survey_code'], $survey_invitation['c_id']) + ) || ($survey_invitation['answered'] == 1 && !isset($_GET['user_id'])) ) { Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false); @@ -523,6 +528,9 @@ if (isset($_POST['finish_survey'])) { $survey_invitation['user'], $survey_invitation['survey_code'] ); + + SurveyUtil::flagSurveyAsAnswered($survey_invitation['survey_code'], $survey_invitation['c_id']); + unset($_SESSION['paged_questions']); unset($_SESSION['page_questions_sec']); Display :: display_footer(); diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index 35003193d4..6863a8581d 100755 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -5190,4 +5190,51 @@ class SurveyUtil return false; } } + + /** + * Set a flag to the current survey as answered by the current user + * @param string $surveyCode The survey code + * @param int $courseId The course ID + */ + public static function flagSurveyAsAnswered($surveyCode, $courseId) + { + $currenUserId = api_get_user_id(); + $flag = sprintf("%s-%s-%d", $courseId, $surveyCode, $currenUserId); + + if (!isset($_SESSION['filled_surveys'])) { + $_SESSION['filled_surveys'] = array(); + } + + $_SESSION['filled_surveys'][] = $flag; + } + + /** + * Check whether a survey was answered by the current user + * @param string $surveyCode The survey code + * @param int $courseId The course ID + * @return boolean + */ + public static function isSurveyAnsweredFlagged($surveyCode, $courseId) + { + $currenUserId = api_get_user_id(); + $flagToCheck = sprintf("%s-%s-%d", $courseId, $surveyCode, $currenUserId); + + if (!isset($_SESSION['filled_surveys'])) { + return false; + } + + if (!is_array($_SESSION['filled_surveys'])) { + return false; + } + + foreach ($_SESSION['filled_surveys'] as $flag) { + if ($flagToCheck != $flag) { + continue; + } + + return true; + } + + return false; + } }