From 917304e830f60215b7f3a662e09e46e8cc81d1db Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Sat, 2 Sep 2023 22:07:51 +0200 Subject: [PATCH] Updated aggregation for junctions --- .../local/aggregators/bistate_aggregator.php | 50 +++++++++++++------ classes/local/aggregators/core_aggregator.php | 50 +++++++++++++------ .../local/aggregators/tristate_aggregator.php | 2 +- classes/studyitem.php | 13 ++--- 4 files changed, 74 insertions(+), 41 deletions(-) diff --git a/classes/local/aggregators/bistate_aggregator.php b/classes/local/aggregators/bistate_aggregator.php index 66d26b7..c4f81b8 100644 --- a/classes/local/aggregators/bistate_aggregator.php +++ b/classes/local/aggregators/bistate_aggregator.php @@ -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; + } } } diff --git a/classes/local/aggregators/core_aggregator.php b/classes/local/aggregators/core_aggregator.php index 9dd922f..a146a8a 100644 --- a/classes/local/aggregators/core_aggregator.php +++ b/classes/local/aggregators/core_aggregator.php @@ -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; + } } } diff --git a/classes/local/aggregators/tristate_aggregator.php b/classes/local/aggregators/tristate_aggregator.php index 09801e9..f5df1b0 100644 --- a/classes/local/aggregators/tristate_aggregator.php +++ b/classes/local/aggregators/tristate_aggregator.php @@ -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; diff --git a/classes/studyitem.php b/classes/studyitem.php index bd72eda..1f9199e 100644 --- a/classes/studyitem.php +++ b/classes/studyitem.php @@ -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.