Merge branch 'MDL-70110-master' of https://github.com/septatrix/moodle
authorSara Arjona <sara@moodle.com>
Thu, 6 May 2021 11:15:59 +0000 (13:15 +0200)
committerSara Arjona <sara@moodle.com>
Thu, 6 May 2021 11:15:59 +0000 (13:15 +0200)
mod/assign/externallib.php
mod/assign/tests/externallib_test.php

index de1b564..86d81e3 100644 (file)
@@ -754,14 +754,15 @@ class mod_assign_external extends external_api {
             $placeholders = array('assignid1' => $assign->get_instance()->id,
                                   'assignid2' => $assign->get_instance()->id);
 
-            $submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt
+            $submissionmaxattempt = 'SELECT mxs.userid, mxs.groupid, MAX(mxs.attemptnumber) AS maxattempt
                                      FROM {assign_submission} mxs
-                                     WHERE mxs.assignment = :assignid1 GROUP BY mxs.userid';
+                                     WHERE mxs.assignment = :assignid1 GROUP BY mxs.userid, mxs.groupid';
 
             $sql = "SELECT mas.id, mas.assignment,mas.userid,".
                    "mas.timecreated,mas.timemodified,mas.status,mas.groupid,mas.attemptnumber ".
                    "FROM {assign_submission} mas ".
                    "JOIN ( " . $submissionmaxattempt . " ) smx ON mas.userid = smx.userid ".
+                   "AND mas.groupid = smx.groupid ".
                    "WHERE mas.assignment = :assignid2 AND mas.attemptnumber = smx.maxattempt";
 
             if (!empty($params['status'])) {
index ec5afbf..d735c04 100644 (file)
@@ -455,6 +455,9 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals(1, count($result['assignments']));
     }
 
+    /**
+     * Test get_submissions with teamsubmission enabled
+     */
     public function test_get_submissions_group_submission() {
         global $DB;
 
@@ -497,6 +500,86 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals(0, $submission['userid']);
     }
 
+    /**
+     * Test get_submissions with teamsubmission enabled
+     * and a group having a higher attemptnumber than another
+     */
+    public function test_get_submissions_group_submission_attemptnumber() {
+        global $DB;
+        $this->resetAfterTest(true);
+
+        $result = $this->create_assign_with_student_and_teacher([
+            'assignsubmission_onlinetext_enabled' => 1,
+            'attemptreopenmethod' => 'manual',
+            'teamsubmission' => 1,
+        ]);
+        $assignmodule = $result['assign'];
+        $course = $result['course'];
+
+        $teacher = $result['teacher'];
+        $student1 = $result['student'];
+        $student2 = self::getDataGenerator()->create_user();
+
+        // Enrol second user into the course.
+        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
+        $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
+
+        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
+        groups_add_member($group1, $student1);
+        $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
+        groups_add_member($group2, $student2);
+
+        $this->setUser($student1);
+        mod_assign_external::save_submission(
+            $assignmodule->id,
+            [
+                'onlinetext_editor' => [
+                    'text' => 'Group 1, Submission 1',
+                    'format' => FORMAT_PLAIN,
+                    'itemid' => file_get_unused_draft_itemid(),
+                ]
+            ]
+        );
+        $this->setUser($student2);
+        mod_assign_external::save_submission(
+            $assignmodule->id,
+            [
+                'onlinetext_editor' => [
+                    'text' => 'Group 2, Submission 1',
+                    'format' => FORMAT_PLAIN,
+                    'itemid' => file_get_unused_draft_itemid(),
+                ]
+            ]
+        );
+        mod_assign_external::submit_for_grading($assignmodule->id, 1);
+        $this->setUser($teacher);
+        mod_assign_external::save_grade($assignmodule->id, $student2->id, 0, -1, 1, "", 1);
+        $this->setUser($student2);
+        mod_assign_external::save_submission(
+            $assignmodule->id,
+            [
+                'onlinetext_editor' => [
+                    'text' => 'Group 2, Submission 2',
+                    'format' => FORMAT_PLAIN,
+                    'itemid' => file_get_unused_draft_itemid(),
+                ]
+            ]
+        );
+
+        $this->setUser($teacher);
+        $result = mod_assign_external::get_submissions([$assignmodule->id]);
+        $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result);
+
+        $this->assertEquals(1, count($result['assignments']));
+        [$assignment] = $result['assignments'];
+        $this->assertEquals($assignmodule->id, $assignment['assignmentid']);
+
+        $this->assertEquals(2, count($assignment['submissions']));
+        [$submission1, $submission2] = $assignment['submissions'];
+        $this->assertEquals("Group 1, Submission 1", $submission1['plugins'][0]['editorfields'][0]['text']);
+        $this->assertEquals("Group 2, Submission 2", $submission2['plugins'][0]['editorfields'][0]['text']);
+    }
+
     /**
      * Test get_user_flags
      */