public function divideTeam($team, $min, $max, $boolit, $compID) { $len = count($team); $numTeam = $len / $min; $totalTeam = array(); //$team = array_flip($team); //return $team; if ($boolit) { $a = array_keys($team); } else { $a = $team; } //print_r($a); while (count($a) >= $min) { $t = array(); while (count($t) < $min) { array_push($t, array_pop($a)); $a = array_reverse($a); } array_push($totalTeam, $t); } if (count($a) > 0 && count($totalTeam) > 0) { $i = 0; foreach ($a as $b) { array_push($totalTeam[$i % count($totalTeam)], $b); $i = $i + 1; } } $unsorted = array(); $teamteam = array(); //return $totalTeam; foreach ($totalTeam as $c) { if (count($c) > $max) { while (count($c) > $max) { print_r($c); array_push($unsorted, array_pop($c)); } array_push($teamteam, $c); } else { array_push($teamteam, $c); } } if (count($teamteam) > 0) { foreach ($teamteam as $t) { team::create(['competition' => $compID, 'teamName' => 'Unnamed Team']); $val = \DB::select(\DB::raw('(SELECT max(teamID) as i FROM team)')); $teamID = $val[0]->i; foreach ($t as $s) { //print_r($s); //print_r(','); team_contents::create(['teamID' => $teamID, 'studentID' => $s]); \DB::table('student_competition')->where('studentID', '=', $s)->delete(); } } } if (count($totalTeam == 0)) { return $a; } return $unsorted; }
public function generateTeam(Request $request) { //DB::table('team_contents')->truncate(); $min = $request->get('min'); $max = $request->get('max'); $compId = $request->get('comp'); $students_p = \DB::table('student_competition')->join('student_preferences', 'student_competition.studentID', '=', 'student_preferences.id')->select('student_competition.studentID', 'student_preferences.python', 'student_preferences.java', 'student_preferences.c', 'student_preferences.teamStyle')->where('student_competition.compID', '=', $compId)->get(); $students_c = student_classes::all(); $std_j = array(); $std_p = array(); $std_c = array(); $std_u = array(); foreach ($students_p as $a) { if ($a->java > $a->python and $a->java > $a->c) { $std_j[$a->studentID] = $a->java; } elseif ($a->python > $a->c and $a->python > $a->c) { $std_p[$a->studentID] = $a->python; } elseif ($a->c > $a->python and $a->c > $a->java) { $std_c[$a->studentID] = $a->c; } else { $std_u[$a->studentID] = $a->c; } } foreach ($students_c as $c) { if (array_key_exists($c->studentID, $std_j)) { $std_j[$c->studentID] = $std_j[$c->studentID] + $c->classID; } if (array_key_exists($c->studentID, $std_p)) { $std_p[$c->studentID] = $std_p[$c->studentID] + $c->classID; } if (array_key_exists($c->studentID, $std_c)) { $std_c[$c->studentID] = $std_c[$c->studentID] + $c->classID; } if (array_key_exists($c->studentID, $std_u)) { $std_u[$c->studentID] = $std_u[$c->studentID] + $c->classID; } } asort($std_j); asort($std_c); asort($std_p); asort($std_u); $unsorted = array(); //return $this->divideTeam($std_j, $min, $max); //return count($std_p) + count($std_c) + count($std_p) + count($std_u); // return $unsorted + $this->divideTeam($std_u, $min, $max, true, $compId); //$unsorted = $unsorted + $this->divideTeam($std_j, $min, $max, true, $compId); //print_r($unsorted); //$unsorted = $unsorted + $this->divideTeam($std_c, $min, $max, true, $compId); //print_r($unsorted); //$unsorted = $unsorted + $this->divideTeam($std_p, $min, $max, true, $compId); //print_r($unsorted); //$unsorted = $unsorted + $this->divideTeam($std_u, $min, $max, true, $compId); $ss = array(); $ss = $std_c + $std_j + $std_p + $std_u; $ss = array_keys($ss); shuffle($ss); $totteam = array(); while (count($ss) > 0) { $a = array(); while (count($a) < $min) { array_push($a, array_pop($ss)); } array_push($totteam, $a); } //print_r($totteam); $notAdded = array(); $added = array(); foreach ($totteam as $t) { $test = false; foreach ($t as $g) { if ($g == null) { $test = true; break; } } if ($test) { array_push($notAdded, $t); } else { array_push($added, $t); } } $i = 0; if ($min != $max) { if (count($notAdded) > 0) { foreach ($notAdded[0] as $nA) { if ($nA != null) { while ($i < count($added[$i])) { if (count($added[$i]) < $max) { array_shift($notAdded[0]); array_push($added[$i], $nA); //array_push($finalAdd, $a); $i++; break; } $i++; break; } } else { array_shift($notAdded[0]); } } } } $notAdded = $notAdded[0]; $stdc = count($notAdded); //return $notAdded; foreach ($added as $t) { team::create(['competition' => $compId, 'teamName' => 'Unnamed Team']); $val = \DB::select(\DB::raw('(SELECT max(teamID) as i FROM team)')); $teamID = $val[0]->i; foreach ($t as $s) { //print_r($s); //print_r(','); team_contents::create(['teamID' => $teamID, 'studentID' => $s]); \DB::table('student_competition')->where('studentID', '=', $s)->delete(); } } $teams = team::all(); $competitions = competition::all(); $compID = array(); $compName = array(); $teamnames = array(); foreach ($competitions as $c) { array_push($compID, $c->compID); array_push($compName, $c->compName); } foreach ($teams as $t) { array_push($teamnames, $t->teamName); } return view('admin', compact('teamnames', 'compID', 'compName', 'notAdded')); }
public function divideTeamone($ss, $min, $max, $compId) { if ($ss == null || count($ss) == 0) { return array(); } $tt = array_chunk($ss, $min); $end = null; if (count(end($tt)) < $min) { $end = array_pop($tt); } if ($end != null) { for ($i = 0; $i < count($tt); $i++) { while (count($tt[$i]) < $max) { if (count($end) == 0) { break; } array_push($tt[$i], array_pop($end)); } } } foreach ($tt as $t) { team::create(['competition' => $compId, 'teamName' => 'Unnamed Team']); $val = \DB::select(\DB::raw('(SELECT max(teamID) as i FROM team)')); $teamID = $val[0]->i; foreach ($t as $s) { team_contents::create(['teamID' => $teamID, 'studentID' => $s]); \DB::table('student_competition')->where('studentID', '=', $s)->where('compID', '=', $compId)->delete(); } } if ($end == null) { $end = array(); } return $end; }
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'); }
public function run() { DB::table('team')->delete(); team::create(['teamName' => 'Flying Circus', 'competition' => '1']); team::create(['teamName' => 'Bladed Maze', 'competition' => '1']); team::create(['teamName' => 'Random Namers', 'competition' => '2']); team::create(['teamName' => 'Team 404', 'competition' => '2']); team::create(['teamName' => 'Out of Time', 'competition' => '3']); team::create(['teamName' => 'Seedy Fellows', 'competition' => '3']); }