$nextWeekUpdateString = chop($nextWeekUpdateString, " OR "); $restUpdateString = chop($restUpdateString, " OR "); // update DB try { $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->beginTransaction(); //Insert Permissions if (sizeof($insertArray) > 0) { $insertQuery = "INSERT INTO Permission (uID, groupID, specialHrs) VALUES {$insertString}"; $insertStmt = $db->prepare($insertQuery); $insertStmt->execute($insertArray); } if (sizeof($usersAddedArray) > 0) { $weekHours = $groupInfo['hours']; //update user table with current active weekly hours if (groupHasCurWeekHours($groupInfo)) { $curWeekUpdateQuery = "UPDATE User SET curWeekHrs = curWeekHrs + {$weekHours} WHERE {$curWeekUpdateString}"; $curWeekUpdateStmt = $db->prepare($curWeekUpdateQuery); $curWeekUpdateStmt->execute($usersAddedArray); } //update user table with active weekly hours for next week if (groupHasNextWeekHours($groupInfo)) { $nextWeekUpdateQuery = "UPDATE User SET nextWeekHrs = nextWeekHrs + {$weekHours} WHERE {$nextWeekUpdateString}"; $nextWeekUpdateStmt = $db->prepare($nextWeekUpdateQuery); $nextWeekUpdateStmt->execute($usersAddedArray); } //update user table with active weekly hours for next week if (groupHasThirdWeekHours($groupInfo)) { $thirdWeekUpdateQuery = "UPDATE User SET thirdWeekHrs = thirdWeekHrs + {$weekHours} WHERE {$nextWeekUpdateString}"; $thirdWeekUpdateStmt = $db->prepare($thirdWeekUpdateQuery); $thirdWeekUpdateStmt->execute($usersAddedArray);
function maybeUpdateUserHours($db, $uID, $groupInfo) { if (groupHasCurWeekHours($groupInfo) || groupHasNextWeekHours($groupInfo) || groupHasThirdWeekHours($groupInfo)) { //get user's hours $hrsQuery = "SELECT curWeekHrs, nextWeekHrs, thirdWeekHrs FROM User WHERE uID = ?"; $hrsStmt = $db->prepare($hrsQuery); $hrsStmt->execute(array($uID)); $hrs = $hrsStmt->fetch(PDO::FETCH_ASSOC); // calculate new user hours. subtract group hours from the hours they have. // can't be less than 0. $newCurHrs = max($hrs['curWeekHrs'] - $groupInfo['hours'], 0); $newNextHrs = max($hrs['nextWeekHrs'] - $groupInfo['hours'], 0); $newThirdHrs = max($hrs['thirdWeekHrs'] - $groupInfo['hours'], 0); //decrement user's hours if (groupHasCurWeekHours($groupInfo)) { $curWeekUpdateQuery = "UPDATE User SET curWeekHrs = {$newCurHrs} WHERE uID= ?"; $curWeekUpdateStmt = $db->prepare($curWeekUpdateQuery); $curWeekUpdateStmt->execute(array($uID)); } if (groupHasNextWeekHours($groupInfo)) { $nextWeekUpdateQuery = "UPDATE User SET nextWeekHrs = {$newNextHrs} WHERE uID= ?"; $nextWeekUpdateStmt = $db->prepare($nextWeekUpdateQuery); $nextWeekUpdateStmt->execute(array($uID)); } if (groupHasThirdWeekHours($groupInfo)) { $thirdWeekUpdateQuery = "UPDATE User SET thirdWeekHrs = {$newThirdHrs} WHERE uID= ?"; $thirdWeekUpdateStmt = $db->prepare($thirdWeekUpdateQuery); $thirdWeekUpdateStmt->execute(array($uID)); } } }