diff --git a/main/inc/ajax/survey.ajax.php b/main/inc/ajax/survey.ajax.php index fc237ad006..19fe3bb848 100644 --- a/main/inc/ajax/survey.ajax.php +++ b/main/inc/ajax/survey.ajax.php @@ -3,19 +3,32 @@ require_once __DIR__.'/../global.inc.php'; -$action = isset($_GET['a']) ? $_GET['a'] : null; - $current_user_id = api_get_user_id(); $courseId = api_get_course_int_id(); +$action = isset($_GET['a']) ? $_GET['a'] : null; +$surveyId = isset($_REQUEST['survey_id']) ? $_REQUEST['survey_id'] : 0; +$questionId = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : 0; + switch ($action) { + case 'load_question_options': + if (!api_is_allowed_to_edit(false, true)) { + exit; + } + $question = SurveyManager::get_question($questionId); + if (!empty($question) && !empty($question['answer_data'])) { + $optionList = []; + foreach ($question['answer_data'] as $answer) { + $optionList[$answer['iid']] = strip_tags($answer['data']); + } + echo json_encode($optionList); + } + break; case 'save_question': if (api_is_anonymous()) { echo ''; break; } - $surveyId = isset($_GET['survey_id']) ? $_GET['survey_id'] : null; - $questionId = isset($_GET['question_id']) ? $_GET['question_id'] : null; $status = isset($_GET['status']) ? (int) $_GET['status'] : null; $userId = api_get_user_id(); diff --git a/main/survey/ch_yesno.php b/main/survey/ch_yesno.php index 9ca9e63a86..2ba84f3859 100644 --- a/main/survey/ch_yesno.php +++ b/main/survey/ch_yesno.php @@ -1,4 +1,5 @@ get_lang('Vertical'), ]; $this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options); - $formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal'; $this->getForm()->setDefaults($formData); diff --git a/main/survey/fillsurvey.php b/main/survey/fillsurvey.php index 4eaaf11ce0..5adcf8866f 100755 --- a/main/survey/fillsurvey.php +++ b/main/survey/fillsurvey.php @@ -491,8 +491,8 @@ if ($survey_data['form_fields'] != '' && } $form->applyFilter('official_code', 'stripslashes'); $form->applyFilter('official_code', 'trim'); - if (api_get_setting('registration', 'officialcode') == 'true' && - api_get_setting('profile', 'officialcode') == 'true' + if (api_get_setting('registration', 'officialcode') === 'true' && + api_get_setting('profile', 'officialcode') === 'true' ) { $form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required'); } @@ -507,7 +507,7 @@ if ($survey_data['form_fields'] != '' && } $form->applyFilter('email', 'stripslashes'); $form->applyFilter('email', 'trim'); - if (api_get_setting('registration', 'email') == 'true') { + if (api_get_setting('registration', 'email') === 'true') { $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required'); } $form->addRule('email', get_lang('EmailWrong'), 'email'); @@ -521,7 +521,7 @@ if ($survey_data['form_fields'] != '' && } $form->applyFilter('phone', 'stripslashes'); $form->applyFilter('phone', 'trim'); - if (api_get_setting('profile', 'phone') == 'true') { + if (api_get_setting('profile', 'phone') === 'true') { $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required'); } } @@ -532,7 +532,7 @@ if ($survey_data['form_fields'] != '' && if (api_get_setting('profile', 'language') !== 'true') { $form->freeze('language'); } - if (api_get_setting('profile', 'language') == 'true') { + if (api_get_setting('profile', 'language') === 'true') { $form->addRule('language', get_lang('ThisFieldIsRequired'), 'required'); } } @@ -556,6 +556,7 @@ if ($survey_data['form_fields'] != '' && $htmlHeadXtra[] = ''; $htmlHeadXtra[] = ch_selectivedisplay::getJs(); +$htmlHeadXtra[] = survey_question::getJs(); Display::display_header(get_lang('ToolSurvey')); @@ -685,8 +686,13 @@ if ((isset($_GET['show']) && $_GET['show'] != '') || // As long as there is no pagebreak fount we keep adding questions to the page $questions_displayed = []; $counter = 0; - //$paged_questions = Session::read('paged_questions'); $paged_questions = []; + + $select = ''; + if (true === api_get_configuration_value('survey_question_dependency')) { + $select = ' survey_question.parent_id, survey_question.parent_option_id, '; + } + // If non-conditional survey if ($survey_data['survey_type'] == '0') { if (empty($paged_questions)) { @@ -699,13 +705,13 @@ if ((isset($_GET['show']) && $_GET['show'] != '') || $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { if ($survey_data['one_question_per_page'] == 1) { - if ($row['type'] != 'pagebreak') { + if ($row['type'] !== 'pagebreak') { $paged_questions[$counter][] = $row['question_id']; $counter++; continue; } } else { - if ($row['type'] == 'pagebreak') { + if ($row['type'] === 'pagebreak') { $counter++; } else { $paged_questions[$counter][] = $row['question_id']; @@ -739,6 +745,7 @@ if ((isset($_GET['show']) && $_GET['show'] != '') || survey_question.max_value, survey_question_option.question_option_id, survey_question_option.option_text, + $select survey_question_option.sort as option_sort FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option @@ -767,6 +774,7 @@ if ((isset($_GET['show']) && $_GET['show'] != '') || survey_question.max_value, survey_question_option.question_option_id, survey_question_option.option_text, + $select survey_question_option.sort as option_sort ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')." FROM $table_survey_question survey_question @@ -788,16 +796,19 @@ if ((isset($_GET['show']) && $_GET['show'] != '') || $questions = []; while ($row = Database :: fetch_array($result, 'ASSOC')) { // If the type is not a pagebreak we store it in the $questions array - if ($row['type'] != 'pagebreak') { - $questions[$row['sort']]['question_id'] = $row['question_id']; - $questions[$row['sort']]['survey_id'] = $row['survey_id']; - $questions[$row['sort']]['survey_question'] = $row['survey_question']; - $questions[$row['sort']]['display'] = $row['display']; - $questions[$row['sort']]['type'] = $row['type']; - $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text']; - $questions[$row['sort']]['maximum_score'] = $row['max_value']; - $questions[$row['sort']]['sort'] = $row['sort']; - $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required']; + if ($row['type'] !== 'pagebreak') { + $sort = $row['sort']; + $questions[$sort]['question_id'] = $row['question_id']; + $questions[$sort]['survey_id'] = $row['survey_id']; + $questions[$sort]['survey_question'] = $row['survey_question']; + $questions[$sort]['display'] = $row['display']; + $questions[$sort]['type'] = $row['type']; + $questions[$sort]['options'][$row['question_option_id']] = $row['option_text']; + $questions[$sort]['maximum_score'] = $row['max_value']; + $questions[$sort]['sort'] = $sort; + $questions[$sort]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required']; + $questions[$sort]['parent_id'] = isset($row['parent_id']) ? $row['parent_id'] : 0; + $questions[$sort]['parent_option_id'] = isset($row['parent_option_id']) ? $row['parent_option_id'] : 0; } $counter++; } @@ -1182,7 +1193,7 @@ if ((isset($_GET['show']) && $_GET['show'] != '') || $questions = []; while ($row = Database :: fetch_array($result, 'ASSOC')) { // If the type is not a pagebreak we store it in the $questions array - if ($row['type'] != 'pagebreak') { + if ($row['type'] !== 'pagebreak') { $questions[$row['sort']]['question_id'] = $row['question_id']; $questions[$row['sort']]['survey_id'] = $row['survey_id']; $questions[$row['sort']]['survey_question'] = $row['survey_question']; @@ -1272,12 +1283,29 @@ if (isset($questions) && is_array($questions)) { $questionCounter = $before + 1; } + $form->addHtml('
'); + $js = ''; foreach ($questions as $key => &$question) { $ch_type = 'ch_'.$question['type']; $questionNumber = $questionCounter; $display = new $ch_type(); + $parent = $question['parent_id']; + $parentClass = ''; + + if (!empty($parent)) { + $parentClass = ' with_parent with_parent_'.$question['question_id']; + $parents = survey_question::getParents($question['question_id']); + if (!empty($parents)) { + foreach($parents as $parentId) { + $parentClass .= ' with_parent_only_hide_'.$parentId; + } + } + } + + $js .= survey_question::getQuestionJs($question); + // @todo move this in a function. - $form->addHtml('
'); + $form->addHtml('
'); $form->addHtml('
'.$questionNumber.'.
'); $form->addHtml('
'.Security::remove_XSS($question['survey_question']).'
'); @@ -1309,6 +1337,8 @@ if (isset($questions) && is_array($questions)) { $form->addHtml('
'); $questionCounter++; } + + $form->addHtml($js); } $form->addHtml('
'); diff --git a/main/survey/preview.php b/main/survey/preview.php index ddaddb7c4d..0570044767 100755 --- a/main/survey/preview.php +++ b/main/survey/preview.php @@ -47,7 +47,7 @@ $interbreadcrumb[] = [ $htmlHeadXtra[] = ''; $htmlHeadXtra[] = ch_selectivedisplay::getJs(); - +$htmlHeadXtra[] = survey_question::getJs(); $show = 0; Display::display_header(get_lang('SurveyPreview')); @@ -111,6 +111,10 @@ if (isset($_GET['show'])) { } if (array_key_exists($_GET['show'], $paged_questions)) { + $select = ''; + if (true === api_get_configuration_value('survey_question_dependency')) { + $select = ' survey_question.parent_id, survey_question.parent_option_id, '; + } $sql = "SELECT survey_question.question_id, survey_question.survey_id, @@ -121,6 +125,7 @@ if (isset($_GET['show'])) { survey_question.max_value, survey_question_option.question_option_id, survey_question_option.option_text, + $select survey_question_option.sort as option_sort ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')." FROM $table_survey_question survey_question @@ -138,7 +143,7 @@ if (isset($_GET['show'])) { $result = Database::query($sql); while ($row = Database::fetch_array($result)) { // If the type is not a pagebreak we store it in the $questions array - if ($row['type'] != 'pagebreak') { + if ($row['type'] !== 'pagebreak') { $sort = $row['sort']; $questions[$sort]['question_id'] = $row['question_id']; $questions[$sort]['survey_id'] = $row['survey_id']; @@ -147,6 +152,8 @@ if (isset($_GET['show'])) { $questions[$sort]['type'] = $row['type']; $questions[$sort]['options'][$row['question_option_id']] = $row['option_text']; $questions[$sort]['maximum_score'] = $row['max_value']; + $questions[$sort]['parent_id'] = isset($row['parent_id']) ? $row['parent_id'] : 0; + $questions[$sort]['parent_option_id'] = isset($row['parent_option_id']) ? $row['parent_option_id'] : 0; $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required']; } } @@ -183,16 +190,34 @@ if (is_array($questions) && count($questions) > 0) { } $counter = $before + 1; } + + $js = ''; foreach ($questions as $key => &$question) { $ch_type = 'ch_'.$question['type']; $display = survey_question::createQuestion($question['type']); - $form->addHtml('
'); + $parent = $question['parent_id']; + $parentClass = ''; + + if (!empty($parent)) { + $parentClass = ' with_parent with_parent_'.$question['question_id']; + $parents = survey_question::getParents($question['question_id']); + if (!empty($parents)) { + foreach($parents as $parentId) { + $parentClass .= ' with_parent_only_hide_'.$parentId; + } + } + } + + $js .= survey_question::getQuestionJs($question); + + $form->addHtml('
'); $form->addHtml('
'.$counter.'.
'); $form->addHtml('
'.Security::remove_XSS($question['survey_question']).'
'); $display->render($form, $question); $form->addHtml('
'); $counter++; } + $form->addHtml($js); } $form->addHtml('
'); diff --git a/main/survey/question.php b/main/survey/question.php index 9e8fb1a20e..f4bd1eed11 100755 --- a/main/survey/question.php +++ b/main/survey/question.php @@ -141,7 +141,6 @@ if (isset($_GET['question_id']) && !empty($_GET['question_id'])) { } $formData = $surveyQuestion->preSave($formData); - $surveyQuestion->createForm($surveyData, $formData); $surveyQuestion->getForm()->setDefaults($formData); $surveyQuestion->renderForm(); diff --git a/main/survey/survey.lib.php b/main/survey/survey.lib.php index aa42682352..18c4e6921f 100755 --- a/main/survey/survey.lib.php +++ b/main/survey/survey.lib.php @@ -993,8 +993,12 @@ class SurveyManager $course_id = api_get_course_int_id(); $question_id = (int) $question_id; + if (empty($question_id)) { + return []; + } + $sql = "SELECT * FROM $tbl_survey_question - WHERE c_id = $course_id AND question_id='".$question_id."' + WHERE c_id = $course_id AND question_id = $question_id ORDER BY `sort` "; $sqlOption = " SELECT * FROM $table_survey_question_option @@ -1006,10 +1010,10 @@ class SurveyManager $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); $sql = "SELECT * FROM $tbl_survey_question - WHERE question_id='".$question_id."' + WHERE question_id = $question_id ORDER BY `sort` "; $sqlOption = "SELECT * FROM $table_survey_question_option - WHERE question_id='".$question_id."' + WHERE question_id = $question_id ORDER BY `sort` "; } @@ -1018,6 +1022,8 @@ class SurveyManager $row = Database::fetch_array($result, 'ASSOC'); $return['survey_id'] = $row['survey_id']; + $return['parent_id'] = isset($row['parent_id']) ? $row['parent_id'] : 0; + $return['parent_option_id'] = isset($row['parent_option_id']) ? $row['parent_option_id'] : 0; $return['question_id'] = $row['question_id']; $return['type'] = $row['type']; $return['question'] = $row['survey_question']; @@ -1044,10 +1050,8 @@ class SurveyManager /** @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++; @@ -1163,9 +1167,10 @@ class SurveyManager if (!$empty_answer) { // Table definitions $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); + $surveyId = (int) $form_content['survey_id']; // Getting all the information of the survey - $survey_data = self::get_survey($form_content['survey_id']); + $survey_data = self::get_survey($surveyId); // Storing the question in the shared database if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) { @@ -1178,7 +1183,7 @@ class SurveyManager // Finding the max sort order of the questions in the given survey $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question - WHERE c_id = $course_id AND survey_id='".intval($form_content['survey_id'])."'"; + WHERE c_id = $course_id AND survey_id = $surveyId "; $result = Database::query($sql); $row = Database::fetch_array($result, 'ASSOC'); $max_sort = $row['max_sort']; @@ -1200,7 +1205,7 @@ class SurveyManager $params = [ 'c_id' => $course_id, - 'survey_id' => $form_content['survey_id'], + 'survey_id' => $surveyId, 'survey_question' => $form_content['question'], 'survey_question_comment' => $questionComment, 'type' => $form_content['type'], @@ -1210,6 +1215,19 @@ class SurveyManager 'max_value' => $maxScore, ]; + if (api_get_configuration_value('survey_question_dependency')) { + $params['parent_id'] = 0; + $params['parent_option_id'] = 0; + if (isset($form_content['parent_id']) && + isset($form_content['parent_option_id']) && + !empty($form_content['parent_id']) && + !empty($form_content['parent_option_id']) + ) { + $params['parent_id'] = $form_content['parent_id']; + $params['parent_option_id'] = $form_content['parent_option_id']; + } + } + if (api_get_configuration_value('allow_required_survey_questions')) { $params['is_required'] = isset($form_content['is_required']); } @@ -1255,6 +1273,19 @@ class SurveyManager $params['is_required'] = isset($form_content['is_required']); } + if (api_get_configuration_value('survey_question_dependency')) { + $params['parent_id'] = 0; + $params['parent_option_id'] = 0; + if (isset($form_content['parent_id']) && + isset($form_content['parent_option_id']) && + !empty($form_content['parent_id']) && + !empty($form_content['parent_option_id']) + ) { + $params['parent_id'] = $form_content['parent_id']; + $params['parent_option_id'] = $form_content['parent_option_id']; + } + } + $params = array_merge($params, $extraParams); Database::update( $tbl_survey_question, diff --git a/main/survey/survey_question.php b/main/survey/survey_question.php index b0030a8830..379f646689 100644 --- a/main/survey/survey_question.php +++ b/main/survey/survey_question.php @@ -16,13 +16,27 @@ class survey_question /** * @param array $surveyData */ - public function addParentMenu(FormValidator $form, $surveyData) + public function addParentMenu($formData, FormValidator $form, $surveyData) { $surveyId = $surveyData['survey_id']; + $questionId = isset($formData['question_id']) ? $formData['question_id'] : 0; + $parentId = isset($formData['parent_id']) ? $formData['parent_id'] : 0; + $optionId = isset($formData['parent_option_id']) ? $formData['parent_option_id'] : 0; $questions = SurveyManager::get_questions($surveyId); - $options = []; + $newQuestionList = []; foreach ($questions as $question) { + $newQuestionList[$question['sort']] = $question; + } + ksort($newQuestionList); + + $options = []; + foreach ($newQuestionList as $question) { + if (!empty($questionId)) { + if ($question['question_id'] == $questionId) { + break; + } + } $options[$question['question_id']] = strip_tags($question['question']); } $form->addSelect( @@ -31,32 +45,52 @@ class survey_question $options, ['id' => 'parent_id', 'placeholder' => get_lang('SelectAnOption')] ); - $url = api_get_path(WEB_AJAX_PATH).'survey.ajax.php?'.api_get_cidreq(); + $url = api_get_path(WEB_AJAX_PATH). + 'survey.ajax.php?'.api_get_cidreq().'&a=load_question_options&survey_id='.$surveyId; $form->addHtml(' '); - $form->addHtml('
'); - $form->addHidden('option_id', 0); + + $style = 'display:none'; + $options = [ ]; + if (!empty($optionId) && !empty($parentId)) { + $parentData = SurveyManager::get_question($parentId); + $style = ''; + foreach ($parentData['answer_data'] as $answer) { + $options[$answer['iid']] = strip_tags($answer['data']); + } + } + + $form->addHtml('
'); + $form->addSelect( + 'parent_option_id', + get_lang('Option'), + $options, + ['id' => 'parent_option_id', 'disable_js' => true] + ); + $form->addHtml('
'); } /** @@ -118,6 +152,7 @@ class survey_question } $questionComment = ''; + $allowParent = false; switch ($type) { case 'open': $toolName = get_lang('Open'); @@ -125,12 +160,15 @@ class survey_question break; case 'yesno': $toolName = get_lang('YesNo'); + $allowParent = true; break; case 'multiplechoice': $toolName = get_lang('UniqueSelect'); + $allowParent = true; break; case 'multipleresponse': $toolName = get_lang('MultipleResponse'); + $allowParent = true; break; case 'selectivedisplay': $toolName = get_lang('SurveyQuestionSelectiveDisplay'); @@ -138,12 +176,15 @@ class survey_question break; case 'multiplechoiceother': $toolName = get_lang('SurveyMultipleAnswerWithOther'); - //$questionComment = get_lang('SurveyQuestionSelectiveDisplayComment'); break; default: $toolName = get_lang(api_ucfirst($type)); } + if (false === api_get_configuration_value('survey_question_dependency')) { + $allowParent = false; + } + $icon = Display::return_icon( SurveyManager::icon_question($type), $toolName, @@ -182,6 +223,10 @@ class survey_question $form->addCheckBox('is_required', get_lang('IsMandatory'), get_lang('Yes')); } + if ($allowParent) { + $this->addParentMenu($formData, $form, $surveyData); + } + if ($surveyData['survey_type'] == 1) { $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); $sql = 'SELECT id, name FROM '.$table_survey_question_group.' @@ -477,6 +522,90 @@ class survey_question ); } + public static function getJs() + { + return ' + + '; + } + + public static function getParents($questionId, $list = []) + { + $courseId = api_get_course_int_id(); + $questionId = (int) $questionId; + + $table = Database::get_course_table(TABLE_SURVEY_QUESTION); + $sql = "SELECT parent_id FROM $table + WHERE c_id = $courseId AND question_id = $questionId "; + $result = Database::query($sql); + $row = Database::fetch_array($result, 'ASSOC'); + if ($row && !empty($row['parent_id'])) { + $list[] = $row['parent_id']; + $list = self::getParents($row['parent_id'], $list); + } + + return $list; + } + + public static function getQuestionJs($question) + { + $list = self::getDependecy($question); + if (empty($list)) { + return ''; + } + + $js = ''; + $questionId = $question['question_id']; + $newList = []; + foreach ($list as $child) { + $childQuestionId = $child['question_id']; + $optionId = $child['parent_option_id']; + $newList[$optionId] = $childQuestionId; + } + + $js .= ' + '; + + return $js; + } + + public static function getDependecy($question) + { + $table = Database::get_course_table(TABLE_SURVEY_QUESTION); + $questionId = $question['question_id']; + $courseId = api_get_course_int_id(); + + // Getting the information of the question + $sql = "SELECT * FROM $table + WHERE c_id = $courseId AND parent_id = $questionId "; + $result = Database::query($sql); + $row = Database::store_result($result, 'ASSOC'); + + return $row; + } + /** * @param array $questionData * @param array $answers