MDL-71189 core_completion: Implementing custom completion sort ordering

This adds the requirement for activities supporting custom completion to
specify the order all completion conditions should be displayed for that
activity. It also implements the sorting that takes place.
This commit is contained in:
Michael Hawkins 2021-04-08 14:20:50 +08:00
parent c997fc7784
commit f105612d7f
4 changed files with 158 additions and 3 deletions

View file

@ -184,4 +184,11 @@ abstract class activity_custom_completion {
* @return array
*/
abstract public function get_custom_rule_descriptions(): array;
/**
* Returns an array of all completion rules, in the order they should be displayed to users.
*
* @return array
*/
abstract public function get_sort_order(): array;
}

View file

@ -79,6 +79,7 @@ class cm_completion_details {
* Fetches the completion details for a user.
*
* @return array An array of completion details for a user containing the completion requirement's description and status.
* @throws \coding_exception
*/
public function get_details(): array {
if (!$this->is_automatic()) {
@ -135,6 +136,8 @@ class cm_completion_details {
'description' => $this->cmcompletion->get_custom_rule_description($rule),
];
}
$details = $this->sort_completion_details($details);
}
} else {
if (function_exists($this->cminfo->modname . '_get_completion_state')) {
@ -149,10 +152,36 @@ class cm_completion_details {
}
}
return $details;
}
/**
* Sort completion details in the order specified by the activity's custom completion implementation.
*
* @param array $details The completion details to be sorted.
* @return array
* @throws \coding_exception
*/
protected function sort_completion_details(array $details): array {
$sortorder = $this->cmcompletion->get_sort_order();
$sorteddetails = [];
foreach ($sortorder as $sortedkey) {
if (isset($details[$sortedkey])) {
$sorteddetails[$sortedkey] = $details[$sortedkey];
}
}
// Make sure the sorted list includes all of the conditions that were set.
if (count($sorteddetails) < count($details)) {
$exceptiontext = get_class($this->cmcompletion) .'::get_sort_order() is missing one or more completion conditions.' .
' All custom and standard conditions that apply to this activity must be listed.';
throw new \coding_exception($exceptiontext);
}
return $sorteddetails;
}
/**
* Fetches the overall completion state of this course module.
*