MDL-65948 core_grade: Use new core/checkbox-toggleall
authorJun Pataleta <jun@moodle.com>
Fri, 29 Mar 2019 06:31:18 +0000 (14:31 +0800)
committerJun Pataleta <jun@moodle.com>
Mon, 30 Dec 2019 06:13:35 +0000 (14:13 +0800)
grade/amd/build/edittree_index.min.js
grade/amd/build/edittree_index.min.js.map
grade/amd/src/edittree_index.js
grade/edit/tree/lib.php
grade/templates/edit_tree.mustache

index cc73e30..4692f4a 100644 (file)
Binary files a/grade/amd/build/edittree_index.min.js and b/grade/amd/build/edittree_index.min.js differ
index e3569f3..26c6df9 100644 (file)
Binary files a/grade/amd/build/edittree_index.min.js.map and b/grade/amd/build/edittree_index.min.js.map differ
index 9316f84..bb9aa79 100644 (file)
@@ -30,12 +30,6 @@ define([
      * @method edittree
      */
     var edittree = function() {
-        // Watch items and toggle the move menu accordingly.
-        $('body').on('change', '.itemselect.ignoredirty', edittree.checkMoveMenuState);
-
-        // Watch for the 'All' and 'None' links.
-        $('body').on('click', '[data-action="grade_edittree-index-bulkselect"]', edittree.toggleAllSelectItems);
-
         // Watch for the weight override checkboxes.
         $('body').on('change', '.weightoverride', edittree.toggleWeightInput);
 
@@ -47,9 +41,6 @@ define([
             bulkmove.val(1);
             form.submit();
         });
-
-        // CHeck the initial state of the move menu.
-        edittree.checkMoveMenuState();
     };
 
     /**
@@ -67,60 +58,6 @@ define([
         $('input[name="weight_' + row.data('itemid') + '"]').prop('disabled', !node.prop('checked'));
     };
 
-    /**
-     * Toggle all select boxes on or off.
-     *
-     * @method toggleAllSelectItems
-     * @param {EventFacade} e
-     * @private
-     */
-    edittree.toggleAllSelectItems = function(e) {
-        e.preventDefault();
-
-        var node = $(this),
-            row = node.closest('tr');
-        $('.' + row.data('category') + ' .itemselect').prop('checked', node.data('checked'));
-
-        edittree.checkMoveMenuState();
-    };
-
-    /**
-     * Get the move menu.
-     *
-     * @method getMoveMenu
-     * @private
-     * @return {jQuery}
-     */
-    edittree.getMoveMenu = function() {
-        return $('#menumoveafter');
-    };
-
-    /**
-     * Check whether any checkboxes are ticked.
-     *
-     * @method checkMoveMenuState
-     * @private
-     * @return {Boolean}
-     */
-    edittree.checkMoveMenuState = function() {
-        var menu = edittree.getMoveMenu();
-        if (!menu.length) {
-            return false;
-        }
-
-        var selected;
-        $('.itemselect').each(function() {
-            selected = $(this).prop('checked');
-
-            // Return early if any are checked.
-            return !selected;
-        });
-
-        menu.prop('disabled', !selected);
-
-        return selected;
-    };
-
     return /** @alias module:core_grades/edittree_index */ {
         enhance: edittree
     };
index 86cf33e..9b42775 100644 (file)
@@ -840,20 +840,24 @@ class grade_edit_tree_column_select extends grade_edit_tree_column {
     }
 
     public function get_category_cell($category, $levelclass, $params) {
+        global $OUTPUT;
+
         if (empty($params['eid'])) {
             throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
         }
-        $selectall = html_writer::link('#', get_string('all'), [
-            'data-action' => 'grade_edittree-index-bulkselect',
-            'data-checked' => true,
-        ]);
-        $selectnone = html_writer::link('#', get_string('none'), [
-            'data-action' => 'grade_edittree-index-bulkselect',
-            'data-checked' => false,
+
+        $togglegroup = $this->get_checkbox_togglegroup($category);
+        $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
+            'id' => $togglegroup,
+            'name' => $togglegroup,
+            'value' => 1,
+            'label' => get_string('all'),
+            'selectall' => get_string('all'),
+            'deselectall' => get_string('none'),
         ]);
 
         $categorycell = parent::get_category_cell($category, $levelclass, $params);
-        $categorycell->text = $selectall . ' / ' . $selectnone;
+        $categorycell->text = $OUTPUT->render($mastercheckbox);
         return $categorycell;
     }
 
@@ -864,12 +868,43 @@ class grade_edit_tree_column_select extends grade_edit_tree_column {
         $itemcell = parent::get_item_cell($item, $params);
 
         if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
-            $itemcell->text = '<label class="accesshide" for="select_'.$params['eid'].'">'.
-                get_string('select', 'grades', $item->itemname).'</label>
-                <input class="itemselect ignoredirty" type="checkbox" name="select_'.$params['eid'].'" id="select_'.$params['eid'].
-                '"/>';
+            global $OUTPUT;
+
+            // Fetch the grade item's category.
+            $category = grade_category::fetch(['id' => $item->categoryid]);
+            $togglegroup = $this->get_checkbox_togglegroup($category);
+
+            $checkboxid = 'select_' . $params['eid'];
+            $checkbox = new \core\output\checkbox_toggleall($togglegroup, false, [
+                'id' => $checkboxid,
+                'name' => $checkboxid,
+                'label' => get_string('select', 'grades', $item->itemname),
+                'labelclasses' => 'accesshide',
+                'classes' => 'itemselect ignoredirty',
+            ]);
+            $itemcell->text = $OUTPUT->render($checkbox);
         }
         return $itemcell;
     }
+
+    /**
+     * Generates a toggle group name for a bulk-action checkbox based on the given grade category.
+     *
+     * @param grade_category $category The grade category.
+     * @return string
+     */
+    protected function get_checkbox_togglegroup(grade_category $category): string {
+        $levels = [];
+        $categories = explode('/', $category->path);
+        foreach ($categories as $categoryid) {
+            $level = 'category' . $categoryid;
+            if (!in_array($level, $levels)) {
+                $levels[] = 'category' . $categoryid;
+            }
+        }
+        $togglegroup = implode(' ', $levels);
+
+        return $togglegroup;
+    }
 }
 
index 5c4c539..5767d76 100644 (file)
 {{!
     @template core_grades/edit_tree
 
-    Edit tree.
+    Edit tree template.
+
+    Context variables required for this template:
+    * actionurl - string - Form action URL.
+    * sesskey - string - The session key.
+    * notification - object - Context data for the notification.
+    * showsave - boolean - Whether to show the save changes button.
+    * showbulkmove - boolean - Whether to show the bulk move select menu.
+    * table - string - HTML content of the grade items table.
+    * bulkmoveoptions - array - Key-value pair array for the options of the the bulk move select menu element.
 
     Example context (json):
     {
-        "actionurl": "https://domain.example/grade/edit/tree/index.php?id=4",
+        "actionurl": "#",
         "sesskey": "fakesesskey",
         "notification": "",
         "table": "<table class='generaltable simple setup-grades' id='grade_edit_tree_table'><thead> <tr><th>Name</th><th>Weights</th><th>Max grade</th><th>Actions</th> </tr></thead><tbody></tbody></table>",
@@ -48,7 +57,8 @@
                 <div class="form-inline mt-3">
                     <input type="hidden" name="bulkmove" value="0" id="bulkmoveinput">
                     <label for="menumoveafter">{{#str}}moveselectedto, grades{{/str}}</label>
-                    <select name="moveafter" id="menumoveafter" class="ignoredirty singleselect custom-select form-control">
+                    <select name="moveafter" id="menumoveafter" class="ignoredirty singleselect custom-select form-control"
+                            data-action="toggle" data-toggle="action" data-togglegroup="category" disabled>
                         {{#bulkmoveoptions}}
                             <option value="{{value}}">{{name}}</option>
                         {{/bulkmoveoptions}}