using the date_selector form element

using the date_selector form element

by Mark B -
Number of replies: 2
Hi there, I am creating a custom built PHP page for my Moodle website and wish to include some date picker fields inside each row of a table. Rather than getting a third party date picker from another website which is already made, i thought that i may as well just use the one which is already incorporated into Moodle. When i have came to use the date_selector which is in moodle i have found that it must be included within an actual Moodle form, is there no way i can just print this out in php onto a page? Also i am yet to get the date_selector function working. Would i would like to do is store the date selected into a variable and also have the functionality of reading in the date from a variable into the date_selector. The code i have tried is as follows: class simplehtml_form extends moodleform { function definition() { global $USER, $CFG; $mform = $mform =& $this->_form; $mform->addElement('date_selector', 'assesstimefinish', get_string('to')); } } $mform_simple = new simplehtml_form( null, array('email'=>$email, 'username'=>$username ) ); the problem i have is, how do i display the date_selector on the screen? How do i pass the date from a variable into the date_selector and how do i store the value selected into a variable? Many thanks, Mark
Average of ratings: -
In reply to Mark B

Re: using the date_selector form element

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To output the form, try
$mform_simple->display();

To set the initial data in the form:
$data = new stdClass();
$data->assesstimefinish = $timestamp;
$mform_simple->set_data($data);

To retrieve the value:
if ($formdata = $mform_simple->get_data()) {
$newtimestamp = $formdata->assesstimefinish;
}

In reply to Davo Smith

Re: using the date_selector form element

by Mark B -
Many thanks for the reply. My only problem now is that i wish to have many of these on the screen and the way it looks like i set it up would be quite bulky with the code. Do you know if there is any way i can change the date selector into a small button and so when i click on it the date selector will pop up? I am thinking that having multiple dates in a HTML table, will this mean that i just add in a different variables to store each date and then call them when needed? e.g. data->assesstimefinish = $timstamp1; data->assesstimefinish2 = $timestamp2; How would i go about getting these values into the actual different date selectors? Do i need to use a get/set method or should i just be able to just assign a variable to the date_selector default value? Here is my latest code and it is telling me that it cannot call get_data on a non object: class simplehtml_form extends moodleform { function definition() { global $USER, $CFG; if ($formdata = $mform->get_data()) { $newtimestamp = $formdata->assesstimefinish1; } $mform = $mform =& $this->_form; $mform->addElement('date_selector', 'assesstimefinish1', get_string('to1')); $mform->addElement('date_selector', 'assesstimefinish2', get_string('to2')); $mform->addElement('date_selector', 'assesstimefinish3', get_string('to3')); $mform->addElement('date_selector', 'assesstimefinish4', get_string('to4')); $mform->display(); } } $mform = new simplehtml_form( null, array('email'=>$email, 'username'=>$username ) ); $data = new stdClass(); $data->assesstimefinish1 = 1349049600; $mform->set_data($data);