public function update($id, Requests\EditProfileRequest $request) { $requestArray = $request->all(); $teamStyle = \App\userPreferredTeamStyle::where('userID', $id); $teamStyle->update(['teamStyle' => $requestArray['teamStyles']]); $language = \App\userKnownLanguages::where('userID', $id); $language->update(['languageName' => $requestArray['languages']]); \App\userCourseTaken::where('userID', $id)->delete(); if (array_key_exists('course261', $requestArray)) { \App\userCourseTaken::create(['userID' => $id, 'courseCode' => 261]); } if (array_key_exists('course262', $requestArray)) { \App\userCourseTaken::create(['userID' => $id, 'courseCode' => 262]); } if (array_key_exists('course306', $requestArray)) { \App\userCourseTaken::create(['userID' => $id, 'courseCode' => 306]); } if (array_key_exists('course406', $requestArray)) { \App\userCourseTaken::create(['userID' => $id, 'courseCode' => 406]); } return redirect('/home'); }
public function redoAssignments(Requests\RedoTeamAssignmentRequest $request) { $minTeamSize = $request->all()['min']; $maxTeamSize = $request->all()['max']; // Return to teams page if form was empty if ($minTeamSize == "" || $maxTeamSize == "") { return redirect('/teams'); } // max must be greater than min for team assignments to work if (!($minTeamSize < $maxTeamSize)) { return redirect('/teams'); } // Create array of student IDs $studentIDs = \App\User::leftJoin('UserRole', 'users.id', '=', 'UserRole.userID')->where('role', 'student')->get()->pluck('id')->toArray(); // max must be less than the number of students if (!($minTeamSize <= count($studentIDs))) { return redirect('/teams'); } // Sort students by the number of courses they have taken. $sortedStudentIDs = array(); for ($i = 0; $i <= 4; $i++) { foreach ($studentIDs as $studentID) { $coursesTaken = \App\userCourseTaken::where('userID', $studentID)->get()->toArray(); if (count($coursesTaken) == $i) { array_push($sortedStudentIDs, $studentID); } } } // Create an array to store the appropriate number of teams $teams = array(); $maxNumberOfTeams = floor(count($studentIDs) / $minTeamSize); $teams = array_fill(0, $maxNumberOfTeams, array_fill(0, $maxTeamSize, -1)); // Place each student in a team using the following priorities: // - 1 - try to place student in team with matching language and team style // - 2 - try to place student in team with matching language // - 3 - try to place student in a brand new team // - 4 - try to place student in a team with matching team style // - 5 - place the student anywhere foreach ($sortedStudentIDs as $studentID) { $studentPlaced = false; $studentLanguage = \App\userKnownLanguages::where('userID', $studentID)->get()->pluck('languageName')->toArray()[0]; $studentStyle = \App\userPreferredTeamStyle::where('userID', $studentID)->get()->pluck('teamStyle')->toArray()[0]; // language + team style for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][0] == -1) { continue; } $teamLanguage = \App\userKnownLanguages::where('userID', $teams[$team][0])->get()->pluck('languageName')->toArray()[0]; $teamStyle = \App\userPreferredTeamStyle::where('userID', $teams[$team][0])->get()->pluck('teamStyle')->toArray()[0]; if ($studentLanguage == $teamLanguage && $studentStyle == $teamStyle) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1) { $studentPlaced = true; $teams[$team][$slot] = $studentID; break; } } if ($studentPlaced) { break; } } } if ($studentPlaced) { continue; } // language for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][0] == -1) { continue; } $teamLanguage = \App\userKnownLanguages::where('userID', $teams[$team][0])->get()->pluck('languageName')->toArray()[0]; if ($studentLanguage == $teamLanguage) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1) { $studentPlaced = true; $teams[$team][$slot] = $studentID; break; } } if ($studentPlaced) { break; } } } if ($studentPlaced) { continue; } // new team for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][0] == -1) { $studentPlaced = true; $teams[$team][0] = $studentID; break; } } if ($studentPlaced) { continue; } // team style for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][0] == -1) { continue; } $teamStyle = \App\userPreferredTeamStyle::where('userID', $teams[$team][0])->get()->pluck('teamStyle')->toArray()[0]; if ($studentStyle == $teamStyle) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1) { $studentPlaced = true; $teams[$team][$slot] = $studentID; break; } } if ($studentPlaced) { break; } } } if ($studentPlaced) { continue; } // place anywhere for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][count($teams[$team]) - 1] == -1) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1) { $teams[$team][$slot] = $studentID; break; } } break; } } } // Locate any teams that are too small $studentsRemaining = array(); $invalidTeams = array(); for ($team = 0; $team < count($teams); $team++) { $studentsInCurrentTeam = array(); for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1 && $slot < $minTeamSize) { $studentsRemaining = array_merge($studentsRemaining, $studentsInCurrentTeam); array_push($invalidTeams, $team); break; } array_push($studentsInCurrentTeam, $teams[$team][$slot]); } } // Determine if students in small teams can be moved to other teams $spotsRemaining = 0; for ($team = 0; $team < count($teams); $team++) { if (!in_array($team, $invalidTeams)) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1) { $spotsRemaining++; } } } } // Rearrange students by removing teams that are too small if ($spotsRemaining >= count($studentsRemaining) && count($studentsRemaining) > 0) { for ($team = 0; $team < count($teams); $team++) { if (in_array($team, $invalidTeams)) { $teams[$team] = array_fill(0, $maxTeamSize, -1); } } foreach ($studentsRemaining as $studentID) { for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][count($teams[$team]) - 1] == -1 && $teams[$team][0] != -1) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] == -1) { $teams[$team][$slot] = $studentID; break; } } break; } } } } else { if (count($studentsRemaining) > 0) { foreach ($invalidTeams as $invalidTeam) { $teamValid = false; while (!$teamValid) { $teamValid = true; $studentToMove = -1; // Find a student to add, and remove them from their old team for ($team = 0; $team < count($teams); $team++) { if (!in_array($team, $invalidTeams) && $teams[$team][$minTeamSize] != -1) { for ($slot = $maxTeamSize - 1; $slot >= 0; $slot--) { if ($teams[$team][$slot] != -1) { $studentToMove = $teams[$team][$slot]; $teams[$team][$slot] = -1; } if ($studentToMove != -1) { break; } } } if ($studentToMove != -1) { break; } } // Add the student to the smaller team for ($slot = 0; $slot < $maxTeamSize; $slot++) { if ($teams[$invalidTeam][$slot] == -1) { $teams[$invalidTeam][$slot] = $studentToMove; if ($slot == $minTeamSize - 1) { $teamValid = true; } break; } } } } } } // Remove empty teams from array for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][0] == -1) { unset($teams[$team]); } } $teams = array_values($teams); // Delete and recreate teams \App\userTeamAssociation::truncate(); \App\team::truncate(); for ($team = 0; $team < count($teams); $team++) { if ($teams[$team][0] != -1) { $teamName = 'Team ' . ($team + 1); $teamLanguage = \App\userKnownLanguages::where('userID', $teams[$team][0])->get()->pluck('languageName')->toArray()[0]; \App\team::create(['teamName' => $teamName, 'language' => $teamLanguage]); } } // Create new team associations for ($team = 0; $team < count($teams); $team++) { for ($slot = 0; $slot < count($teams[$team]); $slot++) { if ($teams[$team][$slot] != -1) { \App\userTeamAssociation::create(['userID' => $teams[$team][$slot], 'teamID' => $team + 1]); } } } return redirect('/teams'); }