Skip to content

Commit

Permalink
MDL-71288 completion: add fallback for plugins
Browse files Browse the repository at this point in the history
This commits adds a fallback for plugins which does not have
custom_completion implementation.

For those cases, it will search for {modulename}_get_completion_state
callback in the plugin and call get_overall_completion() method in
cm_completion_details class to get the overall completion state for
a course module and user.
  • Loading branch information
lameze committed Apr 22, 2021
1 parent 003dadc commit 068f965
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
35 changes: 23 additions & 12 deletions completion/classes/cm_completion_details.php
Expand Up @@ -116,21 +116,32 @@ public function get_details(): array {
];
}

// Custom completion rules.
// Check if this activity has custom_completion class implemented.
$cmcompletionclass = activity_custom_completion::get_cm_completion_class($this->cminfo->modname);
if (!isset($completiondata->customcompletion) || !$cmcompletionclass) {
// Return early if there are no custom rules to process or the cm completion class implementation is not available.
return $details;
if ($cmcompletionclass) {
if (isset($completiondata->customcompletion)) {
/** @var activity_custom_completion $cmcompletion */
$cmcompletion = new $cmcompletionclass($this->cminfo, $this->userid);
foreach ($completiondata->customcompletion as $rule => $status) {
$details[$rule] = (object)[
'status' => !$hasoverride ? $status : $completiondata->completionstate,
'description' => $cmcompletion->get_custom_rule_description($rule),
];
}
}
} else {
if (function_exists($this->cminfo->modname . '_get_completion_state')) {
// If the plugin does not have the custom completion implementation but implements the get_completion_state() callback,
// fallback to displaying the overall completion state of the activity.
$details = [
'plugincompletionstate' => (object)[
'status' => $this->get_overall_completion(),
'description' => get_string('completeactivity', 'completion')
]
];
}
}

/** @var activity_custom_completion $cmcompletion */
$cmcompletion = new $cmcompletionclass($this->cminfo, $this->userid);
foreach ($completiondata->customcompletion as $rule => $status) {
$details[$rule] = (object)[
'status' => !$hasoverride ? $status : $completiondata->completionstate,
'description' => $cmcompletion->get_custom_rule_description($rule),
];
}

return $details;
}
Expand Down
1 change: 1 addition & 0 deletions lang/en/completion.php
Expand Up @@ -55,6 +55,7 @@
$string['checkallsection'] = 'Check or uncheck all activities and resources in the following section: {$a}';
$string['checkactivity'] = 'Checkbox for activity / resource: {$a}';
$string['completed'] = 'Completed';
$string['completeactivity'] = 'Complete the activity';
$string['completedunlocked'] = 'Completion options unlocked';
$string['completedunlockedtext'] = 'When you save changes, completion state for all students will be erased. If you change your mind about this, do not save the form.';
$string['completedwarning'] = 'Completion options locked';
Expand Down

0 comments on commit 068f965

Please sign in to comment.