-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_grant.php
247 lines (207 loc) · 9.96 KB
/
create_grant.php
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
include 'header.php';
include 'db.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
$user_id = $_SESSION['user_id'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'];
$agency = $_POST['agency'];
$start_date = $_POST['start_date'];
$duration = $_POST['duration'];
$total_amount = $_POST['total_amount'];
$start = new DateTime($start_date);
$start->modify("+$duration years");
$end_date = $start->format('Y-m-d');
$selected_user_ids = $_POST['user_ids'];
$selected_roles = $_POST['roles'];
$conn->begin_transaction();
try {
$stmt = $conn->prepare("INSERT INTO grants (title, agency, start_date, end_date, duration_in_years, total_amount, user_id) VALUES (?, ?, ?, ?, ?, ?, ?)");
if (!$stmt) {
throw new Exception("Error preparing statement for inserting grant: " . $conn->error);
}
$stmt->bind_param('sssidid', $title, $agency, $start_date, $end_date, $duration, $total_amount, $user_id);
$stmt->execute();
$grant_id = $stmt->insert_id;
$stmtGrantUser = $conn->prepare("INSERT INTO grant_users (grant_id, user_id, role, status) VALUES (?, ?, ?, 'accepted')");
if (!$stmtGrantUser) {
throw new Exception("Error preparing statement for inserting grant user (creator): " . $conn->error);
}
$creator_role = 'PI';
$stmtGrantUser->bind_param('iis', $grant_id, $user_id, $creator_role);
$stmtGrantUser->execute();
foreach ($selected_user_ids as $index => $selected_user_id) {
$role = $selected_roles[$index];
$status = ($role == 'PI' || $role == 'CO-PI') ? 'pending' : 'accepted';
$stmtGrantUser->prepare("INSERT INTO grant_users (grant_id, user_id, role, status) VALUES (?, ?, ?, ?)");
if (!$stmtGrantUser) {
throw new Exception("Error preparing statement for inserting grant user (additional users): " . $conn->error);
}
$stmtGrantUser->bind_param('iiss', $grant_id, $selected_user_id, $role, $status);
$stmtGrantUser->execute();
if ($status == 'pending') {
$message = "You have been invited to join the grant: $title as a $role.";
$stmtNotification = $conn->prepare("INSERT INTO notifications (user_id, message, grant_id) VALUES (?, ?, ?)");
if (!$stmtNotification) {
throw new Exception("Error preparing statement for inserting notification: " . $conn->error);
}
$stmtNotification->bind_param('isi', $selected_user_id, $message, $grant_id);
$stmtNotification->execute();
}
}
$defaultDescription = 'Initial Budget Item';
$defaultHourlyRate = 0.0;
$defaultYears = array_fill(1, 6, 0.0);
$defaultTotalAmount = 0.0;
$stmtBudget = $conn->prepare("INSERT INTO budget_items (grant_id, category_id, description, hourly_rate, year_1, year_2, year_3, year_4, year_5, year_6, amount)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!$stmtBudget) {
throw new Exception("Error preparing statement for inserting default budget item: " . $conn->error);
}
for ($i = 1; $i <= 8; $i++) {
if ($i === 3) {
continue;
}
$itemCount = 1;
if ($i === 1) {
$itemCount = 2;
} elseif ($i === 2) {
$itemCount = 3;
}
for ($j = 1; $j <= $itemCount; $j++) {
switch ($i) {
case 1:
$defaultDescription = ($j === 1) ? "PI" : "Co-PI";
break;
case 2:
if ($j === 1) {
$defaultDescription = "UI professional staff & Post Docs";
} elseif ($j === 2) {
$defaultDescription = "GRAs/UGrads";
} elseif ($j === 3) {
$defaultDescription = "Temp Help";
}
break;
case 4:
$defaultDescription = "Large Servers";
break;
case 5:
$defaultDescription = "Domestic Travel";
break;
case 6:
$defaultDescription = "Materials and Supplies";
break;
case 7:
$defaultDescription = "Grant of Idaho State University";
break;
case 8:
$defaultDescription = "Back Out GRA T&F";
break;
default:
$defaultDescription = "Initial Budget Item $i";
break;
}
$stmtBudget->bind_param(
'iissddddddd',
$grant_id,
$i,
$defaultDescription,
$defaultHourlyRate,
$defaultYears[1],
$defaultYears[2],
$defaultYears[3],
$defaultYears[4],
$defaultYears[5],
$defaultYears[6],
$defaultTotalAmount
);
$stmtBudget->execute();
}
}
$conn->commit();
header("Location: index.php");
} catch (Exception $e) {
$conn->rollback();
echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
}
?>
<h1 style="font-family: Arial, sans-serif; text-align: center; color: #333; margin-top: 20px; font-size: 2em;">Create
New Grant</h1>
<form action="create_grant.php" method="POST"
style="width: 90%; max-width: 600px; margin: auto; font-family: Arial, sans-serif;">
<label style="display: block; margin: 10px 0 5px; font-size: 1em;">Title:</label>
<input type="text" name="title" required
style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; font-size: 1em;">
<label style="display: block; margin: 10px 0 5px; font-size: 1em;">Funding Agency:</label>
<select name="agency" required style="padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; font-size: 1em;">
<option value="" disabled selected>Select a funding agency</option>
<option value="NSF">NSF</option>
<option value="NIH">NIH</option>
</select>
<label style="display: block; margin: 10px 0 5px; font-size: 1em;">Start Date:</label>
<input type="date" name="start_date" id="start_date" required
style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; font-size: 1em;">
<label style="display: block; margin: 10px 0 5px; font-size: 1em;">Duration (in years):</label>
<input type="number" name="duration" id="duration" max="5" required
style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; font-size: 1em;">
<label style="display: block; margin: 10px 0 5px; font-size: 1em;">Total Amount:</label>
<input type="number" step="0.01" name="total_amount" required
style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; font-size: 1em;">
<label style="display: block; margin: 10px 0 5px; font-size: 1em;">Search and Add Users:</label>
<input type="text" id="user_search" placeholder="Type to search users..."
style="width: 100%; padding: 8px; margin-bottom: 10px; border: 1px solid #ccc; font-size: 1em;">
<div id="user_search_results"
style="min-width: 99% !important; border: 1px solid #ccc; padding: 10px; max-height: 200px; overflow-y: auto; background-color: #f9f9f9; font-size: 1.5em;">
<!-- Search results will appear here -->
</div>
<div id="selected_users" style="margin-top: 20px;">
<h3 style="font-family: Arial, sans-serif; color: #333; font-size: 1.2em;">Selected Users and Assign Roles:</h3>
</div>
<input type="submit" value="Create Grant"
style="margin-top: 20px; padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer; font-size: 1em;">
</form>
<script>
document.getElementById('user_search').addEventListener('input', function() {
const query = this.value;
if (query.length > 1) {
fetch(`search_users.php?query=${query}`)
.then(response => response.text())
.then(data => {
document.getElementById('user_search_results').innerHTML = data;
});
} else {
document.getElementById('user_search_results').innerHTML = '';
}
});
function addUser(userId, username) {
if (document.getElementById(`selected_user_${userId}`)) return;
const userContainer = document.createElement('div');
userContainer.id = `selected_user_${userId}`;
userContainer.style.marginBottom = '10px';
userContainer.innerHTML = `
<input type="hidden" name="user_ids[]" value="${userId}">
${username}
<select name="roles[]" style="margin-left: 10px; font-size: 0.9em;">
<option value="CO-PI">CO-PI</option>
<option value="PI">PI</option>
<option value="viewer">Viewer</option>
</select>
<button type="button" onclick="removeUser(${userId})" style="margin-left: 10px; padding: 5px; background-color: #f44336; color: white; border: none; cursor: pointer; font-size: 0.9em;">Remove</button>
`;
document.getElementById('selected_users').appendChild(userContainer);
}
function removeUser(userId) {
const userElement = document.getElementById(`selected_user_${userId}`);
if (userElement) {
userElement.remove();
}
}
</script>
<hr>
<?php
include 'footer.php';
?>