MDL-55584 gradereport_singleview: Convert some elements to template
authorFrederic Massart <fred@moodle.com>
Tue, 16 Aug 2016 09:40:07 +0000 (17:40 +0800)
committerDan Poltawski <dan@moodle.com>
Fri, 23 Sep 2016 09:53:56 +0000 (10:53 +0100)
Part of MDL-55071

grade/report/singleview/classes/local/screen/tablelike.php
grade/report/singleview/classes/local/ui/bulk_insert.php
grade/report/singleview/classes/local/ui/dropdown_attribute.php
grade/report/singleview/classes/local/ui/text_attribute.php
grade/report/singleview/templates/bulk_insert.mustache [new file with mode: 0644]
grade/report/singleview/templates/button.mustache [new file with mode: 0644]
grade/report/singleview/templates/dropdown_attribute.mustache [new file with mode: 0644]
grade/report/singleview/templates/text_attribute.mustache [new file with mode: 0644]

index 5e5054f..9c5f025 100644 (file)
@@ -230,10 +230,12 @@ abstract class tablelike extends screen {
      * @return array
      */
     public function buttons() {
-        $save = html_writer::empty_tag('input', array(
+        global $OUTPUT;
+
+        $save = $OUTPUT->render_from_template('gradereport_singleview/button', [
             'type' => 'submit',
             'value' => get_string('save', 'gradereport_singleview'),
-        ));
+        ]);
 
         return array($save);
     }
index 24b715e..2eb173f 100644 (file)
@@ -85,62 +85,25 @@ class bulk_insert extends element {
      * @return string HTML
      */
     public function html() {
-        $insertvalue = get_string('bulkinsertvalue', 'gradereport_singleview');
-        $insertappliesto = get_string('bulkappliesto', 'gradereport_singleview');
+        global $OUTPUT;
 
-        $insertoptions = array(
-            'all' => get_string('all_grades', 'gradereport_singleview'),
-            'blanks' => get_string('blanks', 'gradereport_singleview')
-        );
-
-        $selectlabel = html_writer::label(
-            $insertappliesto,
-            'menu' . $this->selectname
-        );
-        $select = html_writer::select(
-            $insertoptions,
-            $this->selectname,
-            'blanks',
-            false,
-            array(
-                'id' => 'menu' . $this->selectname
-            )
-        );
-
-        $textlabel = html_writer::label(
-            $insertvalue,
-            $this->insertname
-        );
         $text = new text_attribute($this->insertname, "0", 'bulk');
-
-        $inner = implode(' ', array(
-            $selectlabel,
-            $select,
-            $textlabel,
-            $text->html()
-        ));
-
-        $fieldset = html_writer::tag(
-            'fieldset',
-            html_writer::tag(
-                'legend',
-                get_string('bulklegend', 'gradereport_singleview'),
-                array(
-                    'class' => 'accesshide'
-                )
-            ) .
-            $inner
-        );
-
-        $apply = html_writer::checkbox(
-            $this->applyname,
-            1,
-            false,
-            get_string('bulkperform', 'gradereport_singleview')
-        );
-        $applydiv = html_writer::div($apply, 'enable');
-
-        return $applydiv . $fieldset;
+        $context = (object) [
+            'label' => get_string('bulklegend', 'gradereport_singleview'),
+            'applylabel' => get_string('bulkperform', 'gradereport_singleview'),
+            'applyname' => $this->applyname,
+            'menuname' => $this->selectname,
+            'menulabel' => get_string('bulkappliesto', 'gradereport_singleview'),
+            'menuoptions' => [
+                ['value' => 'all', 'name' => get_string('all_grades', 'gradereport_singleview')],
+                ['value' => 'blanks', 'name' => get_string('blanks', 'gradereport_singleview'), 'selected' => true],
+            ],
+            'valuename' => $this->insertname,
+            'valuelabel' => get_string('bulkinsertvalue', 'gradereport_singleview'),
+            'valuefield' => $text->html()
+        ];
+
+        return $OUTPUT->render_from_template('gradereport_singleview/bulk_insert', $context);
     }
 
     /**
index 19bc80c..50f1ce9 100644 (file)
@@ -77,22 +77,25 @@ class dropdown_attribute extends element {
      * @return string
      */
     public function html() {
-        $old = array(
-            'type' => 'hidden',
-            'name' => 'old' . $this->name,
-            'value' => $this->selected
-        );
-
-        $attributes = array('tabindex' => '1');
+        global $OUTPUT;
 
-        if (!empty($this->isdisabled)) {
-            $attributes['disabled'] = 'DISABLED';
-        }
+        $options = $this->options;
+        $selected = $this->selected;
 
-        $select = html_writer::select(
-            $this->options, $this->name, $this->selected, false, $attributes
+        $context = array(
+            'name' => $this->name,
+            'value' => $this->selected,
+            'tabindex' => 1,
+            'disabled' => !empty($this->isdisabled),
+            'options' => array_map(function($option) use ($options, $selected) {
+                return [
+                    'name' => $options[$option],
+                    'value' => $option,
+                    'selected' => $selected == $option
+                ];
+            }, array_keys($options))
         );
 
-        return ($select . html_writer::empty_tag('input', $old));
+        return $OUTPUT->render_from_template('gradereport_singleview/dropdown_attribute', $context);
     }
 }
index 0102199..652ae61 100644 (file)
@@ -65,38 +65,24 @@ class text_attribute extends element {
      * @return string The HTML.
      */
     public function html() {
-        $attributes = array(
-            'type' => 'text',
+        global $OUTPUT;
+
+        $context = (object) [
+            'id' => $this->name,
             'name' => $this->name,
             'value' => $this->value,
-            'id' => $this->name
-        );
-
-        if ($this->isdisabled) {
-            $attributes['disabled'] = 'DISABLED';
-        }
-
-        $hidden = array(
-            'type' => 'hidden',
-            'name' => 'old' . $this->name,
-            'value' => $this->value
-        );
+            'disabled' => $this->isdisabled,
+        ];
 
-        $label = '';
+        $context->label = '';
         if (preg_match("/^feedback/", $this->name)) {
-            $labeltitle = get_string('feedbackfor', 'gradereport_singleview', $this->label);
-            $attributes['tabindex'] = '2';
-            $label = html_writer::tag('label', $labeltitle,  array('for' => $this->name, 'class' => 'accesshide'));
+            $context->label = get_string('feedbackfor', 'gradereport_singleview', $this->label);
+            $context->tabindex = '2';
         } else if (preg_match("/^finalgrade/", $this->name)) {
-            $labeltitle = get_string('gradefor', 'gradereport_singleview', $this->label);
-            $attributes['tabindex'] = '1';
-            $label = html_writer::tag('label', $labeltitle,  array('for' => $this->name, 'class' => 'accesshide'));
+            $context->label = get_string('gradefor', 'gradereport_singleview', $this->label);
+            $context->tabindex = '1';
         }
 
-        return (
-            $label .
-            html_writer::empty_tag('input', $attributes) .
-            html_writer::empty_tag('input', $hidden)
-        );
+        return $OUTPUT->render_from_template('gradereport_singleview/text_attribute', $context);
     }
 }
diff --git a/grade/report/singleview/templates/bulk_insert.mustache b/grade/report/singleview/templates/bulk_insert.mustache
new file mode 100644 (file)
index 0000000..ad4b657
--- /dev/null
@@ -0,0 +1,34 @@
+{{!
+    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/>.
+}}
+{{!
+    Bulk insert attribute.
+}}
+<div class="enable">
+    <input type="checkbox" name="{{applyname}}" value="1" id="{{applyname}}">
+    <label for="{{applyname}}">{{applylabel}}</label>
+</div>
+<fieldset>
+    <legend class="accesshide">{{label}}</legend>
+    <label for="{{menuname}}">{{menulabel}}</label>
+    <select name="{{menuname}}" id="{{menuname}}">
+        {{#menuoptions}}
+            <option value="{{value}}" {{#selected}}selected{{/selected}}>{{name}}</option>
+        {{/menuoptions}}
+    </select>
+    <label for="{{valuename}}">{{valuelabel}}</label>
+    {{{valuefield}}}
+</fieldset>
diff --git a/grade/report/singleview/templates/button.mustache b/grade/report/singleview/templates/button.mustache
new file mode 100644 (file)
index 0000000..a922e89
--- /dev/null
@@ -0,0 +1,20 @@
+{{!
+    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/>.
+}}
+{{!
+    Button.
+}}
+<input type="{{type}}" value={{#quote}}{{value}}{{/quote}}>
diff --git a/grade/report/singleview/templates/dropdown_attribute.mustache b/grade/report/singleview/templates/dropdown_attribute.mustache
new file mode 100644 (file)
index 0000000..e6388ca
--- /dev/null
@@ -0,0 +1,25 @@
+{{!
+    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/>.
+}}
+{{!
+    Dropdown attribute.
+}}
+<select id="{{name}}" name="{{name}}" tabindex="1" {{#disabled}}disabled{{/disabled}}>
+    {{#options}}
+        <option value="{{value}}" {{#selected}}selected{{/selected}}>{{name}}</option>
+    {{/options}}
+</select>
+<input type="hidden" name="old{{name}}" value="{{value}}">
diff --git a/grade/report/singleview/templates/text_attribute.mustache b/grade/report/singleview/templates/text_attribute.mustache
new file mode 100644 (file)
index 0000000..3b93780
--- /dev/null
@@ -0,0 +1,22 @@
+{{!
+    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/>.
+}}
+{{!
+    Text attribute.
+}}
+<label for="{{name}}" class="accesshide">{{label}}</label>
+<input id="{{name}}" name="{{name}}" type="text" value="{{value}}" {{#tabindex}}tabindex="{{.}}"{{/tabindex}} {{#disabled}}disabled{{/disabled}}>
+<input type="hidden" name="old{{name}}" value="{{value}}">