-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupHandler.php
217 lines (192 loc) · 7.67 KB
/
groupHandler.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
<?php
ini_set('display_errors', 1);
//returns currently logged in user's group members
//needs DB reference and user's $group
function getGroup($db, $group) {
$query = ('SELECT * FROM users WHERE currentGroup = :myGroup');
$statement = $db -> prepare($query);
$statement -> bindValue(':myGroup', $group);
if (!$statement -> execute()) {
print_r($statement->errorInfo());
}
$result = $statement -> fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function createGroup($groupName, $description, $type, $userId, $db) {
// creates a group and adds the current user to it
strip_tags($groupName);
strip_tags($description);
strip_tags($type);
$qString = ('INSERT INTO groups (name, description, projectType) VALUES (:name, :description, :type)');
$stm = $db -> prepare($qString);
if ($stm -> execute( array(':name' => $groupName, ':description' => $description, ':type' => $type))) {
if(addToGroup($userId, $db, getGroupId($groupName, $db), true)) {
echo "<meta http-equiv='REFRESH' content='0;url=/?page=group'>";
} else {
echo 'couldnt add to group!';
}
} else {
print_r($stm->errorInfo());
}
}
function updateGroup($groupId, $groupName, $description, $type, $db) {
// creates a group and adds the current user to it
strip_tags($groupName);
strip_tags($description);
strip_tags($type);
$qString = ('UPDATE groups SET name = :name, description = :description, projectType = :type WHERE id = :groupId');
$stm = $db -> prepare($qString);
if ($stm -> execute( array(':name' => $groupName, ':description' => $description, ':type' => $type, ':groupId' => $groupId))) {
if(addToGroup($userId, $db, getGroupId($groupName, $db), true)) {
echo "<meta http-equiv='REFRESH' content='0;url=/?page=group'>";
} else {
echo 'couldnt add to group!';
}
} else {
print_r($stm->errorInfo());
}
}
// removes someone from the group
// takes the $userId of the person to remove
// and the database and the group in question
function removeFromGroup($userId, $db, $group) {
$query = ('UPDATE users SET currentGroup = 0 WHERE id = :userId');
$statement = $db -> prepare($query);
$statement -> bindValue(':userId', $userId);
// execute query and print error message if not
if (!$statement -> execute()) { print_r($stm->errorInfo()); return false; } else {
return true; }
}
function addToGroup($userId, $db, $group, $leader) {
// echo "adding to group $group user $userId";
if ($group == 0) {
echo "<meta http-equiv='REFRESH' content='0;url=createGroup.php'>";
return 0;
}
$query = ('UPDATE users SET currentGroup = :group, groupLeader = :leader WHERE id = :userId');
$statement = $db -> prepare($query);
$statement -> bindValue(':userId', $userId);
$statement -> bindValue(':group', $group);
$statement -> bindValue(':leader', $leader);
// execute query and print error message if not
if (!$statement -> execute()) { return print_r($stm->errorInfo(), true);} else {
return true;
}
}
function getGroupName($groupId, $db) {
$query = ('SELECT name FROM groups WHERE id = :groupId');
$statement = $db -> prepare($query);
$statement -> bindValue(':groupId', $groupId);
// execute query and print error message if not
if (!$statement -> execute()) { print_r($stm->errorInfo());}
$result = $statement -> fetch(PDO::FETCH_ASSOC);
return $result['name'];
}
function getGroupId($groupName, $db) {
$query = ('SELECT id FROM groups WHERE name = :groupName');
$statement = $db -> prepare($query);
$statement -> bindValue(':groupName', $groupName);
// execute query and print error message if not
if (!$statement -> execute()) { print_r($stm->errorInfo());}
$result = $statement -> fetch(PDO::FETCH_ASSOC);
return $result['id'];
}
function getGroupDescription($groupId, $db) {
$query = ('SELECT description FROM groups WHERE id = :groupId');
$statement = $db -> prepare($query);
$statement -> bindValue(':groupId', $groupId);
// execute query and print error message if not
if (!$statement -> execute()) { print_r($stm->errorInfo());}
$result = $statement -> fetch(PDO::FETCH_ASSOC);
return $result['description'];
}
function getGroupType($groupId, $db) {
$query = ('SELECT projectType FROM groups WHERE id = :groupId');
$statement = $db -> prepare($query);
$statement -> bindValue(':groupId', $groupId);
// execute query and print error message if not
if (!$statement -> execute()) { print_r($stm->errorInfo());}
$result = $statement -> fetch(PDO::FETCH_ASSOC);
return $result['projectType'];
}
function setLeader($group, $newLeader, $userId, $db) {
$query = ('UPDATE users SET groupLeader = 1 WHERE id = :userId');
$statement = $db -> prepare($query);
$statement -> bindValue(':userId', $newLeader);
// execute query and print error message if not
if (!$statement -> execute()) { return print_r($stm->errorInfo(), true);} else {
$query = ('UPDATE users SET groupLeader = 0 WHERE id = :userId');
$statement = $db -> prepare($query);
$statement -> bindValue(':userId', $userId);
if (!$statement -> execute()) { return print_r($stm->errorInfo(), true);} else {
return true;
}
}
}
// List of group members + details and options
function renderGroupList($db, $group, $groupLeader) { ?>
<table>
<tr> <th>Name</th> <th>Area of Expertise</th> <th>Actions</th> </tr>
<?php // start looping through group members
foreach (getGroup($db, $group) as $a) {
?>
<tr>
<td>
<a href="/?page=viewProfile&id=<?=$a['id']?>">
<?php
if($a['displayName'] == "") {
echo $a['name'];
} else {
echo $a['displayName'];
}
if($a['groupLeader'] == 1) {
echo ' <span class="subtitle">[group leader]</span>';
}
?>
</a>
</td>
<td><?=$a['mainArea']?></td>
<td>
<?php if($a['id'] != $_SESSION['id'] && $groupLeader == true) { // check if person is self or leader dont display remove button ?>
<a class="button" href="/?page=group&action=remove&user=<?=$a['id']?>" onclick="return confirm('Remove <?=$a['name']?>?')")>Remove from group</a>
<a class="button" href="/?page=group&action=setLeader&user=<?=$a['id']?>" onclick="return confirm('Set <?=$a['name']?> as leader? This will remove you as leader.')")>Set as leader</a>
<?php } // end check for self, leader ?></td>
</tr>
<?php } // end looping through group members ?>
</table>
<?php } ?>
<?php
// List of group members showing only their names
// Currently used on the student home page
function renderSmallGroupList($db, $group, $groupLeader) {
?><div class="content">
<!-- make sure theyre in a group -->
<?php if ($group == 0) {
echo "<h2>You're not in a group yet!</h2>";
} else {
$myGroup = getGroup($db, $group);
?>
<h2 class="subtitle"><?php echo sizeof($myGroup) ?> members</h2>
<table>
<?php // start looping through group members
foreach ($myGroup as $a) {
?>
<tr>
<td>
<a href="/?page=viewProfile&id=<?=$a['id']?>">
<?php
if($a['displayName'] == "") {
echo $a['name'];
} else {
echo $a['displayName'];
}
?>
</a>
</td>
</tr>
<?php } // end looping through group members ?>
</table>
<a class="button left" href="/?page=group">Manage Group</a>
<?php } // end if?>
</div><?php
} ?>