%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/nginx/.vscode-server/data/User/History/-7f70a86d/
Upload File :
Create Path :
Current File : //home/nginx/.vscode-server/data/User/History/-7f70a86d/D5kd.php

<?php

namespace App\Http\Controllers;

use App\Enums\ContestPublicationStatusEnum;
use App\Http\Requests\ContestUpdateRequest;
use App\Models\Contest;
use App\Models\ContestCategory;
use App\Models\ContestLevel;
use App\Models\OrganizerGroup;
use App\Models\Range;
use App\Models\Registration;
use App\Models\User;
use App\Workers\ContestImporter;
use App\Workers\RegistrationExporter;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\View\View;

class ContestController extends Controller
{
    public function __construct() {
        $this->middleware('auth')->except(['index', 'show']);
    }

    /* Display a listing of the resource. */
    public function index(Request $request) : View
    {
        $this->authorize('viewAny', Contest::class);

        $list = Contest::orderBy('date')->get();
        if (!Auth::user()?->is_admin) {
            $list = $list->reject(function ($value, $key) {
                 if ($value->published == ContestPublicationStatusEnum::PUBLISHED) return false;        // not removing from list if user is admin
                 if (Auth::user()?->is_organizer_member($value->organizer_group_id)) return false;      // not removing if user is organizer of the contest
                 if ($value->published == ContestPublicationStatusEnum::HIDDEN                          // not removing if contest is hidden but user is registered (by admin/organizer)
                    && $value->registrations->where('user_id', Auth::user()?->id)->count() > 0) return false;

                 return true;
            });
        }

        return view('contests.list', ['contests' => $list]);
    }

    /* Display the specified resource. */
    public function show(Contest $contest) : View
    {
        $this->authorize('view', $contest);

        return view('contests.show', [
            'contest' => $contest,
            'user' => Auth::user(),
            'forcenotcompete' => !Auth::user()?->is_admin &&
                                 $contest->registrations->where('user_id', Auth::user()?->id)
                                                        ->where('notcomp', 0)
                                                        ->count() > 0,
            'results' => $this->getResults($contest, false)
        ]);
    }

    /* Show the form for creating a new resource. */
    public function create(Request $request) : View
    {
        $this->authorize('create', Contest::class);

        return view('contests.edit', [
            'contest' => null,
            'ranges' => Range::where('is_active', 1)->orderBy('name')->get(),
            'orggroups' => Auth::user()->is_admin ? OrganizerGroup::orderBy('name')->get() : Auth::user()->organizer_groups->sortBy('name'),
            'organizators' => $this->getOrganizerUsers(),
            'contestlevels' => ContestLevel::where('is_active', 1)->get(),
            'contestcategories' => ContestCategory::where('is_active', 1)->get(),
        ]);
    }

    /* Show the form for editing the specified resource. */
    public function edit(Contest $contest): View
    {
        $this->authorize('update', $contest);

        return view('contests.edit', [
            'contest' => $contest,
            'ranges' => Range::orderBy('name')->get(),
            'orggroups' => Auth::user()->is_admin ? OrganizerGroup::orderBy('name')->get() : Auth::user()->organizer_groups->sortBy('name'),
            'organizators' => $this->getOrganizerUsers(),
            'contestlevels' => ContestLevel::where('is_active', 1)->get(),
            'contestcategories' => ContestCategory::where('is_active', 1)->get(),
        ]);
    }

    public function maintain(Contest $contest): View
    {
        $this->authorize('update', $contest);

        return view('contests.maintain', [
            'contest' => $contest,
            'users' => User::get()->sortBy('displayname'),
        ]);
    }

    /* Toggle publish status of the contest */
    public function publish(Contest $contest, int $status): RedirectResponse
    {
        $this->authorize('update', $contest);

        $contest->published = $status;
        $contest->save();

        return back();
    }

    /* Store a newly created resource in storage. */
    public function store(ContestUpdateRequest $request): RedirectResponse
    {
        $this->authorize('create', Contest::class);

        $contestData = new Contest;
        $contestData->fill($request->validated());
        $contestData->fillSettings($request);
        $contestData->psmatchguid = Str::uuid();

        $contest = $contestData->save();
        return redirect(route('contest.show', $contestData->id));
    }

    /* Update the specified resource in storage. */
    public function update(ContestUpdateRequest $request, Contest $contest): RedirectResponse
    {
        $this->authorize('update', $contest);

        $oldSquadCount = $contest->squads;  // store old number of squads

        $contest->fill($request->validated());
        $contest->fillSettings($request);

        if ($contest->squads < $oldSquadCount) {
            Registration::where('contest_id', $contest->id)->where('squad', '>', $contest->squads)->update(['squad' => 1]);
        }

        $contest->save();
        return redirect(route('contest.show', $contest->id));
    }

    /* Remove the specified resource from storage. */
    public function destroy(Contest $contest)
    {
    }

    public function changesquad(Contest $contest, Request $request) : RedirectResponse
    {
        $this->authorize('update', $contest);

        Registration::whereIn('id', $request->selection)->update(['squad' => $request->squad]);

        return back();
    }


    /* Calls ExportRegistration model class to create export and returns finalized file */
    public function export(Contest $contest, string $type)
    {
        $this->authorize('update', $contest);

        $regExport = new RegistrationExporter($contest, $type, request()->has('all'));

        $ctypes = array(
            'pscsv' => 'application/csv',
            'excel' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'psc'   => 'application/psc'
        );

        $headers = array("Content-Type" => $ctypes[$type]);

        return Storage::disk('contests')->download($regExport->filename, basename($regExport->filename), $headers);
    }

    public function processImport(Request $request, Contest $contest)
    {
        $this->authorize('update', $contest);

        $file = $request->file('file');
        $extension = $file->getClientOriginalExtension();
        $uploadName = $contest->id . "/import-" . time() . "." . $extension;
        Storage::disk('contests')->put($uploadName, $file->getContent());

        $importer = new ContestImporter($contest, $uploadName, $extension);

        if($importer->hasMessages())
        {
            return view('contests.import-messages', [
                'importer' => $importer,
                'contestId' => $contest->id
            ]);
        }

        return redirect(route('contest.detailed-results', $contest->id));
    }

    public function presentation(Contest $contest)
    {
        $this->authorize('update', $contest);

        $shooters = array();
        $regs = $contest->isLosik() ? $contest->registrations : $contest->registrations->where('present', 1);
        $regs = $regs->sortBy(['squad', 'user.lastname']);

        $data = array(
            'contest' => $contest,
            'registrations' => $regs,
            'freeSlots' => $contest->capacity < 50 ? 5 : 10
        );

        return view("contests.presentation", $data);
    }

    public function detailedResults(Contest $contest, bool $print)
    {
        $results = $this->getResults($contest, true);
        return view("contests.results-detail", $results);
    }

    private function getResults(Contest $contest, bool $detailed)
    {
        $results = DB::select("SELECT * FROM `contest_results` WHERE `contest_id`=?", [$contest->id]);
        if(!$results)
            return null;

        // Prepare empty arrays with detailed results
        $stages = array();

        $divisions = array();
        foreach ($results as $result) {
            if(!isset($divisions[$result->bgdivision]))
            {
                $divisions[$result->bgdivision] = new \stdClass();
                $divisions[$result->bgdivision]->name = $result->bgdivision;
                $divisions[$result->bgdivision]->shooters = array();
            }
            $divisions[$result->bgdivision]->shooters[$result->registration_id] = new \stdClass();
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->percent = number_format($result->percent, 2);
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->name = $result->firstname . " " . $result->lastname . ($result->namesuffix ? " " . $result->namesuffix : "");
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->registrationId = $result->registration_id;
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->origDivision = $result->origdivision;
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->notcomp = $result->notcomp;
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->stages = array();
        }

        foreach (DB::select("SELECT * FROM `stage_results` WHERE `contest_id`=?", [$contest->id]) as $result) {
            $divisions[$result->bgdivision]->shooters[$result->registration_id]->stages[$result->order] = $result;
        }

        if($detailed)
        {
            foreach(DB::select("SELECT `id`, `strings`, `order` FROM `stages` WHERE `contest_id`=?", [$contest->id]) as $stg)
            {
                $stages[$stg->order] = new \stdClass();
                $stages[$stg->order]->strings = $stg->strings;
                $stages[$stg->order]->id = $stg->id;
                $stages[$stg->order]->span = 11 + ($stg->strings > 1 ? $stg->strings : 0);
            }

            foreach(DB::select("SELECT `order`, `time`, `bgdivision`, `registration_id`, `storder` FROM `stage_times` WHERE `contest_id`=?", [$contest->id]) as $str)
            {
                if(!isset($divisions[$str->bgdivision]->shooters[$str->registration_id]->stageStrings[$str->storder]))
                {
                    $divisions[$str->bgdivision]->shooters[$str->registration_id]->stageStrings[$str->storder] = array();
                }
                $divisions[$str->bgdivision]->shooters[$str->registration_id]->stageStrings[$str->storder][$str->order] = $str->time;
            }
        }

        foreach ($divisions as $div)
        {
            uasort($div->shooters, function ($a, $b) {
                if($a->percent < $b->percent) return 1;
                if($a->percent == $b->percent && $a->name < $b->name) return 1;
                return -1;
            });
        }

        uasort($divisions, function ($a, $b) {
            $divPrio = array(
                "ALL" => 1,
                "PI" => 2,
                "KPI" => 3,
                "MPI" => 4,
                "RE" => 5,
                "MRE" => 6,
                "OPT" => 7,
                "PDW" => 8,
                "PSA" => 9,
                "POP" => 10,
                "BSA" => 11,
                "BOP" => 12,
                "BOS" => 13
            );
            if($divPrio[strtoupper($a->name)] > $divPrio[strtoupper($b->name)]) return 1;
            return -1;
        });

        return array(
            "divisions" => $divisions,
            "numStages" => $contest->stages,
            "name" => $contest->contestname,
            "date" => $contest->date,
            "stages" => $stages
        );
    }

    private function getOrganizerUsers()
    {
        if (Auth::user()->is_admin) {
            $organizers = User::get()->sortBy('displayname');
        } else {
            $organizers = Collection::make();
            foreach (Auth::user()->organizer_groups as $group) {
                $organizers = $organizers->merge($group->users);
            }
            $organizers = $organizers->unique('id')->sortBy('displayname');
        }

        return $organizers;
    }
}

Zerion Mini Shell 1.0