Issues (32)

src/Controller/UserController.php (6 issues)

1
<?php
2
3
/*
4
 * @author  Xavier Chopin <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace App\Controller;
11
12
use App\Model\API\Klass;
13
use App\Model\API\User;
14
use Exception;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Routing\Annotation\Route;
18
19
20
class UserController extends AbstractController implements AuthenticatedInterface
21
{
22
23
24
    /**
25
     * Renders the profile of a student with several data from OpenLRW API.
26
     *
27
     * @Route("/me", name="profile")
28
     * @param Request $request
29
     * @return \Symfony\Component\HttpFoundation\Response
30
     */
31
    public function profile(Request $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    public function profile(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        $user = User::find(self::loggedUser());
34
35
        if ($user === null) {
36
            $this->addFlash('error', "Student does not exist");
37
            return $this->redirectToRoute('home');
38
        }
39
40
        $events['all'] = User::eventsFrom(self::loggedUser(), date('Y-m-d H:i', strtotime("-1 week")));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$events was never initialized. Although not strictly required by PHP, it is generally a good practice to add $events = array(); before regardless.
Loading history...
41
42
        $events['cas'] = null;
43
        $events['moodle'] = null;
44
45
        if ($events['all'] != null) {
46
            usort($events['all'], function ($a, $b) {
47
                return $a->eventTime < $b->eventTime;
48
            });
49
50
            for ($i = 7; $i >= 0; $i--) {
51
                $cas_events[date('Y-m-d', strtotime("-$i day"))] = [];
52
                $moodle_events[date('Y-m-d', strtotime("-$i day"))] = [];
53
            }
54
55
            foreach ($events['all'] as $event) {
56
                $date = date('Y-m-d', strtotime($event->eventTime));
57
                if ($event->object->{'@type'} == 'SoftwareApplication') {
58
                    if (array_key_exists($date, $cas_events))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cas_events does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
                        array_push($cas_events[$date], $event);
60
                } else {
61
                    if (array_key_exists($date, $moodle_events))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $moodle_events does not seem to be defined for all execution paths leading up to this point.
Loading history...
62
                        array_push($moodle_events[$date], $event);
63
                }
64
            }
65
66
            $events['cas'] = $cas_events;
67
            $events['moodle'] = $moodle_events;
68
69
        }
70
71
72
        return $this->render('User/profile.twig', [
73
            'givenName' => $user->givenName,
74
            'events' => $events
75
        ]);
76
77
    }
78
79
80
    /**
81
     * Give enrollments for a user given.
82
     *
83
     * @Route("/me/enrollments", name="enrollments")
84
     * @param Request $request
85
     * @return \Symfony\Component\HttpFoundation\Response
86
     */
87
    public function enrollments(Request $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    public function enrollments(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        $id = $_SESSION['phpCAS']['user'];
90
        $classes = [];
91
92
        $enrollments = User::enrollments($id);
93
94
        if ($enrollments != null) {
95
            foreach ($enrollments as $enrollment) {
96
                $class = Klass::find($enrollment->class->sourcedId);
97
                isset($class->title) ? $enrollment->title = $class->title : $enrollment->title = 'null';
98
                array_push($classes, $enrollment);
99
            }
100
        } else {
101
            return new Response('Enrollments not found.', 404);
102
        }
103
104
        usort($classes, function($a, $b) { // ASC Sort
105
            return strtolower($a->title) > strtolower($b->title);
106
        });
107
108
109
        return $this->json($classes);
110
    }
111
112
113
    /**
114
     * Show events for a class and a user given.
115
     *
116
     * @Route("/classes/{id}", name="class")
117
     * @param Request $request
118
     * @param $id
119
     * @return \Symfony\Component\HttpFoundation\Response
120
     */
121
    public function class(Request $request, String $id = '')
122
    {
123
        $class = Klass::find($id);
124
125
        if ($class == null) {
126
            $this->addFlash('error', 'Class does not exist');
127
            return $this->redirectToRoute('home');
128
        }
129
130
        $events = Klass::eventsForUser($id, self::loggedUser());
131
132
        if ($events != null)
133
            usort($events, function($a, $b) {return $a->eventTime < $b->eventTime;});
134
135
        return $this->render('User/class.twig', [
136
            'class' => $class,
137
            'events' => $events
138
        ]);
139
140
    }
141
142
    /**
143
     * Return results for a class and a user given.
144
     *
145
     * @Route("/classes/{id}/results", name="class-results")
146
     * @param Request $request
147
     * @param $id
148
     * @return \Symfony\Component\HttpFoundation\Response
149
     */
150
    public function classResults(Request $request, String $id = '')
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

150
    public function classResults(/** @scrutinizer ignore-unused */ Request $request, String $id = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    {
152
        try {
153
            $results = Klass::resultsForUser($id, self::loggedUser());
154
            $lineItems = Klass::lineItems($id);
155
            $res = []; $i = 0;
156
            foreach ($results as $result) {
157
                $res[$i]['date'] = $result->date;
158
                $res[$i]['score'] = $result->score;
159
                foreach ($lineItems as $lineItem) {
160
                    if ($lineItem->sourcedId == $result->lineitem->sourcedId)
161
                        $res[$i]['title'] = $lineItem->title;
162
                } $i++;
163
            }
164
165
            return $this->json($res);
166
        }catch (Exception $e) {
167
            return new Response($e->getMessage(), 404);
168
        }
169
170
    }
171
172
}