reports_presentation.php 7.7 KB

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