diff --git a/calendar/tests/calendartype_test.php b/calendar/tests/calendartype_test.php index c124477dc83..a8ccc858ddb 100644 --- a/calendar/tests/calendartype_test.php +++ b/calendar/tests/calendartype_test.php @@ -50,6 +50,8 @@ require_once($CFG->dirroot . '/user/profile/index_field_form.php'); * @since Moodle 2.6 */ class core_calendar_type_testcase extends advanced_testcase { + /** @var MoodleQuickForm Keeps reference of dummy form object */ + private $mform; /** * The test user. @@ -63,6 +65,10 @@ class core_calendar_type_testcase extends advanced_testcase { // The user we are going to test this on. $this->user = self::getDataGenerator()->create_user(); self::setUser($this->user); + + // Get form data. + $form = new temp_form_calendartype(); + $this->mform = $form->getform(); } /** @@ -216,15 +222,17 @@ class core_calendar_type_testcase extends advanced_testcase { private function convert_dateselector_to_unixtime_test($element, $type, $date) { $this->set_calendar_type($type); - if ($element == 'dateselector') { - $el = new MoodleQuickForm_date_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1)); - } else { - $el = new MoodleQuickForm_date_time_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1)); - } - $el->_createElements(); - $submitvalues = array('dateselector' => $date); + static $counter = 0; + $counter++; - $this->assertSame($el->exportValue($submitvalues), array('dateselector' => $date['timestamp'])); + if ($element == 'dateselector') { + $el = $this->mform->addElement('date_selector', 'dateselector' . $counter, null, array('timezone' => 0.0, 'step' => 1)); + } else { + $el = $this->mform->addElement('date_time_selector', 'dateselector' . $counter, null, array('timezone' => 0.0, 'step' => 1, 'optional' => false)); + } + $submitvalues = array('dateselector' . $counter => $date); + + $this->assertSame($el->exportValue($submitvalues), array('dateselector' . $counter => $date['timestamp'])); } /** @@ -300,3 +308,25 @@ class core_calendar_type_testcase extends advanced_testcase { \core\session\manager::set_user($this->user); } } + +/** + * Form object to be used in test case. + */ +class temp_form_calendartype extends moodleform { + /** + * Form definition. + */ + public function definition() { + // No definition required. + } + /** + * Returns form reference + * @return MoodleQuickForm + */ + public function getform() { + $mform = $this->_form; + // Set submitted flag, to simulate submission. + $mform->_flagSubmitted = true; + return $mform; + } +} diff --git a/lib/form/dateselector.php b/lib/form/dateselector.php index 29957e3d904..0c8ed846a3c 100644 --- a/lib/form/dateselector.php +++ b/lib/form/dateselector.php @@ -130,18 +130,18 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group { $dateformat = $calendartype->get_date_order($this->_options['startyear'], $this->_options['stopyear']); foreach ($dateformat as $key => $value) { // E_STRICT creating elements without forms is nasty because it internally uses $this - $this->_elements[] = @MoodleQuickForm::createElement('select', $key, get_string($key, 'form'), $value, $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $value, $this->getAttributes(), true); } // The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used. if ($calendartype->get_name() === 'gregorian') { $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle'); - $this->_elements[] = @MoodleQuickForm::createElement('link', 'calendar', + $this->_elements[] = $this->createFormElement('link', 'calendar', null, '#', $image, array('class' => 'visibleifjs')); } // If optional we add a checkbox which the user can use to turn if on if ($this->_options['optional']) { - $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); } foreach ($this->_elements as $element){ if (method_exists($element, 'setHiddenLabel')){ @@ -160,6 +160,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group { * @return bool */ function onQuickFormEvent($event, $arg, &$caller) { + $this->setMoodleForm($caller); switch ($event) { case 'updateValue': // Constant values override both default and submitted ones diff --git a/lib/form/datetimeselector.php b/lib/form/datetimeselector.php index 8055f026e6f..760deae5ba0 100644 --- a/lib/form/datetimeselector.php +++ b/lib/form/datetimeselector.php @@ -139,25 +139,25 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group { $dateformat = $calendartype->get_date_order($this->_options['startyear'], $this->_options['stopyear']); foreach ($dateformat as $key => $date) { // E_STRICT creating elements without forms is nasty because it internally uses $this - $this->_elements[] = @MoodleQuickForm::createElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true); } if (right_to_left()) { // Switch order of elements for Right-to-Left - $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true); - $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true); } else { - $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true); - $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true); } // The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used. if ($calendartype->get_name() === 'gregorian') { $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle'); - $this->_elements[] = @MoodleQuickForm::createElement('link', 'calendar', + $this->_elements[] = $this->createFormElement('link', 'calendar', null, '#', $image, array('class' => 'visibleifjs')); } // If optional we add a checkbox which the user can use to turn if on if ($this->_options['optional']) { - $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); } foreach ($this->_elements as $element){ if (method_exists($element, 'setHiddenLabel')){ @@ -176,6 +176,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group { * @return bool */ function onQuickFormEvent($event, $arg, &$caller) { + $this->setMoodleForm($caller); switch ($event) { case 'updateValue': // Constant values override both default and submitted ones diff --git a/lib/form/duration.php b/lib/form/duration.php index ab2d43462ac..c921d1432b2 100644 --- a/lib/form/duration.php +++ b/lib/form/duration.php @@ -144,12 +144,12 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group { } $this->_elements = array(); // E_STRICT creating elements without forms is nasty because it internally uses $this - $this->_elements[] = @MoodleQuickForm::createElement('text', 'number', get_string('time', 'form'), $attributes, true); + $this->_elements[] = $this->createFormElement('text', 'number', get_string('time', 'form'), $attributes, true); unset($attributes['size']); - $this->_elements[] = @MoodleQuickForm::createElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true); + $this->_elements[] = $this->createFormElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true); // If optional we add a checkbox which the user can use to turn if on if($this->_options['optional']) { - $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); + $this->_elements[] = $this->createFormElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); } foreach ($this->_elements as $element){ if (method_exists($element, 'setHiddenLabel')){ @@ -167,6 +167,7 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group { * @return bool */ function onQuickFormEvent($event, $arg, &$caller) { + $this->setMoodleForm($caller); switch ($event) { case 'updateValue': // constant values override both default and submitted ones diff --git a/lib/form/group.php b/lib/form/group.php index 753977f1087..6750d36258c 100644 --- a/lib/form/group.php +++ b/lib/form/group.php @@ -41,6 +41,9 @@ class MoodleQuickForm_group extends HTML_QuickForm_group{ /** @var string html for help button, if empty then no help */ var $_helpbutton=''; + /** @var MoodleQuickForm */ + protected $_mform = null; + /** * constructor * @@ -107,4 +110,41 @@ class MoodleQuickForm_group extends HTML_QuickForm_group{ } } } + + /** + * Stores the form this element was added to + * This object is later used by {@link MoodleQuickForm_group::createElement()} + * @param null|MoodleQuickForm $mform + */ + public function setMoodleForm($mform) { + if ($mform && $mform instanceof MoodleQuickForm) { + $this->_mform = $mform; + } + } + + /** + * Called by HTML_QuickForm whenever form event is made on this element + * + * If this function is overridden and parent is not called the element must be responsible for + * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()} + * + * @param string $event Name of event + * @param mixed $arg event arguments + * @param mixed $caller calling object + */ + public function onQuickFormEvent($event, $arg, &$caller) { + $this->setMoodleForm($caller); + return parent::onQuickFormEvent($event, $arg, $caller); + } + + /** + * Creates an element to add to the group + * Expects the same arguments as MoodleQuickForm::createElement() + */ + public function createFormElement() { + if (!$this->_mform) { + throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.'); + } + return call_user_func_array([$this->_mform, 'createElement'], func_get_args()); + } } diff --git a/lib/form/modgrade.php b/lib/form/modgrade.php index bced16a4883..1be7cc846c2 100644 --- a/lib/form/modgrade.php +++ b/lib/form/modgrade.php @@ -147,7 +147,7 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group { // Grade scale select box. $scales = get_scales_menu($COURSE->id); $langscale = get_string('modgradetypescale', 'grades'); - $this->scaleformelement = @MoodleQuickForm::createElement('select', 'modgrade_scale', $langscale, + $this->scaleformelement = $this->createFormElement('select', 'modgrade_scale', $langscale, $scales, $attributes); $this->scaleformelement->setHiddenLabel = false; $scaleformelementid = $this->generate_modgrade_subelement_id('modgrade_scale'); @@ -155,7 +155,7 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group { // Maximum grade textbox. $langmaxgrade = get_string('modgrademaxgrade', 'grades'); - $this->maxgradeformelement = @MoodleQuickForm::createElement('text', 'modgrade_point', $langmaxgrade, array()); + $this->maxgradeformelement = $this->createFormElement('text', 'modgrade_point', $langmaxgrade, array()); $this->maxgradeformelement->setHiddenLabel = false; $maxgradeformelementid = $this->generate_modgrade_subelement_id('modgrade_point'); $this->maxgradeformelement->updateAttributes(array('id' => $maxgradeformelementid)); @@ -167,7 +167,7 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group { 'point' => get_string('modgradetypepoint', 'grades'), ); $langtype = get_string('modgradetype', 'grades'); - $this->gradetypeformelement = @MoodleQuickForm::createElement('select', 'modgrade_type', $langtype, $gradetype, + $this->gradetypeformelement = $this->createFormElement('select', 'modgrade_type', $langtype, $gradetype, $attributes, true); $this->gradetypeformelement->setHiddenLabel = false; $gradetypeformelementid = $this->generate_modgrade_subelement_id('modgrade_type'); @@ -184,7 +184,7 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group { $choices[''] = get_string('choose'); $choices['no'] = get_string('no'); $choices['yes'] = get_string('yes'); - $rescalegradesselect = @MoodleQuickForm::createElement('select', + $rescalegradesselect = $this->createFormElement('select', 'modgrade_rescalegrades', $langrescalegrades, $choices); @@ -204,23 +204,23 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group { } $gradesexisthtml = '