Skip to content

Instantly share code, notes, and snippets.

Created December 5, 2013 05:08
Show Gist options
  • Save anonymous/7800449 to your computer and use it in GitHub Desktop.
Save anonymous/7800449 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name choose_quiz_questions
// @namespace com.example.quiz
// @include http://*/mod/quiz/*
// @version 1
// @grant none
// ==/UserScript==
var defaultNumberOfQuizQuestions = 25;
var inputs=document.getElementsByTagName('input');
var checkboxes=[];
for (var i=0; i< inputs.length; i++){
if (inputs[i].type=="checkbox" && inputs[i].title=="Select"){
checkboxes.push(inputs[i]);
}
}
var numberOfBankQuestions = checkboxes.length;
window.addButton = function () {
var buttonDiv = document.createElement('div');
buttonDiv.setAttribute('id','buttonDiv');
var inputButton = document.createElement('input');
inputButton.type = 'button';
inputButton.value = numberOfBankQuestions + ' Bank Questions - choose:';
if(inputButton.addEventListener) {
inputButton.addEventListener('click', go_go_checkboxes, false);
} else if (inputButton.attachEvent) {
inputButton.attachEvent('on'+'click', go_go_checkboxes);
}
document.getElementsByTagName("body")[0].appendChild(buttonDiv);
document.getElementById("buttonDiv").appendChild(inputButton);
}
window.addTextBox = function () {
var textBoxDiv = document.createElement('div');
textBoxDiv.setAttribute('id','textBoxDiv');
var inputTextBox = document.createElement('input');
inputTextBox.type = 'text';
inputTextBox.value = defaultNumberOfQuizQuestions;
inputTextBox.id = 'box'
document.getElementsByTagName("body")[0].appendChild(textBoxDiv);
document.getElementById("textBoxDiv").appendChild(inputTextBox);
}
addButton();
addTextBox();
var myButtonDiv = document.getElementById("buttonDiv");
myButtonDiv.style.position = "fixed";
myButtonDiv.style.left = "0px";
myButtonDiv.style.bottom = "0px";
myButtonDiv.style.backgroundColor = "black";
var myTextBoxDiv = document.getElementById("textBoxDiv");
myTextBoxDiv.style.position = "fixed";
myTextBoxDiv.style.left = myButtonDiv.offsetWidth + "px";
myTextBoxDiv.style.bottom = "0px";
myTextBoxDiv.style.backgroundColor = "black";
function go_go_checkboxes() {
randomArray =[];
var box = document.getElementById("box");
var numberOfQuizQuestions = box.value;
if (numberOfQuizQuestions < 1 || isNaN(numberOfQuizQuestions)){
alert("Number of Questions to choose must be a number > 0");
} else if (numberOfBankQuestions < 1) {
alert("There are no bank questions visible");
} else if (numberOfQuizQuestions > numberOfBankQuestions){
alert("Number of Quiz Questions must be less than or equal to number of Bank Questions")
} else {
while (randomArray.length < numberOfQuizQuestions) {
var randomNumber=RandomNumber(0 , numberOfBankQuestions - 1);
var found=false;
for (var i=0; i<randomArray.length; i++){
if(randomArray[i]==randomNumber){
found=true;
break;
}
}
if(!found){
randomArray.push(randomNumber);
}
}
for (var i=0; i < checkboxes.length; i++){
checkboxes[i].checked=false;
}
for (var i=0; i< randomArray.length; i++) {
checkboxes[randomArray[i]].checked=true;
}
}
}
function RandomNumber(min,max) {
if(max<min) {
var i=max;
max=min;
min=i;
}
return Math.round(Math.random()*(max-min)+min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment