reports_presentation.php 8.0 KB

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