User:Proteins/studyquestions.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Proteins/studyquestions. |
//<pre>
// Script to pose study questions on a given topic
//
// To use this script, add "importScript('User:Proteins/studyquestions.js');" to your monobook.js subpage
// under your user page, as you can see at User:Proteins/monobook.js
//******************
// The Main Function
//******************
function poseStudyQuestions() {
var alert_string = "";
var backup_string = "";
var summary_string = "";
var question_string = "";
var allow_questions_to_be_repeated = true;
var body_content;
var span_nodes;
var temp_span_node;
var num_span_nodes = 0;
var span_node_index = 0;
var random_index = 0;
var max_random_index = 0;
var question_index = 0;
var question_counter = 0;
var study_question_index = 0;
var num_study_questions = 0;
var num_questions_posed = 0;
var num_questions_desired = 0;
var num_questions_selected = 0;
var num_questions_remaining = 0;
var num_answers = 0;
var answer_index = 0;
var answer_counter = 0;
var num_answers_selected = 0;
var num_answers_remaining = 0;
var num_answers_printed_with_question = 5;
var num_correct_answers = 0;
var num_incorrect_answers = 0;
var percentage_of_correct_answers = 0;
var temp_study_question;
var study_question_text = "";
var answer_list = new Array();
var study_question_list = new Array();
var study_question_has_been_posed = new Array();
var selected_study_question_indices = new Array();
var temp_answer;
var correct_answer_node;
var user_answer_index = 0;
var printed_answer_index = 0;
var correct_answer_index = 0;
var correct_printed_answer_index = 0;
var selected_answer_indices = new Array();
var answer_has_been_chosen_already = new Array();
// Get the bodyContent node
body_content = document.getElementById('bodyContent');
if (!body_content) {
error_string = "ERROR: There is no bodyContent node in this article.";
window.alert(error_string);
return;
}
span_nodes = body_content.getElementsByTagName("SPAN");
if (!span_nodes) {
error_string = "ERROR: This page has no SPAN nodes.\n";
window.alert(error_string);
return;
}
num_span_nodes = span_nodes.length;
if (num_span_nodes < 1) {
error_string = "ERROR: This page has zero SPAN nodes.\n";
window.alert(error_string);
return;
}
//**********************************************
// Count the number of study questions on a page
//**********************************************
for (span_node_index=0; span_node_index<num_span_nodes; span_node_index++) {
temp_span_node = span_nodes[span_node_index];
if (!temp_span_node) { continue; }
if (temp_span_node.className.match(/study_question/)) {
num_study_questions++;
study_question_list.push(temp_span_node);
}
} // closes loop over the SPAN nodes in the main article
if (num_study_questions < 1) {
error_string = "No study questions were found on this page.";
window.alert(error_string);
return;
} else if (num_study_questions == 1) {
alert_string = "This page has one type of study question.\n\n";
backup_string = "This page has only one type of study question.\n\n";
// window.alert(alert_string);
} else {
alert_string = "This page has " + num_study_questions + " types of study questions.\n\n";
backup_string = "This page has only " + num_study_questions + " types of study questions.\n\n";
// window.alert(alert_string);
}
// Ask the reader how many study questions they'd like to try
num_questions_desired = window.prompt(alert_string + "How many examples would you like to try?");
if (!allow_questions_to_be_repeated) {
while (num_questions_desired > num_study_questions) {
num_questions_desired = window.prompt(backup_string + "Please choose a number less than or equal to " + num_study_questions);
}
}
// Loop over the questions to initialize the markers
for (question_index=0; question_index<num_study_questions; question_index++) {
temp_study_question = study_question_list[question_index];
if (!temp_study_question) {
error_string = "Study question " + question_index + " is undefined now (initialization).\n";
window.alert(error_string);
continue;
}
study_question_has_been_posed[question_index] = false;
} // closes initialization loop over all the study questions
// Select the questions at random
num_questions_selected = 0;
while (num_questions_selected < num_questions_desired) {
if (allow_questions_to_be_repeated) {
question_index = Math.floor(num_study_questions*Math.random());
selected_study_question_indices[num_questions_selected] = question_index;
num_questions_selected++;
} else {
num_questions_remaining = num_study_questions - num_questions_selected;
random_index = Math.floor(num_questions_remaining*Math.random());
question_counter = 0;
for (question_index=0; question_index<num_study_questions; question_index++) {
if (study_question_has_been_posed[question_index] != false) {
continue;
}
if (question_counter == random_index) {
study_question_has_been_posed[question_index] = true;
selected_study_question_indices[num_questions_selected] = question_index;
num_questions_selected++;
break;
}
question_counter++;
} // closes loop over all study questions
} // closes check whether questions are allowed to be repeated
} // closes while loop selecting the study questions for presentation
//************************
// Loop over the questions
//************************
for (question_index=1; question_index<=num_questions_selected; question_index++) {
study_question_index = selected_study_question_indices[question_index-1];
temp_study_question = study_question_list[study_question_index];
if (!temp_study_question) {
error_string = "Study question " + question_index + " is undefined now (main loop).\n";
window.alert(error_string);
continue;
}
question_string = temp_study_question.title;
if (!question_string) {
error_string = "ERROR: No question string in study question " + study_question_index + "\n";
window.alert(error_string);
continue;
}
if (!question_string.match(/SUBJECT/)) {
error_string = "ERROR: No SUBJECT in question string of study question " + study_question_index + "\n";
window.alert(error_string);
continue;
}
question_string = "Question " + question_index + ": " + question_string;
// window.alert(question_string);
// Determine all the answers
num_answers = 0;
span_nodes = temp_study_question.getElementsByTagName("SPAN");
if (!span_nodes) {
error_string = "ERROR: This study question has no SPAN nodes.\n";
window.alert(error_string);
return;
}
num_span_nodes = span_nodes.length;
if (num_span_nodes < 1) {
error_string = "ERROR: This study question has zero SPAN nodes.\n";
window.alert(error_string);
return;
}
for (span_node_index=0; span_node_index<num_span_nodes; span_node_index++) {
temp_span_node = span_nodes[span_node_index];
if (!temp_span_node) { continue; }
if (temp_span_node.className.match(/answer/)) {
answer_list[num_answers] = temp_span_node;
answer_has_been_chosen_already[num_answers] = false;
num_answers++;
}
} // closes loop over the SPAN nodes in the main article
alert_string = "Study question " + question_index + " (type " + study_question_index + ") has " + num_answers + " possible answers.\n";
// window.alert(alert_string);
// Choose num_answers_printed_with_question random answers for printing
num_answers_selected = 0;
while (num_answers_selected < num_answers_printed_with_question) {
num_answers_remaining = num_answers - num_answers_selected;
random_index = Math.floor(num_answers_remaining*Math.random());
answer_counter = 0;
for (answer_index=0; answer_index<num_answers; answer_index++) {
if (answer_has_been_chosen_already[answer_index] != false) {
continue;
}
if (answer_counter == random_index) {
answer_has_been_chosen_already[answer_index] = true;
selected_answer_indices[num_answers_selected] = answer_index;
num_answers_selected++;
break;
}
answer_counter++;
} // closes loop over all possible answers to the question
} // closes while() loop that selects the answers
// Generate the question, choose correct and incorrect answers
correct_printed_answer_index = Math.floor(num_answers_selected*Math.random());
correct_answer_index = selected_answer_indices[correct_printed_answer_index];
correct_answer_node = answer_list[correct_answer_index];
if (!correct_answer_node) { continue; }
correct_printed_answer_index++;
correct_answer_string = correct_answer_node.innerHTML.replace(/<[^>]+>/ig,"");
question_string = question_string.replace(/SUBJECT/, correct_answer_node.title) + "\n\n";
// window.alert(question_string);
for (printed_answer_index=1; printed_answer_index<=num_answers_selected; printed_answer_index++) {
answer_index = selected_answer_indices[printed_answer_index-1];
// question_string += printed_answer_index + ". Answer " + answer_index + "\n\n";
temp_answer = answer_list[answer_index];
if (!temp_answer) {
error_string = "ERROR: Printed answer " + printed_answer_index + " of study question " + study_question_index + " is undefined.\n\n";
window.alert(error_string);
continue;
}
question_string += printed_answer_index + ". " + temp_answer.innerHTML.replace(/<[^>]+>/ig,"") + "\n\n";
} // closes loop over answers to be printed
// window.alert(question_string);
// Present the question in a prompt window
question_string += "Please type in the number of the correct answer (1–" + num_answers_selected + "):";
user_answer_index = window.prompt(question_string);
num_questions_posed++;
// Evaluate the answer
if (user_answer_index == correct_printed_answer_index) {
num_correct_answers++;
window.alert("Congratulations! You picked the correct answer.\n");
} else {
window.alert("Sorry! The correct answer was " + correct_printed_answer_index + ": " + correct_answer_string + "\n");
}
} // closes loop over the study questions
// Print summary of number of correct answers
percentage_of_correct_answers = 0;
if (num_questions_posed > 0) {
percentage_of_correct_answers = Math.round((100 * num_correct_answers) / num_questions_posed);
summary_string = "You gave " + num_correct_answers + " correct answers out of " + num_questions_posed + " questions posed (" + percentage_of_correct_answers + "%).\n\n";
window.alert(summary_string);
}
} // closes function poseStudyQuestions()
$(function () {
mw.util.addPortletLink('p-navigation', 'javascript:poseStudyQuestions()', 'Study questions', 'ca-questions', 'Poses study questions on this topic to the reader', '', '');
});
//</pre>