You need to sign in to do that
Don't have an account?

Compare "user selected options" with "Correct answers"
I am trying to implement online exam functionality.
In it I have folllowing objects
Courses
Assesments
Questions
Options ====> In Option object I have one checkbox for correct answer
Here is my VF page snippet-
<apex:page standardController="Assesment__c" extensions="surveyController" sidebar="false" showHeader="false"> <apex:sectionHeader title="{!Assesment__c.AssesmentName__c} For {!Assesment__c.Course__r.CourseName__c}" /> <!-- timer script for this page on page load --> <output name="x" id="txt"/> <script> var num=0.100; var c=num.toFixed(2); var t; var myVar; var myVarOne; function myFunction() { t=setInterval(function(){myTimer()},60000); } function myTimer() { document.getElementById('txt').value=c; var z= 0.010; var m=z.toFixed(2); c=(c-m).toFixed(2); if(c==0.05) { myVar=setTimeout(function(){alert("Only 5 minutes are left")},1000); } else if(c==0.00) { myVarOne=setTimeout(function(){alert("Please submit your answers ")},1000); } else if(c==-0.01) { clearInterval(t); } } </script> <body onload="myFunction()"></body> <apex:form > <apex:pageBlock > <apex:repeat value="{!wrapperList}" var="wrapper" id="theRepeat" > <h1> Question{!wrapper.questionNumber}-</h1> <apex:outputText value="{!wrapper.question.QuestionName__c}" /><br/><br/> <h1>Options-</h1> <apex:selectCheckboxes value="{!wrapper.selectedOptions}" layout="pageDirection"> <apex:selectOptions value="{!wrapper.options}"/> </apex:selectCheckboxes> <br /> </apex:repeat> <apex:pageBlockButtons location="bottom"> <apex:commandButton value="Submit" action="{!submit}" onclick="mySubmit();"/> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>
Here is My Apex controller-
public class surveyController { public List<Wrapper> wrapperList{get; set;} public surveyController(ApexPages.StandardController controller) { //here you'll have to have a way to retrieve a list of Questions and their related Option based on a criteria; you need to group them using something... List<Question__c> questionList = [SELECT Id, QuestionName__c,(SELECT Id, Option_Name__c,CorrectAnswer__c FROM Options__r) FROM Question__c where Assesment__c=:ApexPages.currentPage().getParameters().get('id')];//WHERE clause probably needed wrapperList = new List<Wrapper>(); Integer i = 1; for (Question__c question: questionList) { Wrapper wrapper = new Wrapper(question, question.Options__r,i++); wrapperList.add(wrapper); } } // wrapper class public class Wrapper { public List<String> selectedOptions{get; set;}//this will be a List of the Ids of the selected Options public List<SelectOption> options{get; set;}//a list of SelectOption that maps Option id(value) to Option name(label) public Question__c question{get; set;} public Integer questionNumber {get; set;} // to get question number prefixed with questions public String ansobj{get;set;} //to store answers on page public String SingleSelectedValue{get;set;} // for single choice answer. public Wrapper(Question__c question, List<Option__c> optionsList,Integer qn) { String alphabet ='abcdefgh'; this.questionNumber=qn; this.question = question; this.selectedOptions = new List<String>(); this.options = new List<SelectOption>(); if ((optionsList == null) || (optionsList.size() == 0)) { options.add(new SelectOption('none','-- none --')); } else { Integer i=0; for (Option__c option: optionsList) { options.add(new SelectOption(option.Id,alphabet.substring(i, i + 1) + '- ' +option.Option_Name__c)); i++; } } } } // submit method public void submit() { for(Wrapper wrap:wrapperList) { for(Option__c option:wrap.Question.Options__r) { if(option.CorrectAnswer__c==true) { system.debug('Your correct option is=============>'+option.Option_Name__c); wrap.ansobj=option.Option_Name__c; system.debug('option name----'+wrap.ansobj); } } System.debug('My Selected Option=======>'+wrap.SingleSelectedValue); // this values is coming null System.debug('answerobj=======>'+wrap.ansobj); if(wrap.ansobj==wrap.SingleSelectedValue) { System.debug('Correct Answer'); } } } }
In above Apex code I am facing some problems while submitting the answers.
Here are my queries-
1- How to compare "user selected options" to "CorrectAnswer Options"
In above code i tried
System.debug('My Selected Option=======>'+wrap.SingleSelectedValue); // It's coming "null"
System.debug('answerobj=======>'+wrap.ansobj);
if(wrap.ansobj==wrap.SingleSelectedValue)
{
System.debug('Correct Answer');
}
2- I need to show custom message with score i.e. if out of 5 questions 3 are correct so It should come as
"Hi your score is 3."
3- After submitting answers that page should hide and custom message should come on whole screen in place of
that page which were having questions
Could anyone help me to makemy self correct in logic.
Thanks,
JaanVivek
in any place in your code
2.to display messages you can:
add in your page:
<apex:pageMessages />
in the apex code add:
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'HELLO WORLD'));
Thanks for your suggestion.
I tried the below code
In above code in section which is commented-
if(wrap.ansobj==wrap.selectedOptions)
{
System.debug('Hello');
}
This is creating error if I do uncomment it.
in this section I am trying to compare correct options with User selected options and based on it i want to print
Hello world.
Error is coming because of List<String> to String assignment.
Any idea to do it . i think I am very close to do it but right thing is not coming out.
Thanks
JaanVivek
You need to compare a string to a string, hence, you need to loop through the selectedOptions, something like this:
Regards,
Hengky
Thank for your reply , but I did not get it
Here again I am posting my code and with my queries.
In above code I am trying to compare "userselected options" with "correcte answer".
In above code I have used
public List<String> selectedOptions{get; set;}====> It's a collection of Ids which user have selected on page
public String ansobj{get;set;}====> It's String type which will hold correct answer on page.
In the below section every thing is working fine-
But to compare UserSelected options in SelectedOptions list and Correct answer which are stored in ansobj in List format.
I tried this.
It did not work. In "selectedOptions" it contains Ids of user selected options and in "ansobj" it contains value i.e. correct option name.
So could you please suggest me how to compare ids to value or please if another way is possible.
Because I am trying to compare Ids to values.
I also tried to convert "ansob" to List<String> ansobj.
Thanks for your help.
Thanks,
JaanVivek
Hi,
The sample code in my previous reply actually was fine.
Anyway, If you want to use array notation to access the list, here it is:
Please take a look at the "List" documentation
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_lists.htm
Regards,
Hengky