summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/formats/Gantt/src/Gantt.php
blob: 28e6f95f0c04fd6328ae00bd157d408fdb81608b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
 * File holding the Gantt class
 *
 * - Add sections and tasks and
 *   manage the relations between them
 * - Sort elements based on the sortkey
 * - Creates config for gantt diagram
 *
 * @author Sebastian Schmid
 * @file
 * @ingroup SemanticResultFormats
 */

namespace SRF\Gantt;

class Gantt {

	private $mTitle;
	private $mAxisFormat;
	private $mSections = [];
	private $mTasks = [];
	private $mPriorityMapping;
	private $mStatusMapping;

	public function __construct( $headerParam ) {
		$this->setTitle( $headerParam['title'] );
		$this->setAxisFormat( $headerParam['axisformat'] );
		$this->setStatusMapping( $headerParam['statusmapping'] );
		$this->setPriorityMapping( $headerParam['prioritymapping'] );
	}

	private function setPriorityMapping( $priorityMapping ) {
		$this->mPriorityMapping = $priorityMapping;
	}

	private function getPriorityMapping() {
		return $this->mPriorityMapping;
	}

	private function setStatusMapping( $statusMapping ) {
		$this->mStatusMapping = $statusMapping;
	}

	private function getStatusMapping() {
		return $this->mStatusMapping;
	}

	private function setAxisFormat( $axisFormat ) {
		$this->mAxisFormat = $axisFormat;
	}

	private function getAxisFormat() {
		return $this->mAxisFormat;
	}

	private function setTitle( $title ) {
		$this->mTitle = $title;
	}

	private function getTitle() {
		return $this->mTitle;
	}

	private function getSections() {
		return $this->mSections;
	}

	private function getTasks() {
		return $this->mTasks;
	}

	/**
	 * Adds a new Task to array
	 *
	 * @param string $taskID
	 * @param string $taskTitle
	 * @param array $status
	 * @param array $priority
	 * @param string $startDate
	 * @param string $endDate
	 *
	 */

	public function addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ) {
		$task = new GanttTask();
		$task->setID( $taskID );
		$task->setTitle( $taskTitle );
		$task->setTaskParam( $status, $this->getStatusMapping(), 'status' );
		$task->setTaskParam( $priority, $this->getPriorityMapping(), 'priority' );
		$task->setStartDate( $startDate );
		$task->setEndDate( $endDate );
		$this->mTasks[$taskID] = $task;
	}


	/**
	 * Creats a new Section with related tasks
	 *
	 * @param string $sectionID
	 * @param string $sectionTitle
	 * @param string $startDate
	 * @param string $endDate
	 * @param string $taskID
	 *
	 */
	public function addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {

		$sections = $this->getSections();

		if ( array_key_exists( $sectionID, $sections ) ) {

			if ( $sections[$sectionID]->getEarliestStartDate() > $startDate ) {
				$sections[$sectionID]->setEarliestStartDate( $startDate );
			}
			if ( $sections[$sectionID]->getLatestEndDate() < $endDate ) {
				$sections[$sectionID]->setLatestEndDate( $endDate );
			}
			$sections[$sectionID]->addTask( $taskID );

		} else {
			$this->createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID );
		}
	}

	private function createNewSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ) {
		$ganttSection = new GanttSection();
		//check if the id in the object is realy needed or is it enough to have it as array key
		$ganttSection->setID( $sectionID );
		$ganttSection->setTitle( $sectionTitle );
		$ganttSection->setEarliestStartDate( $startDate );
		$ganttSection->setLatestEndDate( $endDate );
		$ganttSection->addTask( $taskID );

		$this->mSections[$sectionID] = $ganttSection;
	}

	/**
	 * Creates output for mermaidjs
	 *
	 * @return string
	 */
	public function getGanttOutput() {

		$sections = $this->getSections();
		$tasks = $this->getTasks();

		/*
		 * Bring the "section" with no title to the first position.
		 * This "section" is the one that hold tasks without any section.
		 * If we don't display it at the beginning we have to put them into a dummy section
		 */
		foreach ( $sections as $key => $section ) {
			if ( $section->getTitle() === '' ) {
				$noSection = $section;
				unset( $sections[$key] );
			}
		}
		// push now the dummy task to the first place of the array
		if ( isset( $noSection ) ) {
			array_unshift( $sections, $noSection );
		}

		$title = $this->getTitle();
		$axisFormat = $this->getAxisFormat();

		$mermaidOut = "gantt\n";
		$mermaidOut .= "dateFormat YYYY-MM-DD\n";
		$mermaidOut .= ( !empty( $title ) ) ? "title $title\n" : '';
		$mermaidOut .= "axisFormat $axisFormat\n";

		// Output section and all related Issues
		foreach ( $sections as $section ) {
			if ( $section->getTitle() !== "" ) {
				$mermaidOut .= 'section ' . $section->getTitle() . "\n";
			}

			//loop through related section tasks
			foreach ( $section->getTasks() as $sectionTask ) {

						$status = $tasks[$sectionTask]->getStatus();

						// Get Date from timestamp
						$date = date_create();
						date_timestamp_set( $date, $tasks[$sectionTask]->getStartDate() );
						$startDate = date_format( $date, 'Y-m-d' ) . ', ';
						date_timestamp_set( $date, $tasks[$sectionTask]->getEndDate() );
						$endDate = date_format( $date, 'Y-m-d' );

						//get Priority
						$priority = $tasks[$sectionTask]->getPriority();

						$mermaidOut .= $tasks[$sectionTask]->getTitle() . "\t :" . $priority . $status . $startDate . $endDate .
									   "\n";
			}
		}

		//Hashtags mark a Comment in Mermaid, so we need to replace it with <esc>35</esc> to replace it again after rendering
		$mermaidOut = str_replace( '#', '<esc>35</esc>', $mermaidOut );

		return $mermaidOut;
	}
}