Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CustomerSurveyFactory
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getSurveysByDateRange
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 calculateResults
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3namespace BuyerKiosk\CustomerSurvey;
4
5class CustomerSurveyFactory {
6
7    private $store;
8    private $storeDB;
9    private $log;
10
11    public function __construct(\Store $store)
12    {
13        $this->log = new \KLogger($_ENV['LOG_DIR']."survey.log", \KLogger::DEBUG);
14        if($store instanceof \Store) {
15            $this->store = $store;
16            $this->storeDB = dbConnectByName($store->getDbName());
17            return true;
18        }
19        return false;
20    }
21
22    //$dateStart and $dateEnd are expected to be MySQL timestamp formatted time strings
23    public function getSurveysByDateRange($dateStart, $dateEnd) {
24        $start = getDateRange($dateStart);
25        $end = getDateRange($dateEnd);
26        $surveyArray = [];
27
28
29        $stmt = $this->storeDB->prepare("SELECT
30                                                    id,
31                                                    customerSurvey.customerID,
32                                                    customerSurvey.buyID,
33                                                    step1,
34                                                    step2,
35                                                    step3,
36                                                    step4,
37                                                    step5,
38                                                    step6,
39                                                    step6Comment,
40                                                    dateSubmitted,
41                                                    userAgent,
42                                                    type,
43                                                    comments,
44                                                    firstName,
45                                                    lastName,
46                                                    buyer.employeeFirstName AS buyerFirstName,
47                                                    buyer.employeeLastName AS buyerLastName,
48                                                    buyer.employeeID AS buyerID,
49                                                    sorter.employeeLastName AS sorterLastName,
50                                                    sorter.employeeFirstName AS sorterFirstName,
51                                                    sorter.employeeID AS sorterID
52                                                FROM
53                                                    customerSurvey
54                                                    JOIN customers ON customerSurvey.customerID = customers.customerID
55                                                    JOIN buyQueue ON customerSurvey.buyID = buyQueue.buyID
56                                                    LEFT JOIN employees AS buyer ON buyQueue.buyerID = buyer.employeeID
57                                                    LEFT JOIN employees AS sorter ON buyQueue.sorterID = sorter.employeeID 
58                                                WHERE
59                                                    dateSubmitted BETWEEN :start 
60                                                    AND :end 
61                                                ORDER BY
62                                                    dateSubmitted DESC");
63        $stmt->bindValue(":start",$start['dateStart']->format("Y-m-d H:i:s"));
64        $stmt->bindValue(":end",$end['dateEnd']->format("Y-m-d H:i:s"));
65        if($stmt->execute()) {
66            while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
67                $survey = new CustomerSurvey($this->store);
68                $survey->createFromRow($row);
69                $surveyArray[] = $survey;
70            }
71            $results = $this->calculateResults($surveyArray);
72            return array("surveys" => $surveyArray, "results" => $results);
73
74        }
75        return null;
76    }
77    public function calculateResults($surveys) {
78        $question1 = [0 => 0, 1 => 0, 2 => 0];
79        $question2 = [-1 => 0, 0 => 0, 1 => 0, 2 => 0, 3 => 0];
80        $question3 = [-1 => 0, 0 => 0, 1 => 0, 2 => 0, 3 => 0];
81        $overallRating = [1=>0, 2=>0, 3=>0, 4=>0, 5=>0];
82        $question6 = [0=>0, 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0, 8=>0];
83        $totalRatingCount = 0;
84        $totalRatingSum =  0;
85        //$question5 = [];
86        foreach($surveys as $survey) {
87            /** @var CustomerSurvey $survey */
88            if (isset($question1[$survey->step1])) {
89                $question1[$survey->step1] += 1;
90            }
91            if (isset($question2[$survey->step2])) {
92                $question2[$survey->step2] += 1;
93            }
94            if (isset($question3[$survey->step3])) {
95                $question3[$survey->step3] += 1;
96            }
97            if (isset($overallRating[$survey->step4])) {
98                $overallRating[$survey->step4] += 1;
99            }
100            if (isset($survey->step6) && isset($question6[$survey->step6])) {
101                $question6[$survey->step6] += 1;
102            }
103            $totalRatingCount++;
104            $totalRatingSum += $survey->step4 ?? 0;
105        }
106        if($totalRatingCount > 0) {
107            $ratingAverage = round($totalRatingSum/$totalRatingCount, 1);
108        } else {
109            $ratingAverage = 0;
110        }
111        //Little Hack to get the data to play nicer with Chart.js
112        $question2[4] = $question2[-1];
113        $question3[4] = $question3[-1];
114        unset($question2[-1]);
115        unset($question3[-1]);
116        return array("question1" => $question1, "question2" => $question2, "question3" => $question3, "question6" => $question6, "overallRating" => $overallRating, "ratingAverage" => $ratingAverage);
117    }
118}