// Renaming - Other Specify Categories as Other
includeWeb('QScript Selection Functions');
includeWeb('QScript Questionnaire Functions');
includeWeb('JavaScript Array Functions');
includeWeb('QScript Functions to Generate Outputs');
relabelOtherSpecifyInSelection();
function relabelOtherSpecifyInSelection() {
// On the web just take from what is selected.
const web_mode = inDisplayr();
let selected_questions;
let questions_without_other = []; // Selected questions that don't contain an Other
let check_questions = []; // Questions which have more than one Other identified, which the user should check
if (web_mode) {
const displayr_fail_message = "Select a table, or one or more variable sets under Data Sources and run this option again."
const user_selections = getAllUserSelections();
selected_questions = user_selections.selected_questions.concat(user_selections.questions_in_rows_of_selected_tables);
if (selected_questions.length == 0) {
log(displayr_fail_message);
return false;
}
let sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, dataReductionHasOther);
selected_questions = sorted_selection.applicable;
questions_without_other = sorted_selection.notApplicable;
if (selected_questions.length == 0) {
log("None of the selected variable sets contain 'Other/Specify' categories to remove.")
return false;
}
} else {
let selected_datafiles = dataFileSelection();
let candidate_questions = getAllQuestionsByTypes(selected_datafiles,
["Pick One", "Pick One - Multi", "Pick Any", "Pick Any - Grid"]).filter(dataReductionHasOther);
if (candidate_questions.length == 0) {
log("There do not appear to be any questions with 'Other/Specify' categories in this project.");
return false;
}
selected_questions = selectManyQuestions("The following questions have 'Other/Specify' categories. Please select questions to relabel: ", candidate_questions).questions;
if (selected_questions.length == 0) {
log('No questions selected.');
return false;
}
}
let other_questions = [];
selected_questions.forEach(function (question) {
relabelOtherCategoryInDataReduction(question, other_questions, check_questions);
});
// If running on the web, create a log
if (web_mode) {
if (check_questions.length > 0)
log("Some variable sets contain more than one 'Other/Specify' options and should be updated manually:\r\n"
+ check_questions.map(function (q) { return q.name; }).join("\r\n") + "\r\n");
if (questions_without_other.length > 0)
log("Could not modify because no \"Other\" categories were found:\r\n"
+ questions_without_other.map(function (q) { return q.name; }).join("\r\n"));
} else {
// Otherwise generate tables and a report
if (other_questions.length > 0) {
generateGroupOfSummaryTables("Questions with 'Other/Specify' categries relabeled", other_questions);
log("Questions whose 'Other/Specify' categories have been relabeled have been added to the folder: Questions with 'Other/Specify' categries relabeled\r\n");
}
if (check_questions.length > 0) {
generateGroupOfSummaryTables("Questions which could not be relabeled", check_questions);
log("Some questions look like they contain more than one 'Other/Specify' category. These have not been relabeled. These questions have been added to the folder: Questions with more than one 'Other/Specify' label");
}
}
return true;
}
function dataReductionHasOther(question) {
if (["Experiment", "Ranking", "Text", "Text - Multi", "Date", "Number"].indexOf(question.questionType) > -1)
return false;
let data_reduction = question.dataReduction;
let labels = data_reduction.rowLabels;
let col_labels = data_reduction.columnLabels;
if (col_labels != null)
labels = labels.concat(col_labels);
return labels.some(isOther);
}
function relabelOtherCategoryInDataReduction(question, other_questions, questions_with_duplicate_codes) {
let data_reduction = question.dataReduction;
let row_labels = data_reduction.rowLabels;
let other_rows = row_labels.filter(isOther);
let other_cols = [];
let col_labels = data_reduction.columnLabels;
if (col_labels != null) {
other_cols = col_labels.filter(isOther);
}
// Can't rename duplicates, labels must be unique
// Also, most normal questions will not have more
// than one Other row or more than one Other column
// so not safe to rename.
if (other_rows.length > 1
|| other_cols.length > 1
|| arrayHasDuplicateElements(other_rows.concat(other_cols))) {
questions_with_duplicate_codes.push(question);
return;
}
other_rows.forEach(function (label) {
data_reduction.rename(label, "Other");
});
other_cols.forEach(function (label) {
data_reduction.rename(label, "Other");
});
other_questions.push(question);
}