performance.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. Core::addScript("public/js/Chart.bundle.min.js");
  3. Core::addScript("public/js/app-performance.js");
  4. Core::addBreadcrumb("/account", "Account");
  5. Core::addBreadcrumb("/account/performance", "Performance");
  6. $customer = Customer::getForAdmin();
  7. if(!$customer) throw new Exception("You do not have permission to access this page.");
  8. $page_title = "$customer->name Performance";
  9. Core::addBreadcrumb("/account/performance?customer_id=$customer->id", $page_title);
  10. $date_range = http::value("date_range");
  11. $date_resolution = http::value("date_resolution");
  12. $project_ids = http::value("project_ids", "int");
  13. if(!is_array($project_ids)) $project_ids = [];
  14. $all_projects = Project::query([
  15. "customer_id" => $customer->id
  16. ]);
  17. $data = [];
  18. $region_data = [];
  19. $area_manager_data = [];
  20. $total_data = false;
  21. $locations_missing_region = [];
  22. $locations_missing_area_manager = [];
  23. $charts = [];
  24. $submit = http::value("submit");
  25. if($submit) {
  26. try {
  27. Core::validateCSRFToken($submit);
  28. $form_exception = new FormException;
  29. if($form_exception->hasErrors()) throw $form_exception;
  30. // Get all tests
  31. $projects = Project::query([
  32. "customer_id" => $customer->id,
  33. "id" => $project_ids,
  34. ]);
  35. $tests = Test::query([
  36. "project_id" => $project_ids,
  37. "status" => Test::STATUS_COMPLETED
  38. ]);
  39. User::load(array_column($tests, "tester_id"));
  40. $locations = Location::load(array_column($tests, "location_id"));
  41. $regions = array_filter(array_column($locations, "region"));
  42. $area_managers = array_filter(array_column($locations, "area_manager"));
  43. uksort($regions, "strnatcasecmp");
  44. uksort($area_managers, "strnatcasecmp");
  45. foreach($regions as $region) {
  46. $region_data[$region] = [];
  47. }
  48. foreach($area_managers as $area_manager) {
  49. $area_manager_data[$area_manager] = [];
  50. }
  51. $has_regions = !empty($region_data);
  52. $has_area_managers = !empty($area_manager_data);
  53. foreach($locations as $location) {
  54. if($has_regions && !$location->region) $locations_missing_region[] = $location;
  55. if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
  56. }
  57. $min_year = date("Y");
  58. $max_year = date("Y");
  59. switch($date_range) {
  60. case "ytd":
  61. // Nothing to do
  62. break;
  63. case "unbound":
  64. // Work out year range
  65. foreach($tests as $test) {
  66. $dt = new DateTime($test->dts_start . " UTC");
  67. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  68. $year = $dt->format("Y");
  69. if($year < $min_year) $min_year = $year;
  70. if($year > $max_year) $max_year = $year;
  71. }
  72. break;
  73. case "custom":
  74. // TODO
  75. break;
  76. }
  77. $empty_data = (object) [
  78. "label" => "",
  79. "testCount" => 0,
  80. "passCount" => 0,
  81. "failCount" => 0,
  82. "maleTesterCount" => 0,
  83. "femaleTesterCount" => 0,
  84. "maleChallengerCount" => 0,
  85. "femaleChallengerCount" => 0,
  86. "maleChallengeMaleCount" => 0,
  87. "maleChallengeMalePassCount" => 0,
  88. "maleChallengeFemaleCount" => 0,
  89. "maleChallengeFemalePassCount" => 0,
  90. "femaleChallengeMaleCount" => 0,
  91. "femaleChallengeMalePassCount" => 0,
  92. "femaleChallengeFemaleCount" => 0,
  93. "femaleChallengeFemalePassCount" => 0,
  94. "stage1Count" => 0,
  95. "stage2Count" => 0,
  96. "stage3Count" => 0,
  97. "stage4Count" => 0,
  98. "stage5Count" => 0,
  99. ];
  100. $total_data = (object) [
  101. "maleTesterCount" => 0,
  102. "femaleTesterCount" => 0,
  103. "maleChallengerCount" => 0,
  104. "femaleChallengerCount" => 0,
  105. "passCount" => 0,
  106. "failCount" => 0,
  107. "stage1Count" => 0,
  108. "stage2Count" => 0,
  109. "stage3Count" => 0,
  110. "stage4Count" => 0,
  111. "stage5Count" => 0,
  112. ];
  113. $resolution_name = "";
  114. // Initialise date-array based on date range and resolution
  115. for($year = $min_year; $year <= $max_year; $year++) {
  116. switch($date_resolution) {
  117. case "monthly":
  118. $resolution_name = "Month";
  119. foreach(range(1, 12) as $month) {
  120. $label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y")));
  121. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  122. $key = "$year-$month";
  123. $data[$key] = clone $empty_data;
  124. $data[$key]->label = $label;
  125. foreach($region_data as &$r) {
  126. $r[$key] = clone $empty_data;
  127. $r[$key]->label = $label;
  128. }
  129. foreach($area_manager_data as &$a) {
  130. $a[$key] = clone $empty_data;
  131. $a[$key]->label = $label;
  132. }
  133. }
  134. break;
  135. case "quarterly":
  136. default:
  137. $resolution_name = "Quarter";
  138. foreach(range(1, 4) as $quarter) {
  139. $label = "Q$quarter $year";
  140. $key = "$year-$quarter";
  141. $data[$key] = clone $empty_data;
  142. $data[$key]->label = $label;
  143. foreach($region_data as &$r) {
  144. $r[$key] = clone $empty_data;
  145. $r[$key]->label = $label;
  146. }
  147. foreach($area_manager_data as &$a) {
  148. $a[$key] = clone $empty_data;
  149. $a[$key]->label = $label;
  150. }
  151. }
  152. break;
  153. }
  154. }
  155. $max_unix = 0;
  156. // Increment counts based on test results
  157. foreach($tests as $test) {
  158. $tester = $test->getTester();
  159. $location = $test->getLocation();
  160. $region = $location->region;
  161. $area_manager = $location->area_manager;
  162. $dt = new DateTime($test->dts_start . " UTC");
  163. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  164. $unix = $dt->format("U");
  165. $year = $dt->format("Y");
  166. $month = $dt->format("m");
  167. $quarter = ceil($month / 3);
  168. if($unix > $max_unix) {
  169. $max_unix = $unix;
  170. }
  171. switch($date_resolution) {
  172. case "monthly":
  173. $key = "$year-$month";
  174. break;
  175. case "quarterly":
  176. default:
  177. $key = "$year-$quarter";
  178. break;
  179. }
  180. $data[$key]->testCount++;
  181. $stage_key = "stage" . $test->challenge_stage . "Count";
  182. $data[$key]->$stage_key++;
  183. if($test->pass) {
  184. $data[$key]->passCount++;
  185. $total_data->passCount++;
  186. if($region) $region_data[$region][$key]->passCount++;
  187. if($area_manager) $area_manager_data[$area_manager][$key]->passCount++;
  188. }
  189. else {
  190. $data[$key]->failCount++;
  191. $total_data->failCount++;
  192. if($region) $region_data[$region][$key]->failCount++;
  193. if($area_manager) $area_manager_data[$area_manager][$key]->failCount++;
  194. }
  195. if($tester->gender == "M") {
  196. $data[$key]->maleTesterCount++;
  197. $total_data->maleTesterCount++;
  198. }
  199. if($tester->gender == "F") {
  200. $data[$key]->femaleTesterCount++;
  201. $total_data->femaleTesterCount++;
  202. }
  203. if($test->challenger_gender == "M") {
  204. $data[$key]->maleChallengerCount++;
  205. $total_data->maleChallengerCount++;
  206. }
  207. if($test->challenger_gender == "F") {
  208. $data[$key]->femaleChallengerCount++;
  209. $total_data->femaleChallengerCount++;
  210. }
  211. if($tester->gender == "M") {
  212. if($test->challenger_gender == "M") {
  213. $data[$key]->maleChallengeMaleCount++;
  214. if($test->pass) $data[$key]->maleChallengeMalePassCount++;
  215. }
  216. if($test->challenger_gender == "F") {
  217. $data[$key]->femaleChallengeMaleCount++;
  218. if($test->pass) $data[$key]->femaleChallengeMalePassCount++;
  219. }
  220. }
  221. if($tester->gender == "F") {
  222. if($test->challenger_gender == "M") {
  223. $data[$key]->maleChallengeFemaleCount++;
  224. if($test->pass) $data[$key]->maleChallengeFemalePassCount++;
  225. }
  226. if($test->challenger_gender == "F") {
  227. $data[$key]->femaleChallengeFemaleCount++;
  228. if($test->pass) $data[$key]->femaleChallengeFemalePassCount++;
  229. }
  230. }
  231. }
  232. $current_month = date("m", $max_unix);
  233. $current_quarter = ceil(date("n", $max_unix) / 3);
  234. $x_data_labels = array_values(array_map(function($d) {
  235. return $d->label;
  236. }, $data));
  237. // Current Period
  238. if($date_resolution == "monthly") {
  239. $key = date("Y") . "-" . $current_month;
  240. $current_period_name = date("M Y", $max_unix);
  241. }
  242. if($date_resolution == "quarterly") {
  243. $key = date("Y") . "-" . $current_quarter;
  244. $current_period_name = "Q" . $current_quarter . " " . date("Y", $max_unix);
  245. }
  246. // Challenger Gender
  247. $chart = new Chart;
  248. $chart->setType("pie");
  249. $chart->setTitle("Staff Gender");
  250. $chart->setLabels(["Male", "Female"]);
  251. $chart->addDataset("Counts", [
  252. $total_data->maleChallengerCount,
  253. $total_data->femaleChallengerCount,
  254. ], [
  255. "background_colours" => [
  256. "#2196F3",
  257. "#E91E63",
  258. ]
  259. ]);
  260. $charts[] = $chart;
  261. // Tester Gender
  262. $chart = new Chart;
  263. $chart->setType("pie");
  264. $chart->setTitle("Tester Gender");
  265. $chart->setLabels(["Male", "Female"]);
  266. $chart->addDataset("Counts", [
  267. $total_data->maleTesterCount,
  268. $total_data->femaleTesterCount,
  269. ], [
  270. "background_colours" => [
  271. "#2196F3",
  272. "#E91E63",
  273. ]
  274. ]);
  275. $charts[] = $chart;
  276. // Pass / Fail
  277. $chart = new Chart;
  278. $chart->setType("pie");
  279. $chart->setTitle("Pass / Fail");
  280. $chart->setLabels(["Pass", "Fail"]);
  281. $chart->addDataset("Counts", [
  282. $total_data->passCount,
  283. $total_data->failCount,
  284. ], [
  285. "background_colours" => [
  286. "#4CAF50",
  287. "#F44336",
  288. ]
  289. ]);
  290. $charts[] = $chart;
  291. // Performance by Period
  292. $performance = array_values(array_map(function($d) {
  293. if($d->testCount == 0) return 0;
  294. return round(($d->passCount / $d->testCount) * 100);
  295. }, $data));
  296. $chart = new Chart;
  297. $chart->setType("bar");
  298. $chart->setTitle("Performance by $resolution_name");
  299. $chart->setLabels($x_data_labels);
  300. $chart->addDataset("Performance (%)", $performance, [
  301. "background_colours" => "#2196F3"
  302. ]);
  303. $charts[] = $chart;
  304. // Point of Challenge (Current Period)
  305. $period_data = $data[$key];
  306. $chart = new Chart;
  307. $chart->setType("pie");
  308. $chart->setTitle("$current_period_name Performance by Point of Challenge");
  309. $chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
  310. $chart->addDataset("Counts", [
  311. $period_data->stage1Count,
  312. $period_data->stage2Count,
  313. $period_data->stage3Count,
  314. $period_data->stage4Count,
  315. $period_data->stage5Count,
  316. ], [
  317. "background_colours" => [
  318. "#4CAF50",
  319. "#FFEB3B",
  320. "#FBC02D",
  321. "#FFAB91",
  322. "#F44336",
  323. ]
  324. ]);
  325. $charts[] = $chart;
  326. // Tester Genders by Period
  327. $male_counts = array_values(array_map(function($d) {
  328. return $d->maleTesterCount;
  329. }, $data));
  330. $female_counts = array_values(array_map(function($d) {
  331. return $d->femaleTesterCount;
  332. }, $data));
  333. $chart = new Chart;
  334. $chart->setType("bar");
  335. $chart->setTitle("Tester Gender by $resolution_name");
  336. $chart->setLabels($x_data_labels);
  337. $chart->addDataset("Male", $male_counts, [
  338. "background_colours" => "#2196F3"
  339. ]);
  340. $chart->addDataset("Female", $female_counts, [
  341. "background_colours" => "#E91E63"
  342. ]);
  343. $charts[] = $chart;
  344. // Test Count by Period
  345. $test_counts = array_values(array_map(function($d) {
  346. return $d->testCount;
  347. }, $data));
  348. $chart = new Chart;
  349. $chart->setType("bar");
  350. $chart->setTitle("Tests by $resolution_name");
  351. $chart->setLabels($x_data_labels);
  352. $chart->addDataset("Tests", $test_counts, [
  353. "background_colours" => "#2196F3"
  354. ]);
  355. $charts[] = $chart;
  356. if($has_regions) {
  357. }
  358. if($has_area_managers) {
  359. }
  360. }
  361. catch(FormException $e) {
  362. $e->generateAlerts();
  363. }
  364. catch(Exception $e) {
  365. Core::addAlert("danger", $e->getMessage());
  366. }
  367. }
  368. View::includeOnce("header", compact("page_title"));
  369. View::include("performance", compact("customer", "date_range", "date_resolution", "all_projects", "project_ids", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager"));
  370. View::includeOnce("footer");
  371. ?>