MDL-55417 forms: Improve some of the new templates for form elements
authorDamyon Wiese <damyon@moodle.com>
Tue, 16 Aug 2016 05:42:32 +0000 (13:42 +0800)
committerDan Poltawski <dan@moodle.com>
Fri, 23 Sep 2016 09:53:30 +0000 (10:53 +0100)
Also - un-deprecate selectwithlink (Fred convinced me).

Part of MDL-55071

grade/edit/outcome/edit_form.php
grade/edit/tree/outcomeitem_form.php
lib/form/editor.php
lib/form/selectwithlink.php
theme/noname/templates/core/form_autocomplete_selection.mustache
theme/noname/templates/core_form/element-password.mustache
theme/noname/templates/core_form/element-selectwithlink.mustache [new file with mode: 0644]

index ae4bacd..fd06079 100644 (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');
 
index 5930ca7..285acbe 100644 (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');
 
index 2049316..0488286 100644 (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)) {
index eb2a0d0..1643e6f 100644 (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 <nicolasconnault@gmail.com>
  * @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;
+    }
 }
index 3c0b252..8d6d191 100644 (file)
@@ -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}}
\ No newline at end of file
+{{/js}}
diff --git a/theme/noname/templates/core_form/element-selectwithlink.mustache b/theme/noname/templates/core_form/element-selectwithlink.mustache
new file mode 100644 (file)
index 0000000..5731fec
--- /dev/null
@@ -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 }}