forked from mitchellg/CSC212-Group-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeGroup.php
executable file
·92 lines (77 loc) · 5 KB
/
makeGroup.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
<?php
makeGroups($_REQUEST['authorID']);
function makeGroups( $authorID){
$db = new PDO('mysql:host=localhost;dbname=group_maker', 'root', 'password',array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); //From: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
try{
$classIDQuery = $db->prepare("select classId from classes where authorId='$authorID'");
$classIDQuery->execute();
$classIDArray = $classIDQuery->fetchAll();
$classID = $classIDArray[0][0];
$studentQuery = $db->prepare("SELECT name from students where class='$classID'");
$studentQuery->execute();
$students = $studentQuery->fetchAll();
$classSize = count($students); // number of students in the class
$groupSize = $db->prepare("SELECT sizeGroups from classes where classId='$classID'");
$groupSize->execute();
$sizeGroups = $groupSize->fetchAll(); // group size indicated by professor
$numGroups = ceil($classSize / $sizeGroups[0][0]); //number of groups in class
$att = $db->prepare("SELECT distinct attribute,weight
FROM $classID
ORDER BY weight DESC");
$att ->execute();
$attList = $att->fetchAll(); // List of all attributes for a class
$stmt = $db->prepare("SELECT attribute, students.name
FROM $classID
INNER JOIN students
ON $classID.studentIndex=students.index");
$stmt ->execute();
$studentAtt = $stmt->fetchAll(); // array containing each attribute that a student has: Array("attrubute", "name)
$currGroup = 1;
for($currAtt = 0 ; $currAtt<count($attList) ; $currAtt++){ // for each attribute
//echo "<br/> <br/><br/>";
foreach( $studentAtt as $student){ // looking at each student
if( strcmp($student['attribute'],$attList[$currAtt]['attribute']) == 0){ // if student's attribute in array is equal to current attribute, add them to group
$stmt = $db->prepare("UPDATE students
SET groupNumber=?
WHERE class=? AND name=?" ); //prepare update
$stmt->execute(array($currGroup,$classID,$student['name'])); //array containing data
$studentName = $student['name'];
for($j = 0; $j<count($studentAtt) ; $j++){ // this loop deletes a student if they have been put into a group
if(strcmp($studentAtt[$j]['name'],$studentName) == 0){
$studentAtt[$j]['name'] = "0"; // sets each instance of the student to 0 to make sure the student isn't
$studentAtt[$j]['attribute'] = "0"; // re-added to a group
}
}
if(++$currGroup > $numGroups){ // this makes sure loop is adding a studnet to each group
$currGroup = 1;
}
}
}
}
/*
* This Finds if there are any students who were not put into a class.
*/
$nullMember = $db->prepare("SELECT name
FROM students
WHERE groupNumber IS NULL AND class='$classID'");
$nullMember ->execute();
$nullStudent = $nullMember->fetchAll();
foreach( $nullStudent as $student){
$stmt1 = $db->prepare("UPDATE students
SET groupNumber=?
WHERE class=? AND name=?" ); //prepare update
$stmt1->execute(array($currGroup,$classID,$student['name']));
if(++$currGroup > $numGroups){ // this makes sure loop is adding a studnet to each group
$currGroup = 1;
}
}
$groupMade = $db->prepare("UPDATE classes
SET groupsMade=1
WHERE classId=?"); // Set GroupsMade to TRUE
$groupMade->execute(array($classID));
} catch(PDOException $ex){
echo "error occured in query ";
}
}
?>