MDL-65946 mod_scorm: Use new core/checkbox-toggleall
authorJun Pataleta <jun@moodle.com>
Fri, 12 Jan 2018 03:27:18 +0000 (11:27 +0800)
committerJun Pataleta <jun@moodle.com>
Fri, 19 Jul 2019 02:21:30 +0000 (10:21 +0800)
mod/scorm/classes/report.php
mod/scorm/report/basic/classes/report.php
mod/scorm/report/interactions/classes/report.php
mod/scorm/report/objectives/classes/report.php

index 60be5f3..10ae4d7 100644 (file)
@@ -54,4 +54,60 @@ class report {
     public function canview($contextmodule) {
         return true;
     }
+
+    /**
+     * Generates a checkbox that can be added to header tables to select/deselect all quiz attempts.
+     *
+     * @return string
+     */
+    protected function generate_master_checkbox(): string {
+        global $OUTPUT;
+
+        // Build the select/deselect all control.
+        $selectalltext = get_string('selectall', 'scorm');
+        $deselectalltext = get_string('selectnone', 'scorm');
+        $mastercheckbox = new \core\output\checkbox_toggleall('scorm-attempts', true, [
+            'name' => 'scorm-selectall-attempts',
+            'value' => 1,
+            'label' => $selectalltext,
+            'labelclasses' => 'accesshide',
+            'selectall' => $selectalltext,
+            'deselectall' => $deselectalltext,
+        ]);
+
+        return $OUTPUT->render($mastercheckbox);
+    }
+
+    /**
+     * Generates a checkbox for a row in the attempts table.
+     *
+     * @param string $name The checkbox's name attribute.
+     * @param string $value The checkbox's value.
+     * @return string
+     */
+    protected function generate_row_checkbox(string $name, string $value): string {
+        global $OUTPUT;
+
+        $checkbox = new \core\output\checkbox_toggleall('scorm-attempts', false, [
+            'name' => $name,
+            'value' => $value,
+        ]);
+        return $OUTPUT->render($checkbox);
+    }
+
+    /**
+     * Generates an action button that deletes the selected attempts.
+     */
+    protected function generate_delete_selected_button(): string {
+        $deleteselectedparams = array(
+            'type' => 'submit',
+            'value' => get_string('deleteselected', 'scorm'),
+            'class' => 'btn btn-secondary',
+            'data-action' => 'toggle',
+            'data-togglegroup' => 'scorm-attempts',
+            'data-toggle' => 'action',
+            'disabled' => true
+        );
+        return \html_writer::empty_tag('input', $deleteselectedparams);
+    }
 }
index eca98d8..5c1cfb3 100644 (file)
@@ -117,7 +117,7 @@ class report extends \mod_scorm\report {
             $headers = array();
             if (!$download && $candelete) {
                 $columns[] = 'checkbox';
-                $headers[] = null;
+                $headers[] = $this->generate_master_checkbox();
             }
             if (!$download && $CFG->grade_report_showuserimage) {
                 $columns[] = 'picture';
@@ -380,7 +380,7 @@ class report extends \mod_scorm\report {
                     }
                     if (in_array('checkbox', $columns)) {
                         if ($candelete && !empty($timetracks->start)) {
-                            $row[] = \html_writer::checkbox('attemptid[]', $scouser->userid . ':' . $scouser->attempt, false);
+                            $row[] = $this->generate_row_checkbox('attemptid[]', "{$scouser->userid}:{$scouser->attempt}");
                         } else if ($candelete) {
                             $row[] = '';
                         }
@@ -490,24 +490,7 @@ class report extends \mod_scorm\report {
                     if ($candelete) {
                         echo \html_writer::start_tag('table', array('id' => 'commands'));
                         echo \html_writer::start_tag('tr').\html_writer::start_tag('td');
-                        echo \html_writer::link('#', get_string('selectall', 'scorm'), array('id' => 'checkattempts'));
-                        echo ' / ';
-                        echo \html_writer::link('#', get_string('selectnone', 'scorm'), array('id' => 'uncheckattempts'));
-                        $PAGE->requires->js_amd_inline("
-                        require(['jquery'], function($) {
-                            $('#checkattempts').click(function(e) {
-                                $('#attemptsform').find('input:checkbox').prop('checked', true);
-                                e.preventDefault();
-                            });
-                            $('#uncheckattempts').click(function(e) {
-                                $('#attemptsform').find('input:checkbox').prop('checked', false);
-                                e.preventDefault();
-                            });
-                        });");
-                        echo '&nbsp;&nbsp;';
-                        echo \html_writer::empty_tag('input', array('type' => 'submit',
-                                                                    'value' => get_string('deleteselected', 'scorm'),
-                                                                    'class' => 'btn btn-secondary'));
+                        echo $this->generate_delete_selected_button();
                         echo \html_writer::end_tag('td').\html_writer::end_tag('tr').\html_writer::end_tag('table');
                         // Close form.
                         echo \html_writer::end_tag('div');
index f20b8c1..1deef30 100644 (file)
@@ -133,7 +133,7 @@ class report extends \mod_scorm\report {
             $headers = array();
             if (!$download && $candelete) {
                 $columns[] = 'checkbox';
-                $headers[] = null;
+                $headers[] = $this->generate_master_checkbox();
             }
             if (!$download && $CFG->grade_report_showuserimage) {
                 $columns[] = 'picture';
@@ -426,7 +426,7 @@ class report extends \mod_scorm\report {
                     }
                     if (in_array('checkbox', $columns)) {
                         if ($candelete && !empty($timetracks->start)) {
-                            $row[] = \html_writer::checkbox('attemptid[]', $scouser->userid . ':' . $scouser->attempt, false);
+                            $row[] = $this->generate_row_checkbox('attemptid[]', "{$scouser->userid}:{$scouser->attempt}");
                         } else if ($candelete) {
                             $row[] = '';
                         }
@@ -581,24 +581,7 @@ class report extends \mod_scorm\report {
                     if ($candelete) {
                         echo \html_writer::start_tag('table', array('id' => 'commands'));
                         echo \html_writer::start_tag('tr').\html_writer::start_tag('td');
-                        echo \html_writer::link('#', get_string('selectall', 'scorm'), array('id' => 'checkattempts'));
-                        echo ' / ';
-                        echo \html_writer::link('#', get_string('selectnone', 'scorm'), array('id' => 'uncheckattempts'));
-                        $PAGE->requires->js_amd_inline("
-                        require(['jquery'], function($) {
-                            $('#checkattempts').click(function(e) {
-                                $('#attemptsform').find('input:checkbox').prop('checked', true);
-                                e.preventDefault();
-                            });
-                            $('#uncheckattempts').click(function(e) {
-                                $('#attemptsform').find('input:checkbox').prop('checked', false);
-                                e.preventDefault();
-                            });
-                        });");
-                        echo '&nbsp;&nbsp;';
-                        echo \html_writer::empty_tag('input', array('type' => 'submit',
-                                                                    'value' => get_string('deleteselected', 'scorm'),
-                                                                    'class' => 'btn btn-secondary'));
+                        echo $this->generate_delete_selected_button();
                         echo \html_writer::end_tag('td').\html_writer::end_tag('tr').\html_writer::end_tag('table');
                         // Close form.
                         echo \html_writer::end_tag('div');
index 3f4f99c..85488c7 100644 (file)
@@ -127,7 +127,7 @@ class report extends \mod_scorm\report {
             $headers = array();
             if (!$download && $candelete) {
                 $columns[] = 'checkbox';
-                $headers[] = null;
+                $headers[] = $this->generate_master_checkbox();
             }
             if (!$download && $CFG->grade_report_showuserimage) {
                 $columns[] = 'picture';
@@ -416,7 +416,7 @@ class report extends \mod_scorm\report {
                     }
                     if (in_array('checkbox', $columns)) {
                         if ($candelete && !empty($timetracks->start)) {
-                            $row[] = \html_writer::checkbox('attemptid[]', $scouser->userid . ':' . $scouser->attempt, false);
+                            $row[] = $this->generate_row_checkbox('attemptid[]', "{$scouser->userid}:{$scouser->attempt}");
                         } else if ($candelete) {
                             $row[] = '';
                         }
@@ -580,24 +580,7 @@ class report extends \mod_scorm\report {
                     if ($candelete) {
                         echo \html_writer::start_tag('table', array('id' => 'commands'));
                         echo \html_writer::start_tag('tr').\html_writer::start_tag('td');
-                        echo \html_writer::link('#', get_string('selectall', 'scorm'), array('id' => 'checkattempts'));
-                        echo ' / ';
-                        echo \html_writer::link('#', get_string('selectnone', 'scorm'), array('id' => 'uncheckattempts'));
-                        $PAGE->requires->js_amd_inline("
-                        require(['jquery'], function($) {
-                            $('#checkattempts').click(function(e) {
-                                $('#attemptsform').find('input:checkbox').prop('checked', true);
-                                e.preventDefault();
-                            });
-                            $('#uncheckattempts').click(function(e) {
-                                $('#attemptsform').find('input:checkbox').prop('checked', false);
-                                e.preventDefault();
-                            });
-                        });");
-                        echo '&nbsp;&nbsp;';
-                        echo \html_writer::empty_tag('input', array('type' => 'submit',
-                                                                    'value' => get_string('deleteselected', 'scorm'),
-                                                                    'class' => 'btn btn-secondary'));
+                        echo $this->generate_delete_selected_button();
                         echo \html_writer::end_tag('td').\html_writer::end_tag('tr').\html_writer::end_tag('table');
                         // Close form.
                         echo \html_writer::end_tag('div');