From 67a6035622681c5ebb3fb991347fd9b92003ce3e Mon Sep 17 00:00:00 2001 From: Julio Date: Wed, 2 Oct 2019 08:12:40 +0200 Subject: [PATCH] Gradebook: Fix best category column calculation BT#16157 --- main/gradebook/lib/be/category.class.php | 112 ++++++++++++------ main/gradebook/lib/be/exerciselink.class.php | 4 - .../gradebook/lib/fe/gradebooktable.class.php | 15 +-- .../lib/flatview_data_generator.class.php | 22 ---- 4 files changed, 80 insertions(+), 73 deletions(-) diff --git a/main/gradebook/lib/be/category.class.php b/main/gradebook/lib/be/category.class.php index aa4271db92..353ceb5359 100755 --- a/main/gradebook/lib/be/category.class.php +++ b/main/gradebook/lib/be/category.class.php @@ -969,10 +969,8 @@ class Category implements GradebookItem $linkres = $link->calc_score($stud_id, null); if (!empty($linkres) && $link->get_weight() != 0) { - $students[$stud_id] = $linkres[0]; $linkweight = $link->get_weight(); $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; - $count++; $weightsum += $linkweight; $ressum += $linkres[0] / $link_res_denom * $linkweight; } else { @@ -1000,10 +998,10 @@ class Category implements GradebookItem } // Calculate score - $count = 0; $ressum = 0; $weightsum = 0; $bestResult = 0; + $totalScorePerStudent = []; if (!empty($cats)) { /** @var Category $cat */ @@ -1033,57 +1031,91 @@ class Category implements GradebookItem } if (!empty($evals)) { - /** @var Evaluation $eval */ - foreach ($evals as $eval) { - $evalres = $eval->calc_score(null, $type); - $eval->setStudentList($this->getStudentList()); - - if (isset($evalres) && $eval->get_weight() != 0) { - $evalweight = $eval->get_weight(); - $weightsum += $evalweight; - $count++; - if (!empty($evalres[1])) { - $ressum += $evalres[0] / $evalres[1] * $evalweight; + if ($type === 'best') { + $studentList = $this->getStudentList(); + foreach ($studentList as $student) { + $studentId = $student['user_id']; + foreach ($evals as $eval) { + $linkres = $eval->calc_score($studentId, null); + $linkweight = $eval->get_weight(); + $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; + $ressum = $linkres[0] / $link_res_denom * $linkweight; + + if (!isset($totalScorePerStudent[$studentId])) { + $totalScorePerStudent[$studentId] = 0; + } + $totalScorePerStudent[$studentId] += $ressum; } + } + } else { + /** @var Evaluation $eval */ + foreach ($evals as $eval) { + $evalres = $eval->calc_score(null, $type); + $eval->setStudentList($this->getStudentList()); - if ($ressum > $bestResult) { - $bestResult = $ressum; - } - } else { - if ($eval->get_weight() != 0) { + if (isset($evalres) && $eval->get_weight() != 0) { $evalweight = $eval->get_weight(); $weightsum += $evalweight; + if (!empty($evalres[1])) { + $ressum += $evalres[0] / $evalres[1] * $evalweight; + } + + if ($ressum > $bestResult) { + $bestResult = $ressum; + } + } else { + if ($eval->get_weight() != 0) { + $evalweight = $eval->get_weight(); + $weightsum += $evalweight; + } } } } } + if (!empty($links)) { - /** @var EvalLink|ExerciseLink $link */ - foreach ($links as $link) { - $link->setStudentList($this->getStudentList()); + $studentList = $this->getStudentList(); + if ($type === 'best') { + foreach ($studentList as $student) { + $studentId = $student['user_id']; + foreach ($links as $link) { + $linkres = $link->calc_score($studentId, null); + $linkweight = $link->get_weight(); + $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; + $ressum = $linkres[0] / $link_res_denom * $linkweight; - if ($session_id) { - $link->set_session_id($session_id); + if (!isset($totalScorePerStudent[$studentId])) { + $totalScorePerStudent[$studentId] = 0; + } + $totalScorePerStudent[$studentId] += $ressum; + } } + } else { + /** @var EvalLink|ExerciseLink $link */ + foreach ($links as $link) { + $link->setStudentList($this->getStudentList()); - $linkres = $link->calc_score($stud_id, $type); - if (!empty($linkres) && $link->get_weight() != 0) { - $students[$stud_id] = $linkres[0]; - $linkweight = $link->get_weight(); - $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; + if ($session_id) { + $link->set_session_id($session_id); + } - $count++; - $weightsum += $linkweight; - $ressum += $linkres[0] / $link_res_denom * $linkweight; + $linkres = $link->calc_score($stud_id, $type); - if ($ressum > $bestResult) { - $bestResult = $ressum; - } - } else { - // Adding if result does not exists - if ($link->get_weight() != 0) { + if (!empty($linkres) && $link->get_weight() != 0) { $linkweight = $link->get_weight(); + $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; + $weightsum += $linkweight; + $ressum += $linkres[0] / $link_res_denom * $linkweight; + if ($ressum > $bestResult) { + $bestResult = $ressum; + } + } else { + // Adding if result does not exists + if ($link->get_weight() != 0) { + $linkweight = $link->get_weight(); + $weightsum += $linkweight; + } } } } @@ -1092,6 +1124,10 @@ class Category implements GradebookItem switch ($type) { case 'best': + arsort($totalScorePerStudent); + $maxScore = current($totalScorePerStudent); + + return [$maxScore, $this->get_weight()]; if (empty($bestResult)) { if ($cacheAvailable) { $cacheDriver->save($key, null); diff --git a/main/gradebook/lib/be/exerciselink.class.php b/main/gradebook/lib/be/exerciselink.class.php index a6a297895f..6d1b65150d 100755 --- a/main/gradebook/lib/be/exerciselink.class.php +++ b/main/gradebook/lib/be/exerciselink.class.php @@ -204,10 +204,6 @@ class ExerciseLink extends AbstractLink break; case 'ranking': return [null, null]; - /* - $newList = []; - $ranking = AbstractLink::getCurrentUserRanking($stud_id, $link->getUserScoreList()); - return $ranking;*/ break; default: if (!empty($stud_id)) { diff --git a/main/gradebook/lib/fe/gradebooktable.class.php b/main/gradebook/lib/fe/gradebooktable.class.php index f85699aefe..2fc391eb9b 100755 --- a/main/gradebook/lib/fe/gradebooktable.class.php +++ b/main/gradebook/lib/fe/gradebooktable.class.php @@ -457,7 +457,6 @@ class GradebookTable extends SortableTable } $category_weight = $item->get_weight(); - $mainCategoryWeight = $main_cat[0]->get_weight(); if ($this->teacherView) { $weight_total_links += $data[3]; @@ -472,12 +471,10 @@ class GradebookTable extends SortableTable } } else { $score = $item->calc_score($this->userId); - $scoreToDisplay = '-'; if (!empty($score[1])) { $completeScore = $scoredisplay->display_score($score, SCORE_DIV_PERCENT); $score = $score[0] / $score[1] * $item->get_weight(); $score = $scoredisplay->display_score([$score, null], SCORE_SIMPLE); - $scoreToDisplay = Display::tip($score, $completeScore); } else { $categoryScore = null; } @@ -493,9 +490,6 @@ class GradebookTable extends SortableTable $data['result_score'][1], ]; - $totalUserResult[0] += $totalResult[0] / ($totalResult[1] ?: 1) * $data[3]; - $totalUserResult[1] += $data[3]; - if (empty($model)) { $totalBest = [ $scoredisplay->format_score($totalBest[0] + $data['best_score'][0]), @@ -521,7 +515,6 @@ class GradebookTable extends SortableTable $mode = SCORE_AVERAGE; if ($userExerciseScoreInCategory) { $mode = SCORE_SIMPLE; - $result = ExerciseLib::convertScoreToPlatformSetting($totalAverage[0], $totalAverage[1]); $totalAverage[0] = $result['score']; $totalAverage[1] = $result['weight']; @@ -808,6 +801,10 @@ class GradebookTable extends SortableTable if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['ranking'])) { $totalRanking = $defaultData[$categoryId]['ranking']; $invalidateRanking = $defaultData[$categoryId]['ranking_invalidate']; + $average = 0; + foreach ($totalRanking as $ranking) { + $average += $ranking; + } } else { $totalRanking = []; $invalidateRanking = true; @@ -817,8 +814,8 @@ class GradebookTable extends SortableTable $score = $main_cat[0]->calc_score( $student['user_id'], null, - api_get_course_id(), - api_get_session_id() + $course_code, + $session_id ); if (!empty($score[0])) { $invalidateRanking = false; diff --git a/main/gradebook/lib/flatview_data_generator.class.php b/main/gradebook/lib/flatview_data_generator.class.php index ed89f3d777..dbca780e32 100755 --- a/main/gradebook/lib/flatview_data_generator.class.php +++ b/main/gradebook/lib/flatview_data_generator.class.php @@ -137,22 +137,6 @@ class FlatViewDataGenerator $mainCategoryId = $mainCourseCategory->get_id(); } - if (isset($this->category) && !empty($this->category)) { - $categories = Category::load( - null, - null, - null, - $this->category->get_id() - ); - if (!empty($categories)) { - foreach ($categories as $category) { - $sum_categories_weight_array[$category->get_id()] = $category->get_weight(); - } - } else { - $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight(); - } - } - // No category was added $course_code = api_get_course_id(); $session_id = api_get_session_id(); @@ -567,8 +551,6 @@ class FlatViewDataGenerator $show_all, $row ); - - $item_total += $result['item_total']; $item_value_total += $result['item_value_total']; $evaluationsAdded = $result['evaluations_added']; $item_total = $main_weight; @@ -589,7 +571,6 @@ class FlatViewDataGenerator $item_total += $result['item_total']; $item_value_total += $result['item_value_total']; $total_score = [$item_value_total, $item_total]; - $style = api_get_configuration_value('gradebook_report_score_style'); if (!$show_all) { @@ -651,7 +632,6 @@ class FlatViewDataGenerator $item_total = 0; $item_value_total = 0; $evaluationsAdded = []; - $model = ExerciseLib::getCourseScoreModel(); for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) { /** @var AbstractLink $item */ @@ -692,7 +672,6 @@ class FlatViewDataGenerator } else { $item_value = $score[0] / $divide * $item->get_weight(); } - $item_total += $item->get_weight(); $style = api_get_configuration_value('gradebook_report_score_style'); @@ -728,7 +707,6 @@ class FlatViewDataGenerator ); $temp_score = Display::tip($temp_score, $complete_score); } - if (!empty($model)) { $scoreToShow = ''; if (isset($score[0]) && isset($score[1])) {