Skip to content

Commit

Permalink
Merge branch 'wip-MDL-55123-master' of git://github.com/marinaglancy/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
stronk7 committed Sep 12, 2016
2 parents 9579b52 + 721e2de commit ed55d47
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 48 deletions.
40 changes: 35 additions & 5 deletions calendar/tests/calendartype_test.php
Expand Up @@ -50,6 +50,8 @@
* @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.
Expand All @@ -63,6 +65,10 @@ protected function setUp() {
// 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();
}

/**
Expand Down Expand Up @@ -216,15 +222,17 @@ private function core_functions_test($type) {
private function convert_dateselector_to_unixtime_test($element, $type, $date) {
$this->set_calendar_type($type);

static $counter = 0;
$counter++;

if ($element == 'dateselector') {
$el = new MoodleQuickForm_date_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1));
$el = $this->mform->addElement('date_selector', 'dateselector' . $counter, null, array('timezone' => 0.0, 'step' => 1));
} else {
$el = new MoodleQuickForm_date_time_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1));
$el = $this->mform->addElement('date_time_selector', 'dateselector' . $counter, null, array('timezone' => 0.0, 'step' => 1, 'optional' => false));
}
$el->_createElements();
$submitvalues = array('dateselector' => $date);
$submitvalues = array('dateselector' . $counter => $date);

$this->assertSame($el->exportValue($submitvalues), array('dateselector' => $date['timestamp']));
$this->assertSame($el->exportValue($submitvalues), array('dateselector' . $counter => $date['timestamp']));
}

/**
Expand Down Expand Up @@ -300,3 +308,25 @@ private function set_calendar_type($type) {
\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;
}
}
7 changes: 4 additions & 3 deletions lib/form/dateselector.php
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
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
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
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
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
8 changes: 4 additions & 4 deletions lib/form/tests/dateselector_test.php
Expand Up @@ -129,8 +129,8 @@ public function test_exportvalue() {

// 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),
Expand All @@ -153,8 +153,8 @@ public function test_onquickformevent() {

// 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']),
Expand Down
8 changes: 4 additions & 4 deletions lib/form/tests/datetimeselector_test.php
Expand Up @@ -141,8 +141,8 @@ public function test_exportvalue() {

// 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),
Expand All @@ -165,8 +165,8 @@ public function test_onquickformevent() {

// 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']),
Expand Down

0 comments on commit ed55d47

Please sign in to comment.