Skip to content

Commit

Permalink
MDL-55123 forms: do not call non-static methods statically
Browse files Browse the repository at this point in the history
this breaks in PHP7.1
  • Loading branch information
marinaglancy committed Sep 12, 2016
1 parent 64c45a2 commit 721e2de
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 28 deletions.
7 changes: 4 additions & 3 deletions lib/form/dateselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ function _createElements() {
$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')){
Expand All @@ -160,6 +160,7 @@ function _createElements() {
* @return bool
*/
function onQuickFormEvent($event, $arg, &$caller) {
$this->setMoodleForm($caller);
switch ($event) {
case 'updateValue':
// Constant values override both default and submitted ones
Expand Down
15 changes: 8 additions & 7 deletions lib/form/datetimeselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,25 @@ function _createElements() {
$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')){
Expand All @@ -176,6 +176,7 @@ function _createElements() {
* @return bool
*/
function onQuickFormEvent($event, $arg, &$caller) {
$this->setMoodleForm($caller);
switch ($event) {
case 'updateValue':
// Constant values override both default and submitted ones
Expand Down
7 changes: 4 additions & 3 deletions lib/form/duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ function _createElements() {
}
$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')){
Expand All @@ -167,6 +167,7 @@ function _createElements() {
* @return bool
*/
function onQuickFormEvent($event, $arg, &$caller) {
$this->setMoodleForm($caller);
switch ($event) {
case 'updateValue':
// constant values override both default and submitted ones
Expand Down
40 changes: 40 additions & 0 deletions lib/form/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -107,4 +110,41 @@ function setElements($elements){
}
}
}

/**
* 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());
}
}
27 changes: 14 additions & 13 deletions lib/form/modgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ public function _createElements() {
// 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');
$this->scaleformelement->updateAttributes(array('id' => $scaleformelementid));

// 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));
Expand All @@ -167,7 +167,7 @@ public function _createElements() {
'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');
Expand All @@ -184,7 +184,7 @@ public function _createElements() {
$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);
Expand All @@ -204,42 +204,42 @@ public function _createElements() {
}

$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') {
// We need to know how to apply any changes to maxgrade - ie to either update, or don't touch exising grades.
$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 />');
}
}

Expand Down Expand Up @@ -340,6 +340,7 @@ protected function validate_point($val) {
* @return mixed
*/
public function onQuickFormEvent($event, $arg, &$caller) {
$this->setMoodleForm($caller);
switch ($event) {
case 'createElement':
// The first argument is the name.
Expand Down
4 changes: 2 additions & 2 deletions lib/pear/HTML/QuickForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ function getMaxFileSize()
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

Expand All @@ -566,7 +566,7 @@ function &createElement($elementType)
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;
}
Expand Down
1 change: 1 addition & 0 deletions lib/pear/README_MOODLE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===

Expand Down

0 comments on commit 721e2de

Please sign in to comment.