Skip to content

Commit

Permalink
StudentQuiz: There are some issues of Add completion criteria #732619
Browse files Browse the repository at this point in the history
  • Loading branch information
Khoa Nguyen Dang committed Oct 26, 2023
1 parent b478a7d commit 7b1dd5d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
12 changes: 10 additions & 2 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function get_state(string $rule): int {
$report = new mod_studentquiz_report($this->cm->id, $this->userid);
$userstats = $report->get_user_stats();

if (!$userstats) {
return COMPLETION_INCOMPLETE;
}

switch ($rule) {
case 'completionpoint':
$status = $studentquiz->completionpoint <= (int) $userstats->points;
Expand Down Expand Up @@ -99,11 +103,15 @@ public function get_sort_order(): array {
* @param stdClass $course The course containing the StudentQuiz to update.
* @param stdClass|cm_info $cm The cm for the StudentQuiz to update.
* @param int|null $userid The user to update state for.
* @return bool Status check, true if update_state is triggered.
*/
public static function update_state(stdClass $course, $cm, ?int $userid = null): void {
public static function update_state(stdClass $course, $cm, ?int $userid = null): bool {
$completion = new \completion_info($course);
if ($completion->is_enabled($cm)) {
if ($completion->is_enabled($cm) && $cm->completion != COMPLETION_TRACKING_MANUAL) {
$completion->update_state($cm, COMPLETION_UNKNOWN, $userid);
return true;
}

return false;
}
}
2 changes: 1 addition & 1 deletion lang/en/studentquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

$string['completionquestionapproved'] = 'Minimum number of unique approved questions required:';
$string['completionquestionapprovedgroup'] = 'Require created approved questions';
$string['completionquestionapprovedgroup_help'] = 'the minimum number of unique questions that a student must author and be approved before the activity is completed. This option can be used with either the Question publishing "Requires approval before publishing" or "Auto-approval" setting, but won\'t be as effective with the latter setting, in case auto-approved questions are later hidden, deleted, or otherwise removed.';
$string['completionquestionapprovedgroup_help'] = 'The minimum number of unique questions that a student must author and be approved before the activity is completed. This option can be used with either the Question publishing "Requires approval before publishing" or "Auto-approval" setting, but won\'t be as effective with the latter setting, in case auto-approved questions are later hidden, deleted, or otherwise removed.';

$string['completionquestionpublished'] = 'Minimum number of unique authored questions required:';
$string['completionquestionpublishedgroup'] = 'Require published questions';
Expand Down
54 changes: 54 additions & 0 deletions tests/custom_completion_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,58 @@ public function test_get_custom_rule_descriptions() {
}
}

/**
* Test update state function in custom_completion.
*
* @dataProvider test_update_provider
* @covers ::update_state
* @param int $completion The completion type: 0 - Do not indicate activity completion,
* 1 - Students can manually mark the activity as completed, 2 - Show activity as complete when conditions are met.
* @param bool $expected Expected result when run update.
*/
public function test_update_state(int $completion, bool $expected): void {
$this->resetAfterTest();
global $DB;

$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);

$user = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);

$studentquiz = $this->getDataGenerator()->create_module('studentquiz', [
'course' => $course->id,
'completion' => $completion,
]);

$cm = cm_info::create(get_coursemodule_from_id('studentquiz', $studentquiz->cmid));
$completioninfo = new \completion_info($course);
$customcompletion = new custom_completion($cm, $user->id, $completioninfo->get_core_completion_state($cm, $user->id));
$status = $customcompletion::update_state($course, $cm, $user->id);

$this->assertEquals($expected, $status);
}

/**
* Data provider for test_update_state() test cases.
*
* @coversNothing
* @return array List of data sets (test cases)
*/
public static function test_update_provider(): array {
return [
'Do not indicate activity completion' => [
0,
false,
],
'Students can manually mark the activity as completed' => [
1,
false,
],
'Show activity as complete when conditions are met' => [
2,
true,
],
];
}
}

0 comments on commit 7b1dd5d

Please sign in to comment.