MDL-34859 forms: new element defaultcustom
This commit is contained in:
@@ -26,7 +26,9 @@
|
||||
$string['addfields'] = 'Add {$a} field(s) to form';
|
||||
$string['advancedelement'] = 'Advanced element';
|
||||
$string['close'] = 'Close';
|
||||
$string['custom'] = 'Custom';
|
||||
$string['day'] = 'Day';
|
||||
$string['default'] = 'Default';
|
||||
$string['display'] = 'Display';
|
||||
$string['err_alphanumeric'] = 'You must enter only letters or numbers here.';
|
||||
$string['err_email'] = 'You must enter a valid email address here.';
|
||||
@@ -47,6 +49,7 @@ $string['miscellaneoussettings'] = 'Miscellaneous settings';
|
||||
$string['modstandardels'] = 'Common module settings';
|
||||
$string['month'] = 'Month';
|
||||
$string['mustbeoverriden'] = 'Abstract form_definition() method in class {$a} must be overridden, please fix the code.';
|
||||
$string['newvaluefor'] = 'New value for {$a}';
|
||||
$string['nomethodforaddinghelpbutton'] = 'There is no method for adding a help button to form element {$a->name} (class {$a->classname})';
|
||||
$string['nonexistentformelements'] = 'Trying to add help buttons to non-existent form elements : {$a}';
|
||||
$string['noselection'] = 'No selection';
|
||||
|
||||
+1
@@ -0,0 +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)});
|
||||
@@ -0,0 +1,47 @@
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Functionality for the form element defaultcustom
|
||||
*
|
||||
* @module core_form/defaultcustom
|
||||
* @package core_form
|
||||
* @class defaultcustom
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 3.3
|
||||
*/
|
||||
define(['jquery'], function($) {
|
||||
var onChangeSelect = function(event) {
|
||||
var element = $(event.target),
|
||||
defaultvalue = JSON.parse(element.attr('data-defaultvalue')),
|
||||
customvalue = JSON.parse(element.attr('data-customvalue')),
|
||||
type = element.attr('data-type'),
|
||||
form = element.closest('form'),
|
||||
elementName = element.attr('name').replace(/\[customize\]$/, '[value]'),
|
||||
newvalue = element.prop('checked') ? customvalue : defaultvalue;
|
||||
|
||||
if (type === 'text') {
|
||||
form.find('[name="' + elementName + '"]').val(newvalue);
|
||||
} else if (type === 'date_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);
|
||||
}
|
||||
};
|
||||
|
||||
var selector = 'input[data-defaultcustom=true]';
|
||||
$('body').on('change', selector, onChangeSelect);
|
||||
});
|
||||
@@ -274,7 +274,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
|
||||
$valuearray += $thisexport;
|
||||
}
|
||||
}
|
||||
if (count($valuearray)){
|
||||
if (count($valuearray) && isset($valuearray['year'])) {
|
||||
if($this->_options['optional']) {
|
||||
// If checkbox is on, the value is zero, so go no further
|
||||
if(empty($valuearray['enabled'])) {
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
/**
|
||||
* Creates an element with a dropdown Default/Custom and an input for the value (text or date_selector)
|
||||
*
|
||||
* @package core_form
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/form/group.php');
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
|
||||
/**
|
||||
* Creates an element with a dropdown Default/Custom and an input for the value (text or date_selector)
|
||||
*
|
||||
* @package core_form
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class MoodleQuickForm_defaultcustom extends MoodleQuickForm_group {
|
||||
|
||||
/**
|
||||
* @var array These complement separators, they are appended to the resultant HTML.
|
||||
*/
|
||||
protected $_wrap = array('', '');
|
||||
|
||||
/**
|
||||
* @var null|bool Keeps track of whether the date selector was initialised using createElement
|
||||
* or addElement. If true, createElement was used signifying the element has been
|
||||
* added to a group - see MDL-39187.
|
||||
*/
|
||||
protected $_usedcreateelement = true;
|
||||
|
||||
/** @var array */
|
||||
protected $_options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $elementname Element's name
|
||||
* @param mixed $elementlabel Label(s) for an element
|
||||
* @param array $options Options to control the element's display
|
||||
* @param mixed $attributes Either a typical HTML attribute string or an associative array
|
||||
*/
|
||||
public function __construct($elementname = null, $elementlabel = null, $options = array(), $attributes = null) {
|
||||
parent::__construct($elementname, $elementlabel);
|
||||
$this->setAttributes($attributes);
|
||||
|
||||
$this->_appendName = true;
|
||||
$this->_type = 'defaultcustom';
|
||||
|
||||
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
||||
$this->_options = [
|
||||
'type' => 'text', // Type of the element. Supported are 'text' and 'date_selector'.
|
||||
'defaultvalue' => null, // Value to be used when not overridden.
|
||||
'customvalue' => null, // Value to be used when overwriting.
|
||||
'customlabel' => get_string('custom', 'form'), // Label for 'customize' checkbox
|
||||
// Other options are the same as the ones that can be passed to 'date_selector' element.
|
||||
'timezone' => 99,
|
||||
'startyear' => $calendartype->get_min_year(),
|
||||
'stopyear' => $calendartype->get_max_year(),
|
||||
'defaulttime' => 0,
|
||||
'step' => 5,
|
||||
'optional' => false,
|
||||
];
|
||||
|
||||
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 === 'optional' && $value) {
|
||||
throw new coding_exception('Date selector can not be optional in ' . $this->_type);
|
||||
}
|
||||
$this->_options[$name] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts timestamp to the day/month/year array in the current calendar format
|
||||
* @param int $value
|
||||
* @return array
|
||||
*/
|
||||
protected function timestamp_to_date_array($value) {
|
||||
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
||||
$currentdate = $calendartype->timestamp_to_date_array($value, $this->_options['timezone']);
|
||||
return array(
|
||||
'day' => $currentdate['mday'],
|
||||
'month' => $currentdate['mon'],
|
||||
'year' => $currentdate['year']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should this element have default/custom switch?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function has_customize_switch() {
|
||||
return $this->_options['defaultvalue'] !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This will create all elements in the group
|
||||
*/
|
||||
public function _createElements() {
|
||||
if (!$this->has_customize_switch()) {
|
||||
$element = $this->createFormElement('hidden', 'customize', 1);
|
||||
} else {
|
||||
$element = $this->createFormElement('checkbox', 'customize', '', $this->_options['customlabel']);
|
||||
}
|
||||
$this->_elements[] = $element;
|
||||
|
||||
if ($this->_options['type'] === 'text') {
|
||||
$element = $this->createFormElement($this->_options['type'], 'value',
|
||||
get_string('newvaluefor', 'form', $this->getLabel()), $this->getAttributes());
|
||||
$element->setHiddenLabel(true);
|
||||
} else if ($this->_options['type'] === 'date_selector') {
|
||||
$element = $this->createFormElement($this->_options['type'], 'value', '', $this->_options,
|
||||
$this->getAttributes());
|
||||
}
|
||||
$this->_elements[] = $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by HTML_QuickForm whenever form event is made on this element
|
||||
*
|
||||
* @param string $event Name of event
|
||||
* @param mixed $arg event arguments
|
||||
* @param object $caller calling object
|
||||
* @return bool
|
||||
*/
|
||||
public function onQuickFormEvent($event, $arg, &$caller) {
|
||||
$this->setMoodleForm($caller);
|
||||
switch ($event) {
|
||||
case 'updateValue':
|
||||
// Constant values override both default and submitted ones
|
||||
// default values are overriden by submitted.
|
||||
$value = $this->_findValue($caller->_constantValues);
|
||||
if (null === $value) {
|
||||
// If no boxes were checked, then there is no value in the array
|
||||
// yet we don't want to display default value in this case.
|
||||
if ($caller->isSubmitted()) {
|
||||
$value = $this->_findValue($caller->_submitValues);
|
||||
} else {
|
||||
$value = $this->_findValue($caller->_defaultValues);
|
||||
}
|
||||
}
|
||||
if (!is_array($value)) {
|
||||
$customize = ($value !== false || !$this->has_customize_switch());
|
||||
if ($this->_options['type'] === 'text') {
|
||||
$elementvalue = $customize ? $value : $this->_options['defaultvalue'];
|
||||
} else {
|
||||
$elementvalue = $this->timestamp_to_date_array($customize ? $value : $this->_options['defaultvalue']);
|
||||
}
|
||||
$value = [
|
||||
'customize' => $customize,
|
||||
'value' => $elementvalue
|
||||
];
|
||||
}
|
||||
$this->setValue($value);
|
||||
break;
|
||||
case 'createElement':
|
||||
$rv = parent::onQuickFormEvent($event, $arg, $caller);
|
||||
if ($this->_options['type'] === 'text') {
|
||||
$caller->disabledIf($arg[0] . '[value]', $arg[0] . '[customize]', 'notchecked');
|
||||
} else {
|
||||
$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');
|
||||
}
|
||||
return $rv;
|
||||
case 'addElement':
|
||||
$this->_usedcreateelement = false;
|
||||
return parent::onQuickFormEvent($event, $arg, $caller);
|
||||
break;
|
||||
default:
|
||||
return parent::onQuickFormEvent($event, $arg, $caller);
|
||||
}
|
||||
}
|
||||
|
||||
public function freeze() {
|
||||
parent::freeze();
|
||||
$this->setPersistantFreeze(true);
|
||||
}
|
||||
|
||||
public function toHtml() {
|
||||
include_once('HTML/QuickForm/Renderer/Default.php');
|
||||
$renderer = new HTML_QuickForm_Renderer_Default();
|
||||
$renderer->setElementTemplate('{element}');
|
||||
parent::accept($renderer);
|
||||
|
||||
$html = $this->_wrap[0];
|
||||
if ($this->_usedcreateelement) {
|
||||
$html .= html_writer::tag('span', $renderer->toHtml(), array('class' => 'fdefaultcustom'));
|
||||
} else {
|
||||
$html .= $renderer->toHtml();
|
||||
}
|
||||
$html .= $this->_wrap[1];
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function accept(&$renderer, $required = false, $error = null) {
|
||||
global $PAGE;
|
||||
|
||||
if (!$this->_flagFrozen && $this->has_customize_switch()) {
|
||||
// Add JS to the default/custom switch.
|
||||
$firstelement = reset($this->_elements);
|
||||
$defaultvalue = $this->_options['defaultvalue'];
|
||||
$customvalue = $this->_options['customvalue'];
|
||||
if ($this->_options['type'] === 'date_selector') {
|
||||
$defaultvalue = $this->timestamp_to_date_array($defaultvalue);
|
||||
$customvalue = $this->timestamp_to_date_array($customvalue);
|
||||
}
|
||||
$firstelement->updateAttributes(['data-defaultcustom' => 'true',
|
||||
'data-type' => $this->_options['type'],
|
||||
'data-defaultvalue' => json_encode($defaultvalue),
|
||||
'data-customvalue' => json_encode($customvalue)]);
|
||||
$PAGE->requires->js_amd_inline("require(['core_form/defaultcustom'], function() {});");
|
||||
}
|
||||
|
||||
$renderer->renderElement($this, $required, $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a value. Give it the name of the group. In case of "default" return false.
|
||||
*
|
||||
* @param array $submitvalues values submitted.
|
||||
* @param bool $assoc specifies if returned array is associative
|
||||
* @return array
|
||||
*/
|
||||
public function exportValue(&$submitvalues, $assoc = false) {
|
||||
$valuearray = array();
|
||||
foreach ($this->_elements as $element) {
|
||||
$thisexport = $element->exportValue($submitvalues[$this->getName()], true);
|
||||
if ($thisexport != null) {
|
||||
$valuearray += $thisexport;
|
||||
}
|
||||
}
|
||||
if (empty($valuearray['customize'])) {
|
||||
return $this->_prepareValue(false, $assoc);
|
||||
}
|
||||
return array_key_exists('value', $valuearray) ? $this->_prepareValue($valuearray['value'], $assoc) : [];
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -123,13 +123,13 @@ class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
|
||||
}
|
||||
}
|
||||
|
||||
$this->_generateId();
|
||||
if ($this->_flagFrozen) {
|
||||
return $this->getFrozenHtml();
|
||||
}
|
||||
$html = $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />';
|
||||
|
||||
if ($this->_hiddenLabel){
|
||||
$this->_generateId();
|
||||
return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
|
||||
$this->getLabel() . '</label>' . $html;
|
||||
} else {
|
||||
|
||||
+8
-1
@@ -2452,8 +2452,14 @@ require(["core/event", "jquery"], function(Event, $) {
|
||||
$elNames = array();
|
||||
foreach ($elsInGroup as $elInGroup){
|
||||
if (is_a($elInGroup, 'HTML_QuickForm_group')) {
|
||||
// not sure if this would work - groups nested in groups
|
||||
// Groups nested in groups: append the group name to the element and then change it back.
|
||||
// We will be appending group name again in MoodleQuickForm_group::export_for_template().
|
||||
$oldname = $elInGroup->getName();
|
||||
if ($element->_appendName) {
|
||||
$elInGroup->setName($element->getName() . '[' . $oldname . ']');
|
||||
}
|
||||
$elNames = array_merge($elNames, $this->_getElNamesRecursive($elInGroup));
|
||||
$elInGroup->setName($oldname);
|
||||
} else {
|
||||
$elNames[] = $element->getElementName($elInGroup->getName());
|
||||
}
|
||||
@@ -3105,6 +3111,7 @@ MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", '
|
||||
MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden');
|
||||
MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'MoodleQuickForm_htmleditor');
|
||||
MoodleQuickForm::registerElementType('listing', "$CFG->libdir/form/listing.php", 'MoodleQuickForm_listing');
|
||||
MoodleQuickForm::registerElementType('defaultcustom', "$CFG->libdir/form/defaultcustom.php", 'MoodleQuickForm_defaultcustom');
|
||||
MoodleQuickForm::registerElementType('modgrade', "$CFG->libdir/form/modgrade.php", 'MoodleQuickForm_modgrade');
|
||||
MoodleQuickForm::registerElementType('modvisible', "$CFG->libdir/form/modvisible.php", 'MoodleQuickForm_modvisible');
|
||||
MoodleQuickForm::registerElementType('password', "$CFG->libdir/form/password.php", 'MoodleQuickForm_password');
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
{{!
|
||||
This file is part of Moodle - http://moodle.org/
|
||||
|
||||
Moodle is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Moodle is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
}}
|
||||
{{!
|
||||
@template core_form/element-defaultcustom-inline
|
||||
|
||||
Defaultcustom form element template.
|
||||
|
||||
Example context (json):
|
||||
{
|
||||
"element": {
|
||||
"elements": [
|
||||
{"html": "<select><option>Default</option><option>Custom</option></select>",
|
||||
"separator": ""},
|
||||
{"html": "<input type=text>",
|
||||
"separator": ""}
|
||||
]
|
||||
}
|
||||
}
|
||||
}}
|
||||
{{< core_form/element-template-inline }}
|
||||
{{$element}}
|
||||
<span class="fdefaultcustom">
|
||||
{{#element.elements}}
|
||||
{{{separator}}}
|
||||
{{{html}}}
|
||||
{{/element.elements}}
|
||||
</span>
|
||||
{{/element}}
|
||||
{{/ core_form/element-template-inline }}
|
||||
@@ -0,0 +1,43 @@
|
||||
{{!
|
||||
This file is part of Moodle - http://moodle.org/
|
||||
|
||||
Moodle is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Moodle is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
}}
|
||||
{{!
|
||||
@template core_form/element-defaultcustom
|
||||
|
||||
Defaultcustom form element template.
|
||||
|
||||
Example context (json):
|
||||
{
|
||||
"element": {
|
||||
"elements": [
|
||||
{"html": "<select><option>Default</option><option>Custom</option></select>",
|
||||
"separator": ""},
|
||||
{"html": "<input type=text>",
|
||||
"separator": ""}
|
||||
]
|
||||
}
|
||||
}
|
||||
}}
|
||||
{{< core_form/element-template }}
|
||||
{{$element}}
|
||||
<span class="fdefaultcustom">
|
||||
{{#element.elements}}
|
||||
{{{separator}}}
|
||||
{{{html}}}
|
||||
{{/element.elements}}
|
||||
</span>
|
||||
{{/element}}
|
||||
{{/ core_form/element-template }}
|
||||
Reference in New Issue
Block a user