芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.sksb.smartcon-survey.com/app/Livewire/Dashboard/PieChart.php
quarter; $year = Carbon::now()->year; $topic = Topic::find($this->topicId); $surveyJSHelper = new SurveyJSHelper; $surveyJSHelper->setSurveyJson($topic->surveyJson); $this->title = $topic->name.' - Aktuelles Quartal'; $questions = $surveyJSHelper->getQuestions(); $this->isQuestion = $this->question != 'all' ? 'true' : 'false'; if ($this->question != 'all') { $questionName = $this->topicId === 6 ? str_replace('f2', 'f3', $this->question) : $this->question; $this->title = QuestionHelper::shortname($questionName); $questions = array_filter($questions, fn ($q) => $q['name'] == $this->question); } $currentResults = $this->getResults($questions, $quarter, $year); foreach (range(1, 5) as $id) { if (! isset($currentResults[$id])) { $currentResults[$id] = 0; } } ksort($currentResults); $lastResults = $this->getResults( $questions, Carbon::now()->quarter, Carbon::now()->year, ); $fullYear = $this->getResults( $questions, null, Carbon::now()->subQuarter()->year, ); $this->dispatch('rebuildBarChart'); return view('livewire.dashboard.pie-chart', [ 'data' => json_encode($currentResults), 'topicName' => $topic->name, // stats: 'current' => $this->average($currentResults), 'currentN' => $this->getInterviewCount( year: Carbon::now()->year, quarter: Carbon::now()->quarter, ), 'last' => $this->average($lastResults), 'lastN' => $this->getInterviewCount( year: Carbon::now()->subQuarter()->year, quarter: Carbon::now()->subQuarter()->quarter, ), 'fullYear' => $this->average($fullYear), 'fullYearN' => $this->getInterviewCount( year: Carbon::now()->subYear()->year ), ]); } private function sum(array $results): int { return collect($results)->sum(); } private function average(array $results): float { $set = collect([]); foreach ($results as $value => $amount) { foreach (range(1, $amount) as $test) { $set->add($value); } } $set = $set->filter(); return round($set->average(), 1); } public function getInterviewCount(int $year, ?int $quarter = null): int { $quarterFilter = $quarter != null ? " AND QUARTER(created_at) = $quarter" : ''; return Interview::where([ ['topic_id', $this->topicId], ['is_test', 0], ])->whereRaw("YEAR(created_at) = $year".$quarterFilter)->count(); } private function getResults(array $questions, $quarter, $year): array { $results = []; foreach ($questions as $key => $question) { $interviews = Interview::where('topic_id', $this->topicId) ->questionResults($question['name']) ->byQuarter($quarter, $year) ->get()->toArray(); foreach ($interviews as $int) { if (isset($results[$int['answer']])) { $results[$int['answer']] += $int['count']; } else { $results[$int['answer']] = $int['count']; } } } return $results; } }