How to create dynamic link button in moodle

Viewed 327

I want to create a button in moodle which redirects the user to the next topic so the topic's URL can change according to activity.

I don't know to do it I have tried using the below code.

 $output .= html_writer::div("<a class='mod_quiz-next-nav' href='$url'>Goto Next Session</a>");
        // redirect('https://web.whatsapp.com/');
        $output .= html_writer::link($url, get_string('finishreview', 'quiz'),
                array('class' => 'mod_quiz-next-nav'));
        return $output;

please suggest to me how to set a button which URL can change dynamically.

1 Answers

A simple solution would be

global $OUTPUT;

...

$params = ['something' => 'something'];

$url = new \moodle_url('/myurl/something.php', $params);

$button = $OUTPUT->single_button(
    $url,
    get_string('finishreview', 'quiz'),
    'get',
    ['class' => 'mod_quiz-next-nav']
);

$output = \html_writer::div($button, 'mod_quiz-next-nav');

A more complex and better solution would be to add the code to a renderer class. But that will take longer to explain.

See here for more details: https://docs.moodle.org/dev/Renderer_best_practices

Related