Merge branch 'wip-MDL-55123-master' of git://github.com/marinaglancy/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2016-09-12 19:37:40 +02:00
commit ed55d47d03
12 changed files with 154 additions and 51 deletions

View File

@ -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;
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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());
}
}

View File

@ -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 = '<div class=\'alert\'>' . $gradesexistmsg . '</div>';
$this->_elements[] = @MoodleQuickForm::createElement('static', 'gradesexistmsg', '', $gradesexisthtml);
$this->_elements[] = $this->createFormElement('static', 'gradesexistmsg', '', $gradesexisthtml);
}
// Grade type select box.
$label = html_writer::tag('label', $this->gradetypeformelement->getLabel(),
array('for' => $this->gradetypeformelement->getAttribute('id')));
$this->_elements[] = @MoodleQuickForm::createElement('static', 'gradetypelabel', '', '&nbsp;'.$label);
$this->_elements[] = $this->createFormElement('static', 'gradetypelabel', '', '&nbsp;'.$label);
$this->_elements[] = $this->gradetypeformelement;
$this->_elements[] = @MoodleQuickForm::createElement('static', 'gradetypespacer', '', '<br />');
$this->_elements[] = $this->createFormElement('static', 'gradetypespacer', '', '<br />');
// Only show the grade scale select box when applicable.
if (!$this->isupdate || !$this->hasgrades || $this->currentgradetype == 'scale') {
$label = html_writer::tag('label', $this->scaleformelement->getLabel(),
array('for' => $this->scaleformelement->getAttribute('id')));
$this->_elements[] = @MoodleQuickForm::createElement('static', 'scalelabel', '', $label);
$this->_elements[] = $this->createFormElement('static', 'scalelabel', '', $label);
$this->_elements[] = $this->scaleformelement;
$this->_elements[] = @MoodleQuickForm::createElement('static', 'scalespacer', '', '<br />');
$this->_elements[] = $this->createFormElement('static', 'scalespacer', '', '<br />');
}
if ($this->isupdate && $this->hasgrades && $this->canrescale && $this->currentgradetype == 'point') {
@ -228,18 +228,18 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group {
$label = html_writer::tag('label', $rescalegradesselect->getLabel(),
array('for' => $rescalegradesselect->getAttribute('id')));
$labelhelp = new help_icon('modgraderescalegrades', 'grades');
$this->_elements[] = @MoodleQuickForm::createElement('static', 'scalelabel', '', $label . $OUTPUT->render($labelhelp));
$this->_elements[] = $this->createFormElement('static', 'scalelabel', '', $label . $OUTPUT->render($labelhelp));
$this->_elements[] = $rescalegradesselect;
$this->_elements[] = @MoodleQuickForm::createElement('static', 'scalespacer', '', '<br />');
$this->_elements[] = $this->createFormElement('static', 'scalespacer', '', '<br />');
}
// Only show the max points form element when applicable.
if (!$this->isupdate || !$this->hasgrades || $this->currentgradetype == 'point') {
$label = html_writer::tag('label', $this->maxgradeformelement->getLabel(),
array('for' => $this->maxgradeformelement->getAttribute('id')));
$this->_elements[] = @MoodleQuickForm::createElement('static', 'pointlabel', '', $label);
$this->_elements[] = $this->createFormElement('static', 'pointlabel', '', $label);
$this->_elements[] = $this->maxgradeformelement;
$this->_elements[] = @MoodleQuickForm::createElement('static', 'pointspacer', '', '<br />');
$this->_elements[] = $this->createFormElement('static', 'pointspacer', '', '<br />');
}
}
@ -340,6 +340,7 @@ class MoodleQuickForm_modgrade extends MoodleQuickForm_group {
* @return mixed
*/
public function onQuickFormEvent($event, $arg, &$caller) {
$this->setMoodleForm($caller);
switch ($event) {
case 'createElement':
// The first argument is the name.

View File

@ -129,8 +129,8 @@ class core_form_dateselector_testcase extends advanced_testcase {
// Create dateselector element with different timezones.
$elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
$el = new MoodleQuickForm_date_selector('dateselector', null, $elparams);
$el->_createElements();
$el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
$this->assertTrue($el instanceof MoodleQuickForm_date_selector);
$submitvalues = array('dateselector' => $vals);
$this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues),
@ -153,8 +153,8 @@ class core_form_dateselector_testcase extends advanced_testcase {
// Create dateselector element with different timezones.
$elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
$el = new MoodleQuickForm_date_selector('dateselector', null, $elparams);
$el->_createElements();
$el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
$this->assertTrue($el instanceof MoodleQuickForm_date_selector);
$expectedvalues = array(
'day' => array($vals['day']),
'month' => array($vals['month']),

View File

@ -141,8 +141,8 @@ class core_form_datetimeselector_testcase extends advanced_testcase {
// Create dateselector element with different timezones.
$elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
$el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams);
$el->_createElements();
$el = $this->mform->addElement('date_time_selector', 'dateselector', null, $elparams);
$this->assertTrue($el instanceof MoodleQuickForm_date_time_selector);
$submitvalues = array('dateselector' => $vals);
$this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues),
@ -165,8 +165,8 @@ class core_form_datetimeselector_testcase extends advanced_testcase {
// Create dateselector element with different timezones.
$elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
$el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams);
$el->_createElements();
$el = $this->mform->addElement('date_time_selector', 'dateselector', null, $elparams);
$this->assertTrue($el instanceof MoodleQuickForm_date_time_selector);
$expectedvalues = array(
'day' => array($vals['day']),
'month' => array($vals['month']),

View File

@ -41,6 +41,8 @@ require_once($CFG->libdir . '/form/duration.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_duration_testcase extends basic_testcase {
/** @var MoodleQuickForm Keeps reference of dummy form object */
private $mform;
/** @var MoodleQuickForm_duration Keeps reference of MoodleQuickForm_duration object */
private $element;
@ -49,7 +51,11 @@ class core_form_duration_testcase extends basic_testcase {
*/
protected function setUp() {
parent::setUp();
$this->element = new MoodleQuickForm_duration();
// Get form data.
$form = new temp_form_duration();
$this->mform = $form->getform();
$this->element = $this->mform->addElement('duration', 'duration');
}
/**
@ -67,7 +73,7 @@ class core_form_duration_testcase extends basic_testcase {
*/
public function test_constructor() {
// Test trying to create with an invalid unit.
$this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123));
$this->element = $this->mform->addElement('duration', 'testel', null, array('defaultunit' => 123, 'optional' => false));
}
/**
@ -94,7 +100,7 @@ class core_form_duration_testcase extends basic_testcase {
$this->assertEquals(array(1, 86400), $this->element->seconds_to_unit(86400));
$this->assertEquals(array(25, 3600), $this->element->seconds_to_unit(90000));
$this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 86400));
$this->element = $this->mform->addElement('duration', 'testel', null, array('defaultunit' => 86400, 'optional' => false));
$this->assertEquals(array(0, 86400), $this->element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
}
@ -102,8 +108,7 @@ class core_form_duration_testcase extends basic_testcase {
* Testcase to check generated timestamp
*/
public function test_exportValue() {
$el = new MoodleQuickForm_duration('testel');
$el->_createElements();
$el = $this->mform->addElement('duration', 'testel');
$values = array('testel' => array('number' => 10, 'timeunit' => 1));
$this->assertEquals(array('testel' => 10), $el->exportValue($values));
$values = array('testel' => array('number' => 3, 'timeunit' => 60));
@ -117,11 +122,32 @@ class core_form_duration_testcase extends basic_testcase {
$values = array('testel' => array('number' => 0, 'timeunit' => 3600));
$this->assertEquals(array('testel' => 0), $el->exportValue($values));
$el = new MoodleQuickForm_duration('testel', null, array('optional' => true));
$el->_createElements();
$el = $this->mform->addElement('duration', 'testel', null, array('optional' => true));
$values = array('testel' => array('number' => 10, 'timeunit' => 1));
$this->assertEquals(array('testel' => 0), $el->exportValue($values));
$values = array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1));
$this->assertEquals(array('testel' => 20), $el->exportValue($values));
}
}
/**
* Form object to be used in test case.
*/
class temp_form_duration 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;
}
}

View File

@ -545,7 +545,7 @@ class HTML_QuickForm extends HTML_Common {
function &createElement($elementType)
{
$args = func_get_args();
$element =& HTML_QuickForm::_loadElement('createElement', $elementType, array_slice($args, 1));
$element = self::_loadElement('createElement', $elementType, array_slice($args, 1));
return $element;
} // end func createElement
@ -566,7 +566,7 @@ class HTML_QuickForm extends HTML_Common {
function &_loadElement($event, $type, $args)
{
$type = strtolower($type);
if (!HTML_QuickForm::isTypeRegistered($type)) {
if (!self::isTypeRegistered($type)) {
$error = self::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, "Element '$type' does not exist in HTML_QuickForm::_loadElement()", 'HTML_QuickForm_Error', true);
return $error;
}

View File

@ -31,6 +31,7 @@ MDL-52081 - made all constructors PHP7 compatible
MDL-52826 - Remove onsubmit events pointing to the global validation functions and script
tag moved after the HTML
MDL-50484 - _getPersistantData() returns id with _persistant prefixed to element id.
MDL-55123 - corrected call to non-static functions in HTML_QuickForm to be PHP7.1-compliant
Pear

View File

@ -71,6 +71,8 @@ information provided here is intended especially for developers.
- 3. Run: Which process behat should be initialise for.
* behat_context_helper::set_session() has been deprecated, please use behat_context_helper::set_environment() instead.
* data-fieldtype="type" attribute has been added to form field default template.
* form elements extending MoodleQuickForm_group must call $this->createFormElement() instead of
@MoodleQuickForm::createElement() in order to be compatible with PHP 7.1
=== 3.1 ===