Преглед изворни кода

Merge branch 'main' of https://git.yunzhixue.cn/yms/math_cms into hotfix/fix_add_name

yemeishu пре 1 недеља
родитељ
комит
3cd28f0078
1 измењених фајлова са 30 додато и 0 уклоњено
  1. 30 0
      app/Http/Controllers/Api/IntelligentExamController.php

+ 30 - 0
app/Http/Controllers/Api/IntelligentExamController.php

@@ -1071,6 +1071,36 @@ class IntelligentExamController extends Controller
             $adjustedQuestions[$index]['score'] = $questionScores[$index] ?? 5;
         }
 
+        $total = array_sum(array_column($adjustedQuestions, 'score'));
+        $diff = (int) $targetTotalScore - (int) $total;
+        if ($diff !== 0 && ! empty($adjustedQuestions)) {
+            $count = count($adjustedQuestions);
+            $i = $count - 1;
+            while ($diff !== 0) {
+                $score = $adjustedQuestions[$i]['score'];
+                if ($diff > 0) {
+                    $adjustedQuestions[$i]['score'] = $score + 1;
+                    $diff--;
+                } else {
+                    if ($score > 1) {
+                        $adjustedQuestions[$i]['score'] = $score - 1;
+                        $diff++;
+                    }
+                }
+
+                $i--;
+                if ($i < 0) {
+                    $i = $count - 1;
+                    if ($diff < 0) {
+                        $minScore = min(array_column($adjustedQuestions, 'score'));
+                        if ($minScore <= 1) {
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
         return $adjustedQuestions;
     }