Skip to content

Commit

Permalink
MDL-75173 core_completion: Fixes after review.
Browse files Browse the repository at this point in the history
Unit tests added. Used new steps in Behat. Code style fix for long line.
  • Loading branch information
ilyatregubov committed Oct 14, 2022
1 parent 43f98f6 commit effd848
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
3 changes: 2 additions & 1 deletion completion/classes/api.php
Expand Up @@ -152,7 +152,8 @@ public static function mark_course_completions_activity_criteria($userdata = nul
AND (
mc.completionstate = :completionstate
OR (cm.completionpassgrade = 1 AND mc.completionstate = :completionstatepass1)
OR (cm.completionpassgrade = 0 AND (mc.completionstate = :completionstatepass2 OR mc.completionstate = :completionstatefail))
OR (cm.completionpassgrade = 0 AND (mc.completionstate = :completionstatepass2
OR mc.completionstate = :completionstatefail))
)";

$params = [
Expand Down
97 changes: 97 additions & 0 deletions completion/tests/api_test.php
Expand Up @@ -287,4 +287,101 @@ public function test_mark_course_completions_activity_criteria() {
$this->assertEquals($student1->id, reset($actual)->userid);
$this->assertEquals($student2->id, end($actual)->userid);
}

/**
* Test for mark_course_completions_activity_criteria() with different completionpassgrade settings.
* @covers ::mark_course_completions_activity_criteria
*/
public function test_mark_course_completions_activity_criteria_completion_states() {
global $DB, $CFG;
require_once($CFG->dirroot . '/completion/criteria/completion_criteria_activity.php');
$this->resetAfterTest(true);

$courses[] = $this->getDataGenerator()->create_course(['shortname' => 'completionpassgradenotset',
'enablecompletion' => 1]);
$courses[] = $this->getDataGenerator()->create_course(['shortname' => 'completionpassgradeset',
'enablecompletion' => 1]);

$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();

$teacher = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));

foreach ($courses as $course) {
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);

$completioncriteria = [
'completionusegrade' => 1,
'gradepass' => 50
];

if ($course->shortname == 'completionpassgradeset') {
$completioncriteria['completionpassgrade'] = 1;
}

/** @var \mod_assign_generator $assigngenerator */
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$assign = $assigngenerator->create_instance([
'course' => $course->id,
'completion' => COMPLETION_ENABLED,
] + $completioncriteria);

$cmassing = get_coursemodule_from_id('assign', $assign->cmid);
$cm = get_coursemodule_from_instance('assign', $assign->id);
$c = new \completion_info($course);

// Add activity completion criteria.
$criteriadata = new \stdClass();
$criteriadata->id = $course->id;
$criteriadata->criteria_activity = array();
// Some activities.
$criteriadata->criteria_activity[$cmassing->id] = 1;
$criterion = new \completion_criteria_activity();
$criterion->update_config($criteriadata);

$this->setUser($teacher);

// Mark user completions.
$completion = new \stdClass();
$completion->coursemoduleid = $cm->id;
$completion->timemodified = time();
$completion->viewed = COMPLETION_NOT_VIEWED;
$completion->overrideby = null;

// Student1 achieved passgrade.
$completion->id = 0;
$completion->completionstate = COMPLETION_COMPLETE_PASS;
$completion->userid = $student1->id;
$c->internal_set_data($cm, $completion, true);

// Student2 has not achieved passgrade.
$completion->id = 0;
$completion->completionstate = COMPLETION_COMPLETE_FAIL;
$completion->userid = $student2->id;
$c->internal_set_data($cm, $completion, true);

$actual = $DB->get_records('course_completions', ['course' => $course->id]);
$this->assertEmpty($actual);

// Run course completions cron.
$coursecompletionid = \core_completion\api::mark_course_completions_activity_criteria();
$this->assertEquals(0, $coursecompletionid);
$actual = $DB->get_records('course_completions', ['course' => $course->id]);

if ($course->shortname == 'completionpassgradeset') {
// Only student1 has completed a course.
$this->assertEquals(1, count($actual));
$this->assertEquals($student1->id, reset($actual)->userid);
} else {
// Both students completed a course.
$this->assertEquals(2, count($actual));
$this->assertEquals($student1->id, reset($actual)->userid);
$this->assertEquals($student2->id, end($actual)->userid);
}
}
}
}
Expand Up @@ -46,8 +46,7 @@ Feature: Course completion state should match completion criteria
And I log out

Scenario: Completion status show match completion criteria.
Given I log in as "student1"
And I am on "Course 1" course homepage
Given I am on the "Course 1" course page logged in as "student1"
And the "Receive a grade" completion condition of "Test assignment name" is displayed as "todo"
And the "Receive a passing grade" completion condition of "Test assignment name" is displayed as "todo"
And I should see "Status: Not yet started" in the "Course completion status" "block"
Expand All @@ -66,8 +65,7 @@ Feature: Course completion state should match completion criteria
| Grade out of 100 | 50.0 |
And I press "Save changes"
And I log out
When I log in as "student1"
And I am on "Course 1" course homepage
When I am on the "Course 1" course page logged in as "student1"
And I should see "Status: Pending" in the "Course completion status" "block"
And the "Receive a grade" completion condition of "Test assignment name" is displayed as "done"
And the "Receive a passing grade" completion condition of "Test assignment name" is displayed as "failed"
Expand All @@ -81,8 +79,7 @@ Feature: Course completion state should match completion criteria
| Grade out of 100 | 75.0 |
And I press "Save changes"
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I am on the "Course 1" course page logged in as "student1"
And I should see "Status: Complete" in the "Course completion status" "block"
And the "Receive a grade" completion condition of "Test assignment name" is displayed as "done"
And the "Receive a passing grade" completion condition of "Test assignment name" is displayed as "done"
Expand Down

0 comments on commit effd848

Please sign in to comment.