diff --git a/main/survey/ch_dropdown.php b/main/survey/ch_dropdown.php index 49f1023d51..53b33ebed2 100644 --- a/main/survey/ch_dropdown.php +++ b/main/survey/ch_dropdown.php @@ -20,6 +20,12 @@ class ch_dropdown extends survey_question } } + if (isset($formData['answersid']) && !empty($formData['answersid'])) { + foreach ($formData['answersid'] as $value) { + $this->getForm()->addHidden('answersid[]', $value); + } + } + parent::addRemoveButtons($formData); } diff --git a/main/survey/ch_multiplechoice.php b/main/survey/ch_multiplechoice.php index 8326f1be15..4a3749815f 100644 --- a/main/survey/ch_multiplechoice.php +++ b/main/survey/ch_multiplechoice.php @@ -27,24 +27,22 @@ class ch_multiplechoice extends survey_question $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120']; $total = count($formData['answers']); + if (is_array($formData['answers'])) { foreach ($formData['answers'] as $key => $value) { $this->getForm()->addHtmlEditor('answers['.$key.']', null, false, false, $config); - - if ($key < $total - 1) { - //$this->getForm()->addButton("move_down[$key]", get_lang('Down')); - } - - if ($key > 0) { - //$this->getForm()->addButton("move_up[$key]", get_lang('Up')); - } - if ($total > 2) { $this->getForm()->addButton("delete_answer[$key]", get_lang('Delete'), 'trash', 'danger'); } } } + if (isset($formData['answersid']) && !empty($formData['answersid'])) { + foreach ($formData['answersid'] as $value) { + $this->getForm()->addHidden('answersid[]', $value); + } + } + parent::addRemoveButtons($formData); } diff --git a/main/survey/ch_multipleresponse.php b/main/survey/ch_multipleresponse.php index 57f9418765..cdde14977d 100644 --- a/main/survey/ch_multipleresponse.php +++ b/main/survey/ch_multipleresponse.php @@ -35,6 +35,12 @@ class ch_multipleresponse extends survey_question } } + if (isset($formData['answersid']) && !empty($formData['answersid'])) { + foreach ($formData['answersid'] as $value) { + $this->getForm()->addHidden('answersid[]', $value); + } + } + parent::addRemoveButtons($formData); } @@ -49,12 +55,11 @@ class ch_multipleresponse extends survey_question ) { $class = 'checkbox-inline'; $labelClass = 'checkbox-inline'; - if ($questionData['display'] == 'vertical') { + if ($questionData['display'] === 'vertical') { $class = 'checkbox-vertical'; } $name = 'question'.$questionData['question_id']; - $form->addCheckBoxGroup( $name, null, @@ -63,7 +68,6 @@ class ch_multipleresponse extends survey_question ); $defaults = []; - if (!empty($answers)) { foreach ($answers as $answer) { $defaults[$name.'['.$answer.']'] = true; diff --git a/main/survey/ch_percentage.php b/main/survey/ch_percentage.php index 2536965aaa..b724059aca 100644 --- a/main/survey/ch_percentage.php +++ b/main/survey/ch_percentage.php @@ -21,7 +21,6 @@ class ch_percentage extends survey_question } $name = 'question'.$questionData['question_id']; - $form->addSelect( $name, null, diff --git a/main/survey/ch_score.php b/main/survey/ch_score.php index 702aecf8e0..2499eb37af 100644 --- a/main/survey/ch_score.php +++ b/main/survey/ch_score.php @@ -13,9 +13,7 @@ class ch_score extends survey_question public function createForm($survey_data, $formData) { parent::createForm($survey_data, $formData); - $this->getForm()->addText('maximum_score', get_lang('MaximumScore')); - $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120']; if (is_array($formData['answers'])) { foreach ($formData['answers'] as $key => $value) { @@ -29,6 +27,12 @@ class ch_score extends survey_question } } + if (isset($formData['answersid']) && !empty($formData['answersid'])) { + foreach ($formData['answersid'] as $value) { + $this->getForm()->addHidden('answersid[]', $value); + } + } + parent::addRemoveButtons($formData); } @@ -49,6 +53,7 @@ class ch_score extends survey_question $name = 'question'.$questionData['question_id'].'['.$key.']'; + $form->addHidden('question_id', $questionData['question_id']); $form->addSelect( $name, $value, diff --git a/main/survey/ch_yesno.php b/main/survey/ch_yesno.php index 467d6a1eb9..fda70de628 100644 --- a/main/survey/ch_yesno.php +++ b/main/survey/ch_yesno.php @@ -29,6 +29,7 @@ class ch_yesno extends survey_question 'Width' => '100%', 'Height' => '120', ]; + $this->getForm()->addHtmlEditor( 'answers[0]', get_lang('AnswerOptions'), @@ -36,6 +37,7 @@ class ch_yesno extends survey_question false, $config ); + $this->getForm()->addHtmlEditor( 'answers[1]', null, @@ -43,6 +45,12 @@ class ch_yesno extends survey_question false, $config ); + + if (isset($formData['answersid']) && !empty($formData['answersid'])) { + foreach ($formData['answersid'] as $value) { + $this->getForm()->addHidden('answersid[]', $value); + } + } } /** @@ -54,7 +62,7 @@ class ch_yesno extends survey_question if (is_array($questionData['options'])) { $class = 'radio-inline'; $labelClass = 'radio-inline'; - if ($questionData['display'] == 'vertical') { + if ($questionData['display'] === 'vertical') { $class = 'radio-vertical'; } diff --git a/main/survey/question.php b/main/survey/question.php index a4a7107ff3..83ca631e78 100755 --- a/main/survey/question.php +++ b/main/survey/question.php @@ -108,7 +108,6 @@ if (!in_array($_GET['type'], $possible_types)) { $ch_type = 'ch_'.$_GET['type']; /** @var survey_question $surveyQuestion */ $surveyQuestion = new $ch_type(); -//$surveyQuestion = survey_question::createQuestion($_GET['type']) // The defaults values for the form $formData = []; @@ -143,13 +142,14 @@ if (isset($_GET['question_id']) && !empty($_GET['question_id'])) { } $formData = $surveyQuestion->preSave($formData); + $surveyQuestion->createForm($surveyData, $formData); $surveyQuestion->getForm()->setDefaults($formData); $surveyQuestion->renderForm(); if ($surveyQuestion->getForm()->validate()) { $values = $surveyQuestion->getForm()->getSubmitValues(); - $surveyQuestion->save($surveyData, $values); + $surveyQuestion->save($surveyData, $values, $formData); } Display::display_header($tool_name, 'Survey'); diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index ca95d4114b..e994246523 100755 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -1015,7 +1015,6 @@ class SurveyManager } // Getting the information of the question - $result = Database::query($sql); $row = Database::fetch_array($result, 'ASSOC'); @@ -1041,13 +1040,18 @@ class SurveyManager // Getting the information of the question options $result = Database::query($sqlOption); + $counter = 0; while ($row = Database::fetch_array($result, 'ASSOC')) { /** @todo this should be renamed to options instead of answers */ $return['answers'][] = $row['option_text']; $return['values'][] = $row['value']; + $return['answer_data'][$counter]['data'] = $row['option_text']; + $return['answer_data'][$counter]['iid'] = $row['iid']; + /** @todo this can be done more elegantly (used in reporting) */ $return['answersid'][] = $row['question_option_id']; + $counter++; } return $return; @@ -1119,13 +1123,12 @@ class SurveyManager * * @version January 2007 */ - public static function save_question($survey_data, $form_content, $showMessage = true) + public static function save_question($survey_data, $form_content, $showMessage = true, $dataFromDatabase = []) { $return_message = ''; if (strlen($form_content['question']) > 1) { // Checks length of the question $empty_answer = false; - if ($survey_data['survey_type'] == 1) { if (empty($form_content['choose'])) { @@ -1156,7 +1159,9 @@ class SurveyManager $empty_answer = true; } } + $course_id = api_get_course_int_id(); + if (!$empty_answer) { // Table definitions $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); @@ -1280,7 +1285,7 @@ class SurveyManager } // Storing the options of the question - self::save_question_options($form_content, $survey_data); + self::save_question_options($form_content, $survey_data, $dataFromDatabase); } else { $return_message = 'PleasFillAllAnswer'; } @@ -1536,7 +1541,7 @@ class SurveyManager * * @todo writing the update statement when editing a question */ - public static function save_question_options($form_content, $survey_data) + public static function save_question_options($form_content, $survey_data, $dataFromDatabase = []) { $course_id = api_get_course_int_id(); // A percentage question type has options 1 -> 100 @@ -1551,35 +1556,71 @@ class SurveyManager } // Table definition - $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); + $table = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); // We are editing a question so we first have to remove all the existing options from the database - if (is_numeric($form_content['question_id'])) { - $sql = "DELETE FROM $table_survey_question_option + /*if (is_numeric($form_content['question_id'])) { + $sql = "DELETE FROM $table WHERE c_id = $course_id AND question_id = '".intval($form_content['question_id'])."'"; Database::query($sql); + }*/ + $optionsToDelete = []; + if (isset($dataFromDatabase['answer_data'])) { + foreach ($dataFromDatabase['answer_data'] as $data) { + if (!in_array($data['iid'], $form_content['answersid'])) { + $optionsToDelete[] = $data['iid']; + } + } + } + + if (!empty($optionsToDelete)) { + foreach ($optionsToDelete as $iid) { + $iid = (int) $iid; + $sql = "DELETE FROM $table + WHERE + iid = $iid AND + c_id = $course_id AND + question_id = '".intval($form_content['question_id'])."' + "; + Database::query($sql); + } } $counter = 1; if (isset($form_content['answers']) && is_array($form_content['answers'])) { for ($i = 0; $i < count($form_content['answers']); $i++) { $values = isset($form_content['values']) ? $form_content['values'][$i] : ''; - - $params = [ - 'c_id' => $course_id, - 'question_id' => $form_content['question_id'], - 'survey_id' => $form_content['survey_id'], - 'option_text' => $form_content['answers'][$i], - 'value' => $values, - 'sort' => $counter, - ]; - $insertId = Database::insert($table_survey_question_option, $params); - if ($insertId) { - $sql = "UPDATE $table_survey_question_option - SET question_option_id = $insertId - WHERE iid = $insertId"; - Database::query($sql); - + $answerId = 0; + if (isset($form_content['answersid']) && isset($form_content['answersid'][$i])) { + $answerId = $form_content['answersid'][$i]; + } + if (empty($answerId)) { + $params = [ + 'c_id' => $course_id, + 'question_id' => $form_content['question_id'], + 'survey_id' => $form_content['survey_id'], + 'option_text' => $form_content['answers'][$i], + 'value' => $values, + 'sort' => $counter, + ]; + $insertId = Database::insert($table, $params); + if ($insertId) { + $sql = "UPDATE $table + SET question_option_id = $insertId + WHERE iid = $insertId"; + Database::query($sql); + $counter++; + } + } else { + $params = [ + //'c_id' => $course_id, + //'question_id' => $form_content['question_id'], + //'survey_id' => $form_content['survey_id'], + 'option_text' => $form_content['answers'][$i], + 'value' => $values, + 'sort' => $counter, + ]; + Database::update($table, $params, ['iid = ?' => [$answerId]]); $counter++; } } diff --git a/main/survey/survey_question.php b/main/survey/survey_question.php index 71b543fd54..3eeebeedd9 100644 --- a/main/survey/survey_question.php +++ b/main/survey/survey_question.php @@ -41,8 +41,7 @@ class survey_question "survey_id": "'.$surveyId.'", "question_id": questionId, }; - - $.ajax({ + $.ajax({ type: "GET", url: "'.$url.'", data: params, @@ -328,21 +327,28 @@ class survey_question } $newAnswers = []; + $newAnswersId = []; foreach ($formData['answers'] as $key => &$value) { if ($key > $deleted) { // swap with previous (deleted) option slot $newAnswers[$key - 1] = $formData['answers'][$key]; + $newAnswersId[$key - 1] = $formData['answersid'][$key]; unset($formData['answers'][$key]); + unset($formData['answersid'][$key]); } elseif ($key === $deleted) { // delete option unset($formData['answers'][$deleted]); + unset($formData['answersid'][$deleted]); } else { // keep as is $newAnswers[$key] = $value; + $newAnswersId[$key] = $formData['answersid'][$key]; } } unset($formData['answers']); + unset($formData['answersid']); $formData['answers'] = $newAnswers; + $formData['answersid'] = $newAnswersId; } // Adding an answer @@ -358,6 +364,7 @@ class survey_question foreach ($formData['answers'] as $index => &$data) { if ($index > $counter) { unset($formData['answers'][$index]); + unset($formData['answersid'][$index]); } } } @@ -398,13 +405,13 @@ class survey_question * * @return mixed */ - public function save($surveyData, $formData) + public function save($surveyData, $formData, $dataFromDatabase = []) { // Saving a question if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) { Session::erase('answer_count'); Session::erase('answer_list'); - $message = SurveyManager::save_question($surveyData, $formData); + $message = SurveyManager::save_question($surveyData, $formData, true, $dataFromDatabase); if ($message === 'QuestionAdded' || $message === 'QuestionUpdated') { header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());