Add configuration setting to allow required survey questions - refs BT#13203

Requires DB change:
ALTER TABLE c_survey_question ADD is_required TINYINT(1) DEFAULT 0 NOT NULL;

Applies to yesno/multiplechoice question type
pull/2487/head
Angel Fernando Quiroz Campos 7 years ago
parent 48f6056e68
commit 333a822b5b
  1. 5
      main/install/configuration.dist.php
  2. 7
      main/survey/ch_yesno.php
  3. 9
      main/survey/fillsurvey.php
  4. 8
      main/survey/survey.lib.php
  5. 5
      main/survey/survey_question.php

@ -460,6 +460,11 @@ INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, v
VALUES (12, 13, 'is_mandatory', 'IsMandatory', 1, 1, NOW()); VALUES (12, 13, 'is_mandatory', 'IsMandatory', 1, 1, NOW());
*/ */
//$_configuration['allow_mandatory_survey'] = false; //$_configuration['allow_mandatory_survey'] = false;
// Allow required survey questions. Applies to yesno/multiplechoice question type. Requires DB change:
/*
ALTER TABLE c_survey_question ADD is_required TINYINT(1) DEFAULT 0 NOT NULL;
*/
//$_configuration['allow_required_survey_questions'] = false;
// ------ // ------
// Allow career diagram, requires a DB change: // Allow career diagram, requires a DB change:

@ -60,12 +60,17 @@ class ch_yesno extends survey_question
} }
$name = 'question'.$questionData['question_id']; $name = 'question'.$questionData['question_id'];
$radioAttributes = ['radio-class' => $class, 'label-class' => $class];
if ($questionData['is_required']) {
$radioAttributes['required'] = 'required';
}
$form->addRadio( $form->addRadio(
$name, $name,
null, null,
$questionData['options'], $questionData['options'],
['radio-class' => $class, 'label-class' => $class] $radioAttributes
); );
if (!empty($answers)) { if (!empty($answers)) {

@ -28,6 +28,8 @@ $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION
$table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION); $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
$table_user = Database::get_main_table(TABLE_MAIN_USER); $table_user = Database::get_main_table(TABLE_MAIN_USER);
$allowRequiredSurveyQuestions = api_get_configuration_value('allow_required_survey_questions');
// Check if user is anonymous or not // Check if user is anonymous or not
if (api_is_anonymous(api_get_user_id(), true)) { if (api_is_anonymous(api_get_user_id(), true)) {
$isAnonymous = true; $isAnonymous = true;
@ -192,6 +194,8 @@ $survey_data['survey_id'] = $survey_invitation['survey_id'];
// Storing the answers // Storing the answers
if (count($_POST) > 0) { if (count($_POST) > 0) {
if ($survey_data['survey_type'] === '0') { if ($survey_data['survey_type'] === '0') {
$types = [];
$required = [];
// Getting all the types of the question // Getting all the types of the question
// (because of the special treatment of the score question type // (because of the special treatment of the score question type
$sql = "SELECT * FROM $table_survey_question $sql = "SELECT * FROM $table_survey_question
@ -202,6 +206,7 @@ if (count($_POST) > 0) {
while ($row = Database::fetch_array($result, 'ASSOC')) { while ($row = Database::fetch_array($result, 'ASSOC')) {
$types[$row['question_id']] = $row['type']; $types[$row['question_id']] = $row['type'];
$required[$row['question_id']] = $allowRequiredSurveyQuestions && $row['is_required'];
} }
// Looping through all the post values // Looping through all the post values
@ -700,6 +705,7 @@ if (isset($_GET['show']) || isset($_POST['personality'])) {
survey_question_option.question_option_id, survey_question_option.question_option_id,
survey_question_option.option_text, survey_question_option.option_text,
survey_question_option.sort as option_sort survey_question_option.sort as option_sort
".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
FROM $table_survey_question survey_question FROM $table_survey_question survey_question
LEFT JOIN $table_survey_question_option survey_question_option LEFT JOIN $table_survey_question_option survey_question_option
ON survey_question.question_id = survey_question_option.question_id AND ON survey_question.question_id = survey_question_option.question_id AND
@ -727,6 +733,7 @@ if (isset($_GET['show']) || isset($_POST['personality'])) {
$questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text']; $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
$questions[$row['sort']]['maximum_score'] = $row['max_value']; $questions[$row['sort']]['maximum_score'] = $row['max_value'];
$questions[$row['sort']]['sort'] = $row['sort']; $questions[$row['sort']]['sort'] = $row['sort'];
$questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
} else { } else {
// If the type is a pagebreak we are finished loading the questions for this page // If the type is a pagebreak we are finished loading the questions for this page
break; break;
@ -1113,6 +1120,7 @@ if (isset($_GET['show']) || isset($_POST['personality'])) {
survey_question_option.question_option_id, survey_question_option.question_option_id,
survey_question_option.option_text, survey_question_option.option_text,
survey_question_option.sort as option_sort survey_question_option.sort as option_sort
".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
FROM $table_survey_question survey_question FROM $table_survey_question survey_question
LEFT JOIN $table_survey_question_option survey_question_option LEFT JOIN $table_survey_question_option survey_question_option
ON survey_question.question_id = survey_question_option.question_id AND ON survey_question.question_id = survey_question_option.question_id AND
@ -1140,6 +1148,7 @@ if (isset($_GET['show']) || isset($_POST['personality'])) {
$questions[$row['sort']]['type'] = $row['type']; $questions[$row['sort']]['type'] = $row['type'];
$questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text']; $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
$questions[$row['sort']]['maximum_score'] = $row['max_value']; $questions[$row['sort']]['maximum_score'] = $row['max_value'];
$questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
// Personality params // Personality params
$questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1']; $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
$questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2']; $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];

@ -1122,6 +1122,10 @@ class SurveyManager
'max_value' => $maxScore, 'max_value' => $maxScore,
]; ];
if (isset($form_content['is_required'])) {
$params['is_required'] = true;
}
$params = array_merge($params, $extraParams); $params = array_merge($params, $extraParams);
$question_id = Database::insert($tbl_survey_question, $params); $question_id = Database::insert($tbl_survey_question, $params);
if ($question_id) { if ($question_id) {
@ -1157,6 +1161,10 @@ class SurveyManager
'display' => $form_content['horizontalvertical'], 'display' => $form_content['horizontalvertical'],
]; ];
if (isset($form_content['is_required'])) {
$params['is_required'] = true;
}
$params = array_merge($params, $extraParams); $params = array_merge($params, $extraParams);
Database::update( Database::update(

@ -75,6 +75,11 @@ class survey_question
$config $config
); );
if (api_get_configuration_value('allow_required_survey_questions') &&
in_array($_GET['type'], ['yesno', 'multiplechoice'])) {
$form->addCheckBox('is_required', get_lang('IsRequired'), get_lang('Yes'));
}
// When survey type = 1?? // When survey type = 1??
if ($surveyData['survey_type'] == 1) { if ($surveyData['survey_type'] == 1) {
$table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);

Loading…
Cancel
Save