reports_presentation.php 8.3 KB

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