Completed
Push — Framework-Dependencies-Upgrade ( 998813...01c962 )
by Sheela
07:38
created

GroupController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.0013

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1.0013
1
<?php
2
3
namespace SET\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Event;
8
use Illuminate\Support\Facades\Input;
9
use SET\Events\TrainingAssigned;
10
use SET\Group;
11
use SET\Http\Requests\GroupRequest;
12
use SET\Training;
13
use SET\TrainingUser;
14
use SET\User;
15
16
/**
17
 * Class GroupController.
18
 */
19
class GroupController extends Controller
20
{
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26 1
    public function index()
27
    {
28 1
        $this->authorize('view');
29
30 1
        $groups = Group::with([
31 1
            'users' => function ($q) {
32
                $q->orderBy('last_name');
33 1
            },
34 1
            'trainings',
35 1
        ])->get()->sortBy('name');
36
37 1
        return view('group.index', compact('groups'));
38
    }
39
40 1
    public function create()
41
    {
42 1
        $this->authorize('edit');
43
44 1
        $training = Training::orderBy('name')->get()->pluck('name', 'id')->toArray();
45 1
        $users = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('UserFullName', 'id')->toArray();
46 1
        $selectedTraining = $selectedUsers = [];
47
48 1
        return view('group.create', compact('users', 'training', 'selectedTraining', 'selectedUsers'));
49
    }
50
51
    /**
52
     * @param GroupRequest $request
53
     *
54
     * @return \Illuminate\Http\RedirectResponse
55
     */
56 1
    public function store(GroupRequest $request)
57
    {
58 1
        $group = Group::create($request->all());
0 ignored issues
show
Bug introduced by
The method create() does not exist on SET\Group. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59 1
        $group->trainings()->attach($request->input('trainings'));
60 1
        $group->users()->attach($request->input('users'));
61
62 1
        $this->assignTraining($group, $request->input('users'));
63
64 1
        return redirect()->action('GroupController@index');
65
    }
66
67 1
    public function edit($groupId)
68
    {
69 1
        $this->authorize('edit');
70
71 1
        $group = Group::findOrFail($groupId);
72 1
        $training = Training::orderBy('name')->get()->pluck('name', 'id')->toArray();
73 1
        $users = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('UserFullName', 'id')->toArray();
74 1
        $selectedTraining = $group->trainings()->pluck('id')->toArray();
75 1
        $selectedUsers = $group->users()->pluck('id')->toArray();
76
77 1
        return view('group.edit', compact('group', 'users', 'training', 'selectedTraining', 'selectedUsers'));
78
    }
79
80
    /**
81
     * @param GroupRequest $request
82
     * @param $groupId
83
     *
84
     * @return \Illuminate\Http\RedirectResponse
85
     */
86 1
    public function update(GroupRequest $request, $groupId)
87
    {
88 1
        $group = Group::findOrFail($groupId);
89 1
        $group->update($request->all());
90
91
        //If we don't get anything back, sync as an empty array.
92 1
        $group->users()->sync($request->input('users') ?: []);
93 1
        $group->trainings()->sync($request->input('trainings') ?: []);
94
95 1
        $this->assignTraining($group, $request->input('users'));
96
97 1
        return redirect()->action('GroupController@index');
98
    }
99
100
    /**
101
     * Remove the specified resource from storage.
102
     *
103
     * @param int $groupId
104
     *
105
     * @return \Illuminate\Http\Response
106
     */
107 1
    public function destroy($groupId)
108
    {
109 1
        $this->authorize('edit');
110 1
        Group::find($groupId)->delete();
111 1
    }
112
113
    /**
114
     * Get the users associated with a group.
115
     * Used for ajax/js calls so we can populate a user select field based on group select field.
116
     *
117
     * @return mixed
118
     */
119 1
    public function getUserIDs()
120
    {
121 1
        $request = Input::all();
122 1
        if (empty($request['groups'])) {
123
            return 'nothing sent.';
124
        }
125
126 1
        return User::whereHas('groups', function ($query) use ($request) {
127 1
            $query->whereIn('id', $request['groups']);
128 1
        })->pluck('id');
129
    }
130
131
    /**
132
     * Cycle through each user & determine what training is missing.
133
     * Then assign said training to be due in 1 month.
134
     *
135
     * Called via store & update methods above.
136
     *
137
     * @param $group - Collection
138
     * @param $users - array
139
     */
140 3
    public function assignTraining($group, $users)
141
    {
142
        //If users is empty, then we don't need to do any more.
143 3
        if ($users === null) {
144 1
            return;
145
        }
146
147
        //We need to take the group and get all the training records so we can assign them to users.
148 3
        $groupList = $group->trainings()->get();
149
        $data = [
150 3
            'author_id' => Auth::user()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
151 3
            'type'      => 'training',
152 3
            'due_date'  => Carbon::now()->AddWeeks(2)->format('Y-m-d'),
153
        ];
154
155
        // Cycle through each user & figure what training they are missing.
156 3
        foreach ($users as $userID) {
157 3
            $data['user_id'] = $userID;
158 3
            $userList = User::find($userID)->trainings()->get();
159 3
            $trainingList = $groupList->diff($userList)->pluck('id');
160
161
            // Assign the missing training to the user & send them an email.
162 3
            foreach ($trainingList as $trainingId) {
163 3
                $data['training_id'] = $trainingId;
164 3
                $trainingUser = TrainingUser::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on SET\TrainingUser. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
165
166 3
                Event::fire(new TrainingAssigned($trainingUser));
167
            }
168
        }
169 3
    }
170
}
171