Alan Lam 7 лет назад
Родитель
Сommit
e607a9a37d
2 измененных файлов с 214 добавлено и 156 удалено
  1. 86 155
      controllers/reports_presentation.php
  2. 128 1
      engine/chart.php

+ 86 - 155
controllers/reports_presentation.php

@@ -9,54 +9,95 @@ use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
 $presentation = new PhpPresentation(); // 960x720
 
 // Draw title slide
-{
-	// Create slide
-	$slide = $presentation->getActiveSlide();
 
-	// Create a shape (drawing)
-	$shape = $slide->createDrawingShape();
-	$shape->setName("Check Policy logo")
-		  ->setDescription("Check Policy logo")
-		  ->setPath("./public/img/logo.png")
-		  ->setWidth(200)
-		  ->setOffsetX(380)
-		  ->setOffsetY(10);
+$slide = $presentation->getActiveSlide();
 
-	$title = $customer->name . " " . $template->name . " " . date("F Y") . " Tests";
+$shape = $slide->createDrawingShape();
+$shape->setName("Check Policy logo")
+	  ->setDescription("Check Policy logo")
+	  ->setPath("./public/img/logo.png")
+	  ->setWidth(200)
+	  ->setOffsetX(380)
+	  ->setOffsetY(10);
 
-	if($has_logo) {
+$title = $customer->name . " " . $template->name . " " . date("F Y") . " Tests";
 
-		$shape = $slide->createDrawingShape();
-		$shape->setName("$customer->name logo")
-			->setDescription("$customer->name logo")
-			->setPath($logo_file_path)
-			->setWidth(720)
-			->setOffsetX(120)
-			->setOffsetY(180);
+if($has_logo) {
 
-		$shape = $slide->createRichTextShape()
-			->setHeight(60)
-			->setWidth(940)
-			->setOffsetX(120)
-			->setOffsetY(120);
-		$run = $shape->createTextRun($title);
-		$run->getFont()->setBold(true)->setSize(16);
+	$shape = $slide->createDrawingShape();
+	$shape->setName("$customer->name logo")
+		->setDescription("$customer->name logo")
+		->setPath($logo_file_path)
+		->setWidth(720)
+		->setOffsetX(120)
+		->setOffsetY(180);
+
+	$shape = $slide->createRichTextShape()
+		->setHeight(60)
+		->setWidth(940)
+		->setOffsetX(120)
+		->setOffsetY(120);
+	$run = $shape->createTextRun($title);
+	$run->getFont()->setBold(true)->setSize(16);
 
 
-	}
-	else {
-		// Title
-		$shape = $slide->createRichTextShape()
-			  ->setHeight(120)
-			  ->setWidth(940)
-			  ->setOffsetX(10)
-			  ->setOffsetY(315);
-		$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
-		$run = $shape->createTextRun($title);
-		$run->getFont()->setBold(true)
-						   ->setSize(24);
-	}
+}
+else {
+	// Title
+	$shape = $slide->createRichTextShape()
+		  ->setHeight(120)
+		  ->setWidth(940)
+		  ->setOffsetX(10)
+		  ->setOffsetY(315);
+	$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
+	$run = $shape->createTextRun($title);
+	$run->getFont()->setBold(true)
+					   ->setSize(24);
+}
+
+// Create summary slide
+$slide = $presentation->createSlide();
+
+// Title
+$shape = $slide->createRichTextShape()
+	->setHeight(60)->setWidth(940)
+	->setOffsetX(30)->setOffsetY(10);
+$run = $shape->createTextRun("Summary");
+$run->getFont()->setBold(true)->setSize(24);
+
+// Date range
+$shape = $slide->createRichTextShape()
+	->setHeight(60)->setWidth(940)
+	->setOffsetX(30)->setOffsetY(70);
+$run = $shape->createTextRun($date_range_label);
+$run->getFont()->setBold(true)->setSize(24);
+
+// Test count
+$shape = $slide->createRichTextShape()
+	->setHeight(60)->setWidth(940)
+	->setOffsetX(30)->setOffsetY(130);
+$run = $shape->createTextRun("Test Count: {$total_data->testCount}");
+$run->getFont()->setBold(true)->setSize(24);
+
+// Pass/Fail count
+if(isset($total_data->passCount)) {
+	$shape = $slide->createRichTextShape()
+		->setHeight(60)->setWidth(940)
+		->setOffsetX(30)->setOffsetY(190);
+	$run = $shape->createTextRun("Pass/Fail Count: {$total_data->passCount} / {$total_data->failCount}");
+	$run->getFont()->setBold(true)->setSize(24);
+}
+
+// Tests by stage
+foreach($charts as $chart) {
+	if(stripos($chart->getTitle(), "Tests by Stage") === false) continue;
+	$chart->addDataTable($slide, 30, 260);
+}
 
+// Totals by month/quarter
+foreach($charts as $chart) {
+	if(stripos($chart->getTitle(), "Totals by") === false) continue;
+	$chart->addDataTable($slide, 30, 360);
 }
 
 $names = [];
@@ -66,13 +107,9 @@ foreach($charts_b64 as $index => $b64) {
 
 	$chart = $charts[$index];
 
-	$labels = $chart->getLabels();
-	$datasets = array_merge($chart->getDatasets(), $chart->getPrintDatasets());
-
-	$column_count = 1 + count($labels);
-
 	$slide = $presentation->createSlide();
 
+	// Write chart
 	list($ignore, $png_b64) = explode("base64,", $b64);
 
 	$name = tempnam("/tmp", "ppt");
@@ -83,120 +120,14 @@ foreach($charts_b64 as $index => $b64) {
 
 	fclose($handle);
 
-	$chart_offset_y = 100;
-	if($chart->print) {
-		$chart_offset_y = 10;
-	}
-
-
-	$shape = $slide->createDrawingShape();
-	$shape->setName("Chart")
-		->setDescription("Chart")
-		->setPath($name)
-		->setHeight(520)
-		->setOffsetX(133)
-		->setOffsetY($chart_offset_y);
+	$names[] = $name;
 
+	$chart->addChart($slide, $name);
 
+	// Write data table
 	if($chart->print) {
-		// Table
-		if($chart->printTotalColumn) ++$column_count;
-
-		$shape = $slide->createTableShape($column_count)
-			->setWidth(900)
-			->setOffsetX(30)
-			->setOffsetY(540);
-		// Create header row
-		$row = $shape->createRow();
-		$row->setHeight(20);
-		$cell = $row->nextCell();
-
-		$labels = $chart->getPrintLabels() ?? $chart->getLabels();
-
-		if($chart->printTotalColumn) $labels[] = "Total";
-
-		foreach($labels as $label) {
-			$cell = $row->nextCell();
-			$cell->getActiveParagraph()->getAlignment()
-				->setMarginBottom(4)
-				->setMarginLeft(4)
-				->setMarginRight(4)
-				->setMarginTop(4);
-			$cell->createTextRun($label)
-				->getFont()->setBold(true);
-		}
-
-		$suffixes = $chart->getPrintSuffixes() ?? $chart->getDatalabelSuffixes();
-
-		// Datasets
-		foreach($datasets as $index => $dataset) {
-			$label = $dataset->label;
-			$data = $dataset->data;
-			$print_data = array_slice($data, 0);
-
-			$total = array_sum($data);
-			$suffix = $suffixes[$index] ?? "";
-
-			if($chart->printTotalColumn) {
-				$print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
-			}
-
-			$row = $shape->createRow();
-			$row->setHeight(20);
-			$cell = $row->nextCell();
-			$cell->getActiveParagraph()->getAlignment()
-				->setMarginBottom(4)
-				->setMarginLeft(4)
-				->setMarginRight(4)
-				->setMarginTop(4);
-			$cell->createTextRun($label)
-				->getFont()->setBold(true);
-			foreach($print_data as $value) {
-				$string = strlen($value) == 0 ? "" : $value . $suffix;
-
-				$cell = $row->nextCell();
-				$cell->getActiveParagraph()->getAlignment()
-					->setHorizontal(Alignment::HORIZONTAL_RIGHT)
-					->setMarginBottom(4)
-					->setMarginLeft(4)
-					->setMarginRight(4)
-					->setMarginTop(4);
-				$cell->createTextRun($string);
-			}
-
-			if($chart->printPercentages) {
-
-				$percentages = [];
-				foreach($data as $value) {
-					$percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
-				}
-
-				$row = $shape->createRow();
-				$row->setHeight(20);
-				$cell = $row->nextCell();
-				$cell->getActiveParagraph()->getAlignment()
-					->setMarginBottom(4)
-					->setMarginLeft(4)
-					->setMarginRight(4)
-					->setMarginTop(4);
-				$cell->createTextRun("Percentage")
-					->getFont()->setBold(true);
-				foreach($percentages as $string) {
-					$cell = $row->nextCell();
-					$cell->getActiveParagraph()->getAlignment()
-						->setHorizontal(Alignment::HORIZONTAL_RIGHT)
-						->setMarginBottom(4)
-						->setMarginLeft(4)
-						->setMarginRight(4)
-						->setMarginTop(4);
-					$cell->createTextRun($string);
-				}
-			}
-
-		}
+		$chart->addDataTable($slide);
 	}
-
-	$names[] = $name;
 }
 
 // Region/Area Manager Stats

+ 128 - 1
engine/chart.php

@@ -1,5 +1,9 @@
 <?php
 
+use PhpOffice\PhpPresentation\Style\Color;
+use PhpOffice\PhpPresentation\Style\Alignment;
+use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
+
 class Chart {
 
 	const DATALABELS_BOX = [
@@ -318,7 +322,7 @@ class Chart {
 					$total = array_sum($data);
 					$percentages = [];
 					foreach($data as $value) {
-						$percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
+						$percentages[] = ($total == 0) ? "0%" : (floor(($value * 10000) / $total) / 100) . "%";
 					}
 					?>
 					<tr>
@@ -356,6 +360,129 @@ class Chart {
 		$this->tooltipsIntersect = $intersect;
 	}
 
+	// Presentation methods
+	public function addChart($slide, $path) {
+
+		$chart_offset_y = 100;
+		if($this->print) {
+			$chart_offset_y = 10;
+		}
+
+		$shape = $slide->createDrawingShape();
+		$shape->setName("Chart")
+			->setDescription("Chart")
+			->setPath($path)
+			->setHeight(520)
+			->setOffsetX(133)
+			->setOffsetY($chart_offset_y);
+
+		return true;
+	}
+
+	public function addDataTable($slide, $offset_x = 30, $offset_y = 540) {
+
+		$labels = $this->getLabels();
+		$datasets = array_merge($this->getDatasets(), $this->getPrintDatasets());
+
+		$column_count = 1 + count($labels);
+
+		// Table
+		if($this->printTotalColumn) ++$column_count;
+
+		$shape = $slide->createTableShape($column_count)
+			->setWidth(900)
+			->setOffsetX($offset_x)
+			->setOffsetY($offset_y);
+		// Create header row
+		$row = $shape->createRow();
+		$row->setHeight(20);
+		$cell = $row->nextCell();
+
+		$labels = $this->getPrintLabels() ?? $this->getLabels();
+
+		if($this->printTotalColumn) $labels[] = "Total";
+
+		foreach($labels as $label) {
+			$cell = $row->nextCell();
+			$cell->getActiveParagraph()->getAlignment()
+				->setMarginBottom(4)
+				->setMarginLeft(4)
+				->setMarginRight(4)
+				->setMarginTop(4);
+			$cell->createTextRun($label)
+				->getFont()->setBold(true);
+		}
+
+		$suffixes = $this->getPrintSuffixes() ?? $this->getDatalabelSuffixes();
+
+		// Datasets
+		foreach($datasets as $index => $dataset) {
+			$label = $dataset->label;
+			$data = $dataset->data;
+			$print_data = array_slice($data, 0);
+
+			$total = array_sum($data);
+			$suffix = $suffixes[$index] ?? "";
+
+			if($this->printTotalColumn) {
+				$print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
+			}
+
+			$row = $shape->createRow();
+			$row->setHeight(20);
+			$cell = $row->nextCell();
+			$cell->getActiveParagraph()->getAlignment()
+				->setMarginBottom(4)
+				->setMarginLeft(4)
+				->setMarginRight(4)
+				->setMarginTop(4);
+			$cell->createTextRun($label)
+				->getFont()->setBold(true);
+			foreach($print_data as $value) {
+				$string = strlen($value) == 0 ? "" : $value . $suffix;
+
+				$cell = $row->nextCell();
+				$cell->getActiveParagraph()->getAlignment()
+					->setHorizontal(Alignment::HORIZONTAL_RIGHT)
+					->setMarginBottom(4)
+					->setMarginLeft(4)
+					->setMarginRight(4)
+					->setMarginTop(4);
+				$cell->createTextRun($string);
+			}
+
+			if($this->printPercentages) {
+
+				$percentages = [];
+				foreach($data as $value) {
+					$percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
+				}
+
+				$row = $shape->createRow();
+				$row->setHeight(20);
+				$cell = $row->nextCell();
+				$cell->getActiveParagraph()->getAlignment()
+					->setMarginBottom(4)
+					->setMarginLeft(4)
+					->setMarginRight(4)
+					->setMarginTop(4);
+				$cell->createTextRun("Percentage")
+					->getFont()->setBold(true);
+				foreach($percentages as $string) {
+					$cell = $row->nextCell();
+					$cell->getActiveParagraph()->getAlignment()
+						->setHorizontal(Alignment::HORIZONTAL_RIGHT)
+						->setMarginBottom(4)
+						->setMarginLeft(4)
+						->setMarginRight(4)
+						->setMarginTop(4);
+					$cell->createTextRun($string);
+				}
+			}
+
+		}
+	}
+
 }
 
 ?>