芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.sksb.smartcon-survey.com/app/Livewire/Dashboard/LineChart.php
topicId); $surveyHelper = new SurveyJSHelper($topic); $questions = $surveyHelper->getQuestions(); if ($this->question != 'all') { $questions = array_filter($questions, fn ($q) => $q['name'] == $this->question); $questions = array_values($questions); } $results = []; $last10days = []; foreach ($questions as $question) { $top2ByQuarter = Interview::where(['topic_id' => $this->topicId, 'is_test' => 0]) ->whereQuestion($question['name'], range(1, 2)) ->resultsByQuarter()->get()->toArray(); $totalByQuarter = Interview::where(['topic_id' => $this->topicId, 'is_test' => 0]) ->whereQuestion($question['name'], range(1, 5)) ->resultsByQuarter()->get()->toArray(); $results[] = array_map(function ($top2, $total) { if ($top2 == null) { return [ 'label' => 'Q'.$total['quarter'].' '.$total['year'], 'count' => 0, 'total' => $total['count'] ?? 0, ]; } return [ 'label' => 'Q'.$top2['quarter'].' '.$top2['year'], 'count' => $top2['count'] ?? 0, 'total' => $total['count'] ?? 0, ]; }, $top2ByQuarter, $totalByQuarter); // $last10daysTotal = Interview::where(['topic_id' => $this->topicId, 'is_test' => 0]) // ->whereQuestion($question['name'], range(1, 5)) // ->last10Days()->count(); // $last10daysTotalTop2 = Interview::where(['topic_id' => $this->topicId, 'is_test' => 0]) // ->whereQuestion($question['name'], range(1, 2)) // ->last10Days()->count(); // $last10days[$question['name']] = [ // 'total' => $last10daysTotal, // 'top2' => $last10daysTotalTop2, // ]; } $indexResults = []; // merge all question into index foreach ($questions as $key => $question) { foreach ($results[$key] as $value) { if (isset($indexResults[$value['label']])) { $indexResults[$value['label']]['count'] += $value['count']; $indexResults[$value['label']]['total'] += $value['total']; } else { $indexResults[$value['label']] = [ 'label' => $value['label'], 'count' => $value['count'], 'total' => $value['total'], ]; } } } $indexResults = array_map(function ($item) { $item['value'] = round($item['count'] / $item['total'] * 100, 1); return $item; }, $indexResults); // $last10days['all'] = array_reduce($last10days, function ($prev, $item) { // if (!isset($prev['total'])) $prev['total'] = 0; // if (!isset($prev['top2'])) $prev['top2'] = 0; // $prev['total'] += $item['total']; // $prev['top2'] += $item['top2']; // return $prev; // }, []); // $last10daysTotal = $last10days[$this->question]['total'] > 0 ? $last10days[$this->question]['total'] : 1; // $last10daysTotalTop2 = $last10days[$this->question]['top2']; return view('livewire.dashboard.line-chart', [ 'data' => json_encode($indexResults), 'last10days' => $this->last10Days(), ]); } private function last10Days() { $interviews = Interview::where([ ['topic_id', $this->topicId], ['is_test', 0], ])->last10Days()->get(); return $this->getValue($interviews); } public function getValue($interviews): float { $top2 = $this->gettop2($interviews)->count(); $total = $this->getAllAnswers($interviews)->count(); if ($total <= 0) { return 0; } return round( $top2 / $total * 100, 2 ); } public function getAllAnswers($interviews) { return $interviews ->map(function ($interview) { if ($this->question != 'all') { return array_values(array_filter($interview->answers, fn ($key) => $key == $this->question, ARRAY_FILTER_USE_KEY)); } else { return $interview->answers; } }) ->flatten() ->filter(fn ($answer) => is_numeric($answer)); } public function gettop2($interviews) { return $interviews->map(function ($interview) { if ($this->question != 'all') { return array_values(array_filter($interview->answers, fn ($key) => $key == $this->question, ARRAY_FILTER_USE_KEY)); } else { return $interview->answers; } })->flatten()->filter(fn ($answer) => is_numeric($answer) && in_array($answer, [1, 2])); } }