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); } if (strcmp($groupInfo['hasBookingDurationRestriction'], 'No') == 0) { $restUpdateQuery = "UPDATE User SET hasBookingDurationRestriction = 'No' WHERE {$restUpdateString}"; $restUpdateStmt = $db->prepare($restUpdateQuery); $restUpdateStmt->execute($usersAddedArray); } } $db->commit(); $result = array(); $result["addedUsers"] = $usersAddedArray; $result["usersAlreadyInGroup"] = $alreadyInGroupArray; $result["usersNotInMaster"] = $notInMasterArray;
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)); } } }