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 updateTeam(Request $request)
 {
     $students = User::all();
     \DB::table('team')->where('teamID', $request->get('id'))->update(['teamName' => $request->get('team')]);
     $t = team::where('teamName', '=', $request->get('team'))->first();
     team_contents::where('teamID', '=', $t->teamID)->delete();
     foreach ($students as $s) {
         $numberwang = $request->get($s->id);
         if ($numberwang == "on") {
             team_contents::create(['teamID' => $t->teamID, 'studentID' => $s->id]);
         }
     }
     return redirect('home');
 }
 public function run()
 {
     DB::table('team_contents')->delete();
     team_contents::create(['teamID' => '1', 'studentID' => '1']);
     team_contents::create(['teamID' => '1', 'studentID' => '2']);
     team_contents::create(['teamID' => '1', 'studentID' => '3']);
     team_contents::create(['teamID' => '1', 'studentID' => '4']);
     team_contents::create(['teamID' => '1', 'studentID' => '5']);
     team_contents::create(['teamID' => '2', 'studentID' => '6']);
     team_contents::create(['teamID' => '2', 'studentID' => '7']);
     team_contents::create(['teamID' => '2', 'studentID' => '8']);
     team_contents::create(['teamID' => '2', 'studentID' => '9']);
     team_contents::create(['teamID' => '2', 'studentID' => '10']);
     team_contents::create(['teamID' => '3', 'studentID' => '1']);
     team_contents::create(['teamID' => '3', 'studentID' => '2']);
     team_contents::create(['teamID' => '3', 'studentID' => '11']);
     team_contents::create(['teamID' => '4', 'studentID' => '12']);
     team_contents::create(['teamID' => '4', 'studentID' => '13']);
     team_contents::create(['teamID' => '4', 'studentID' => '14']);
     team_contents::create(['teamID' => '5', 'studentID' => '1']);
     team_contents::create(['teamID' => '5', 'studentID' => '15']);
     team_contents::create(['teamID' => '5', 'studentID' => '16']);
     team_contents::create(['teamID' => '5', 'studentID' => '17']);
     team_contents::create(['teamID' => '6', 'studentID' => '18']);
     team_contents::create(['teamID' => '6', 'studentID' => '19']);
     team_contents::create(['teamID' => '6', 'studentID' => '20']);
     team_contents::create(['teamID' => '6', 'studentID' => '21']);
 }