Adding ‘Clear Button’ to the Multi-choice Type of Quiz

Re: Adding ‘Clear Button’ to the Multi-choice Type of Quiz

by Amrata Ramchandani -
Number of replies: 0

Here are the Manual Code Instructions for those who can’t apply the patch to make the clear functionality working completely.


Step 1 through 5 are required to make the clear button feature as an optional,so that one can include or exclude as per their need.


1) Strings to add in Quiz Lang File

moodle/mod/quiz/lang/en/quiz.php -> add the below code in alphabetical order of strings

$string['configenableclearbutton'] = 'If enabled, the clear button will be added in the quiz with multichoice questions in order to support an option to clear selected radio buttons';
$string['enableclearbutton'] = 'Enable clear button for single answer questions';
$string['enableclearbutton_help'] = 'If enabled, the clear button will be added in the quiz with multichoice questions in order to support an option to clear selected radio buttons';

2)Code to add “Enable Clear button” option in Quiz Settings->Question Behaviour

moodle/mod/quiz/mod_form.php ->after each attempts build on block of code 

      $mform->addElement('selectyesno', 'enableclear', get_string('enableclearbutton', 'quiz'));
      $mform->addHelpButton('enableclear', 'enableclearbutton', 'quiz');
      $mform->setAdvanced('enableclear', $quizconfig->enableclear_adv);
      $mform->setDefault('enableclear', $quizconfig->enableclear);


3) Code to include JS Files if clear button is enabled in quiz settings 

moodle/mod/quiz/attempt.php -> after attemptobject is created

/* Check for clear button option and insert corresponding JS(clearbutton.js file is attached here and jquery is already present in moodle just write the correct path to include it here) */
if($attemptobj->get_quiz()->enableclear) {
$PAGE->requires->js( '/lib/jquery/jquery-3.1.0.min.js');
$PAGE->requires->js( '/mod/quiz/clearbutton.js');
}


4)Code to include enableclear=0 and enableclear_adv=1 records in config_plugin moodle table

moodle/mod/quiz/settings.php -> after each attempts build on block of code

// Clear Button for multichoice questions.
$quizsettings->add(new admin_setting_configcheckbox_with_advanced('quiz/enableclear',get_string('enableclearbutton', 'quiz'), get_string('configenableclearbutton', 'quiz'),array('value' => 0, 'adv' => true)));


5)Code to include enableclear attribute in quiz table

moodle/mod/quiz/db/install.xml -> after each attempt builds on block of code

 <FIELD NAME="enableclear" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Whether the clear button should be enabled for multichoice questions"/>

Step 6 through 9 are required to make the clear functionality work properly


6) Code to check if the submitted data is empty ?

moodle/question/engine/questionattempt.php -> in process_action()

/* While clearing the quiz option,we are trying to bring the question attempt step data to '_order' and we could find that '_order' data only in initial step  i.e step[0] */
if( empty($submitteddata)){
        $step_zero_data = $this->steps[0]->get_all_data();
        $pendingstep->set_data($step_zero_data);
      }


7)Code to set the data back to order when clear button is clicked

moodle/question/engine/questionattemptstep.php ->after get_all_data()

// Setting Step Data
   public function set_data($data){
       $this->data = $data;
   }


8)Code to get the last answer or _order step name instead of directly passing answer

moodle/question/engine/questionattempt.php -> after get_last_step()

    public function get_unfinished_step() {
    foreach($this -> get_reverse_step_iterator() as $step) {
    $name = array('answer', '_order');
    foreach($name as $key) {
    if ($step -> has_qt_var($key))
    return $key;
    }
    }
   }


9)Code to call the last step name(out of answer or _order) instead of directly passing answer

 moodle/question/type/multichoice/question.php -> inside get_response()

public function get_response(question_attempt $qa) {
        $laststep_name=$qa->get_unfinished_step();
        return $qa->get_last_qt_var($laststep_name, -1);
     }

Please comment,if anyone gets any issue while doing these changes.