Преглед изворни кода

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

José Loguercio пре 8 година
родитељ
комит
8a086f8575
2 измењених фајлова са 48 додато и 1 уклоњено
  1. 27 0
      main/survey/survey.lib.php
  2. 21 1
      main/survey/survey_question.php

+ 27 - 0
main/survey/survey.lib.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;
+    }
 }

+ 21 - 1
main/survey/survey_question.php

@@ -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);
         }