MDL-66372 forumreport_summary: Added ability to select pagination size
authorMichael Hawkins <michaelh@moodle.com>
Mon, 14 Oct 2019 15:35:39 +0000 (23:35 +0800)
committerMichael Hawkins <michaelh@moodle.com>
Thu, 7 Nov 2019 04:58:02 +0000 (12:58 +0800)
This also includes some refactoring of the bulk actions menu, so both
elements are correctly position within the page, as well as fixing the
alignment of the bulk dropdown with the download dropdown.

mod/forum/report/summary/amd/build/filters.min.js
mod/forum/report/summary/amd/build/filters.min.js.map
mod/forum/report/summary/amd/src/filters.js
mod/forum/report/summary/classes/summary_table.php
mod/forum/report/summary/index.php
mod/forum/report/summary/renderer.php
mod/forum/report/summary/templates/bulk_action_menu.mustache

index dd42dab..305364c 100644 (file)
Binary files a/mod/forum/report/summary/amd/build/filters.min.js and b/mod/forum/report/summary/amd/build/filters.min.js differ
index 86c45a9..ccaac77 100644 (file)
Binary files a/mod/forum/report/summary/amd/build/filters.min.js.map and b/mod/forum/report/summary/amd/build/filters.min.js.map differ
index 30fc20d..1950f50 100644 (file)
@@ -105,6 +105,14 @@ export const init = (root) => {
         generateWithFilters(event, event.target.search.substr(1));
     });
 
+    // Override rows per page submission so it generates with filters.
+    if (document.forms.selectperpage) {
+        document.forms.selectperpage.onsubmit = (event) => {
+            let getparam = 'perpage=' + document.forms.selectperpage.elements.perpage.value;
+            generateWithFilters(event, getparam);
+        };
+    }
+
     // Submit report via filter
     const submitWithFilter = (containerelement) => {
         // Close the container (eg popover).
index 3032c49..11b199f 100644 (file)
@@ -54,7 +54,10 @@ class summary_table extends table_sql {
     public $sql;
 
     /** @var int The number of rows to be displayed per page. */
-    protected $perpage = 25;
+    protected $perpage = 50;
+
+    /** @var array The values available for pagination size per page. */
+    protected $perpageoptions = [50, 100, 200];
 
     /** @var \stdClass The course module object of the forum being reported on. */
     protected $cm;
@@ -75,6 +78,9 @@ class summary_table extends table_sql {
      */
     protected $context = null;
 
+    /** @var bool Whether the user has the capability/capabilities to perform bulk operations. */
+    protected $bulkoperations = false;
+
     /**
      * @var bool
      */
@@ -92,8 +98,9 @@ class summary_table extends table_sql {
      * @param array $filters Report filters in the format 'type' => [values].
      * @param bool $bulkoperations Is the user allowed to perform bulk operations?
      * @param bool $canseeprivatereplies Whether the user can see all private replies or not.
+     * @param int $perpage The number of rows to display per page.
      */
-    public function __construct(int $courseid, array $filters, bool $bulkoperations, bool $canseeprivatereplies) {
+    public function __construct(int $courseid, array $filters, bool $bulkoperations, bool $canseeprivatereplies, int $perpage) {
         global $USER, $OUTPUT;
 
         $forumid = $filters['forums'][0];
@@ -103,6 +110,7 @@ class summary_table extends table_sql {
         $this->cm = get_coursemodule_from_instance('forum', $forumid, $courseid);
         $this->context = \context_module::instance($this->cm->id);
         $this->canseeprivatereplies = $canseeprivatereplies;
+        $this->perpage = $perpage;
 
         // Only show their own summary unless they have permission to view all.
         if (!has_capability('forumreport/summary:viewall', $this->context)) {
@@ -794,4 +802,83 @@ class summary_table extends table_sql {
 
         return $this->showwordcharcounts;
     }
+
+    /**
+     * Set whether the user has the capability/capabilities to perform bulk operations.
+     *
+     * @param bool $allowbulkoperations Whether the user has the relevant capability/capabilities.
+     * @return void
+     */
+    public function set_bulkoperations(bool $allowbulkoperations): void {
+        $this->bulkoperations = $allowbulkoperations;
+    }
+
+    /**
+     * Fetch whether the user has the capability/capabilities to perform bulk operations.
+     *
+     * @return bool
+     */
+    public function get_bulkoperations(): bool {
+        return $this->bulkoperations;
+    }
+
+    /**
+     * Fetch the number of items to be displayed per page.
+     *
+     * @return int
+     */
+    public function get_perpage(): int {
+        return $this->perpage;
+    }
+
+    /**
+     * Overriding method to render the bulk actions and items per page pagination options directly below the table.
+     *
+     * @return void
+     */
+    public function wrap_html_finish(): void {
+        global $OUTPUT;
+
+        $data = new \stdClass();
+        $data->showbulkactions = $this->get_bulkoperations();
+
+        if ($data->showbulkactions) {
+            $data->id = 'formactionid';
+            $data->attributes = [
+                [
+                    'name' => 'data-action',
+                    'value' => 'toggle'
+                ],
+                [
+                    'name' => 'data-togglegroup',
+                    'value' => 'summaryreport-table'
+                ],
+                [
+                    'name' => 'data-toggle',
+                    'value' => 'action'
+                ],
+                [
+                    'name' => 'disabled',
+                    'value' => true
+                ]
+            ];
+            $data->actions = [
+                [
+                    'value' => '#messageselect',
+                    'name' => get_string('messageselectadd')
+                ]
+            ];
+        }
+
+        // Include the pagination size selector.
+        $perpageoptions = array_combine($this->perpageoptions, $this->perpageoptions);
+        $selected = in_array($this->perpage, $this->perpageoptions) ? $this->perpage : $this->perpageoptions[0];
+        $perpageselect = new \single_select(new \moodle_url(''), 'perpage',
+                $perpageoptions, $selected, null, 'selectperpage');
+        $perpageselect->label = get_string('perpage', 'moodle');
+
+        $data->perpage = $OUTPUT->render($perpageselect);
+
+        echo $OUTPUT->render_from_template('forumreport_summary/bulk_action_menu', $data);
+    }
 }
index 9872fc9..f2e7e3d 100644 (file)
@@ -30,7 +30,7 @@ if (isguestuser()) {
 
 $courseid = required_param('courseid', PARAM_INT);
 $forumid = required_param('forumid', PARAM_INT);
-$perpage = optional_param('perpage', 25, PARAM_INT);
+$perpage = optional_param('perpage', 50, PARAM_INT);
 $filters = [];
 
 // Establish filter values.
@@ -81,7 +81,7 @@ $PAGE->navbar->add(get_string('nodetitle', "forumreport_summary"));
 $bulkoperations = !$download && !empty($CFG->messaging) && has_capability('moodle/course:bulkmessaging', $context);
 $canseeprivatereplies = has_capability('mod/forum:readprivatereplies', $context);
 
-$table = new \forumreport_summary\summary_table($courseid, $filters, $bulkoperations, $canseeprivatereplies);
+$table = new \forumreport_summary\summary_table($courseid, $filters, $bulkoperations, $canseeprivatereplies, $perpage);
 $table->baseurl = $url;
 
 if ($download) {
@@ -99,11 +99,6 @@ if ($download) {
 
     echo $renderer->render_filters_form($cm, $url, $filters);
     $table->show_download_buttons_at(array(TABLE_P_BOTTOM));
-    echo $renderer->render_summary_table($table, $perpage);
-
-    if ($bulkoperations) {
-        echo $renderer->render_bulk_action_menu();
-    }
-
+    echo $renderer->render_summary_table($table, $bulkoperations);
     echo $OUTPUT->footer();
 }
index 0ee81e8..6e3181b 100644 (file)
@@ -53,55 +53,22 @@ class forumreport_summary_renderer extends plugin_renderer_base {
      * Render the summary report table.
      *
      * @param summary_table $table The summary table to be rendered.
-     * @param int $perpage Number of results to render per page.
+     * @param int $bulkoperations Whether the user has bulk operations capability/capabilities.
      * @return string The report table HTML.
      */
-    public function render_summary_table(summary_table $table, int $perpage): string {
+    public function render_summary_table(summary_table $table, bool $bulkoperations): string {
         // Buffer so calling script can output the report as required.
         ob_start();
 
+        $table->set_bulkoperations($bulkoperations);
+
         // Render table.
-        $table->out($perpage, false);
+        $table->out($table->get_perpage(), false);
 
         $tablehtml = ob_get_contents();
 
         ob_end_clean();
 
-        return $this->render_from_template('forumreport_summary/report', ['tablehtml' => $tablehtml, 'placeholdertext' => false]);
-    }
-
-    /**
-     * Render the bulk action menu for the forum summary report.
-     * @return string
-     */
-    public function render_bulk_action_menu(): string {
-        $data = new stdClass();
-        $data->id = 'formactionid';
-        $data->attributes = [
-            [
-                'name' => 'data-action',
-                'value' => 'toggle'
-            ],
-            [
-                'name' => 'data-togglegroup',
-                'value' => 'summaryreport-table'
-            ],
-            [
-                'name' => 'data-toggle',
-                'value' => 'action'
-            ],
-            [
-                'name' => 'disabled',
-                'value' => true
-            ]
-        ];
-        $data->actions = [
-            [
-                'value' => '#messageselect',
-                'name' => get_string('messageselectadd')
-            ]
-        ];
-
-        return $this->render_from_template('forumreport_summary/bulk_action_menu', $data);
+        return $this->render_from_template('forumreport_summary/report', ['tablehtml' => $tablehtml]);
     }
 }
index bc01440..5f88711 100644 (file)
@@ -21,6 +21,7 @@
 
     Example context (json):
     {
+        "showbulkactions": true,
         "id": "formactionid",
         "attributes": [
             {
                 "name": "Send a message",
                 "value": "#messageselect"
             }
-        ]
+        ],
+        "perpage" : "<div class='singleselect'></div>"
     }
 }}
 
-<br /><div class="buttons"><div class="form-inline">
-    <label for="{{id}}">{{#str}}withselectedusers{{/str}}</label>
-    <select id="{{id}}" class="select custom-select ml-2" {{#attributes}}{{name}}="{{value}}" {{/attributes}}>
-        <option value="">{{#str}}choosedots{{/str}}</option>
-    {{#actions}}
-        <option value="{{value}}">{{name}}</option>
-    {{/actions}}
-    </select>
-</div></div>
+<br />
+<div class="d-inline-block w-100">
+    {{#showbulkactions}}
+    <div class="buttons float-left">
+        <div class="form-inline pl-1">
+            <label for="{{id}}">{{#str}}withselectedusers{{/str}}</label>
+            <select id="{{id}}" class="select custom-select ml-4" {{#attributes}}{{name}}="{{value}}" {{/attributes}}>
+                <option value="">{{#str}}choosedots{{/str}}</option>
+                {{#actions}}
+                <option value="{{value}}">{{name}}</option>
+                {{/actions}}
+            </select>
+        </div>
+    </div>
+    {{/showbulkactions}}
+    <div class="float-right">
+        {{{perpage}}}
+    </div>
+</div>
+
 {{#js}}
 require(['jquery', 'core_message/message_send_bulk'], function($, BulkSender) {
     $('#{{id}}').on('change', function(e) {