MDL-55417 forms: Improve some of the new templates for form elements

Also - un-deprecate selectwithlink (Fred convinced me).

Part of MDL-55071
This commit is contained in:
Damyon Wiese
2016-09-23 10:53:30 +01:00
committed by Dan Poltawski
parent 97a00c9b8e
commit 7e2cd5b84e
7 changed files with 68 additions and 18 deletions
+2 -6
View File
@@ -49,12 +49,8 @@ class edit_outcome_form extends moodleform {
$options = array();
$mform->addElement('select', 'scaleid', get_string('scale'), $options);
$url = new moodle_url('/grade/edit/scale/edit.php', array('courseid' => $COURSE->id));
$label = get_string('scalescustomcreate');
$mform->addElement('static', 'scaleidlink', '', html_writer::link($url, $label));
$mform->addElement('selectwithlink', 'scaleid', get_string('scale'), $options, null,
array('link' => $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$COURSE->id, 'label' => get_string('scalescustomcreate')));
$mform->addHelpButton('scaleid', 'typescale', 'grades');
$mform->addRule('scaleid', get_string('required'), 'required');
+2 -5
View File
@@ -56,11 +56,8 @@ class edit_outcomeitem_form extends moodleform {
$options[$outcome->id] = $outcome->get_name();
}
}
$url = new moodle_url('/grade/edit/outcome/course.php', array('id' => $COURSE->id));
$label = get_string('outcomeassigntocourse', 'grades');
$mform->addElement('select', 'outcomeid', get_string('outcome', 'grades'), $options);
$mform->addElement('static', 'outcomeidlink', '', html_writer::link($url, $label));
$mform->addElement('selectwithlink', 'outcomeid', get_string('outcome', 'grades'), $options, null,
array('link' => $CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$COURSE->id, 'label' => get_string('outcomeassigntocourse', 'grades')));
$mform->addHelpButton('outcomeid', 'outcome', 'grades');
$mform->addRule('outcomeid', get_string('required'), 'required');
+2 -2
View File
@@ -415,12 +415,12 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element implements templatab
$context['value'] = $text;
$context['format'] = $format;
$str .= $OUTPUT->render_from_template('core_form/editor_textarea', $context);
if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
$context['changelistener'] = true;
}
$str .= $OUTPUT->render_from_template('core_form/editor_textarea', $context);
// during moodle installation, user area doesn't exist
// so we need to disable filepicker here.
if (!during_initial_install() && empty($CFG->adminsetuppending)) {
+43 -2
View File
@@ -26,6 +26,7 @@
*/
require_once('HTML/QuickForm/select.php');
require_once('templatable_form_element.php');
/**
* select type form element
@@ -38,7 +39,11 @@ require_once('HTML/QuickForm/select.php');
* @copyright 2008 Nicolas Connault <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select{
class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
@@ -64,7 +69,6 @@ class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select{
* @param bool $linkdata data to be posted
*/
public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
debugging('Element type selectwithlink is deprecated. Use 2 elements.', DEBUG_DEVELOPER);
if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
$this->_link = $linkdata['link'];
$this->_linklabel = $linkdata['label'];
@@ -75,6 +79,8 @@ class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select{
}
parent::__construct($elementName, $elementLabel, $options, $attributes);
$this->_type = 'selectwithlink';
}
/**
@@ -220,4 +226,39 @@ class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select{
return $this->_prepareValue($cleaned[0], $assoc);
}
}
public function export_for_template(renderer_base $output) {
$context = $this->export_for_template_base($output);
$options = [];
foreach ($this->_options as $option) {
if (is_array($this->_values) && in_array( (string) $option['attr']['value'], $this->_values)) {
$this->_updateAttrArray($option['attr'], ['selected' => 'selected']);
}
$o = [
'text' => $option['text'],
'value' => $option['attr']['value'],
'selected' => !empty($option['attr']['selected'])
];
$options[] = $o;
}
$context['options'] = $options;
if (!empty($this->_link)) {
if (!empty($this->_linkreturn) && is_array($this->_linkreturn)) {
$appendchar = '?';
if (strstr($this->_link, '?')) {
$appendchar = '&amp;';
}
foreach ($this->_linkreturn as $key => $val) {
$this->_link .= $appendchar."$key=$val";
$appendchar = '&amp;';
}
}
}
$context['link'] = $this->_link;
$context['linklabel'] = $this->_linklabel;
return $context;
}
}
@@ -48,4 +48,3 @@
<span>{{noSelectionString}}</span>
{{/items}}
</div>
</div>
@@ -16,8 +16,8 @@
require(['core/yui'], function(Y) {
Y.use('moodle-form-passwordunmask', function() {
M.form.passwordunmask({ formid: {{#quote}}{{element.id}}{{/quote}},
checkboxlabel: '{{#str}}unmaskpassword, form{{/str}}',
checkboxlabel: {{#quote}}{{#str}}unmaskpassword, form{{/str}}{{/quote}},
checkboxname: {{#quote}}{{element.name}}{{/quote}} });
});
});
{{/js}}
{{/js}}
@@ -0,0 +1,17 @@
{{< core_form/element-template }}
{{$element}}
<select class="custom-select {{#error}}form-control-danger{{/error}}" name="{{element.name}}"
id="{{element.id}}"
{{#element.multiple}}multiple{{/element.multiple}}
{{#element.frozen}}readonly disabled{{/element.frozen}}
{{#error}}
autofocus aria-describedby="id_error_{{element.name}}"
{{/error}}
{{element.attributes}} >
{{#element.options}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{text}}</option>
{{/element.options}}
</select>
<a class="m-x-1" href="{{{element.link}}}">{{element.linklabel}}</a>
{{/element}}
{{/ core_form/element-template }}