Merge branch 'MDL-64761-master' of git://github.com/dpalou/moodle
authorSara Arjona <sara@moodle.com>
Wed, 27 Feb 2019 14:36:18 +0000 (15:36 +0100)
committerSara Arjona <sara@moodle.com>
Wed, 27 Feb 2019 14:36:18 +0000 (15:36 +0100)
mod/assign/externallib.php
mod/assign/locallib.php
mod/assign/tests/externallib_test.php

index ad761dc..f567078 100644 (file)
@@ -211,7 +211,7 @@ class mod_assign_external extends external_api {
                 'attemptnumber'     => new external_value(PARAM_INT, 'attempt number'),
                 'timecreated'       => new external_value(PARAM_INT, 'grade creation time'),
                 'timemodified'      => new external_value(PARAM_INT, 'grade last modified time'),
-                'grader'            => new external_value(PARAM_INT, 'grader'),
+                'grader'            => new external_value(PARAM_INT, 'grader, -1 if grader is hidden'),
                 'grade'             => new external_value(PARAM_TEXT, 'grade'),
                 'gradefordisplay'   => new external_value(PARAM_RAW, 'grade rendered into a format suitable for display',
                                                             VALUE_OPTIONAL),
@@ -2383,7 +2383,7 @@ class mod_assign_external extends external_api {
             $result['gradingsummary'] = $gradingsummary;
         }
         // Show the grader's identity if 'Hide Grader' is disabled or has the 'Show Hidden Grader' capability.
-        $showgradername = (has_capability('mod/assign:showhiddengrader', $context, $user) or
+        $showgradername = (has_capability('mod/assign:showhiddengrader', $context) or
             !$assign->is_hidden_grader());
 
         // Did we submit anything?
@@ -2432,7 +2432,7 @@ class mod_assign_external extends external_api {
         if ($feedback) {
             if ($feedback->grade) {
                 if (!$showgradername) {
-                    $feedback->grade->grader = false;
+                    $feedback->grade->grader = -1;
                 }
                 $feedbackplugins = $assign->get_feedback_plugins();
                 $feedback->plugins = self::get_plugins_data($assign, $feedbackplugins, $feedback->grade);
@@ -2473,7 +2473,7 @@ class mod_assign_external extends external_api {
                 if ($grade) {
                     // From object to id.
                     if (!$showgradername) {
-                        $grade->grader = false;
+                        $grade->grader = -1;
                     } else {
                         $grade->grader = $grade->grader->id;
                     }
index cacc73a..d766387 100644 (file)
@@ -5287,7 +5287,7 @@ class assign {
 
             // Show the grader's identity if 'Hide Grader' is disabled or has the 'Show Hidden Grader' capability.
             $showgradername = (
-                    has_capability('mod/assign:showhiddengrader', $this->context, $user) or
+                    has_capability('mod/assign:showhiddengrader', $this->context) or
                     !$this->is_hidden_grader()
             );
 
index f4dbf08..b9754a7 100644 (file)
@@ -2159,6 +2159,64 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
 
     }
 
+    /**
+     * Test hidden grader for get_submission_status.
+     */
+    public function test_get_submission_status_hidden_grader() {
+        $this->resetAfterTest(true);
+
+        list($assign, $instance, $student1, $student2, $teacher, $g1, $g2) = $this->create_submission_for_testing_status(true);
+
+        // Grade the assign for the student1.
+        $this->setUser($teacher);
+
+        $data = new stdClass();
+        $data->grade = '50.0';
+        $data->assignfeedbackcomments_editor = ['text' => ''];
+        $assign->testable_apply_grade_to_user($data, $student1->id, 0);
+
+        $this->setUser($student1);
+
+        // Check that the student can see the grader by default.
+        $result = mod_assign_external::get_submission_status($assign->get_instance()->id);
+        // We expect debugging because of the $PAGE object, this won't happen in a normal WS request.
+        $this->assertDebuggingCalled();
+
+        $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result);
+
+        $this->assertTrue(isset($result['feedback']));
+        $this->assertTrue(isset($result['feedback']['grade']));
+        $this->assertEquals($teacher->id, $result['feedback']['grade']['grader']);
+
+        // Now change the setting so the grader is hidden.
+        $this->setAdminUser();
+
+        $instance = $assign->get_instance();
+        $instance->instance = $instance->id;
+        $instance->hidegrader = true;
+        $assign->update_instance($instance);
+
+        $this->setUser($student1);
+
+        // Check that the student cannot see the grader anymore.
+        $result = mod_assign_external::get_submission_status($assign->get_instance()->id);
+        $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result);
+
+        $this->assertTrue(isset($result['feedback']));
+        $this->assertTrue(isset($result['feedback']['grade']));
+        $this->assertEquals(-1, $result['feedback']['grade']['grader']);
+
+        // Check that the teacher can see the grader.
+        $this->setUser($teacher);
+
+        $result = mod_assign_external::get_submission_status($assign->get_instance()->id, $student1->id);
+        $result = external_api::clean_returnvalue(mod_assign_external::get_submission_status_returns(), $result);
+
+        $this->assertTrue(isset($result['feedback']));
+        $this->assertTrue(isset($result['feedback']['grade']));
+        $this->assertEquals($teacher->id, $result['feedback']['grade']['grader']);
+    }
+
     /**
      * get_participant should throw an excaption if the requested assignment doesn't exist.
      *