Till Moodle 3.3+ there was no feature of clearing the selected options in Mutlichoice
Type of Questions in Quiz.Our Institution felt a need for it and we tried implementing it and sharing here the logic which we implemented in order to add that feature and even the patch file for the same so that others can apply and check if it is working as expected.
Below is the Logic which we used to implement it.
Adding the HTML Part i.e Clear Button,to add this part we are currently using
simple way to get the button without touching the core moodle HTML Part.
Which is through the Additional HTML feature available to site administrators.
The path for the same is :
Site Administration -> Appearance -> Additional HTML
i) Within Head, add following link to Jquery.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
ii) Before body is closed add following script to add clear button after options of every question of type Multichoice
<script type="text/javascript">
if ($("input[type]").is(":radio"))
document.getElementById("clear").onclick = clear_me;
function clear_me()
</script>
PS : This clear button as of now appears everywhere there is question type "Multichoice"
including in Review Page of Quiz,it will be away once we put the html code
of clear button in the core moodle,we would make it appear only in quiz attempt page.
2) After checking the database tables involved in quiz attempt we came to know that question_attempt_steps and question_attempt_steps_data tables are being updated with new records after every attempt on particular question.
Two important attributes in these tables were
a)State (in question_attempt_steps)
b)Name (in question_attempt_steps_data)
When the quiz is started the state of question is 'todo' and name is '_order'
So when we press the clear button it clears the selected option by javascript given above but the option is unselected is not reflected in database yet.When you will come back to question you will see the options selected by previous answer.It takes the last answer from db.Inorder to make the last answer ignored we will have to make 2 changes.
i) Add a record in both of these tables,i.e update the state="todo" and name="_order".
Basically on clear press the question should come back to state as it was in the start of attempt where no option was selected.
For this we updated the question/engine/questionattempt.php->process_action() with following code
if( empty($submitteddata))
{ $step_zero_data = $this->steps[0]->get_all_data(); $pendingstep->set_data($step_zero_data); }In this code we are checking if the submitteddata is empty,If it is, then change the pending step data with step zero data which is [_order] otherwise it would have been taken as no option has changed and the new row in the table wouldn't have been added and therefore previously selected option was fetched and displayed in the quiz.To avoid this
we are updating the pending step data with [_order].
The state of question was by default set to 'todo' so no need to change the state.
This way on clearing the option and pressing next/prev buttons,db tables are updated with new row with above values.
ii) Make changes in get_response function to take the last step data instead of last answer
Till this point,db is updated properly but while rendering the quiz page it reads the previous answer and fetches the old value.The below code takes care of that
Update the question/type/multichoice/question.php->get_response () as below:
public function get_response(question_attempt $qa) {
$laststep = $qa->get_last_step();
$laststep_name = key($laststep->get_all_data());
if($laststepname == '-finish')
else
{ return $qa->get_last_qt_var($laststep_name, -1); }}
Summary:
1)Add Clear button
2)OnClick event Clear the selected option using javascript and update the table with 'todo' state and [_order] name
3)Make get_response function to fetch response from the last step data instead of last answer.
Things Pending :
1) Adding the clear button HTML code in core moodle code.
2) The above functionality is tested when there is single question on single page. It will be implemented for all layouts soon.
To make the clear functionality work at your moodle,please do the following
1) Write the HTML Part to add Clear button,Just add the two scripts described in step 1.
2) Apply the patch(It is in the attachment)
PS : The code given above in the explanation is not complete,so please use patch file to get the changes.
- duplicates
-
MDL-5311 clear answer choice in multiple choice
-
- Closed
-