Surveys: Fix option ID, update iid if set. BT#17114

pull/3213/head
Julio Montoya 6 years ago
parent 2019d0a444
commit 1872f906de
  1. 6
      main/survey/ch_dropdown.php
  2. 16
      main/survey/ch_multiplechoice.php
  3. 10
      main/survey/ch_multipleresponse.php
  4. 1
      main/survey/ch_percentage.php
  5. 9
      main/survey/ch_score.php
  6. 10
      main/survey/ch_yesno.php
  7. 4
      main/survey/question.php
  8. 89
      main/survey/survey.lib.php
  9. 15
      main/survey/survey_question.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); parent::addRemoveButtons($formData);
} }

@ -27,24 +27,22 @@ class ch_multiplechoice extends survey_question
$config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120']; $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'];
$total = count($formData['answers']); $total = count($formData['answers']);
if (is_array($formData['answers'])) { if (is_array($formData['answers'])) {
foreach ($formData['answers'] as $key => $value) { foreach ($formData['answers'] as $key => $value) {
$this->getForm()->addHtmlEditor('answers['.$key.']', null, false, false, $config); $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) { if ($total > 2) {
$this->getForm()->addButton("delete_answer[$key]", get_lang('Delete'), 'trash', 'danger'); $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); parent::addRemoveButtons($formData);
} }

@ -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); parent::addRemoveButtons($formData);
} }
@ -49,12 +55,11 @@ class ch_multipleresponse extends survey_question
) { ) {
$class = 'checkbox-inline'; $class = 'checkbox-inline';
$labelClass = 'checkbox-inline'; $labelClass = 'checkbox-inline';
if ($questionData['display'] == 'vertical') { if ($questionData['display'] === 'vertical') {
$class = 'checkbox-vertical'; $class = 'checkbox-vertical';
} }
$name = 'question'.$questionData['question_id']; $name = 'question'.$questionData['question_id'];
$form->addCheckBoxGroup( $form->addCheckBoxGroup(
$name, $name,
null, null,
@ -63,7 +68,6 @@ class ch_multipleresponse extends survey_question
); );
$defaults = []; $defaults = [];
if (!empty($answers)) { if (!empty($answers)) {
foreach ($answers as $answer) { foreach ($answers as $answer) {
$defaults[$name.'['.$answer.']'] = true; $defaults[$name.'['.$answer.']'] = true;

@ -21,7 +21,6 @@ class ch_percentage extends survey_question
} }
$name = 'question'.$questionData['question_id']; $name = 'question'.$questionData['question_id'];
$form->addSelect( $form->addSelect(
$name, $name,
null, null,

@ -13,9 +13,7 @@ class ch_score extends survey_question
public function createForm($survey_data, $formData) public function createForm($survey_data, $formData)
{ {
parent::createForm($survey_data, $formData); parent::createForm($survey_data, $formData);
$this->getForm()->addText('maximum_score', get_lang('MaximumScore')); $this->getForm()->addText('maximum_score', get_lang('MaximumScore'));
$config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120']; $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'];
if (is_array($formData['answers'])) { if (is_array($formData['answers'])) {
foreach ($formData['answers'] as $key => $value) { 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); parent::addRemoveButtons($formData);
} }
@ -49,6 +53,7 @@ class ch_score extends survey_question
$name = 'question'.$questionData['question_id'].'['.$key.']'; $name = 'question'.$questionData['question_id'].'['.$key.']';
$form->addHidden('question_id', $questionData['question_id']);
$form->addSelect( $form->addSelect(
$name, $name,
$value, $value,

@ -29,6 +29,7 @@ class ch_yesno extends survey_question
'Width' => '100%', 'Width' => '100%',
'Height' => '120', 'Height' => '120',
]; ];
$this->getForm()->addHtmlEditor( $this->getForm()->addHtmlEditor(
'answers[0]', 'answers[0]',
get_lang('AnswerOptions'), get_lang('AnswerOptions'),
@ -36,6 +37,7 @@ class ch_yesno extends survey_question
false, false,
$config $config
); );
$this->getForm()->addHtmlEditor( $this->getForm()->addHtmlEditor(
'answers[1]', 'answers[1]',
null, null,
@ -43,6 +45,12 @@ class ch_yesno extends survey_question
false, false,
$config $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'])) { if (is_array($questionData['options'])) {
$class = 'radio-inline'; $class = 'radio-inline';
$labelClass = 'radio-inline'; $labelClass = 'radio-inline';
if ($questionData['display'] == 'vertical') { if ($questionData['display'] === 'vertical') {
$class = 'radio-vertical'; $class = 'radio-vertical';
} }

@ -108,7 +108,6 @@ if (!in_array($_GET['type'], $possible_types)) {
$ch_type = 'ch_'.$_GET['type']; $ch_type = 'ch_'.$_GET['type'];
/** @var survey_question $surveyQuestion */ /** @var survey_question $surveyQuestion */
$surveyQuestion = new $ch_type(); $surveyQuestion = new $ch_type();
//$surveyQuestion = survey_question::createQuestion($_GET['type'])
// The defaults values for the form // The defaults values for the form
$formData = []; $formData = [];
@ -143,13 +142,14 @@ if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
} }
$formData = $surveyQuestion->preSave($formData); $formData = $surveyQuestion->preSave($formData);
$surveyQuestion->createForm($surveyData, $formData); $surveyQuestion->createForm($surveyData, $formData);
$surveyQuestion->getForm()->setDefaults($formData); $surveyQuestion->getForm()->setDefaults($formData);
$surveyQuestion->renderForm(); $surveyQuestion->renderForm();
if ($surveyQuestion->getForm()->validate()) { if ($surveyQuestion->getForm()->validate()) {
$values = $surveyQuestion->getForm()->getSubmitValues(); $values = $surveyQuestion->getForm()->getSubmitValues();
$surveyQuestion->save($surveyData, $values); $surveyQuestion->save($surveyData, $values, $formData);
} }
Display::display_header($tool_name, 'Survey'); Display::display_header($tool_name, 'Survey');

@ -1015,7 +1015,6 @@ class SurveyManager
} }
// Getting the information of the question // Getting the information of the question
$result = Database::query($sql); $result = Database::query($sql);
$row = Database::fetch_array($result, 'ASSOC'); $row = Database::fetch_array($result, 'ASSOC');
@ -1041,13 +1040,18 @@ class SurveyManager
// Getting the information of the question options // Getting the information of the question options
$result = Database::query($sqlOption); $result = Database::query($sqlOption);
$counter = 0;
while ($row = Database::fetch_array($result, 'ASSOC')) { while ($row = Database::fetch_array($result, 'ASSOC')) {
/** @todo this should be renamed to options instead of answers */ /** @todo this should be renamed to options instead of answers */
$return['answers'][] = $row['option_text']; $return['answers'][] = $row['option_text'];
$return['values'][] = $row['value']; $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) */ /** @todo this can be done more elegantly (used in reporting) */
$return['answersid'][] = $row['question_option_id']; $return['answersid'][] = $row['question_option_id'];
$counter++;
} }
return $return; return $return;
@ -1119,13 +1123,12 @@ class SurveyManager
* *
* @version January 2007 * @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 = ''; $return_message = '';
if (strlen($form_content['question']) > 1) { if (strlen($form_content['question']) > 1) {
// Checks length of the question // Checks length of the question
$empty_answer = false; $empty_answer = false;
if ($survey_data['survey_type'] == 1) { if ($survey_data['survey_type'] == 1) {
if (empty($form_content['choose'])) { if (empty($form_content['choose'])) {
@ -1156,7 +1159,9 @@ class SurveyManager
$empty_answer = true; $empty_answer = true;
} }
} }
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
if (!$empty_answer) { if (!$empty_answer) {
// Table definitions // Table definitions
$tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
@ -1280,7 +1285,7 @@ class SurveyManager
} }
// Storing the options of the question // Storing the options of the question
self::save_question_options($form_content, $survey_data); self::save_question_options($form_content, $survey_data, $dataFromDatabase);
} else { } else {
$return_message = 'PleasFillAllAnswer'; $return_message = 'PleasFillAllAnswer';
} }
@ -1536,7 +1541,7 @@ class SurveyManager
* *
* @todo writing the update statement when editing a question * @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(); $course_id = api_get_course_int_id();
// A percentage question type has options 1 -> 100 // A percentage question type has options 1 -> 100
@ -1551,35 +1556,71 @@ class SurveyManager
} }
// Table definition // 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 // 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'])) { /*if (is_numeric($form_content['question_id'])) {
$sql = "DELETE FROM $table_survey_question_option $sql = "DELETE FROM $table
WHERE c_id = $course_id AND question_id = '".intval($form_content['question_id'])."'"; WHERE c_id = $course_id AND question_id = '".intval($form_content['question_id'])."'";
Database::query($sql); 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; $counter = 1;
if (isset($form_content['answers']) && is_array($form_content['answers'])) { if (isset($form_content['answers']) && is_array($form_content['answers'])) {
for ($i = 0; $i < count($form_content['answers']); $i++) { for ($i = 0; $i < count($form_content['answers']); $i++) {
$values = isset($form_content['values']) ? $form_content['values'][$i] : ''; $values = isset($form_content['values']) ? $form_content['values'][$i] : '';
$answerId = 0;
$params = [ if (isset($form_content['answersid']) && isset($form_content['answersid'][$i])) {
'c_id' => $course_id, $answerId = $form_content['answersid'][$i];
'question_id' => $form_content['question_id'], }
'survey_id' => $form_content['survey_id'], if (empty($answerId)) {
'option_text' => $form_content['answers'][$i], $params = [
'value' => $values, 'c_id' => $course_id,
'sort' => $counter, 'question_id' => $form_content['question_id'],
]; 'survey_id' => $form_content['survey_id'],
$insertId = Database::insert($table_survey_question_option, $params); 'option_text' => $form_content['answers'][$i],
if ($insertId) { 'value' => $values,
$sql = "UPDATE $table_survey_question_option 'sort' => $counter,
SET question_option_id = $insertId ];
WHERE iid = $insertId"; $insertId = Database::insert($table, $params);
Database::query($sql); 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++; $counter++;
} }
} }

@ -41,8 +41,7 @@ class survey_question
"survey_id": "'.$surveyId.'", "survey_id": "'.$surveyId.'",
"question_id": questionId, "question_id": questionId,
}; };
$.ajax({
$.ajax({
type: "GET", type: "GET",
url: "'.$url.'", url: "'.$url.'",
data: params, data: params,
@ -328,21 +327,28 @@ class survey_question
} }
$newAnswers = []; $newAnswers = [];
$newAnswersId = [];
foreach ($formData['answers'] as $key => &$value) { foreach ($formData['answers'] as $key => &$value) {
if ($key > $deleted) { if ($key > $deleted) {
// swap with previous (deleted) option slot // swap with previous (deleted) option slot
$newAnswers[$key - 1] = $formData['answers'][$key]; $newAnswers[$key - 1] = $formData['answers'][$key];
$newAnswersId[$key - 1] = $formData['answersid'][$key];
unset($formData['answers'][$key]); unset($formData['answers'][$key]);
unset($formData['answersid'][$key]);
} elseif ($key === $deleted) { } elseif ($key === $deleted) {
// delete option // delete option
unset($formData['answers'][$deleted]); unset($formData['answers'][$deleted]);
unset($formData['answersid'][$deleted]);
} else { } else {
// keep as is // keep as is
$newAnswers[$key] = $value; $newAnswers[$key] = $value;
$newAnswersId[$key] = $formData['answersid'][$key];
} }
} }
unset($formData['answers']); unset($formData['answers']);
unset($formData['answersid']);
$formData['answers'] = $newAnswers; $formData['answers'] = $newAnswers;
$formData['answersid'] = $newAnswersId;
} }
// Adding an answer // Adding an answer
@ -358,6 +364,7 @@ class survey_question
foreach ($formData['answers'] as $index => &$data) { foreach ($formData['answers'] as $index => &$data) {
if ($index > $counter) { if ($index > $counter) {
unset($formData['answers'][$index]); unset($formData['answers'][$index]);
unset($formData['answersid'][$index]);
} }
} }
} }
@ -398,13 +405,13 @@ class survey_question
* *
* @return mixed * @return mixed
*/ */
public function save($surveyData, $formData) public function save($surveyData, $formData, $dataFromDatabase = [])
{ {
// Saving a question // Saving a question
if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) { if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) {
Session::erase('answer_count'); Session::erase('answer_count');
Session::erase('answer_list'); Session::erase('answer_list');
$message = SurveyManager::save_question($surveyData, $formData); $message = SurveyManager::save_question($surveyData, $formData, true, $dataFromDatabase);
if ($message === 'QuestionAdded' || $message === 'QuestionUpdated') { 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()); header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());

Loading…
Cancel
Save