To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

fullcalendar_feed.php 7.39 KB
Newer Older
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
<?php 
	/*
	 * __author__      = "Christoph Walser <walser@tik.ee.ethz.ch>"
	 * __copyright__   = "Copyright 2010, ETH Zurich, Switzerland, Christoph Walser"
	 * __license__     = "GPL"
	 * __version__     = "$Revision$"
	 * __date__        = "$Date$"
	 * __id__          = "$Id$"
	 * __source__      = "$URL$" 
	 */

	require_once('include/auth.php'); 
	ob_start("ob_gzhandler");
	//debug();
	// Transform request parameters to MySQL datetime format.
	$mysqlstart = date( 'Y-m-d H:i:s T', $_GET['start']);
	$mysqlend = date('Y-m-d H:i:s T', $_GET['end']);
	$mini = isset($_GET['mini']) && $_GET['mini']==TRUE;
	
	// Connect to database and get the corresponding events:
	$guard_setup_min = $CONFIG['tests']['setuptime'];
	$guard_cleanup_min = $CONFIG['tests']['cleanuptime'];
	$db = db_connect();
	// planned tests
	$sql = 	"SELECT `a`.serv_tests_key, `a`.title, `a`.description, `a`.time_start_wish, `a`.time_end_wish, `a`.owner_fk, `b`.username, `b`.firstname, `b`.lastname, `a`.time_start_act, `a`.time_end_act, `a`.test_status,
				DATE_ADD(`a`.time_start_wish, INTERVAL -".$guard_setup_min." MINUTE) as time_start_offset,
				DATE_ADD(`a`.time_end_wish, INTERVAL ".$guard_cleanup_min." MINUTE) as time_end_offset
			FROM `tbl_serv_tests` AS `a`
			LEFT JOIN `tbl_serv_users` AS `b` ON `a`.owner_fk = `b`.serv_users_key
			WHERE 
				(
					(`a`.test_status NOT IN ('not schedulable', 'todelete', 'deleted') OR (`a`.test_status_preserved IS NOT NULL AND `a`.test_status_preserved IN ('finished','failed'))) 
					AND
					(
						(`a`.time_start_wish BETWEEN '" . $mysqlstart . "' AND '" . $mysqlend . "') 
						OR (`a`.time_end_wish BETWEEN '" . $mysqlstart . "' AND '" . $mysqlend . "')
					)
				) ORDER BY `a`.time_start_wish
		 ";
40
	$rs = mysqli_query($db, $sql) or flocklab_die('Cannot get calendar data from database because: ' . mysqli_error($db));
41 42 43
	
	// Build the array of events:
	$events = array();
44
	while ($row = mysqli_fetch_array($rs)) {
45 46 47 48 49 50
		// Create the event depending on the owner: if it is an event which belongs to the logged-in user, show more info and color it differently.
		if ($row['owner_fk'] == $_SESSION['serv_users_key']) {
			if ($row['test_status'] == 'failed' || $row['test_status'] == 'finished' || $row['test_status'] == 'retention expiring' || $row['test_status'] == 'deleted' || $row['test_status'] == 'todelete' ) {
			      $events[] = array(
					'id'          => $row['serv_tests_key'],
					'title'       => 'Test ' . $row['serv_tests_key'] . ': ' . $row['title'],
51
					'description' => $mini?'':'Test-ID: ' . $row['serv_tests_key'] . '<br/>Duration: '.date("H:i", strtotime($row['time_start_act'])).' - '.date("H:i", strtotime($row['time_end_act'])).'<br/>Title: ' . $row['title'] . '<br/> Description: ' . $row['description'].'<br />Status: '.$row['test_status'],
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
					'start'       => $row['time_start_act'],
					'end'         => $row['time_end_act'],
					'allDay'      => false,
					'color'       => 'chocolate',
				);
			}
			else {
				// Insert offset for test setup as separate event in order not to confuse the user:
				$events[] = array(
					'id'          => 'service',
					'title'       => 'Test setup',
					'description' => $mini?'':'Time needed by FlockLab to setup your test.',
					'start'       => $row['time_start_offset'],
					'end'         => $row['time_start_wish'],
					'allDay'      => false,
					'color'       => 'orange',
				);
				// Insert actual test:
				$events[] = array(
					'id'          => $row['serv_tests_key'],
					'title'       => 'Test ' . $row['serv_tests_key'] . ': ' . $row['title'],
73
					'description' => $mini?'':'Test-ID: ' . $row['serv_tests_key'] . '<br/>Duration: '.date("H:i", strtotime($row['time_start_wish'])).' - '.date("H:i", strtotime($row['time_end_wish'])).'<br/>Title: ' . $row['title'] . '<br/> Description: ' . $row['description'].'<br />Status: '.$row['test_status'],
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
					'start'       => $row['time_start_wish'],
					'end'         => $row['time_end_wish'],
					'allDay'      => false,
					'color'       => 'chocolate',
				);
				// Insert offset for test finish as separate event in order not to confuse the user:
				$events[] = array(
					'id'          => 'service',
					'title'       => 'Test cleanup',
					'description' => $mini?'':'Time needed by FlockLab to cleanup your test.',
					'start'       => $row['time_end_wish'],
					'end'         => $row['time_end_offset'],
					'allDay'      => false,
					'color'       => 'orange',
				);
			}
		} elseif ($_SESSION['is_admin'] == true) {
			// The user is admin and can thus see all tests:
			$event = array(
					'id'          => $row['serv_tests_key'],
					'title'       => $row['username'] . ' (' . $row['firstname'] . ' ' . $row['lastname'] . ')',
95
					'description' => $mini?'':'ID: ' . $row['serv_tests_key'] . '<br/>Duration: '.date("H:i", strtotime($row['time_start_wish'])).' - '.date("H:i", strtotime($row['time_end_wish'])).'<br/>Title: ' . $row['title'] . '<br/> Description: ' . $row['description'] . '<br/> User: ' . $row['username'] . ' (' . $row['firstname'] . ' ' . $row['lastname'] . ')' . '<br/>Status: ' . $row['test_status'],
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
					'allDay'      => false,
			);
			if (isset($row['time_start_act']))
				$event['start'] = $row['time_start_act'];
			else
				$event['start'] = $row['time_start_offset'];
			if (isset($row['time_end_act']))
				$event['end'] = $row['time_end_act'];
			else
				$event['end'] = $row['time_end_offset'];
			array_push($events, $event);
		
		} else {
			// The event is not owned by the logged-in user, thus just show one event without details and add the offsets directly to the event:			
	    $event = array(
				'id'          => $row['serv_tests_key'],
				'title'       => 'Occupied',
				'description' => $mini?'':'Another user is running a test.',
				'allDay'      => false,
			);
			if (isset($row['time_start_act']))
				$event['start'] = $row['time_start_act'];
			else
				$event['start'] = $row['time_start_offset'];
			if (isset($row['time_end_act']))
				$event['end'] = $row['time_end_act'];
			else
				$event['end'] = $row['time_end_offset'];
			array_push($events, $event);
		}
	}
	
	// add reservation slots that affect this user (i.e., blocks time) 
	$sql = 'SELECT max(`user_fk` = '.$_SESSION['serv_users_key'].') as `reservation_match`, `time_start`, `time_end`, `serv_reservation_key`, `group_id_fk`
		FROM `tbl_serv_reservations` LEFT JOIN `tbl_serv_user_groups` ON `group_fk`=`group_id_fk`
		WHERE `time_end` > NOW() AND
		(`time_start` BETWEEN "' . $mysqlstart . '" AND "' . $mysqlend . '" OR
		`time_end` BETWEEN "' . $mysqlstart . '" AND "' . $mysqlend . '")
		GROUP BY serv_reservation_key
		'. ($_SESSION['is_admin'] == true?'':'HAVING `reservation_match` is NULL OR `reservation_match` <> 1');
136 137
	$rs = mysqli_query($db, $sql) or flocklab_die('Cannot get calendar data from database because: ' . mysqli_error($db));
	while ($row = mysqli_fetch_array($rs)) {	
138 139 140 141 142 143 144 145 146 147
		$event = array(
			'id'          => $row['serv_reservation_key'],
			'title'       => ($_SESSION['is_admin'] == true?'Reservation for group '.$row['group_id_fk']:'Occupied'),
			'description' => $mini?'':'Another user is running a test.',
			'allDay'      => false,
		);
		$event['start'] = $row['time_start'];
		$event['end'] = $row['time_end'];
		array_push($events, $event);
	}
148
	mysqli_close($db);
149 150 151 152 153

	// JSON-encode the array and return it to the calendar:
	echo json_encode($events);

?>