diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php index 99c19e7e..e104c129 100644 --- a/classes/completion/custom_completion.php +++ b/classes/completion/custom_completion.php @@ -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; @@ -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; } } diff --git a/lang/en/studentquiz.php b/lang/en/studentquiz.php index ebc1de9e..4a9949d9 100644 --- a/lang/en/studentquiz.php +++ b/lang/en/studentquiz.php @@ -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'; diff --git a/tests/custom_completion_test.php b/tests/custom_completion_test.php index e8207ab2..e7a207ca 100644 --- a/tests/custom_completion_test.php +++ b/tests/custom_completion_test.php @@ -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, + ], + ]; + } }