Completed
Pull Request — master (#467)
by
unknown
17:56
created

Gantt::getTasks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
	public function __construct( $headerParam ) {
27
		$this->setTitle( $headerParam['title'] );
28
		$this->setAxisFormat( $headerParam['axisformat'] );
29
		$this->setStatusMapping( $headerParam['statusmapping'] );
30
		$this->setPriorityMapping( $headerParam['prioritymapping'] );
31
	}
32
33
	private function setPriorityMapping( $priorityMapping ) {
34
		$this->mPriorityMapping = $priorityMapping;
35
	}
36
37
	private function getPriorityMapping() {
38
		return $this->mPriorityMapping;
39
	}
40
41
	private function setStatusMapping( $statusMapping ) {
42
		$this->mStatusMapping = $statusMapping;
43
	}
44
45
	private function getStatusMapping() {
46
		return $this->mStatusMapping;
47
	}
48
49
	private function setAxisFormat( $axisFormat ) {
50
		$this->mAxisFormat = $axisFormat;
51
	}
52
53
	private function getAxisFormat() {
54
		return $this->mAxisFormat;
55
	}
56
57
	private function setTitle( $title ) {
58
		$this->mTitle = $title;
59
	}
60
61
	private function getTitle() {
62
		return $this->mTitle;
63
	}
64
65
	private function getSections() {
66
		return $this->mSections;
67
	}
68
69
	private function getTasks() {
70
		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
	public function addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ) {
86
		$task = new GanttTask();
87
		$task->setID( $taskID );
88
		$task->setTitle( $taskTitle );
89
		$task->setTaskParam( $status, $this->getStatusMapping(), 'status' );
90
		$task->setTaskParam( $priority, $this->getPriorityMapping(), 'priority' );
91
		$task->setStartDate( $startDate );
92
		$task->setEndDate( $endDate );
93
		$this->mTasks[$taskID] = $task;
94
	}
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
	public function addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
108
109
		$sections = $this->getSections();
110
		if ( count( $sections ) !== 0 ) {
111
112
			if ( array_key_exists( $sectionID, $sections ) ) {
113
114
				foreach ( $sections as $sectionObj ) {
115
					if ( $sectionObj->getID() == $sectionID ) {
116
						if ( $sectionObj->getEarliestStartDate() > $startDate ) {
117
							$sectionObj->setEarliestStartDate( $startDate );
118
						}
119
						if ( $sectionObj->getLatestEndDate() < $endDate ) {
120
							$sectionObj->setLatestEndDate( $endDate );
121
						}
122
						$sectionObj->addTask( $taskID );
123
					}
124
				}
125
			} else {
126
				$this->createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID );
127
			}
128
129
		} else {
130
			// initialize GanttSection Array
131
			$this->createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID );
132
		}
133
	}
134
135
	/**
136
	 * Sort Sections based on the sortkey
137
	 *
138
	 * @param GanttSection $a
139
	 * @param GanttSection $b
140
	 *
141
	 */
142
143
	public function sortSections( $a, $b ) {
144
		// sort based on title
145
		return strcmp( $a->getTitle(), $b->getTitle() );
146
	}
147
148
	private function createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
149
		$ganttSection = new GanttSection();
150
		//check if the id in the object is realy needed or is it enough to have it as array key
151
		$ganttSection->setID( $sectionID );
152
		$ganttSection->setTitle( $sectionTitle );
153
		$ganttSection->setEarliestStartDate( $startDate );
154
		$ganttSection->setLatestEndDate( $endDate );
155
		$ganttSection->addTask( $taskID );
156
157
		$this->mSections[$sectionID] = $ganttSection;
158
	}
159
160
	/**
161
	 * Creates output for mermaidjs
162
	 *
163
	 * @return string
164
	 */
165
	public function getGanttOutput() {
166
167
		$sections = $this->getSections();
168
		$tasks = $this->getTasks();
169
170
		// Sort Sections
171
		usort( $sections, [ $this, 'sortSections' ] );
172
173
		// reorder TaskArray of current section
174
		foreach ( $sections as $section ) {
175
			$orderedTasks = [];
176
			foreach ( $tasks as $task ) {
177
178
				// check if $section->getTasks() holds ID of current task
179
				if ( in_array( $task->getID(), $section->getTasks() ) ) {
180
					$sectionTasks = $section->getTasks();
181
					//loop through tasks of current section
182
					foreach ( $sectionTasks as $taskKey => $sectionTask ) {
183
						if ( $task->getID() === $sectionTask ) {
184
							$orderedTasks[] = $sectionTask;
185
						}
186
					}
187
				}
188
			}
189
			$section->setTasks( $orderedTasks );
190
		}
191
192
		/*
193
		 * Bring the "section" with no title to the first position.
194
		 * This "section" is the one that hold tasks without any section.
195
		 * If we don't display it at the beginning we have to put them into a dummy section
196
		 */
197
		foreach ( $sections as $key => $section ) {
198
			if ( $section->getTitle() === '' ) {
199
				$noSection = $section;
200
				unset( $sections[$key] );
201
			}
202
		}
203
		// push now the dummy task to the first place of the array
204
		if ( isset( $noSection ) ) {
205
			array_unshift( $sections, $noSection );
206
		}
207
208
		$title = $this->getTitle();
209
		$axisFormat = $this->getAxisFormat();
210
211
		$mermaidOut = "gantt\n";
212
		$mermaidOut .= "dateFormat YYYY-MM-DD\n";
213
		$mermaidOut .= ( !empty( $title ) ) ? "title $title\n" : '';
214
		$mermaidOut .= "axisFormat $axisFormat\n";
215
216
		// Output section and all related Issues
217
		foreach ( $sections as $section ) {
218
			if ( $section->getTitle() !== "" ) {
219
				$mermaidOut .= 'section ' . $section->getTitle() . "\n";
220
			}
221
222
			//loop through related section tasks
223
			foreach ( $section->getTasks() as $sectionTaskValue ) {
224
				foreach ( $tasks as $taskObj ) {
225
					if ( $taskObj->getID() === $sectionTaskValue ) {
226
227
						$status = $taskObj->getStatus();
228
229
						// Get Date from timestamp
230
						$date = date_create();
231
						date_timestamp_set( $date, $taskObj->getStartDate() );
232
						$startDate = date_format( $date, 'Y-m-d' ) . ', ';
233
						date_timestamp_set( $date, $taskObj->getEndDate() );
234
						$endDate = date_format( $date, 'Y-m-d' );
235
236
						//get Priority
237
						$priority = $taskObj->getPriority();
238
239
						$mermaidOut .= $taskObj->getTitle() . "\t :" . $priority . $status . $startDate . $endDate .
240
									   "\n";
241
					}
242
				}
243
			}
244
		}
245
246
		//Hashtags mark a Comment in Mermaid, so we need to replace it with <esc>35</esc> to replace it again after rendering
247
		$mermaidOut = str_replace( '#', '<esc>35</esc>', $mermaidOut );
248
249
		return $mermaidOut;
250
	}
251
}