Limit number of answers in multiple choice question

Limit number of answers in multiple choice question

by Jill Kearney -
Number of replies: 28

I have seen this question asked before but couldn't find a solution.  We are using 3.11.

We want to ask a multiple-choice question "Select 3 ways to do .." from a list of x answers. I want the respondent to only be able to select 3 of the x answers.   Otherwise, they can just select all the x answers to get a full score. I don't want to do this with a complicated / penalty scoring scheme; this activity is for practice and we'd like people to think before making the selection. 

I gather it is not possible to limit the # of choices in the Moodle MC question (or the H5P multiple choice question) .. so has anyone developed code/plugin to enable this option?  This would be extremely helpful to us. 

Thanks.

Average of ratings: -
In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Jill,

Have you thought about the All or nothing multiple choice question type ↗? I think it might meet your needs.

In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Jill Kearney -
As I mentioned above, I really don't want a penalty scoring scheme - the activity is for practice. We would just like people to only be able to select x number of items if that is how many are correct.. so they think before selecting them.
In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Jill "We would just like people to only be able to select x number of items if that is how many are correct.. so they think before selecting them."

You want people to think before they click? Tough task.big grin

Average of ratings: Useful (4)
In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hello Jill,

The "All or nothing multiple choice" question type will do more or less what you want. Add a sentence to the question text saying "Select only 3 answers". If it's for practice only, simply don't show the quiz results in the gradebook. Better yet, use the Embed question filter: https://moodle.org/plugins/filter_embedquestion
Average of ratings: Useful (2)
In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hello Jill,
Joseph and Marcus always like to joke and I guess so Shirley and Germán from time to time. Please don't take offense. Your question is very interesting. I don't think there is a plugin for exactly what you want. However, it would probably be a simple matter to code a little JavaScript to limit the number of choices. I am on vacation now (for two weeks with my wife in a 5 star resort in Riviera Maya, Mexico) and can't do it now because the computer room is closed because of the COVID and I'm not patient enough to write code on my phone or tablet, but I will do it when I come back.
Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
I am jealous! We had a trip planned for the new year but are having to delay about a month...can't wait.
Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Jill,

You can limit the number of selections in a core multiple choice question using a small JavaScript code inserted either in the text of the question, a block appearing in the quiz or the Additional HTML of the Site administration, depending on the desired extent of the code effect.

The code is simply:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    let $total = 0;
    $("div.answer input[type=checkbox]").each(function(){
        $(this).on('click', function(event) {
            if (!$(this).is(":checked")) {
                $total = $total - 1;
            } else if ($total <= 2) {
                $total = $total + 1 ;
            } else {
                event.preventDefault();
                event.stopPropagation();
            }
        });
    });
});
</script>

In the above code, the maximum number of selections is set to three on the line

            } else if ($total <=2) }

The maximum number of selections can be easily changed by editing this line. For example, for a maximum of two selections, change the '2' to '1', for a maximum of four selections, change the '2' to '3', and so on.

With the above code, the student can select and deselect choices as long as there are only three. If three choices are selected, the student can deselect one choice but cannot select a fourth.

Please see https://moodleformulas.org/course/view.php?id=38§ion=16 ↗ where you will find the example below that you can try, as well as the question XML file containing the latest version of the code, if there have been any changes.

Do not hesitate to ask me for changes, modifications, etc. For example, would you have liked that students could not deselect their choice? Would you like a message to appear when a fourth selection is attempted, for example "You have selected three choices, you cannot select more."? Would you like the choices to be read when the student selects a choice?

MoodleForum_20220103_1156.png

References

  • British Broadcasting Corporation (BBC) - Why can't people with dyslexia do multiple choice?
  • International Dyslexia Association (IDA)
  • Wikipedia - Dyslexia

    Dyslexia, also known as reading disorder, is a disorder characterized by difficulty reading in individuals with otherwise unaffected intelligence. Different people are affected to different degrees. Problems may include difficulties in spelling words, reading quickly, writing words, "sounding out" words in the head, pronouncing words when reading aloud and understanding what one reads. Often these difficulties are first noticed at school. When someone who previously could read loses their ability, it is known as "alexia". The difficulties are involuntary and people with this disorder have a normal desire to learn. People with dyslexia have higher rates of attention deficit hyperactivity disorder (ADHD), developmental language disorders, and difficulties with numbers. Dyslexia is believed to be caused by the interaction of genetic and environmental factors. Some cases run in families. Dyslexia that develops due to a traumatic brain injury, stroke, or dementia is called "acquired dyslexia". The underlying mechanisms of dyslexia result from differences within the brain's language processing. Dyslexia is diagnosed through a series of tests of memory, vision, spelling, and reading skills. Dyslexia is separate from reading difficulties caused by hearing or vision problems or by insufficient teaching or opportunity to learn.

    Treatment involves adjusting teaching methods to meet the person's needs.While not curing the underlying problem, it may decrease the degree or impact of symptoms. Treatments targeting vision are not effective. Dyslexia is the most common learning disability and occurs in all areas of the world. It affects 3–7% of the population; however, up to 20% of the general population may have some degree of symptoms. While dyslexia is more often diagnosed in men, it has been suggested that it affects men and women equally. Some believe that dyslexia should be best considered as a different way of learning, with both benefits and downsides.

Average of ratings: Useful (2)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Jill Kearney -
Hi Dominique,
Thank you so much for taking the time to do this! We will certainly try it.
Much appreciated,
Jill Kearney
In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Jill,

Keep us posted. It would help if you could give your feedback.

Here is a multiple choice question in a standard format:


Here is the same question in a format that should make it easier to read. You can try it at https://moodleformulas.org/course/view.php?id=38&section=17 ↗.

Initial:

On hovering:

On selecting:

On checking:

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Box shadows can further improve readability:

In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Jill Kearney -
Hi,
We have tried the code and it works perfectly!! Thanks so much. To others who are reading this .. this code works in multiple choice questions. You paste it into the Question text box (after hitting the down arrow and <> so you can enter the code. Just change the number "2" to n-1 where n is the number of answers you want to be the limit for that question. So if you say "select 3 of the following list", they will not be able to select 4 or more .. they can only select 3. Then you can make feedback for the incorrect answers that will show when they hit "Check". This avoids someone selecting all the answers and getting the feedback "your answer is correct".
Thanks very much to Dominque for providing this. Much appreciated.
Jill
In reply to Jill Kearney

Re: Limit number of answers in multiple choice question

by Julian Prommer -

Hi

solution would meet our multiple choice regulations at the Technische Universität Darmstadt.

Some court decisions in germany made our legal staff to change the regulations of multiple choice grading before problems will come up.

So we the constraint, that the choices are counted. Either only the expected count of choices can be set or if a student sets to many choices it will be graded to 0. 

We have to show the expected count of choices. We have to give partial points.

There it would be nice, when this nice peace of code could toggled on and off in the MC question type. 

Some additional features could be:

  • autodection of the amount of choices which have to be selected
  • manual overide by typing in a smaller number of choices as the maximum set.
  • showing the expected amount of selected choices as part of the GUI (perhapps with toggle on/off)


This would be nice for moodle 3.11+ and 4

In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Rick Whitehead -

Hi Dominique,
Thanks for this code.
Hard to believe that in 2022 we still can't define the number of expected answers (and restricted clicking of checkboxes) in core multichoice?
I added an extra line to crudely catch existing checked boxes and calculate a new $total for users revisiting previous attempts (where moodle remembers and loads the page with previously checked options checked!).

<script>
    $(document).ready(function() {
        $("div.que.multichoice").each(function() {
            if ($(this).find("#maximumselections").length) {
                let maxselect = parseInt($(this).find("#maximumselections").text());
                $(this).find("#numberselections").text(maxselect);
                let $total = 1;
                $(this).find("div.answer input[type=checkbox]").each(function() {
                    if($(this).is(":checked")){ $total++; }
                    $(this).on('click', function(event) {
                        if (!$(this).is(":checked")) {
                            $total = $total - 1;
                            $("div.validationerror").remove();
                        } else if ($total <= maxselect) {
                            $total = $total + 1;
                            $("div.validationerror").remove();
                        } else {
                            event.preventDefault();
                            event.stopPropagation();
                            if (!$("div.validationerror").length) {
                                $("div.answer").after("<div class='validationerror'>Please select " + maxselect + " errors.</div>");
                            }
                        }
                    });
                });
            }
        });
    });
</script>
<span id="maximumselections" style="display:none;">2</span>
Average of ratings: Useful (1)
In reply to Rick Whitehead

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hello Rick,

Thank you for this improvement. I'll take a look and get back to you later.

I believe it wouldn't be too difficult to incorporate this feature into the PHP code of the question. I also think the demand is there, but it's not me who decides. smile
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Rick Whitehead -
Hey Dominique,
I had started looking at updating the multichoice qtype, until I found your code (thank you again), which gave me a quicker fix than to rewrite this plugin.
I'm no expert but the admin UI simply isn't good UX, IMHO - is that too many acronyms? I'd like to see answers added one at a time (why 3 answer blanks at a time?), and then to swap the One or multiple answers? |#id_single form field to a number field setting (this would replace your $total variable). 
If I get around to doing this myself I'll give you a shout. Thanks again for the code. 
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Moodle is open source. You can have a very large influence over what gets implemented if you learn to write PHP code (or, if you pay a Moodle partner to develop something). This issue is MDL-3784 if anyone wants to have a go.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Limit number of answers in multiple choice question

by Rick Whitehead -
Hi Tim, Thank you kindly for the issue link.
I'd very much like to have a go at this.
Shame our paths never crossed when I was actually in MK - we probably know some of the same OU people.
Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
You can have a very large influence over what gets implemented...
Really! That would be amazing!

...if you learn to write PHP code.
Learning PHP is definitely not the problem.
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Jenni Fogg -
Hey Dominique

Thank you so much for this code. This works perfectly when selecting 3 options. I'm struggling to get the code to work when I have 4 options. As instructed, to get 4 options, I have set the code to
} else if ($total <=3) }

but this still gives me 3 options. I've played around by changing the number to 4, 5, 6 and it's still stuck on 3 options. Is there something else I need to change?

Thank you!
In reply to Jenni Fogg

Re: Limit number of answers in multiple choice question

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Jenni,

A newer version of the code is available at https://moodleformulas.org/course/view.php?id=38&section=16 ↗ which works for more than one question on the same quiz page.

You can also try Rick Whitehead's proposal (see above). I haven't had time to study it, but it's probably another improvement.

My code is quite simple and seems to work well. What version of Moodle are you using? A few people have reported issues, in Moodle 4, updating their quizzes after a change (the quizzes don't update easily?).

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Milagros Huerta Gómez de Merodio -
Picture of Plugin developers
Hello everyone.

In Spain we have a saying that says "If Mohammed doesn't go to the mountain, the mountain goes to Mohammed", which means that, if someone else has to do something, they don't do it, so you look for a way to do it yourself.

Therefore, I have included these codes in the new version of FastTest PlugIn (V7.4 - https://moodle.org/plugins/view.php?id=2831), so that the user only has to indicate if he wants limit the student's answers, in the drop-down menu of the image. If you put in a number, the code you've developed between Dominigue and Rick is automatically included. In addition, the message that tells the student what the maximum number of possible answers is, is automatically included in the language you are working with in FastTest PlugIn (it is in all the languages ​​that Google translate supports).

The images show the example working in Spanish and English.

FastTest PlugIn in English Language

 


FastTest PlugIn in Spanish Language

 


I hope you like this novelty, also including the codes that Dominique has developed, on the images in drop-down menus. I mention it in the corresponding thread...

Regards
Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Limit number of answers in multiple choice question

by Esther I.K -
Is this functionality available by default in recent Moodle version? Students can check all the available options and they get 100% score for that. Our teachers want to set limit of the choosen answer. We don't have IT person who can change to code, unfortunately. So we rely on the official update from Moodle.
In reply to Esther I.K

Re: Limit number of answers in multiple choice question

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
You are setting the points for the questions in correctly. Put negative marks on the wrong answers and that will fix the problem.
Average of ratings: Useful (2)
In reply to Emma Richardson

Re: Limit number of answers in multiple choice question

by Esther I.K -
Got it! Thank you Emma!
In reply to Esther I.K

Re: Limit number of answers in multiple choice question

by Milagros Huerta Gómez de Merodio -
Picture of Plugin developers
The new version of FastTest PlugIn "automatically" includes Dominique's codes. You just have to write the number of possible answers in the corresponding box. If you have Excel and Windows, I invite you to try it.

Regards
Average of ratings: Useful (1)
In reply to Milagros Huerta Gómez de Merodio

Re: Limit number of answers in multiple choice question

by Esther I.K -
Thank you Milagros and Tim for the recommendation!