Skip to content

Commit

Permalink
add backport for core_cm_completion_details::is_overall_complete for …
Browse files Browse the repository at this point in the history
…Moodle versions < 4.3
  • Loading branch information
Glutamat42 committed Sep 3, 2024
1 parent 35a3750 commit 57b9ea8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions classes/adler_score.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
Expand Down
55 changes: 55 additions & 0 deletions classes/local/backport/backport_cm_completion_details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace local_adler\local\backport;


use core_completion\cm_completion_details as core_cm_completion_details;

if (get_config('moodle', 'version') < 2023100900) {
class backport_cm_completion_details extends core_cm_completion_details {
/**
* Backport from Moodle 4.4 for Moodle versions below 4.3
*
* Whether this activity module instance tracks completion manually.
*
* @return bool
*/
public function is_manual(): bool {
return $this->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 {
}
}

0 comments on commit 57b9ea8

Please sign in to comment.