Show correct answers after last attempt in quiz

Show correct answers after last attempt in quiz

by Nicolas Ndalpe -
Number of replies: 1

Hi,

I would like to know if it is possible to show the correct answer on the quiz review page only after the last attempt is completed. As I can see in the quiz settings "review options", displaying the correct answers in the review page seems to be an on/off settings.


My intention here is to develop a plugin to do that. I have experience developing with PHP but not so much with Moodle. Therefore I would like to ask the community some pointer to help me get started.


What plugin types should I use? (https://docs.moodle.org/dev/Plugin_types)

There is quiz and quizaccess but none of them seem to be appropriate for what I want to do.


Could this behaviour affect other Moodle settings? I am thinking about question behaviour, feedback, quiz access, etc


Is it just to override a renderer? If so, which one?


Is there someone out there who made this already or maybe a part of it?


Any other info that would be helpful are welcome.


Old discussion about the topic

https://moodle.org/mod/forum/discuss.php?d=341948


Thank you in advance for your help


$release  = '3.4.3+ (Build: 20180524)';

Average of ratings: -
In reply to Nicolas Ndalpe

Re: Show correct answers after last attempt in quiz

by Nicolas Ndalpe -

Ok I answered my own question. You just need to overwrite the quiz renderer.

Here is what I did:


In my theme, I create a file:

/theme/mytheme/renderers.php


Then add the following code in the file:

<?php

include_once($CFG->dirroot . "/mod/quiz/renderer.php");

class theme_themenamehere_mod_quiz_renderer extends mod_quiz_renderer {

public function review_page(quiz_attempt $attemptobj, $slots, $page, $showall, $lastpage, mod_quiz_display_options $displayoptions, $summarydata) {
        // Number of allowed attempt for the quiz
        $allowedAttempt = $attemptobj->get_num_attempts_allowed();

        // current attempt #
        $currentAttempt = $attemptobj->get_attempt_number();


        // Display the correct answer if the current attempt # equal the total allowed attempt

        if ($allowedAttempt == $currentAttempt) {
            $displayoptions->rightanswer = 1;
        } else {
            $displayoptions->rightanswer = 0;
        }

        return parent::review_page($attemptobj, $slots, $page, $showall, $lastpage, $displayoptions, $summarydata);
}

Any suggestion are welcome

Average of ratings: Useful (1)