芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.herta-bht.smartcon-survey.com/app/Services/StatementsService.php
publishedWaves = DataUpload::getPublishedWaves($this->countryId); // If getAllWaves is false, we will only get the last 2 quarters if (! $this->getAllWaves) { $this->quarterInfo = Interview::getLastTowQuarterInfo($this->countryId, $this->publishedWaves); } else { $this->quarterInfo = Interview::getQuarterInfo($this->countryId, $this->publishedWaves); } $this->aggregatedInterviews = $this->getAggregatedInterviews(); $this->statements = Statement::select(['id', 'sid', 'text'])->get()->toArray(); $this->totalNs = $this->getTotals(); } /** * Get the total counts of unique users per wave for each brand. * * @return array The total counts of unique users per wave for each brand */ private function getTotals(): array { return $this->brands->reduce(function ($curry, $brand) { foreach ($this->quarterInfo as $quarter) { $key = $quarter['quarter'].'-'.$quarter['year']; $curry[$brand->sid][$key] = (int) DataAggregation::where([ 'country_id' => $this->countryId, 'question_name' => 'Q2', 'model_id' => $brand->sid, 'quarter' => $quarter['quarter'], 'year' => $quarter['year'], 'value' => '1', ])->sum('aggregate'); } return $curry; }, []); } /** * Get the collection of interviews. * * @return EloquentCollection The collection of interviews */ private function getAggregatedInterviews(): EloquentCollection { return DataAggregation::whereIn('wave', $this->publishedWaves) ->whereIn('question_name', ['Q9', 'Q19']) ->where('country_id', $this->countryId) ->where('value', '1') ->get(); } /** * Calculate the wave data for a given statement and wave. * * @param int $statementId The ID of the statement * @return array The wave data */ public function calculateWaveData(int $statementId, array $quarter): array { return $this->brands->sortBy('id')->reduce(function ($curry, $brand) use ($statementId, $quarter) { $totalN = $this->totalNs[$brand->sid][$quarter['quarter'].'-'.$quarter['year']]; $count = $this->aggregatedInterviews->where('model_id', $brand->sid) ->where('statement_id', $statementId) ->where('quarter', $quarter['quarter']) ->where('year', $quarter['year']) ->where('value', '1') ->sum('aggregate'); $curry[] = [ 'brand' => $brand, 'n' => $count, 'label' => 'Q'.$quarter['quarter'].' '.$quarter['year'], 'totalN' => $totalN, 'value' => $totalN > 0 ? round(($count / $totalN) * 100) : 0, ]; return $curry; }, []); } /** * Get the values for a given statement. * * @param int $statementId The ID of the statement * @return array The values for the statement */ public function getValues(int $statementId): array { return [ 'last' => $this->calculateWaveData($statementId, $this->quarterInfo->first()), 'current' => $this->calculateWaveData($statementId, $this->quarterInfo->last()), ]; } /** * Get the data for all statements. * * @param bool $topStatements Whether to return only the top statements * @return array The data for all statements */ public function getData(bool $topStatements = false): array { $statements = $this->statements; /* * If topStatements is true, we will only return the top statements * */ if ($topStatements) { $statements = array_filter($statements, fn ($statement) => in_array($statement['sid'], $this->topStatements, true)); } else { $statements = array_filter($statements, fn ($statement) => ! in_array($statement['sid'], $this->topStatements, true)); } if (! $this->getAllWaves) { $quarters = [ 'last' => "Q{$this->quarterInfo->first()['quarter']} {$this->quarterInfo->first()['year']}", 'current' => "Q{$this->quarterInfo->last()['quarter']} {$this->quarterInfo->last()['year']}", ]; } else { $quarters = $this->quarterInfo->map(fn ($wave) => "Q{$wave['quarter']} {$wave['year']}")->values()->toArray(); } return [ 'totalN' => $this->quarterInfo->sum('aggregate'), 'quarterLabels' => $quarters, 'data' => array_map(function ($statement) { return [ 'id' => $statement['sid'], 'text' => $statement['text'], 'values' => $this->getValues($statement['sid']), ]; }, array_values($statements)), ]; } /** * Get the top statements. * * @return array The top statements */ public function getTopStatements(): array { return $this->getData(true); } public function getStatements(): array { return $this->statements; } }