Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
76.60% covered (warning)
76.60%
36 / 47
BidController
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
10.04
76.60% covered (warning)
76.60%
36 / 47
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 show
0.00% covered (danger)
0.00%
0 / 1
6.76
72.41% covered (warning)
72.41%
21 / 29
 setWinner
0.00% covered (danger)
0.00%
0 / 1
2.03
81.25% covered (success)
81.25%
13 / 16
<?php
namespace App\Http\Controllers;
use App\Bid;
use App\Skill;
use App\User;
use App\Team;
use App\Faculty;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator;
use App\Http\Controllers\ProposalController;
class BidController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function show($id)
    {
        $bid = Bid::find($id);
        $bid->submissiondate = date('Y-m-d', strtotime($bid->submissiondate));
        $bid->team = Team::where('id', $bid->idteam)->get()->first();
        //members
        $facultys = [];
        $members = DB::select(
            'SELECT * from users,
            team_member WHERE team_member.idteam = ? and team_member.iduser = users.id',
            [$bid->idteam]
        );
        foreach ($members as $member) {
            $aux = Faculty::where('id', $member->idfaculty)->get()->first();
            array_push($facultys, $aux);
        }
        $bid->facultysMembers = $facultys;
        //leader
        $leader = User::where('id', $bid->team->idleader)->get()->first();
        $facultyNameLeader = Faculty::where('id', $leader->idfaculty)->get()->first();
        $bid->facultyLeader= $facultyNameLeader;
        if (($key = array_search($facultyNameLeader, $facultys)) !== false) {
            unset($facultys[$key]);
        }
        $bid->facultysMembers = $facultys;
        //skills
        $skills = [];
        foreach ($members as $member) {
            $skillsMember = DB::select('SELECT * from skill_user WHERE iduser = ? ', [$member->id]);
            foreach ($skillsMember as $skill) {
                $skillName =  Skill::where('id', $skill->idskill)->get()->first();
                array_push($skills, $skillName);
            }
        }
        $skillsTeam = [];
        foreach ($skills as $skill) {
            array_push($skillsTeam, $skill->skillname);
        }
        $skillsTeam = array_unique($skillsTeam);
        $bid->skills = $skillsTeam;
        return view('pages.bid', [ 'bid' => $bid]);
    }
    public function setWinner(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'bidid' => 'required|integer|exists:bid,id',
        ]);
        if ($validator->fails()) {
            return redirect()->back()
                             ->withErrors($validator)
                             ->withInput();
        }
        $bid = Bid::find($request->input('bidid'));
        $bid->winner = true;
        $bid->save();
        $proposal = $bid->proposal;
        $proposal->proposal_status = "finished";
        $proposal->save();
        ProposalController::notifyBidders($bid->proposal->id);
        ProposalController::notifyWinner($bid->proposal->id);
        ProposalController::notifyOwner($bid->proposal->id);
        return redirect()->back()->withInput();
    }
}