reports_presentation.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var string $date_range_label
  4. * @var string[] $key_labels
  5. * @var ?bool $retests
  6. * @var Customer $customer
  7. * @var Template $template
  8. * @var object $template_config
  9. * @var bool $has_logo
  10. * @var string $logo_file_path
  11. * @var array $total_data
  12. * @var array $charts
  13. * @var array $region_data
  14. * @var array $area_manager_data
  15. * @var array $operations_manager_data
  16. * @var array $brand_data
  17. * @var array $gaming_manager_data
  18. * @var array $senior_gaming_manager_data
  19. * @var string $action
  20. */
  21. use Carbon\CarbonImmutable;
  22. use PhpOffice\PhpPresentation\PhpPresentation;
  23. use PhpOffice\PhpPresentation\IOFactory;
  24. use PhpOffice\PhpPresentation\Shape\RichText\Run;
  25. use PhpOffice\PhpPresentation\Style\Alignment;
  26. $presentation = new PhpPresentation(); // 960x720
  27. // Draw title slide
  28. $slide = $presentation->getActiveSlide();
  29. $shape = $slide->createDrawingShape();
  30. $shape->setName(APP_NAME . " logo")
  31. ->setDescription(APP_NAME . " logo")
  32. ->setPath("./public/img/logo.png")
  33. ->setWidth(200)
  34. ->setOffsetX(380)
  35. ->setOffsetY(10);
  36. $retests_title = "All Tests";
  37. if($retests !== null) {
  38. switch($retests) {
  39. case 0: $retests_title = "Excluding Retests"; break;
  40. case 1: $retests_title = "Retests Only"; break;
  41. }
  42. }
  43. $dt_now = CarbonImmutable::now(TIMEZONE);
  44. if($has_logo) {
  45. $shape = $slide->createDrawingShape();
  46. $shape->setName("$customer->name logo")
  47. ->setDescription("$customer->name logo")
  48. ->setPath($logo_file_path)
  49. ->setWidth(720)
  50. ->setOffsetX(120)
  51. ->setOffsetY(180);
  52. $shape = $slide->createRichTextShape()
  53. ->setHeight(60)
  54. ->setWidth(940)
  55. ->setOffsetX(120)
  56. ->setOffsetY(120);
  57. }
  58. else {
  59. // Title
  60. $shape = $slide->createRichTextShape()
  61. ->setHeight(120)
  62. ->setWidth(940)
  63. ->setOffsetX(10)
  64. ->setOffsetY(315);
  65. }
  66. $title_lines = [
  67. $customer->name,
  68. $template->name,
  69. $date_range_label,
  70. $retests_title,
  71. "",
  72. __("Report Generated at %s", $dt_now->format("d/m/Y H:i")),
  73. ];
  74. foreach($title_lines as $title_line) {
  75. $paragraph = $shape->getActiveParagraph();
  76. $paragraph->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  77. $run = new Run($title_line);
  78. $run->getFont()->setBold(true)->setSize(16);
  79. $paragraph->addText($run);
  80. $paragraph = $shape->createParagraph();
  81. }
  82. $create_summary_slide = static function(array $options) use ($presentation, $charts, $date_range_label, $total_data) {
  83. $title = $options["title"] ?? "Summary";
  84. $array_key = $options["array_key"] ?? null;
  85. $chart_prefix = $options["chart_prefix"] ?? "Totals by";
  86. if($array_key) {
  87. $pass_count = $total_data->$array_key["pass"];
  88. $fail_count = $total_data->$array_key["fail"];
  89. $test_count = $pass_count + $fail_count;
  90. $has_stages = false;
  91. }
  92. else {
  93. $test_count = $total_data->testCount;
  94. $pass_count = $total_data->passCount;
  95. $fail_count = $total_data->failCount;
  96. $has_stages = true;
  97. }
  98. // Create summary slide
  99. $slide = $presentation->createSlide();
  100. // Title
  101. $shape = $slide->createRichTextShape()
  102. ->setHeight(60)->setWidth(900)
  103. ->setOffsetX(30)->setOffsetY(10);
  104. $run = $shape->createTextRun($title);
  105. $run->getFont()->setBold(true)->setSize(24);
  106. // Date range
  107. $shape = $slide->createRichTextShape()
  108. ->setHeight(60)->setWidth(900)
  109. ->setOffsetX(30)->setOffsetY(70);
  110. $run = $shape->createTextRun($date_range_label);
  111. $run->getFont()->setBold(true)->setSize(24);
  112. // Test Count
  113. $count_text = "Test Count: {$test_count}";
  114. // Pass/Fail count
  115. if(isset($pass_count)) {
  116. $pass_percentage = 0;
  117. $fail_percentage = 0;
  118. if($test_count > 0) {
  119. $pass_percentage = number_format(($pass_count * 100) / $test_count, 2);
  120. $fail_percentage = 100 - $pass_percentage;
  121. }
  122. $count_text .= "\nPass Count: {$pass_count} ({$pass_percentage}%)";
  123. $count_text .= "\nFail Count: {$fail_count} ({$fail_percentage}%)";
  124. }
  125. $shape = $slide->createRichTextShape()
  126. ->setHeight(60)->setWidth(900)
  127. ->setOffsetX(30)->setOffsetY(130);
  128. $run = $shape->createTextRun($count_text);
  129. $run->getFont()->setBold(true)->setSize(16);
  130. if($has_stages) {
  131. // Tests by stage
  132. /** @var Chart $chart */
  133. foreach($charts as $chart) {
  134. if(stripos($chart->getTitle(), "Tests by Stage") === false) {
  135. continue;
  136. }
  137. $chart->addDataTable($slide, 30, 260);
  138. break;
  139. }
  140. }
  141. // Totals by period
  142. /** @var Chart $chart */
  143. foreach($charts as $chart) {
  144. if(stripos($chart->getTitle(), $chart_prefix) !== false) {
  145. $chart->addDataTable($slide, 30, 360);
  146. break;
  147. }
  148. }
  149. };
  150. $create_summary_slide([
  151. "title" => $template_config->reports->gamingStatsPage->title ?? null,
  152. "chart_prefix" => $template_config->reports->gamingStatsPage->chartPrefix ?? null
  153. ]);
  154. if(isset($template_config->reports->barStatsPage)) {
  155. $create_summary_slide([
  156. "title" => $template_config->reports->barStatsPage->title ?? null,
  157. "array_key" => "barCounts",
  158. "chart_prefix" => $template_config->reports->barStatsPage->chartPrefix ?? null
  159. ]);
  160. }
  161. if(isset($template_config->reports->combinedStatsPage)) {
  162. $create_summary_slide([
  163. "title" => $template_config->reports->combinedStatsPage->title ?? null,
  164. "array_key" => "combinedCounts",
  165. "chart_prefix" => $template_config->reports->combinedStatsPage->chartPrefix ?? null
  166. ]);
  167. }
  168. $names = [];
  169. $charts_b64 = http::value("charts_b64");
  170. foreach($charts_b64 as $index => $b64) {
  171. $chart = $charts[$index] ?? null;
  172. if(!$chart) {
  173. continue;
  174. }
  175. $slide = $presentation->createSlide();
  176. // Write chart
  177. list($ignore, $png_b64) = explode("base64,", $b64);
  178. $name = tempnam("/tmp", "ppt");
  179. $name .= ".png";
  180. $handle = fopen($name, "wb");
  181. fwrite($handle, base64_decode($png_b64));
  182. fclose($handle);
  183. $names[] = $name;
  184. $chart->addChart($slide, $name);
  185. // Write data table
  186. if($chart->print) {
  187. $chart->addDataTable($slide);
  188. }
  189. }
  190. // Region/Area Manager Stats
  191. foreach([0, 1, 2, 3, 4, 5] as $mode) {
  192. switch($mode) {
  193. case 0:
  194. $current_data = $region_data;
  195. $mode_text = $customer->getRegionLabel(1);
  196. break;
  197. case 1:
  198. $current_data = $area_manager_data;
  199. $mode_text = $customer->getAreaManagerLabel(1);
  200. break;
  201. case 2:
  202. $current_data = $operations_manager_data;
  203. $mode_text = $customer->getOperationsManagerLabel(1);
  204. break;
  205. case 3:
  206. $current_data = $brand_data;
  207. $mode_text = $customer->getBrandLabel(1);
  208. break;
  209. case 4:
  210. $current_data = $gaming_manager_data;
  211. $mode_text = $customer->getGamingManagerLabel(1);
  212. break;
  213. case 5:
  214. $current_data = $senior_gaming_manager_data;
  215. $mode_text = $customer->getSeniorGamingManagerLabel(1);
  216. break;
  217. }
  218. foreach($current_data as $data_id => $rows) {
  219. $slide = $presentation->createSlide();
  220. // Title
  221. $shape = $slide->createRichTextShape()
  222. ->setHeight(120)->setWidth(940)
  223. ->setOffsetX(30)->setOffsetY(30);
  224. $run = $shape->createTextRun("$mode_text: $data_id");
  225. $run->getFont()->setSize(36);
  226. // Table
  227. $shape = $slide->createTableShape(5)
  228. ->setWidth(900)
  229. ->setOffsetX(30)
  230. ->setOffsetY(110);
  231. // Create header row
  232. $row = $shape->createRow();
  233. $row->setHeight(20);
  234. foreach(["Period", "Tests", "Passes", "Fails", "Rate"] as $label) {
  235. $cell = $row->nextCell();
  236. $cell->getActiveParagraph()->getAlignment()
  237. ->setMarginBottom(4)
  238. ->setMarginLeft(4)
  239. ->setMarginRight(4)
  240. ->setMarginTop(4);
  241. $cell->createTextRun($label)
  242. ->getFont()->setBold(true);
  243. }
  244. $index = 0;
  245. foreach($rows as $key => $data) {
  246. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  247. $label = $key_labels[$index];
  248. $values = [$data->testCount, $data->passCount, $data->failCount, "$rate%"];
  249. $row = $shape->createRow();
  250. $row->setHeight(20);
  251. $cell = $row->nextCell();
  252. $cell->getActiveParagraph()->getAlignment()
  253. ->setMarginBottom(4)
  254. ->setMarginLeft(4)
  255. ->setMarginRight(4)
  256. ->setMarginTop(4);
  257. $cell->createTextRun($label)
  258. ->getFont()->setBold(true);
  259. foreach($values as $value) {
  260. $cell = $row->nextCell();
  261. $cell->getActiveParagraph()->getAlignment()
  262. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  263. ->setMarginBottom(4)
  264. ->setMarginLeft(4)
  265. ->setMarginRight(4)
  266. ->setMarginTop(4);
  267. $cell->createTextRun($value);
  268. }
  269. $index++;
  270. }
  271. }
  272. }
  273. if($action === "export-pptx") {
  274. header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
  275. http::setDownloadHeaders("report.pptx");
  276. $pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
  277. $pptx_writer->save("php://output");
  278. }
  279. if($action === "export-odp") {
  280. header("Content-Type: application/vnd.oasis.opendocument.presentation");
  281. http::setDownloadHeaders("report.odp");
  282. $odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
  283. $odp_writer->save("php://output");
  284. }
  285. foreach($names as $name) {
  286. @unlink($name);
  287. }
  288. exit;