From b50ff710525e467101fff9f73051d71e05bd861a Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 9 May 2017 09:38:25 +0200 Subject: [PATCH] MDL-58860 mod_lesson: Fix get_attempts_overview for no attempts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WS get_attempts_overview was failing when there weren’t attempts in the given assignment. --- mod/lesson/classes/external.php | 17 +++++++++-------- mod/lesson/tests/external_test.php | 11 +++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/mod/lesson/classes/external.php b/mod/lesson/classes/external.php index a3abccdc7e4..7763d6e3fe8 100644 --- a/mod/lesson/classes/external.php +++ b/mod/lesson/classes/external.php @@ -1681,7 +1681,7 @@ class mod_lesson_external extends external_api { $params = array('lessonid' => $lessonid, 'groupid' => $groupid); $params = self::validate_parameters(self::get_attempts_overview_parameters(), $params); - $studentsdata = $warnings = array(); + $warnings = array(); list($lesson, $course, $cm, $context, $lessonrecord) = self::validate_lesson($params['lessonid']); require_capability('mod/lesson:viewreports', $context); @@ -1705,15 +1705,15 @@ class mod_lesson_external extends external_api { } } - list($table, $data) = lesson_get_overview_report_table_and_data($lesson, $groupid); - if ($data !== false) { - $studentsdata = $data; - } - $result = array( - 'data' => $studentsdata, 'warnings' => $warnings ); + + list($table, $data) = lesson_get_overview_report_table_and_data($lesson, $groupid); + if ($data !== false) { + $result['data'] = $data; + } + return $result; } @@ -1756,7 +1756,8 @@ class mod_lesson_external extends external_api { ) ), 'Students data, including attempts.', VALUE_OPTIONAL ), - ) + ), + 'Attempts overview data (empty for no attemps).', VALUE_OPTIONAL ), 'warnings' => new external_warnings(), ) diff --git a/mod/lesson/tests/external_test.php b/mod/lesson/tests/external_test.php index f747614b212..3131639f75f 100644 --- a/mod/lesson/tests/external_test.php +++ b/mod/lesson/tests/external_test.php @@ -1181,6 +1181,17 @@ class mod_lesson_external_testcase extends externallib_advanced_testcase { $this->assertCount(2, $result['data']['students']); } + /** + * Test get_attempts_overview when there aren't attempts. + */ + public function test_get_attempts_overview_no_attempts() { + $this->setAdminUser(); + $result = mod_lesson_external::get_attempts_overview($this->lesson->id); + $result = external_api::clean_returnvalue(mod_lesson_external::get_attempts_overview_returns(), $result); + $this->assertCount(0, $result['warnings']); + $this->assertArrayNotHasKey('data', $result); + } + /** * Test get_user_attempt */