reports_presentation.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. use PhpOffice\PhpPresentation\PhpPresentation;
  3. use PhpOffice\PhpPresentation\IOFactory;
  4. use PhpOffice\PhpPresentation\Style\Color;
  5. use PhpOffice\PhpPresentation\Style\Alignment;
  6. use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
  7. $presentation = new PhpPresentation(); // 960x720
  8. // Draw title slide
  9. $slide = $presentation->getActiveSlide();
  10. $shape = $slide->createDrawingShape();
  11. $shape->setName("Check Policy logo")
  12. ->setDescription("Check Policy logo")
  13. ->setPath("./public/img/logo.png")
  14. ->setWidth(200)
  15. ->setOffsetX(380)
  16. ->setOffsetY(10);
  17. $title = $customer->name . " " . $template->name . " " . date("F Y") . " Tests";
  18. if($has_logo) {
  19. $shape = $slide->createDrawingShape();
  20. $shape->setName("$customer->name logo")
  21. ->setDescription("$customer->name logo")
  22. ->setPath($logo_file_path)
  23. ->setWidth(720)
  24. ->setOffsetX(120)
  25. ->setOffsetY(180);
  26. $shape = $slide->createRichTextShape()
  27. ->setHeight(60)
  28. ->setWidth(940)
  29. ->setOffsetX(120)
  30. ->setOffsetY(120);
  31. $run = $shape->createTextRun($title);
  32. $run->getFont()->setBold(true)->setSize(16);
  33. }
  34. else {
  35. // Title
  36. $shape = $slide->createRichTextShape()
  37. ->setHeight(120)
  38. ->setWidth(940)
  39. ->setOffsetX(10)
  40. ->setOffsetY(315);
  41. $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
  42. $run = $shape->createTextRun($title);
  43. $run->getFont()->setBold(true)
  44. ->setSize(24);
  45. }
  46. // Create summary slide
  47. $slide = $presentation->createSlide();
  48. // Title
  49. $shape = $slide->createRichTextShape()
  50. ->setHeight(60)->setWidth(940)
  51. ->setOffsetX(30)->setOffsetY(10);
  52. $run = $shape->createTextRun("Summary");
  53. $run->getFont()->setBold(true)->setSize(24);
  54. // Date range
  55. $shape = $slide->createRichTextShape()
  56. ->setHeight(60)->setWidth(940)
  57. ->setOffsetX(30)->setOffsetY(70);
  58. $run = $shape->createTextRun($date_range_label);
  59. $run->getFont()->setBold(true)->setSize(24);
  60. // Test count
  61. $shape = $slide->createRichTextShape()
  62. ->setHeight(60)->setWidth(940)
  63. ->setOffsetX(30)->setOffsetY(130);
  64. $run = $shape->createTextRun("Test Count: {$total_data->testCount}");
  65. $run->getFont()->setBold(true)->setSize(24);
  66. // Pass/Fail count
  67. if(isset($total_data->passCount)) {
  68. $shape = $slide->createRichTextShape()
  69. ->setHeight(60)->setWidth(940)
  70. ->setOffsetX(30)->setOffsetY(190);
  71. $run = $shape->createTextRun("Pass/Fail Count: {$total_data->passCount} / {$total_data->failCount}");
  72. $run->getFont()->setBold(true)->setSize(24);
  73. }
  74. // Tests by stage
  75. foreach($charts as $chart) {
  76. if(stripos($chart->getTitle(), "Tests by Stage") === false) continue;
  77. $chart->addDataTable($slide, 30, 260);
  78. }
  79. // Totals by month/quarter
  80. foreach($charts as $chart) {
  81. if(stripos($chart->getTitle(), "Totals by") === false) continue;
  82. $chart->addDataTable($slide, 30, 360);
  83. }
  84. $names = [];
  85. $charts_b64 = http::value("charts_b64");
  86. foreach($charts_b64 as $index => $b64) {
  87. $chart = $charts[$index];
  88. $slide = $presentation->createSlide();
  89. // Write chart
  90. list($ignore, $png_b64) = explode("base64,", $b64);
  91. $name = tempnam("/tmp", "ppt");
  92. $name .= ".png";
  93. $handle = fopen($name, "w");
  94. fwrite($handle, base64_decode($png_b64));
  95. fclose($handle);
  96. $names[] = $name;
  97. $chart->addChart($slide, $name);
  98. // Write data table
  99. if($chart->print) {
  100. $chart->addDataTable($slide);
  101. }
  102. }
  103. // Region/Area Manager Stats
  104. foreach([0, 1] as $mode) {
  105. switch($mode) {
  106. case 0:
  107. $current_data = $region_data;
  108. $mode_text = "Region";
  109. break;
  110. case 1:
  111. $current_data = $area_manager_data;
  112. $mode_text = "Area Manager";
  113. break;
  114. }
  115. foreach($current_data as $data_id => $rows) {
  116. $slide = $presentation->createSlide();
  117. // Title
  118. $shape = $slide->createRichTextShape()
  119. ->setHeight(120)->setWidth(940)
  120. ->setOffsetX(30)->setOffsetY(30);
  121. $run = $shape->createTextRun("$mode_text: $data_id");
  122. $run->getFont()->setSize(36);
  123. // Table
  124. $shape = $slide->createTableShape(5)
  125. ->setWidth(900)
  126. ->setOffsetX(30)
  127. ->setOffsetY(110);
  128. // Create header row
  129. $row = $shape->createRow();
  130. $row->setHeight(20);
  131. foreach(["Period", "Tests", "Passes", "Fails", "Rate"] as $label) {
  132. $cell = $row->nextCell();
  133. $cell->getActiveParagraph()->getAlignment()
  134. ->setMarginBottom(4)
  135. ->setMarginLeft(4)
  136. ->setMarginRight(4)
  137. ->setMarginTop(4);
  138. $cell->createTextRun($label)
  139. ->getFont()->setBold(true);
  140. }
  141. $index = 0;
  142. foreach($rows as $key => $data) {
  143. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  144. $label = $key_labels[$index];
  145. $values = [$data->testCount, $data->passCount, $data->failCount, "$rate%"];
  146. $row = $shape->createRow();
  147. $row->setHeight(20);
  148. $cell = $row->nextCell();
  149. $cell->getActiveParagraph()->getAlignment()
  150. ->setMarginBottom(4)
  151. ->setMarginLeft(4)
  152. ->setMarginRight(4)
  153. ->setMarginTop(4);
  154. $cell->createTextRun($label)
  155. ->getFont()->setBold(true);
  156. foreach($values as $value) {
  157. $cell = $row->nextCell();
  158. $cell->getActiveParagraph()->getAlignment()
  159. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  160. ->setMarginBottom(4)
  161. ->setMarginLeft(4)
  162. ->setMarginRight(4)
  163. ->setMarginTop(4);
  164. $cell->createTextRun($value);
  165. }
  166. $index++;
  167. }
  168. }
  169. }
  170. if($action == "export-pptx") {
  171. header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
  172. $pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
  173. $pptx_writer->save("php://output");
  174. }
  175. if($action == "export-odp") {
  176. header("Content-Type: application/vnd.oasis.opendocument.presentation");
  177. $odp_writer = IOFactory::createWriter($presentation, "ODPresentation");
  178. $odp_writer->save("php://output");
  179. }
  180. foreach($names as $name) {
  181. @unlink($name);
  182. }
  183. exit;
  184. ?>