How to Create dynamic link button

How to Create dynamic link button

by M.M V -
Number of replies: 3

I'm new to moodle I have a little bit of experience in moodle. I want to create a dynamic button. please explain to me how to get links from the database and how to work moodle class HTML::writer?

Please tell me how to understand moodle code as soon as

thank you.

Average of ratings: -
In reply to M.M V

Re: How to Create dynamic link button

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
You can use html_writer::link($url, $text, $attributes) where $attributes is an array, so you could have ['class' => 'btn btn-primary'] (assuming you're using a bootstrap-based theme).

You could also use $OUTPUT->single_button($url, $label). This creates a form around the button, so you can include other variables if that's important, and it can use either the post or get method.

The url should contain all the form elements as arguments e.g. /index.php?dbid=1&action=dosomething. These will be included in the form as hidden elements.
In reply to Mark Sharp

Re: How to Create dynamic link button

by Kurt Armbruster -
Looking for something similar but for a dashboard link which takes the user to their first incomplete course module.


I've figured out how to check the user's enrolled courses by calling enrol_get_my_courses to generate an array of the user's courses, and then iterating through the array looking for the course ID matching the course name, then using the Course ID to build the link.

But how do I dynamically generate the link to the next unfinished course module? 

Is there a built in Moodle function that takes in the course ID as an argument and returns the url of the user's next unfinished cm/cmid where I can get the $page info?

Or do we have to dig through the database to find that information?
(ie: a join on course_module_completion for the completion state, and course sections for the course module sequence, and then go looking for the $page ...)

My ultimate goal is:
If the  user has finished course section 3, module 2 (assignment 1), the link will point to module 3 (assignment 2). When they finish that, it points to section 3, module 3 (section 3 quiz). When they complete the quiz, the link points to section 4 module 1 (introduction lecture), etc...
In reply to Kurt Armbruster

Re: How to Create dynamic link button

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi Kurt, you're piggie-backing an unrelated question, you're better off starting a new discussion on this topic.
I suspect the answer to your question is you'll have to figure it out yourself as there are all sorts of assumptions in what you're looking at. But ask the question in its own post, you might get a better response.