Added validation to prevent issues with the answers if already exists when a question is editing

pull/2487/head
José Loguercio 8 years ago
parent 2abb407d3b
commit 8a086f8575
  1. 27
      main/survey/survey.lib.php
  2. 22
      main/survey/survey_question.php

@ -4971,4 +4971,31 @@ class SurveyUtil
return false;
}
/**
* Check if the current survey has answers
*
* @param $surveyId
* @return boolean return true if the survey has answers, false otherwise
*/
public static function checkIfSurveyHasAnswers($surveyId)
{
$tableSurveyAnswer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
$courseId = api_get_course_int_id();
$sql = "SELECT * FROM $tableSurveyAnswer
WHERE
c_id = $courseId AND
survey_id='".$surveyId."'
ORDER BY answer_id, user ASC";
$result = Database::query($sql);
$response = Database::affected_rows($result);
if ($response > 0) {
return true;
}
return false;
}
}

@ -116,7 +116,27 @@ class survey_question
public function renderForm()
{
if (isset($_GET['question_id']) and !empty($_GET['question_id'])) {
$this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
/**
* Check if survey has answers first before update it, this is because if you update it, the question
* options will delete and re-insert in database loosing the iid and question_id to verify the correct answers
*/
$surveyId = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : 0;
$answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId);
if (!$answersChecker) {
$this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
} else {
$this->getForm()->addHtml('
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-8">
<div class="alert alert-info">' . get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers') . '</div>
</div>
<div class="col-sm-2"></div>
</div>
');
}
} else {
$this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true);
}

Loading…
Cancel
Save