Skip to content

Commit

Permalink
Check for full mark award issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 committed Sep 27, 2024
1 parent 834302d commit 4c8fbb7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function wallet_completion_awards(\core\event\course_completed $ev
return;
}

$percentage = ($usergrade / $maxgrade) * 100;
$percentage = min(($usergrade / $maxgrade) * 100, 100);

// Getting the enrol wallet instance in the course.
$instances = enrol_get_instances($courseid, true);
Expand Down Expand Up @@ -99,6 +99,10 @@ public static function wallet_completion_awards(\core\event\course_completed $ev

// Calculating the total award.
$award = ($percentage - $condition) * $maxgrade * $awardper / 100;
if ($award <= 0) {
return;
}

$course = get_course($courseid);
$coursename = $course->shortname;

Expand Down
12 changes: 11 additions & 1 deletion tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function test_wallet_completion_awards(): void {
// Create user and check that there is no balance.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();

$op = new balance_op($user1->id);
$this->assertEquals(0, $op->get_main_balance());
Expand Down Expand Up @@ -102,6 +103,8 @@ public function test_wallet_completion_awards(): void {
// Let's start.
$walletplugin->enrol_self($instance1, $user1);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
// Check the issue for full mark award;
$this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'student');

$balance = new balance($user1->id);
$this->assertEquals(50, $balance->get_total_balance());
Expand All @@ -112,6 +115,7 @@ public function test_wallet_completion_awards(): void {

$this->course_completion_trigger($cm, $user1, $course1, 90);
$this->course_completion_trigger($cm, $user2, $course1, 35);
$this->course_completion_trigger($cm, $user3, $course1, 100);

// The event should be triggered and caught by our observer.
$balance = new balance($user1->id, $course1->category);
Expand All @@ -128,7 +132,13 @@ public function test_wallet_completion_awards(): void {
$balance = new balance($user1->id, $course1->category);
$this->assertEquals(70, $balance->get_valid_balance());

$this->assertEquals(1, $DB->count_records('enrol_wallet_awards'));
$this->assertEquals(2, $DB->count_records('enrol_wallet_awards'));

$balance = new balance($user3->id, $course1->category);
$this->assertEquals(0, $balance->get_main_balance());
$this->assertEquals(25, $balance->get_valid_balance());
$this->assertEquals(25, $balance->get_valid_nonrefundable());
$this->assertEquals(25, $balance->get_valid_free());
}

/**
Expand Down

0 comments on commit 4c8fbb7

Please sign in to comment.