Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 86 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| CustomerSurveyController | |
0.00% |
0 / 86 |
|
0.00% |
0 / 6 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| showResultsPage | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
| getAllSurveyLinks | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| addSurveyLink | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| submitSurvey | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
156 | |||
| getSurveysByDateRange | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\CustomerSurvey\Controllers; |
| 4 | |
| 5 | use BuyerKiosk\CustomerSurvey\CustomerSurveyFactory; |
| 6 | use BuyerKiosk\CustomerSurvey\CustomerSurvey; |
| 7 | use BuyerKiosk\CustomerSurvey\Link; |
| 8 | |
| 9 | class CustomerSurveyController extends \BuyerKiosk\Core\Controllers\BaseController { |
| 10 | |
| 11 | private $store; |
| 12 | private $storeDB; |
| 13 | private $log; |
| 14 | //private $redis; |
| 15 | /** @var \UserFrosting\UserFrosting $_app */ |
| 16 | protected $_app; |
| 17 | |
| 18 | public function __construct($app, \Store $store) |
| 19 | { |
| 20 | parent::__construct($app); |
| 21 | $this->store = $store; |
| 22 | $this->log = new \KLogger($_ENV['LOG_DIR']."survey.log", \KLogger::DEBUG); |
| 23 | //$this->redis = new \Predis\Client(); |
| 24 | $this->_app = $app; |
| 25 | $this->storeDB = dbConnectByName($store->getDbName()); |
| 26 | } |
| 27 | |
| 28 | public function showResultsPage() { |
| 29 | $surveys = new CustomerSurveyFactory($this->store); |
| 30 | $today = new \DateTime('now', new \DateTimeZone($this->store->getTimeZone())); |
| 31 | $begin = new \DateTime('now', new \DateTimeZone($this->store->getTimeZone())); |
| 32 | $begin->sub(new \DateInterval("P30D")); |
| 33 | $results = $surveys->getSurveysByDateRange($begin->format("Y-m-d H:i:s"),$today->format("Y-m-d H:i:s")); |
| 34 | $this->_app->render('survey/results.html', [ |
| 35 | 'page' => [ |
| 36 | 'author' => $this->_app->site->author, |
| 37 | 'title' => "Post-Buy Survey Results", |
| 38 | 'description' => "Post-Buy Survey Results", |
| 39 | 'alerts' => $this->_app->alerts->getAndClearMessages() |
| 40 | ], |
| 41 | "surveys" => $results['surveys'], |
| 42 | "results" => $results['results'], |
| 43 | 'groups' => $this->_app->user->getGroups(), |
| 44 | "box_title" => "Post-Buy Survey Results", |
| 45 | "store" => getStoreInfo($this->store), |
| 46 | ]); |
| 47 | } |
| 48 | |
| 49 | public function getAllSurveyLinks() { |
| 50 | $linkArray = []; |
| 51 | try { |
| 52 | $stmt = $this->storeDB->prepare("SELECT * FROM surveyLinks WHERE 1"); |
| 53 | $stmt->execute(); |
| 54 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 55 | $tempLink = new Link($this->store, $this->storeDB); |
| 56 | $tempLink->name = $row['name']; |
| 57 | $tempLink->link = $row['link']; |
| 58 | $tempLink->active = $row['active']; |
| 59 | $tempLink->setID($row['id']); |
| 60 | $linkArray[] = $tempLink; |
| 61 | } |
| 62 | return $linkArray; |
| 63 | } catch (\PDOException $e) { |
| 64 | |
| 65 | } |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | public function addSurveyLink(Link $link) |
| 70 | { |
| 71 | try { |
| 72 | //If active was not set, go ahead and assume it should be 1 before we do the validity check |
| 73 | if (!isset($link->active)) { |
| 74 | $link->active = 1; |
| 75 | } |
| 76 | //Check if all values are valid and ready to be inserted |
| 77 | if ($link->isValid()) { |
| 78 | $stmt = $this->storeDB->prepare("INSERT INTO surveyLinks (name, link, active) VALUES (:name, :link, 1)"); |
| 79 | $stmt->bindValue(":name", $link->name); |
| 80 | $stmt->bindValue(":link", $link->url); |
| 81 | if ($stmt->execute()) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } catch (\PDOException $e) { |
| 86 | |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Handle the submission of a customer survey |
| 93 | * |
| 94 | * @param array $surveyData The survey data submitted by the user |
| 95 | * @return bool True if submission was successful, false otherwise |
| 96 | */ |
| 97 | public function submitSurvey($surveyData) |
| 98 | { |
| 99 | $this->log->LogDebug("Submitting survey for buyID: " . (isset($surveyData['buyID']) ? $surveyData['buyID'] : 'unknown')); |
| 100 | |
| 101 | try { |
| 102 | // Ensure step5 is an integer |
| 103 | if (isset($surveyData['step5']) && $surveyData['step5'] === '') { |
| 104 | $surveyData['step5'] = 0; // Default to 0 if empty |
| 105 | } |
| 106 | |
| 107 | // Prepare the insert statement |
| 108 | $stmt = $this->storeDB->prepare("INSERT INTO customerSurvey |
| 109 | (buyID, customerID, step1, step2, step3, step4, step5, step6, step6Comments, comments, userAgent, dateSubmitted, type) |
| 110 | VALUES |
| 111 | (:buyID, :customerID, :step1, :step2, :step3, :step4, :step5, :step6, :step6Comments, :comments, :userAgent, NOW(), :type)"); |
| 112 | |
| 113 | // Bind parameters |
| 114 | $stmt->bindValue(":buyID", $surveyData['buyID']); |
| 115 | $stmt->bindValue(":customerID", $surveyData['customerID']); |
| 116 | $stmt->bindValue(":step1", $surveyData['step1']); |
| 117 | $stmt->bindValue(":step2", $surveyData['step2']); |
| 118 | $stmt->bindValue(":step3", $surveyData['step3']); |
| 119 | $stmt->bindValue(":step4", $surveyData['step4']); |
| 120 | $stmt->bindValue(":step5", isset($surveyData['step5']) ? $surveyData['step5'] : 0); |
| 121 | $stmt->bindValue(":step6", isset($surveyData['step6']) ? $surveyData['step6'] : 0); |
| 122 | $stmt->bindValue(":step6Comments", isset($surveyData['step6Comments']) ? $surveyData['step6Comments'] : ''); |
| 123 | $stmt->bindValue(":comments", isset($surveyData['comments']) ? $surveyData['comments'] : ''); |
| 124 | $stmt->bindValue(":userAgent", isset($surveyData['userAgent']) ? $surveyData['userAgent'] : ''); |
| 125 | $stmt->bindValue(":type", isset($surveyData['type']) ? $surveyData['type'] : 'buy'); |
| 126 | |
| 127 | // Execute the query |
| 128 | if ($stmt->execute()) { |
| 129 | $this->log->LogDebug("Successfully submitted survey for buyID: " . $surveyData['buyID']); |
| 130 | return true; |
| 131 | } else { |
| 132 | $this->log->LogError("Failed to submit survey for buyID: " . $surveyData['buyID']); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | } catch (\PDOException $e) { |
| 137 | $this->log->LogError("Database error submitting survey: " . $e->getMessage()); |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get survey results by date range |
| 144 | * |
| 145 | * @param string $startDate Start date in format Y-m-d H:i:s |
| 146 | * @param string $endDate End date in format Y-m-d H:i:s |
| 147 | * @return array Survey results or empty array if none found |
| 148 | */ |
| 149 | public function getSurveysByDateRange($startDate, $endDate) |
| 150 | { |
| 151 | $factory = new CustomerSurveyFactory($this->store); |
| 152 | $results = $factory->getSurveysByDateRange($startDate, $endDate); |
| 153 | |
| 154 | // If no results, return empty arrays instead of null |
| 155 | if (!$results) { |
| 156 | return [ |
| 157 | 'surveys' => [], |
| 158 | 'results' => [ |
| 159 | 'question1' => [0 => 0, 1 => 0, 2 => 0], |
| 160 | 'question2' => [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0], |
| 161 | 'question3' => [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0], |
| 162 | 'question6' => [0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0], |
| 163 | 'overallRating' => [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0], |
| 164 | 'ratingAverage' => 0 |
| 165 | ] |
| 166 | ]; |
| 167 | } |
| 168 | |
| 169 | return $results; |
| 170 | } |
| 171 | } |