Completed
Push — master ( 51e0cc...3b6349 )
by GIT
04:52
created

Gantt::setPriorityMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * File holding the Gantt class
4
 *
5
 * - Add sections and tasks and
6
 *   manage the relations between them
7
 * - Sort elements based on the sortkey
8
 * - Creates config for gantt diagram
9
 *
10
 * @author Sebastian Schmid
11
 * @file
12
 * @ingroup SemanticResultFormats
13
 */
14
15
namespace SRF\Gantt;
16
17
class Gantt {
18
19
	private $mTitle;
20
	private $mAxisFormat;
21
	private $mSections = [];
22
	private $mTasks = [];
23
	private $mPriorityMapping;
24
	private $mStatusMapping;
25
26 1
	public function __construct( $headerParam ) {
27 1
		$this->setTitle( $headerParam['title'] );
28 1
		$this->setAxisFormat( $headerParam['axisformat'] );
29 1
		$this->setStatusMapping( $headerParam['statusmapping'] );
30 1
		$this->setPriorityMapping( $headerParam['prioritymapping'] );
31 1
	}
32
33 1
	private function setPriorityMapping( $priorityMapping ) {
34 1
		$this->mPriorityMapping = $priorityMapping;
35 1
	}
36
37 1
	private function getPriorityMapping() {
38 1
		return $this->mPriorityMapping;
39
	}
40
41 1
	private function setStatusMapping( $statusMapping ) {
42 1
		$this->mStatusMapping = $statusMapping;
43 1
	}
44
45 1
	private function getStatusMapping() {
46 1
		return $this->mStatusMapping;
47
	}
48
49 1
	private function setAxisFormat( $axisFormat ) {
50 1
		$this->mAxisFormat = $axisFormat;
51 1
	}
52
53 1
	private function getAxisFormat() {
54 1
		return $this->mAxisFormat;
55
	}
56
57 1
	private function setTitle( $title ) {
58 1
		$this->mTitle = $title;
59 1
	}
60
61 1
	private function getTitle() {
62 1
		return $this->mTitle;
63
	}
64
65 1
	private function getSections() {
66 1
		return $this->mSections;
67
	}
68
69 1
	private function getTasks() {
70 1
		return $this->mTasks;
71
	}
72
73
	/**
74
	 * Adds a new Task to array
75
	 *
76
	 * @param string $taskID
77
	 * @param string $taskTitle
78
	 * @param array $status
79
	 * @param array $priority
80
	 * @param string $startDate
81
	 * @param string $endDate
82
	 *
83
	 */
84
85 1
	public function addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ) {
86 1
		$task = new GanttTask();
87 1
		$task->setID( $taskID );
88 1
		$task->setTitle( $taskTitle );
89 1
		$task->setTaskParam( $status, $this->getStatusMapping(), 'status' );
90 1
		$task->setTaskParam( $priority, $this->getPriorityMapping(), 'priority' );
91 1
		$task->setStartDate( $startDate );
92 1
		$task->setEndDate( $endDate );
93 1
		$this->mTasks[$taskID] = $task;
94 1
	}
95
96
97
	/**
98
	 * Creats a new Section with related tasks
99
	 *
100
	 * @param string $sectionID
101
	 * @param string $sectionTitle
102
	 * @param string $startDate
103
	 * @param string $endDate
104
	 * @param string $taskID
105
	 *
106
	 */
107 1
	public function addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
108
109 1
		$sections = $this->getSections();
110
111 1
		if ( array_key_exists( $sectionID, $sections ) ) {
112
113 1
			if ( $sections[$sectionID]->getEarliestStartDate() > $startDate ) {
114
				$sections[$sectionID]->setEarliestStartDate( $startDate );
115
			}
116 1
			if ( $sections[$sectionID]->getLatestEndDate() < $endDate ) {
117 1
				$sections[$sectionID]->setLatestEndDate( $endDate );
118
			}
119 1
			$sections[$sectionID]->addTask( $taskID );
120
121
		} else {
122 1
			$this->createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID );
123
		}
124 1
	}
125
126 1
	private function createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
127 1
		$ganttSection = new GanttSection();
128
		//check if the id in the object is realy needed or is it enough to have it as array key
129 1
		$ganttSection->setID( $sectionID );
130 1
		$ganttSection->setTitle( $sectionTitle );
131 1
		$ganttSection->setEarliestStartDate( $startDate );
132 1
		$ganttSection->setLatestEndDate( $endDate );
133 1
		$ganttSection->addTask( $taskID );
134
135 1
		$this->mSections[$sectionID] = $ganttSection;
136 1
	}
137
138
	/**
139
	 * Creates output for mermaidjs
140
	 *
141
	 * @return string
142
	 */
143 1
	public function getGanttOutput() {
144
145 1
		$sections = $this->getSections();
146 1
		$tasks = $this->getTasks();
147
148
		/*
149
		 * Bring the "section" with no title to the first position.
150
		 * This "section" is the one that hold tasks without any section.
151
		 * If we don't display it at the beginning we have to put them into a dummy section
152
		 */
153 1
		foreach ( $sections as $key => $section ) {
154 1
			if ( $section->getTitle() === '' ) {
155
				$noSection = $section;
156
				unset( $sections[$key] );
157
			}
158
		}
159
		// push now the dummy task to the first place of the array
160 1
		if ( isset( $noSection ) ) {
161
			array_unshift( $sections, $noSection );
162
		}
163
164 1
		$title = $this->getTitle();
165 1
		$axisFormat = $this->getAxisFormat();
166
167 1
		$mermaidOut = "gantt\n";
168 1
		$mermaidOut .= "dateFormat YYYY-MM-DD\n";
169 1
		$mermaidOut .= ( !empty( $title ) ) ? "title $title\n" : '';
170 1
		$mermaidOut .= "axisFormat $axisFormat\n";
171
172
		// Output section and all related Issues
173 1
		foreach ( $sections as $section ) {
174 1
			if ( $section->getTitle() !== "" ) {
175 1
				$mermaidOut .= 'section ' . $section->getTitle() . "\n";
176
			}
177
178
			//loop through related section tasks
179 1
			foreach ( $section->getTasks() as $sectionTask ) {
180
181 1
						$status = $tasks[$sectionTask]->getStatus();
182
183
						// Get Date from timestamp
184 1
						$date = date_create();
185 1
						date_timestamp_set( $date, $tasks[$sectionTask]->getStartDate() );
186 1
						$startDate = date_format( $date, 'Y-m-d' ) . ', ';
187 1
						date_timestamp_set( $date, $tasks[$sectionTask]->getEndDate() );
188 1
						$endDate = date_format( $date, 'Y-m-d' );
189
190
						//get Priority
191 1
						$priority = $tasks[$sectionTask]->getPriority();
192
193 1
						$mermaidOut .= $tasks[$sectionTask]->getTitle() . "\t :" . $priority . $status . $startDate . $endDate .
194 1
									   "\n";
195
			}
196
		}
197
198
		//Hashtags mark a Comment in Mermaid, so we need to replace it with <esc>35</esc> to replace it again after rendering
199 1
		$mermaidOut = str_replace( '#', '<esc>35</esc>', $mermaidOut );
200
201 1
		return $mermaidOut;
202
	}
203
}