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. }
  120. }
  121. // Totals by period
  122. /** @var Chart $chart */
  123. foreach($charts as $chart) {
  124. if(stripos($chart->getTitle(), $chart_prefix) !== false) {
  125. $chart->addDataTable($slide, 30, 360);
  126. break;
  127. }
  128. }
  129. };
  130. $create_summary_slide([
  131. "title" => $template_config->reports->gamingStatsPage->title ?? null,
  132. "chart_prefix" => $template_config->reports->gamingStatsPage->chartPrefix ?? null
  133. ]);
  134. if(isset($template_config->reports->barStatsPage)) {
  135. $create_summary_slide([
  136. "title" => $template_config->reports->barStatsPage->title ?? null,
  137. "array_key" => "barCounts",
  138. "chart_prefix" => $template_config->reports->barStatsPage->chartPrefix ?? null
  139. ]);
  140. }
  141. if(isset($template_config->reports->combinedStatsPage)) {
  142. $create_summary_slide([
  143. "title" => $template_config->reports->combinedStatsPage->title ?? null,
  144. "array_key" => "combinedCounts",
  145. "chart_prefix" => $template_config->reports->combinedStatsPage->chartPrefix ?? null
  146. ]);
  147. }
  148. $names = [];
  149. $charts_b64 = http::value("charts_b64");
  150. foreach($charts_b64 as $index => $b64) {
  151. $chart = $charts[$index] ?? null;
  152. if(!$chart) {
  153. continue;
  154. }
  155. $slide = $presentation->createSlide();
  156. // Write chart
  157. list($ignore, $png_b64) = explode("base64,", $b64);
  158. $name = tempnam("/tmp", "ppt");
  159. $name .= ".png";
  160. $handle = fopen($name, "wb");
  161. fwrite($handle, base64_decode($png_b64));
  162. fclose($handle);
  163. $names[] = $name;
  164. $chart->addChart($slide, $name);
  165. // Write data table
  166. if($chart->print) {
  167. $chart->addDataTable($slide);
  168. }
  169. }
  170. // Region/Area Manager Stats
  171. foreach([0, 1] as $mode) {
  172. switch($mode) {
  173. case 0:
  174. $current_data = $region_data;
  175. $mode_text = "Region";
  176. break;
  177. case 1:
  178. $current_data = $area_manager_data;
  179. $mode_text = "Area Manager";
  180. break;
  181. }
  182. foreach($current_data as $data_id => $rows) {
  183. $slide = $presentation->createSlide();
  184. // Title
  185. $shape = $slide->createRichTextShape()
  186. ->setHeight(120)->setWidth(940)
  187. ->setOffsetX(30)->setOffsetY(30);
  188. $run = $shape->createTextRun("$mode_text: $data_id");
  189. $run->getFont()->setSize(36);
  190. // Table
  191. $shape = $slide->createTableShape(5)
  192. ->setWidth(900)
  193. ->setOffsetX(30)
  194. ->setOffsetY(110);
  195. // Create header row
  196. $row = $shape->createRow();
  197. $row->setHeight(20);
  198. foreach(["Period", "Tests", "Passes", "Fails", "Rate"] as $label) {
  199. $cell = $row->nextCell();
  200. $cell->getActiveParagraph()->getAlignment()
  201. ->setMarginBottom(4)
  202. ->setMarginLeft(4)
  203. ->setMarginRight(4)
  204. ->setMarginTop(4);
  205. $cell->createTextRun($label)
  206. ->getFont()->setBold(true);
  207. }
  208. $index = 0;
  209. foreach($rows as $key => $data) {
  210. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  211. $label = $key_labels[$index];
  212. $values = [$data->testCount, $data->passCount, $data->failCount, "$rate%"];
  213. $row = $shape->createRow();
  214. $row->setHeight(20);
  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. foreach($values as $value) {
  224. $cell = $row->nextCell();
  225. $cell->getActiveParagraph()->getAlignment()
  226. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  227. ->setMarginBottom(4)
  228. ->setMarginLeft(4)
  229. ->setMarginRight(4)
  230. ->setMarginTop(4);
  231. $cell->createTextRun($value);
  232. }
  233. $index++;
  234. }
  235. }
  236. }
  237. if($action === "export-pptx") {
  238. header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
  239. http::setDownloadHeaders("report.pptx");
  240. $pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
  241. $pptx_writer->save("php://output");
  242. }
  243. if($action === "export-odp") {
  244. header("Content-Type: application/vnd.oasis.opendocument.presentation");
  245. http::setDownloadHeaders("report.odp");
  246. $odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
  247. $odp_writer->save("php://output");
  248. }
  249. foreach($names as $name) {
  250. @unlink($name);
  251. }
  252. exit;