forcedotcom / quiz-host-app

Multiplayer quiz app built on Salesforce technology (host app)
Creative Commons Zero v1.0 Universal
108 stars 67 forks source link

Generate Random Quiz #56

Closed codefriar closed 4 years ago

codefriar commented 4 years ago

What is missing from the application? Please describe. A way to easily generate a 10 question quiz from a stockpile of questions.

Describe the solution you'd like A button? some ui control that would generate a random 10 question quiz

Describe alternatives you've considered I've been using this Anonymous Apex snippet, which could form the basis for this feature:

List<Quiz_Question__C> questions = [SELECT Id FROM Quiz_question__c];
List<Quiz_Session_question__c> sessionQuestions = [SELECT Id, question__r.name, Question_Index__c FROM Quiz_Session_Question__c];
Set<Quiz_Question__c> chosenQuestions = new Set<Quiz_Question__c>();

for(Quiz_Session_Question__c question: sessionQuestions) {
    question.question__c = getRandomIdNotAlreadyChosen();    
}

update sessionQuestions;

public id getRandomIdNotAlreadyChosen(){
    Integer i = Integer.valueOf((Math.random() * questions.size()));
    if(!chosenQuestions.contains(questions.get(i))){
        chosenQuestions.add(questions.get(i));
        return questions.get(i).id;
    } else {
        return getRandomIdNotAlreadyChosen();
    }
}

NB: This is a rough and tumble, but functional implementation.

Additional context

codefriar commented 4 years ago

Additionally, this script enforces uniqueness of the questions preventing #55 from coming into play

codefriar commented 4 years ago

ps, i'm likely to PR this, once I find the time.

pozil commented 4 years ago

Addressed by #57