From 57422d7158c7c5f3e63887459a38e5f31de9ed83 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Thu, 14 Dec 2017 19:48:31 -0500 Subject: [PATCH] MDL-43648 course: Allow start /end dates to have times --- admin/tool/uploadcourse/classes/step2_form.php | 4 ++-- backup/moodle2/restore_course_task.class.php | 2 +- backup/util/plan/restore_step.class.php | 4 ---- course/edit_form.php | 9 +++++---- course/reset_form.php | 4 ++-- lib/form/amd/build/defaultcustom.min.js | 2 +- lib/form/amd/src/defaultcustom.js | 6 ++++++ lib/form/defaultcustom.php | 18 +++++++++++++++--- 8 files changed, 32 insertions(+), 17 deletions(-) diff --git a/admin/tool/uploadcourse/classes/step2_form.php b/admin/tool/uploadcourse/classes/step2_form.php index abdb88a4092..2e394637c33 100644 --- a/admin/tool/uploadcourse/classes/step2_form.php +++ b/admin/tool/uploadcourse/classes/step2_form.php @@ -93,11 +93,11 @@ class tool_uploadcourse_step2_form extends tool_uploadcourse_base_form { $mform->addHelpButton('defaults[visible]', 'coursevisibility'); $mform->setDefault('defaults[visible]', $courseconfig->visible); - $mform->addElement('date_selector', 'defaults[startdate]', get_string('startdate')); + $mform->addElement('date_time_selector', 'defaults[startdate]', get_string('startdate')); $mform->addHelpButton('defaults[startdate]', 'startdate'); $mform->setDefault('defaults[startdate]', time() + 3600 * 24); - $mform->addElement('date_selector', 'defaults[enddate]', get_string('enddate'), array('optional' => true)); + $mform->addElement('date_time_selector', 'defaults[enddate]', get_string('enddate'), array('optional' => true)); $mform->addHelpButton('defaults[enddate]', 'enddate'); $courseformats = get_sorted_course_formats(true); diff --git a/backup/moodle2/restore_course_task.class.php b/backup/moodle2/restore_course_task.class.php index 2121c02b06c..6bcf826d297 100644 --- a/backup/moodle2/restore_course_task.class.php +++ b/backup/moodle2/restore_course_task.class.php @@ -200,7 +200,7 @@ class restore_course_task extends restore_task { $startdatedefaultvalue = $this->get_info()->original_course_startdate; $startdate = new restore_course_defaultcustom_setting('course_startdate', base_setting::IS_INTEGER, $startdatedefaultvalue); $startdate->set_ui(new backup_setting_ui_defaultcustom($startdate, get_string('setting_course_startdate', 'backup'), - ['customvalue' => $startdatedefaultvalue, 'defaultvalue' => $course->startdate, 'type' => 'date_selector'])); + ['customvalue' => $startdatedefaultvalue, 'defaultvalue' => $course->startdate, 'type' => 'date_time_selector'])); $this->add_setting($startdate); $keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false); diff --git a/backup/util/plan/restore_step.class.php b/backup/util/plan/restore_step.class.php index 18e77ef2f6f..4890b6c8a0b 100644 --- a/backup/util/plan/restore_step.class.php +++ b/backup/util/plan/restore_step.class.php @@ -84,10 +84,6 @@ abstract class restore_step extends base_step { // Original course has not startdate or setting doesn't exist, offset = 0. $cache[$this->get_restoreid()] = 0; - } else if (abs($setting - $original) < 24 * 60 * 60) { - // Less than 24h of difference, offset = 0 (this avoids some problems with timezones). - $cache[$this->get_restoreid()] = 0; - } else { // Arrived here, let's calculate the real offset. $cache[$this->get_restoreid()] = $setting - $original; diff --git a/course/edit_form.php b/course/edit_form.php index 4c03e2db761..b53dcb687d4 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -120,12 +120,13 @@ class course_edit_form extends moodleform { $mform->setConstant('visible', $courseconfig->visible); } } - - $mform->addElement('date_selector', 'startdate', get_string('startdate')); + $mform->addElement('date_time_selector', 'startdate', get_string('startdate')); $mform->addHelpButton('startdate', 'startdate'); - $mform->setDefault('startdate', time() + 3600 * 24); + $date = (new DateTime())->setTimestamp(usergetmidnight(time())); + $date->modify('+1 day'); + $mform->setDefault('startdate', $date->getTimestamp()); - $mform->addElement('date_selector', 'enddate', get_string('enddate'), array('optional' => true)); + $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true)); $mform->addHelpButton('enddate', 'enddate'); $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"'); diff --git a/course/reset_form.php b/course/reset_form.php index 6314a16b186..1accadd6cfd 100644 --- a/course/reset_form.php +++ b/course/reset_form.php @@ -41,9 +41,9 @@ class course_reset_form extends moodleform { $mform->addElement('header', 'generalheader', get_string('general')); - $mform->addElement('date_selector', 'reset_start_date', get_string('startdate'), array('optional'=>true)); + $mform->addElement('date_time_selector', 'reset_start_date', get_string('startdate'), array('optional' => true)); $mform->addHelpButton('reset_start_date', 'startdate'); - $mform->addElement('date_selector', 'reset_end_date', get_string('enddate'), array('optional' => true)); + $mform->addElement('date_time_selector', 'reset_end_date', get_string('enddate'), array('optional' => true)); $mform->addHelpButton('reset_end_date', 'enddate'); $mform->addElement('checkbox', 'reset_events', get_string('deleteevents', 'calendar')); $mform->addElement('checkbox', 'reset_notes', get_string('deletenotes', 'notes')); diff --git a/lib/form/amd/build/defaultcustom.min.js b/lib/form/amd/build/defaultcustom.min.js index e1d7944648b..aeda1d55d93 100644 --- a/lib/form/amd/build/defaultcustom.min.js +++ b/lib/form/amd/build/defaultcustom.min.js @@ -1 +1 @@ -define(["jquery"],function(a){var b=function(b){var c=a(b.target),d=JSON.parse(c.attr("data-defaultvalue")),e=JSON.parse(c.attr("data-customvalue")),f=c.attr("data-type"),g=c.closest("form"),h=c.attr("name").replace(/\[customize\]$/,"[value]"),i=c.prop("checked")?e:d;"text"===f?g.find('[name="'+h+'"]').val(i):"date_selector"===f&&(g.find('[name="'+h+'[day]"]').val(i.day),g.find('[name="'+h+'[month]"]').val(i.month),g.find('[name="'+h+'[year]"]').val(i.year))},c="input[data-defaultcustom=true]";a("body").on("change",c,b)}); \ No newline at end of file +define(["jquery"],function(a){var b=function(b){var c=a(b.target),d=JSON.parse(c.attr("data-defaultvalue")),e=JSON.parse(c.attr("data-customvalue")),f=c.attr("data-type"),g=c.closest("form"),h=c.attr("name").replace(/\[customize\]$/,"[value]"),i=c.prop("checked")?e:d;"text"===f?g.find('[name="'+h+'"]').val(i):"date_selector"===f?(g.find('[name="'+h+'[day]"]').val(i.day),g.find('[name="'+h+'[month]"]').val(i.month),g.find('[name="'+h+'[year]"]').val(i.year)):"date_time_selector"===f&&(g.find('[name="'+h+'[day]"]').val(i.day),g.find('[name="'+h+'[month]"]').val(i.month),g.find('[name="'+h+'[year]"]').val(i.year),g.find('[name="'+h+'[hour]"]').val(i.hour),g.find('[name="'+h+'[minute]"]').val(i.minute))},c="input[data-defaultcustom=true]";a("body").on("change",c,b)}); \ No newline at end of file diff --git a/lib/form/amd/src/defaultcustom.js b/lib/form/amd/src/defaultcustom.js index 0f280d55e17..e2e4d114c7d 100644 --- a/lib/form/amd/src/defaultcustom.js +++ b/lib/form/amd/src/defaultcustom.js @@ -39,6 +39,12 @@ define(['jquery'], function($) { form.find('[name="' + elementName + '[day]"]').val(newvalue.day); form.find('[name="' + elementName + '[month]"]').val(newvalue.month); form.find('[name="' + elementName + '[year]"]').val(newvalue.year); + } else if (type === 'date_time_selector') { + form.find('[name="' + elementName + '[day]"]').val(newvalue.day); + form.find('[name="' + elementName + '[month]"]').val(newvalue.month); + form.find('[name="' + elementName + '[year]"]').val(newvalue.year); + form.find('[name="' + elementName + '[hour]"]').val(newvalue.hour); + form.find('[name="' + elementName + '[minute]"]').val(newvalue.minute); } }; diff --git a/lib/form/defaultcustom.php b/lib/form/defaultcustom.php index ab8929af738..2169b764db5 100644 --- a/lib/form/defaultcustom.php +++ b/lib/form/defaultcustom.php @@ -84,8 +84,8 @@ class MoodleQuickForm_defaultcustom extends MoodleQuickForm_group { if (is_array($options)) { foreach ($options as $name => $value) { if (array_key_exists($name, $this->_options)) { - if ($name === 'type' && !in_array($value, ['text', 'date_selector'])) { - throw new coding_exception('Only text and date_selector elements are supported in ' . $this->_type); + if ($name === 'type' && !in_array($value, ['text', 'date_selector', 'date_time_selector'])) { + throw new coding_exception('Only text, date_selector, and date_time_selector elements are supported in ' . $this->_type); } if ($name === 'optional' && $value) { throw new coding_exception('Date selector can not be optional in ' . $this->_type); @@ -105,6 +105,8 @@ class MoodleQuickForm_defaultcustom extends MoodleQuickForm_group { $calendartype = \core_calendar\type_factory::get_calendar_instance(); $currentdate = $calendartype->timestamp_to_date_array($value, $this->_options['timezone']); return array( + 'minutes' => $currentdate['minutes'], + 'hours' => $currentdate['hours'], 'day' => $currentdate['mday'], 'month' => $currentdate['mon'], 'year' => $currentdate['year']); @@ -137,6 +139,9 @@ class MoodleQuickForm_defaultcustom extends MoodleQuickForm_group { } else if ($this->_options['type'] === 'date_selector') { $element = $this->createFormElement($this->_options['type'], 'value', '', $this->_options, $this->getAttributes()); + } else if ($this->_options['type'] === 'date_time_selector') { + $element = $this->createFormElement($this->_options['type'], 'value', '', $this->_options, + $this->getAttributes()); } $this->_elements[] = $element; } @@ -184,10 +189,17 @@ class MoodleQuickForm_defaultcustom extends MoodleQuickForm_group { if ($this->has_customize_switch()) { if ($this->_options['type'] === 'text') { $caller->disabledIf($arg[0] . '[value]', $arg[0] . '[customize]', 'notchecked'); - } else { + } else if ($this->_options['type'] === 'date_selector') { $caller->disabledIf($arg[0] . '[value][day]', $arg[0] . '[customize]', 'notchecked'); $caller->disabledIf($arg[0] . '[value][month]', $arg[0] . '[customize]', 'notchecked'); $caller->disabledIf($arg[0] . '[value][year]', $arg[0] . '[customize]', 'notchecked'); + } else { + // Date / Time selector. + $caller->disabledIf($arg[0] . '[value][day]', $arg[0] . '[customize]', 'notchecked'); + $caller->disabledIf($arg[0] . '[value][month]', $arg[0] . '[customize]', 'notchecked'); + $caller->disabledIf($arg[0] . '[value][year]', $arg[0] . '[customize]', 'notchecked'); + $caller->disabledIf($arg[0] . '[value][hours]', $arg[0] . '[customize]', 'notchecked'); + $caller->disabledIf($arg[0] . '[value][minutes]', $arg[0] . '[customize]', 'notchecked'); } } return $rv;