/** * Change the detail for a competition (after the user has entered the event) */ public function changeDetail(Request $request, $entry_id) { $data = $request->all(); $entry = Entry::findOrFail($entry_id); $entry['detail_id'] = $data['newDetail']; $entry->save(); $detail = Detail::findOrFail($data['newDetail']); \Flash::success("No problem! You're detail has been changed to " . $detail->dateTime->toDateTimeString()); return redirect(action('PagesController@userEntries')); }
/** * Export list of competitors and competitions */ public function export($type, $id) { $header = array('Competitor name', 'ID', 'Email', 'Club', 'Home Country'); if ($type == 'competitors') { $event = Event::findOrFail($id); $entries = $event->entries()->get()->sortBy('user_lastname'); $competitor_ids = $entries->pluck('user_id')->unique(); $competitions = $event->competitions()->get(); $header = array(); foreach ($competitions as $competition) { $header[] = $competition->name; } $sheetdata = array($header); foreach ($competitor_ids as $competitor_id) { $competitorEntries = $event->entries()->get()->where('user_id', $competitor_id); $thisCompetitor = User::findOrFail($competitor_id); $name = $thisCompetitor->lastname . ", " . $thisCompetitor->firstname; $row = array($name); foreach ($competitions as $competition) { $entered_competition_ids = array(); foreach ($competitorEntries as $entry) { $entered_competition_ids[] = $entry->competition_id; } if (in_array($competition->id, $entered_competition_ids)) { $row[] = "Entered"; } else { $row[] = ""; } } $sheetdata[] = $row; } Excel::create('competitors', function ($excel) use($sheetdata) { $excel->setTitle('Competitors List'); $excel->setCreator('Foresight Entries'); $excel->setCompany('Foresightentries.com'); $excel->setDescription('Competitors list'); // Create the sheet $excel->sheet('Competitors', function ($sheet) use($sheetdata) { $sheet->fromArray($sheetdata); }); })->export('xlsx'); return redirect()->back(); } // Download ALL the data in one mega spreadsheet (for the overview tab) if ($type == 'event') { $event = Event::findOrFail($id); $competitions = $event->competitions()->get(); $megaheader = $header; $sheets = array(); //Sheet showing all competitors, their entries, their answers, extras and how much paid // -- HEADERS -- // ---- competitions -- foreach ($event->competitions()->get() as $competition) { $megaheader[] = $competition->name; } // ---- questions -- foreach ($event->questions()->get() as $question) { $megaheader[] = $question->question; } // ---- extras -- foreach ($event->extras()->get() as $extra) { //fill in once extras is worked out } // ---- financial -- $megaheader[] = "Entry fees (total)"; //MEGASHEET INITIALISE $megasheet = array($megaheader); // DATA ENTRY TO MEGASHEET $entries = $event->entries()->get()->sortBy('user_lastname'); $competitor_ids = $entries->pluck('user_id')->unique(); foreach ($competitor_ids as $competitor_id) { $thisCompetitor = User::findOrFail($competitor_id); $row = array($thisCompetitor->firstname . " " . $thisCompetitor->lastname, $thisCompetitor->id + 1000, $thisCompetitor->email, $thisCompetitor->club, $thisCompetitor->homeCountry); // -- ENTRIES -- $competitorEntries = $event->entries()->get()->where('user_id', $competitor_id); $competitorEntriesCompetitionIDs = array(); foreach ($competitorEntries as $entry) { $competitorEntriesCompetitionIDs[$entry->competition_id] = $entry->competition_id; } foreach ($event->competitions()->get() as $competition) { // if entry->competition_id exists in $competitorEntries then entered row, else empty row. if (key_exists($competition->id, $competitorEntriesCompetitionIDs)) { $row[] = "x"; } else { $row[] = " "; } } // -- ANSWERS -- $competitorAnswers = $event->answers()->get()->where('competitor_id', $competitor_id); $competitorAnsweredQuestionIDs = array(); foreach ($competitorAnswers as $answer) { $competitorAnsweredQuestionIDs[$answer->question_id] = $answer->question_id; } //@TODO: this needs to be tested with a user who has answered questions. 5/7/16 foreach ($competitorAnswers as $competitorAnswer) { if (key_exists($competitorAnswer->question_id, $competitorAnsweredQuestionIDs)) { $row[] = $competitorAnswer->answer; } else { $row[] = "N/A"; } } // -- EXTRAS -- //@TODO: fill in once extras is worked out // -- ENTRY FEES TOTAL -- $row[] = "subtotal here (£)"; // Add the row to the megasheet $megasheet[] = $row; } //Add the megasheet to sheets $sheets["Competitors"] = $megasheet; //Sheet for each competition and detail with the entries foreach ($competitions as $competition) { $details = $competition->details()->get(); foreach ($details as $detail) { $entries = $detail->entries()->get()->sortBy('user_lastname'); $sheetdata = array($header); $competitor_ids = $entries->pluck('user_id')->unique(); foreach ($competitor_ids as $competitor_id) { $competitor = User::findOrFail($competitor_id); $row = array($competitor->firstname . " " . $competitor->lastname, $competitor->id + 1000, $competitor->email, $competitor->club, $competitor->homeCountry); $sheetdata[] = $row; } $sheets[$competition->name . "-" . $detail->name] = $sheetdata; } } Excel::create($event->name, function ($excel) use($sheets, $competitions) { $excel->setTitle('Competitors'); $excel->setCreator('Foresight Entries'); $excel->setCompany('ForesightEntries.com'); $excel->setDescription('Competitors List'); $infosheet = $sheets["Competitors"]; $excel->sheet('Competitors', function ($sheet) use($infosheet) { $sheet->fromArray($infosheet); }); foreach ($competitions as $competition) { $details = $competition->details()->get(); foreach ($details as $detail) { $sheet = $sheets[$competition->name . '-' . $detail->name]; $excel->sheet($competition->name . '-' . $detail->name, function ($thissheet) use($sheet) { $thissheet->fromArray($sheet); }); } } })->export('xlsx'); return redirect()->back(); } if ($type == 'competition') { $competition = Competition::findOrFail($id); $details = $competition->details()->get(); $sheets = array(); foreach ($details as $detail) { $entries = $detail->entries()->get()->sortBy('user_lastname'); $sheetdata = array($header); $competitor_ids = $entries->pluck('user_id')->unique(); foreach ($competitor_ids as $competitor_id) { $competitor = User::findOrFail($competitor_id); $row = array($competitor->firstname . " " . $competitor->lastname, $competitor->id + 1000, $competitor->email, $competitor->club, $competitor->homeCountry); $sheetdata[] = $row; } $sheets[$detail->name] = $sheetdata; } Excel::create($competition->name, function ($excel) use($sheets, $details) { $excel->setTitle('Competitors'); $excel->setCreator('Foresight Entries'); $excel->setCompany('ForesightEntries.com'); $excel->setDescription('Competitors List'); foreach ($details as $detail) { $sheet = $sheets[$detail->name]; $excel->sheet($detail->name, function ($thissheet) use($sheet) { $thissheet->fromArray($sheet); }); } })->export('xlsx'); return redirect()->back(); } if ($type == 'detail') { $detail = Detail::findOrFail($id); $entries = $detail->entries()->get()->sortBy('user_lastname'); $sheetdata = array($header); $competitor_ids = $entries->pluck('user_id')->unique(); foreach ($competitor_ids as $competitor_id) { $competitor = User::findOrFail($competitor_id); $row = array($competitor->firstname . " " . $competitor->lastname, $competitor->id + 1000, $competitor->email, $competitor->club, $competitor->homeCountry); $sheetdata[] = $row; } Excel::create($detail->competition()->first()->name . "-" . $detail->name, function ($excel) use($sheetdata) { $excel->setTitle('Competitors'); $excel->setCreator("Foresight Entries"); $excel->setCompany("ForesightEntries.com"); $excel->setDescription("Competitors List"); $excel->sheet('Competitors', function ($sheet) use($sheetdata) { $sheet->fromArray($sheetdata); }); })->export('xlsx'); return redirect()->back(); } if ($type == 'competitor_entries') { $event = Event::findOrFail($id); $entries = $event->entries()->get()->sortBy('user_lastname'); $competitor_ids = $entries->pluck('user_id')->unique(); //Add competition names to the header foreach ($event->competitions()->get() as $competition) { $header[] = $competition->name; } $sheetdata = array($header); foreach ($competitor_ids as $competitor_id) { $competitorEntries = $event->entries()->get()->where('user_id', $competitor_id); $competitorEntriesCompetitionIDs = array(); foreach ($competitorEntries as $entry) { $competitorEntriesCompetitionIDs[$entry->competition_id] = $entry->competition_id; } $thisCompetitor = User::findOrFail($competitor_id); $row = array($thisCompetitor->firstname . " " . $thisCompetitor->lastname, $thisCompetitor->id + 1000, $thisCompetitor->email, $thisCompetitor->club, $thisCompetitor->homeCountry); foreach ($event->competitions()->get() as $competition) { // if entry->competition_id exists in $competitorEntries then entered row, else empty row. if (key_exists($competition->id, $competitorEntriesCompetitionIDs)) { $row[] = "x"; } else { $row[] = " "; } } $sheetdata[] = $row; } Excel::create("Competitor entries", function ($excel) use($sheetdata) { $excel->setTitle('Competitor_entries'); $excel->setCreator("Foresight Entries"); $excel->setCompany("ForesightEntries.com"); $excel->setDescription("Competitors List"); $excel->sheet('Competitor_entries', function ($sheet) use($sheetdata) { $sheet->fromArray($sheetdata); }); })->export('xlsx'); return redirect()->back(); } if ($type == 'competitor_answers') { $event = Event::findOrFail($id); $entries = $event->entries()->get()->sortBy('user_lastname'); $competitors = $entries->pluck('user_id')->unique(); //Add the questions to the header foreach ($event->questions()->get() as $question) { $header[] = $question->question; } $sheetdata = array($header); foreach ($competitors as $competitor) { $competitorAnswers = $event->answers()->get()->where('competitor_id', $competitor); $competitorAnsweredQuestionIDs = array(); foreach ($competitorAnswers as $answer) { $competitorAnsweredQuestionIDs[$answer->question_id] = $answer->question_id; } $thisCompetitor = User::findOrFail($competitor); $row = array($thisCompetitor->firstname . " " . $thisCompetitor->lastname, $thisCompetitor->id + 1000, $thisCompetitor->email, $thisCompetitor->club, $thisCompetitor->homeCountry); //@TODO: this needs to be tested with a user who has answered questions. 5/7/16 foreach ($competitorAnswers as $competitorAnswer) { if (key_exists($competitorAnswer->question_id, $competitorAnsweredQuestionIDs)) { $row[] = $competitorAnswer->answer; } else { $row[] = "N/A"; } } $sheetdata[] = $row; } Excel::create("Competitor answers", function ($excel) use($sheetdata) { $excel->setTitle('Competitor_answers'); $excel->setCreator("Foresight Entries"); $excel->setCompany("ForesightEntries.com"); $excel->setDescription("Competitor answers"); $excel->sheet('Competitor_answers', function ($sheet) use($sheetdata) { $sheet->fromArray($sheetdata); }); })->export('xlsx'); return redirect()->back(); } }