From b9c148dbdecedc36f7d218733428eeac7e043541 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 21 Dec 2023 16:37:34 +0100 Subject: [PATCH] MDL-80392 completion: Temporary fix for completion state via exporters --- .../external/completion_info_exporter.php | 11 ++- completion/tests/externallib_test.php | 69 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/completion/classes/external/completion_info_exporter.php b/completion/classes/external/completion_info_exporter.php index de710d276a9..c1a1b55a24c 100644 --- a/completion/classes/external/completion_info_exporter.php +++ b/completion/classes/external/completion_info_exporter.php @@ -65,6 +65,9 @@ class completion_info_exporter extends \core\external\exporter { * @return array Keys are the property names, values are their values. */ protected function get_other_values(renderer_base $output): array { + global $CFG; + require_once($CFG->libdir . '/completionlib.php'); + $cmcompletion = \core_completion\cm_completion_details::get_instance($this->cminfo, $this->userid); $cmcompletiondetails = $cmcompletion->get_details(); @@ -75,8 +78,14 @@ class completion_info_exporter extends \core\external\exporter { 'rulevalue' => (array)$rulevalue, ]; } + // Temporary fix for 4.3 only to return via state COMPLETION_COMPLETE depending on the current state and overall status. + $state = $cmcompletion->get_overall_completion(); + if ($state == COMPLETION_COMPLETE_FAIL && $cmcompletion->is_overall_complete()) { + $state = COMPLETION_COMPLETE; + } + return [ - 'state' => $cmcompletion->get_overall_completion(), + 'state' => $state, 'timecompleted' => $cmcompletion->get_timemodified(), 'overrideby' => $cmcompletion->overridden_by(), 'valueused' => \core_availability\info::completion_value_used($this->course, $this->cminfo->id), diff --git a/completion/tests/externallib_test.php b/completion/tests/externallib_test.php index 8289932ec1b..4c0008bd9ed 100644 --- a/completion/tests/externallib_test.php +++ b/completion/tests/externallib_test.php @@ -34,6 +34,7 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); * @copyright 2015 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.9 + * @coversDefaultClass \core_completion_external */ class externallib_test extends externallib_advanced_testcase { @@ -306,6 +307,74 @@ class externallib_test extends externallib_advanced_testcase { $this->assertCount($numberofcompletions, $result['statuses']); } + /** + * Test get_activities_completion_status with overall completion + * @covers ::get_activities_completion_status + */ + public function test_get_activities_completion_status_overall() { + global $DB; + + $this->resetAfterTest(true); + + $student = $this->getDataGenerator()->create_user(); + $anotherstudent = $this->getDataGenerator()->create_user(); + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + \availability_completion\condition::wipe_static_cache(); + + // Create assignment with automatic completion and NO passing grade. + $assingnopassgrade = $this->getDataGenerator()->create_module('assign', + ['course' => $course->id], [ + 'showdescription' => true, + 'completionview' => 1, + 'completion' => COMPLETION_TRACKING_AUTOMATIC, + 'completiongradeitemnumber' => 1, + ], + ); + $cmassingnopassgrade = get_coursemodule_from_id('assign', $assingnopassgrade->cmid); + + $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student'); + $this->getDataGenerator()->enrol_user($anotherstudent->id, $course->id, 'student'); + + $completion = new \completion_info($course); + $cinfo = new \stdClass(); + $cinfo->coursemoduleid = $assingnopassgrade->cmid; + $cinfo->timemodified = time(); + $cinfo->viewed = COMPLETION_NOT_VIEWED; + $cinfo->overrideby = null; + + // Test student has achieved completion grade and it should pass. + $this->setUser($student); + + $cinfo->id = 0; + $cinfo->completionstate = COMPLETION_COMPLETE_PASS; + $cinfo->userid = $student->id; + $completion->internal_set_data($cmassingnopassgrade, $cinfo, true); + + $result = core_completion_external::get_activities_completion_status($course->id, $student->id); + $result = external_api::clean_returnvalue( + core_completion_external::get_activities_completion_status_returns(), $result); + + $this->assertCount(1, $result['statuses']); + $status = reset($result['statuses']); + $this->assertEquals(COMPLETION_COMPLETE_PASS, $status['state']); + + // Test student has failed but not passing grade is required for completion so it should pass. + $this->setUser($anotherstudent); + + $cinfo->id = 0; + $cinfo->completionstate = COMPLETION_COMPLETE_FAIL; + $cinfo->userid = $anotherstudent->id; + $completion->internal_set_data($cmassingnopassgrade, $cinfo, true); + + $result = core_completion_external::get_activities_completion_status($course->id, $anotherstudent->id); + $result = external_api::clean_returnvalue( + core_completion_external::get_activities_completion_status_returns(), $result); + + $this->assertCount(1, $result['statuses']); + $status = reset($result['statuses']); + $this->assertEquals(COMPLETION_COMPLETE, $status['state']); + } + /** * Test override_activity_completion_status */