Add a new 'years' option to the duration form element.

Add a new 'years' option to the duration form element.

by Paul M -
Number of replies: 3

Hi

I am trying to add a new 'Years' option to the 'duration' form element using Moodle forms. You currently get the option of weeks, days, hours, minutes and seconds. I appreciate that due to leap years, a year isn't always the same value however I am assuming that it will always be a 365 day year. I'm trying to extend the 'duration' class but hitting problems - is there an easier way?

Thanks in advance.

Paul



Average of ratings: -
In reply to Paul M

Re: Add a new 'years' option to the duration form element.

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Paul,

Can you share what you've tried so far, and what problems you're hitting?  It's easier for people to make suggestions if they can see what you're already done smile

In reply to Mark Johnson

Re: Add a new 'years' option to the duration form element.

by Paul M -

Hi Mark

In the MoodleQuickForm_duration class (lib/form/duration.php) I can see the following function which provides the different values for the dropdown menu (in seconds):

public function get_units() {
        if (is_null($this->_units)) {
            $this->_units = array(
                604800 => get_string('weeks'),
                86400 => get_string('days'),
                3600 => get_string('hours'),
                60 => get_string('minutes'),
                1 => get_string('seconds'),
            );
        }
        return $this->_units;
    }

For my plugin, I need to do add another value for years ie 31536000 => get_string('years') but I'm trying to avoid hacking core Moodle. I wondered if there is an easy was to define this or is there a set_units function or similar that I could use (I couldn't find one).

My attempt at extending the duration class with my own duration_ext class threw an error:

QuickForm Error: unregistered element Element 'duration_ext' does not exist in HTML_QuickForm::_loadElement()

I'm guessing this probably the wrong path to go down because the form element 'types' are already defined within Moodle and it's not easy to extend these or add new ones.

Cheers
Paul

In reply to Paul M

Re: Add a new 'years' option to the duration form element.

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Why not just submit a patch to Moodle core, to add the years option?

Average of ratings: Useful (1)