Updated aggregation for junctions

This commit is contained in:
PMKuipers 2023-09-02 22:07:51 +02:00
parent 2ef8b65421
commit 917304e830
4 changed files with 74 additions and 41 deletions

View File

@ -253,7 +253,7 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
* @param int $userid Id of user to check completion for
* @return int Aggregated completion as completion class constant
*/
public function aggregate_junction(array $completion, studyitem $studyitem = null, $userid = 0) {
public function aggregate_junction(array $completion, studyitem $studyitem, $userid = 0) {
// Aggregate multiple incoming states into one junction or finish.
// Possible states:.
// - completion::EXCELLENT - All incoming states are excellent.
@ -263,25 +263,43 @@ class bistate_aggregator extends \local_treestudyplan\aggregator {
// - completion::INCOMPLETE - All incoming states are incomplete.
// - completion::PROGRESS - All other states.
$method = strtoupper($studyitem->conditions()); // One of ANY or ALL.
// First count all states.
$statecount = completion::count_states($completion);
$total = count($completion);
if ( $total == $statecount[completion::EXCELLENT]) {
return completion::EXCELLENT;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]) ) {
return completion::GOOD;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]
+ $statecount[completion::COMPLETED]) ) {
return completion::COMPLETED;
} else if ( $statecount[completion::FAILED]) {
return completion::FAILED;
} else if ( $total == $statecount[completion::INCOMPLETE]) {
return completion::INCOMPLETE;
} else {
return completion::PROGRESS;
if($method == "ANY"){
if ( $statecount[completion::EXCELLENT] >= 1 ) {
return completion::EXCELLENT;
} else if ( $statecount[completion::GOOD] >= 1 ) {
return completion::GOOD;
} else if ( $statecount[completion::COMPLETED] >= 1 ) {
return completion::COMPLETED;
} else if ( $statecount[completion::PROGRESS] >= 1 ) {
return completion::PROGRESS;
} else if ( $statecount[completion::FAILED] >= 1) {
return completion::FAILED;
} else {
return completion::INCOMPLETE;
}
} else { /* ALL (default) */
if ( $total == $statecount[completion::EXCELLENT]) {
return completion::EXCELLENT;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]) ) {
return completion::GOOD;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]
+ $statecount[completion::COMPLETED]) ) {
return completion::COMPLETED;
} else if ( $statecount[completion::FAILED]) {
return completion::FAILED;
} else if ( $total == $statecount[completion::INCOMPLETE]) {
return completion::INCOMPLETE;
} else {
return completion::PROGRESS;
}
}
}

View File

@ -193,7 +193,7 @@ class core_aggregator extends \local_treestudyplan\aggregator {
* @param int $userid Id of user to check completion for
* @return int Aggregated completion as completion class constant
*/
public function aggregate_junction(array $completion, studyitem $studyitem = null, $userid = 0) {
public function aggregate_junction(array $completion, studyitem $studyitem, $userid = 0) {
// Aggregate multiple incoming states into one junction or finish.
// Possible states:.
// - completion::EXCELLENT - All incoming states are excellent.
@ -203,25 +203,43 @@ class core_aggregator extends \local_treestudyplan\aggregator {
// - completion::INCOMPLETE - All incoming states are incomplete.
// - completion::PROGRESS - All other states.
$method = strtoupper($studyitem->conditions()); // One of ANY or ALL.
// First count all states.
$statecount = completion::count_states($completion);
$total = count($completion);
if ( $total == $statecount[completion::EXCELLENT]) {
return completion::EXCELLENT;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]) ) {
return completion::GOOD;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]
+ $statecount[completion::COMPLETED]) ) {
return completion::COMPLETED;
} else if ( $statecount[completion::FAILED]) {
return completion::FAILED;
} else if ( $total == $statecount[completion::INCOMPLETE]) {
return completion::INCOMPLETE;
} else {
return completion::PROGRESS;
if($method == "ANY"){
if ( $statecount[completion::EXCELLENT] >= 1 ) {
return completion::EXCELLENT;
} else if ( $statecount[completion::GOOD] >= 1 ) {
return completion::GOOD;
} else if ( $statecount[completion::COMPLETED] >= 1 ) {
return completion::COMPLETED;
} else if ( $statecount[completion::PROGRESS] >= 1 ) {
return completion::PROGRESS;
} else if ( $statecount[completion::FAILED] >= 1) {
return completion::FAILED;
} else {
return completion::INCOMPLETE;
}
} else { /* ALL (default) */
if ( $total == $statecount[completion::EXCELLENT]) {
return completion::EXCELLENT;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]) ) {
return completion::GOOD;
} else if ( $total == ( $statecount[completion::EXCELLENT]
+ $statecount[completion::GOOD]
+ $statecount[completion::COMPLETED]) ) {
return completion::COMPLETED;
} else if ( $statecount[completion::FAILED]) {
return completion::FAILED;
} else if ( $total == $statecount[completion::INCOMPLETE]) {
return completion::INCOMPLETE;
} else {
return completion::PROGRESS;
}
}
}

View File

@ -75,7 +75,7 @@ class tristate_aggregator extends \local_treestudyplan\aggregator {
* @return int Aggregated completion as completion class constant
*/
protected function aggregate_completion(array $a, $condition = "50") {
if (in_array($condition, ['ALL', '67', '50', 'ANY'])) {
if (in_array(strtoupper($condition), ['ALL', '67', '50', 'ANY'])) {
// Condition is one of the valid conditions.
$ccompleted = 0;
$cexcellent = 0;

View File

@ -84,7 +84,7 @@ class studyitem {
* Return the condition string for this item
*/
public function conditions() : string {
return $this->r->conditions;
return (!empty($this->r->conditions)) ? $this->r->conditions : "ALL";
}
/**
@ -198,7 +198,7 @@ class studyitem {
$model = [
'id' => $this->r->id, // Id is needed in export model because of link references.
'type' => $this->valid() ? $this->r->type : self::INVALID,
'conditions' => $this->r->conditions,
'conditions' => $this->conditions(),
'slot' => $this->r->slot,
'layer' => $this->r->layer,
'span' => $this->r->span,
@ -214,9 +214,6 @@ class studyitem {
unset($model["layer"]);
unset($model["continuation_id"]);
$model["connections"] = []; // In export mode, connections is just an array of outgoing connections.
if (!isset($this->r->conditions)) {
unset($model["conditions"]);
}
}
// Add course link if available.
@ -548,14 +545,14 @@ class studyitem {
}
} else if (in_array(strtolower($this->r->type), ['junction', 'finish'])) {
// Completion of the linked items, according to the rule.
$incompleted = [];
$incomingcompletions = [];
// Retrieve incoming connections.
$incoming = $DB->get_records(studyitemconnection::TABLE, ['to_id' => $this->r->id]);
foreach ($incoming as $conn) {
$item = self::find_by_id($conn->from_id);
$incompleted[] = $item->completion($userid);
$incomingcompletions[] = $item->completion($userid);
}
return $this->aggregator->aggregate_junction($incompleted, $this, $userid);
return $this->aggregator->aggregate_junction($incomingcompletions, $this, $userid);
} else if (strtolower($this->r->type) == 'badge') {
global $DB;
// Badge awarded.