From 57b9ea8acbd65aed1123d9ffaf2c43e3806d9326 Mon Sep 17 00:00:00 2001 From: Markus Date: Tue, 3 Sep 2024 17:41:55 +0200 Subject: [PATCH] add backport for core_cm_completion_details::is_overall_complete for Moodle versions < 4.3 --- classes/adler_score.php | 4 +- .../backport_cm_completion_details.php | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 classes/local/backport/backport_cm_completion_details.php diff --git a/classes/adler_score.php b/classes/adler_score.php index 1bebf7f..8effebc 100644 --- a/classes/adler_score.php +++ b/classes/adler_score.php @@ -5,7 +5,7 @@ use cm_info; use context_course; -use core_completion\cm_completion_details; +use local_adler\local\backport\backport_cm_completion_details; use local_adler\local\exceptions\user_not_enrolled_exception; use local_logging\logger; use moodle_exception; @@ -60,7 +60,7 @@ public function __construct(cm_info $course_module, int $user_id = null) { * @throws moodle_exception If completion is not enabled for the course module. */ public function get_score_by_completion_state(): float { - $cm_completion_details = cm_completion_details::get_instance( + $cm_completion_details = backport_cm_completion_details::get_instance( get_fast_modinfo($this->course_module->course)->get_cm($this->course_module->id), $this->user_id ); diff --git a/classes/local/backport/backport_cm_completion_details.php b/classes/local/backport/backport_cm_completion_details.php new file mode 100644 index 0000000..06eaaef --- /dev/null +++ b/classes/local/backport/backport_cm_completion_details.php @@ -0,0 +1,55 @@ +cminfo->completion == COMPLETION_TRACKING_MANUAL; + } + + /** + * Backport from Moodle 4.4 for Moodle versions below 4.3 + * + * Returns whether the overall completion state of this course module should be marked as complete or not. + * This is based on the completion settings of the course module, so when the course module requires a passing grade, + * it will only be marked as complete when the user has passed the course module. Otherwise, it will be marked as complete + * even when the user has failed the course module. + * + * @return bool True when the module can be marked as completed. + */ + public function is_overall_complete(): bool { + $completionstates = []; + if ($this->is_manual()) { + $completionstates = [COMPLETION_COMPLETE]; + } else if ($this->is_automatic()) { + // Successfull completion states depend on the completion settings. + if (property_exists($this->completiondata, 'customcompletion') && !empty($this->completiondata->customcompletion)) { + // If the module has any failed custom completion rule the state could be COMPLETION_COMPLETE_FAIL. + $completionstates = [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS]; + } else if (isset($this->completiondata->passgrade)) { + // Passing grade is required. Don't mark it as complete when state is COMPLETION_COMPLETE_FAIL. + $completionstates = [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS]; + } else { + // Any grade is required. Mark it as complete even when state is COMPLETION_COMPLETE_FAIL. + $completionstates = [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL]; + } + } + + return in_array($this->get_overall_completion(), $completionstates); + } + } +} else { + class backport_cm_completion_details extends core_cm_completion_details { + } +}