MDL-80392 completion: Temporary fix for completion state via exporters

This commit is contained in:
Juan Leyva
2024-02-06 11:05:06 +01:00
parent 48fbf7ed14
commit b9c148dbde
2 changed files with 79 additions and 1 deletions
+10 -1
View File
@@ -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),
+69
View File
@@ -34,6 +34,7 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php');
* @copyright 2015 Juan Leyva <[email protected]>
* @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
*/