MDL-62487 quiz manual grading: store options as user preferences
authorTim Hunt <T.J.Hunt@open.ac.uk>
Sat, 28 Mar 2020 22:49:43 +0000 (22:49 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 15 May 2020 12:12:08 +0000 (13:12 +0100)
mod/quiz/report/grading/gradingsettings_form.php
mod/quiz/report/grading/report.php
mod/quiz/report/grading/tests/behat/grading.feature
mod/quiz/tests/behat/behat_mod_quiz.php

index 84983ee..3bb8314 100644 (file)
@@ -48,7 +48,7 @@ class quiz_grading_settings_form extends moodleform {
         $this->counts = $counts;
         $this->shownames = $shownames;
         $this->showidnumbers = $showidnumbers;
-        parent::__construct($CFG->wwwroot . '/mod/quiz/report.php', null, 'get');
+        parent::__construct($CFG->wwwroot . '/mod/quiz/report.php');
     }
 
     protected function definition() {
index eb376fc..0bbcecb 100644 (file)
@@ -84,9 +84,13 @@ class quiz_grading_report extends quiz_default_report {
         if (!in_array($grade, array('all', 'needsgrading', 'autograded', 'manuallygraded'))) {
             $grade = null;
         }
-        $pagesize = optional_param('pagesize', self::DEFAULT_PAGE_SIZE, PARAM_INT);
+        $pagesize = optional_param('pagesize',
+                get_user_preferences('quiz_grading_pagesize', self::DEFAULT_PAGE_SIZE),
+                PARAM_INT);
         $page = optional_param('page', 0, PARAM_INT);
-        $order = optional_param('order', self::DEFAULT_ORDER, PARAM_ALPHA);
+        $order = optional_param('order',
+                get_user_preferences('quiz_grading_order', self::DEFAULT_ORDER),
+                PARAM_ALPHA);
 
         // Assemble the options required to reload this page.
         $optparams = array('includeauto', 'page');
@@ -143,6 +147,12 @@ class quiz_grading_report extends quiz_default_report {
         }
 
         $hasquestions = quiz_has_questions($this->quiz->id);
+        if (!$hasquestions) {
+            $this->print_header_and_tabs($cm, $course, $quiz, 'grading');
+            echo $this->renderer->render_quiz_no_question_notification($quiz, $cm, $this->context);
+            return true;
+        }
+
         $counts = null;
         if ($slot && $hasquestions) {
             // Make sure there is something to do.
@@ -159,15 +169,9 @@ class quiz_grading_report extends quiz_default_report {
             }
         }
 
-        // Start output.
-        $this->print_header_and_tabs($cm, $course, $quiz, 'grading');
-
         // What sort of page to display?
-        if (!$hasquestions) {
-            echo $this->renderer->render_quiz_no_question_notification($quiz, $cm, $this->context);
-
-        } else if (!$slot) {
-            echo $this->display_index($includeauto);
+        if (!$slot) {
+            $this->display_index($includeauto);
 
         } else {
             echo $this->display_grading_interface($slot, $questionid, $grade,
@@ -305,9 +309,15 @@ class quiz_grading_report extends quiz_default_report {
         return $result;
     }
 
+    /**
+     * Display the report front page which summarises the number of attempts to grade.
+     *
+     * @param bool $includeauto whether to show automatically-graded questions.
+     */
     protected function display_index($includeauto) {
         global $PAGE;
-        $output = '';
+
+        $this->print_header_and_tabs($this->cm, $this->course, $this->quiz, 'grading');
 
         if ($groupmode = groups_get_activity_groupmode($this->cm)) {
             // Groups is being used.
@@ -319,7 +329,7 @@ class quiz_grading_report extends quiz_default_report {
         } else {
             $linktext = get_string('alsoshowautomaticallygraded', 'quiz_grading');
         }
-        $output .= $this->renderer->render_display_index_heading($linktext, $this->list_questions_url(!$includeauto));
+        echo $this->renderer->render_display_index_heading($linktext, $this->list_questions_url(!$includeauto));
         $data = [];
         $header = [];
 
@@ -361,23 +371,30 @@ class quiz_grading_report extends quiz_default_report {
 
             $data[] = $row;
         }
-        $output .= $this->renderer->render_questions_table($includeauto, $data, $header);
-        return $output;
+        echo $this->renderer->render_questions_table($includeauto, $data, $header);
     }
 
+    /**
+     * Display the UI for grading attempts at one question.
+     *
+     * @param int $slot identifies which question to grade.
+     * @param int $questionid identifies which question to grade.
+     * @param string $grade type of attempts to grade.
+     * @param int $pagesize number of questions to show per page.
+     * @param int $page current page number.
+     * @param bool $shownames whether student names should be shown.
+     * @param bool $showidnumbers wither student idnumbers should be shown.
+     * @param string $order preferred order of attempts.
+     * @param stdClass $counts object that stores the number of each type of attempt.
+     */
     protected function display_grading_interface($slot, $questionid, $grade,
             $pagesize, $page, $shownames, $showidnumbers, $order, $counts) {
-        $output = '';
 
         if ($pagesize * $page >= $counts->$grade) {
             $page = 0;
         }
 
-        list($qubaids, $count) = $this->get_usage_ids_where_question_in_state(
-                $grade, $slot, $questionid, $order, $page, $pagesize);
-        $attempts = $this->load_attempts_by_usage_ids($qubaids);
-
-        // Prepare the form.
+        // Prepare the options form.
         $hidden = [
             'id' => $this->cm->id,
             'mode' => 'grading',
@@ -397,6 +414,18 @@ class quiz_grading_report extends quiz_default_report {
         $settings->order = $order;
         $mform->set_data($settings);
 
+        // If the form was submitted, save the user preferences, and
+        // redirect to a cleaned-up GET URL.
+        if ($mform->get_data()) {
+            set_user_preference('quiz_grading_pagesize', $pagesize);
+            set_user_preference('quiz_grading_order', $order);
+            redirect($this->grade_question_url($slot, $questionid, $grade, $page));
+        }
+
+        list($qubaids, $count) = $this->get_usage_ids_where_question_in_state(
+                $grade, $slot, $questionid, $order, $page, $pagesize);
+        $attempts = $this->load_attempts_by_usage_ids($qubaids);
+
         // Question info.
         $questioninfo = new stdClass();
         $questioninfo->number = $this->questions[$slot]->number;
@@ -409,6 +438,8 @@ class quiz_grading_report extends quiz_default_report {
         $paginginfo->of = $count;
         $qubaidlist = implode(',', $qubaids);
 
+        $this->print_header_and_tabs($this->cm, $this->course, $this->quiz, 'grading');
+
         $gradequestioncontent = '';
         foreach ($qubaids as $qubaid) {
             $attempt = $attempts[$qubaid];
@@ -442,7 +473,7 @@ class quiz_grading_report extends quiz_default_report {
                 'sesskey' => sesskey()
         ];
 
-        $output .= $this->renderer->render_grading_interface(
+        echo $this->renderer->render_grading_interface(
                 $questioninfo,
                 $this->list_questions_url(),
                 $mform,
@@ -452,8 +483,6 @@ class quiz_grading_report extends quiz_default_report {
                 $hiddeninputs,
                 $gradequestioncontent
         );
-
-        return $output;
     }
 
     /**
index 253f70a..2a308a1 100644 (file)
@@ -4,8 +4,7 @@ Feature: Basic use of the Manual grading report
   As a teacher
   I need to use the manual grading report
 
-  @javascript
-  Scenario: Use the Manual grading report
+  Background:
     Given the following "users" exist:
       | username | firstname | lastname | email                | idnumber |
       | teacher1 | T1        | Teacher1 | teacher1@example.com | T1000    |
@@ -30,10 +29,10 @@ Feature: Basic use of the Manual grading report
       | question          | page |
       | Short answer 001  | 1    |
 
+  Scenario: Use the Manual grading report
+
     # Check report shows nothing when there are no attempts.
-    When I log in as "teacher1"
-    And I am on "Course 1" course homepage
-    And I follow "Quiz 1"
+    When I am on the "Quiz 1" "mod_quiz > View" page logged in as "teacher1"
     And I navigate to "Results > Manual grading" in current page administration
     Then I should see "Manual grading"
     And I should see "Quiz 1"
@@ -44,7 +43,7 @@ Feature: Basic use of the Manual grading report
     # Use the manual grading report.
     And user "student1" has attempted "Quiz 1" with responses:
       | slot | response |
-      |   1  | Paris    |
+      | 1    | Paris    |
     And I reload the page
     And I should see "Short answer 001"
     And "Short answer 001" row "To grade" column of "questionstograde" table should contain "0"
@@ -65,3 +64,22 @@ Feature: Basic use of the Manual grading report
     And I should see "All selected attempts have been graded. Returning to the list of questions."
     And "Short answer 001" row "To grade" column of "questionstograde" table should contain "0"
     And "Short answer 001" row "Already graded" column of "questionstograde" table should contain "1"
+
+  Scenario: Manual grading settings are remembered as user preferences
+    Given user "student1" has attempted "Quiz 1" with responses:
+      | slot | response |
+      | 1    | Paris    |
+    When I am on the "Quiz 1" "mod_quiz > Manual grading report" page logged in as "teacher1"
+    And I follow "Also show questions that have been graded automatically"
+    And I click on "update grades" "link" in the "Short answer 001" "table_row"
+    And I set the following fields to these values:
+      | Questions per page | 42      |
+      | Order attempts     | By date |
+    And I press "Change options"
+    And I log out
+    And I am on the "Quiz 1" "mod_quiz > Manual grading report" page logged in as "teacher1"
+    And I follow "Also show questions that have been graded automatically"
+    And I click on "update grades" "link" in the "Short answer 001" "table_row"
+    Then the following fields match these values:
+      | Questions per page | 42      |
+      | Order attempts     | By date |
index 563ffbe..5db5ba7 100644 (file)
@@ -108,6 +108,10 @@ class behat_mod_quiz extends behat_question_base {
                 return new moodle_url('/mod/quiz/report.php',
                     ['id' => $this->get_cm_by_quiz_name($identifier)->id, 'mode' => 'statistics']);
 
+            case 'Manual grading report':
+                return new moodle_url('/mod/quiz/report.php',
+                        ['id' => $this->get_cm_by_quiz_name($identifier)->id, 'mode' => 'grading']);
+
             case 'Attempt review':
                 if (substr_count($identifier, ' > ') !== 2) {
                     throw new coding_exception('For "attempt review", name must be ' .