Passed
Push — master ( c7e265...e8c097 )
by Xavier
03:54
created

AdminController::studentProfile()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
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\User;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
17
/**
18
 * @Route("/tools")
19
 */
20
class AdminController extends AbstractController implements AdminAuthenticatedController
21
{
22
    /**
23
     * GET : Renders a form page to find a student
24
     * POST : Returns JSON data for a name given
25
     *
26
     * @Route("/find-student", name="find-student")
27
     * @param Request $request
28
     * @return \Symfony\Component\HttpFoundation\JsonResponse | \Symfony\Component\HttpFoundation\Response
29
     */
30
    public function findStudent(Request $request)
31
    {
32
        if ($request->isMethod('POST')) {
33
            $keyword = $request->get('name');
34
            $filter = '(&(businesscategory=E*)(displayname=*' . $keyword . '*))';
35
            $students = $this->ldap($filter, ['displayname', 'uid']);
36
            $res = [];
37
38
            foreach($students as $student) {
39
                if ($student['uid'][0] != null)
40
                    $res += [ $student['uid'][0] => $student['displayname'][0] ];
41
            }
42
43
            return $this->json($res);
44
        }
45
46
        return $this->render('Admin/find-student.twig');
47
    }
48
49
50
51
    /**
52
     * Renders the profile of a student with several data from OpenLRW API.
53
     *
54
     * @Route("/student-profile/{id}", name="student-profile")
55
     * @param Request $request
56
     * @param $id
57
     * @return \Symfony\Component\HttpFoundation\Response
58
     */
59
    public function studentProfile(Request $request, $id = '')
0 ignored issues
show
Unused Code introduced by
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

59
    public function studentProfile(/** @scrutinizer ignore-unused */ Request $request, $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...
60
    {
61
        $classes = [];
62
        $moodleId = User::moodleId($id);
63
64
        if ($moodleId === null) {
65
            $this->addFlash('error', "Student `$id` does not exist");
66
            return $this->redirectToRoute('home');
67
        }
68
69
        $enrollments = $this->http->get('users/' . User::moodleId($id) . '/enrollments', [
70
            'headers' => ['Authorization' => 'Bearer ' . $this->createJWT()->token]
71
        ]);
72
73
        foreach (json_decode($enrollments->getBody()->getContents()) as $enrollment) {
74
            if ($enrollment->class->title != null)
75
                array_push($classes, $enrollment->class);
76
        }
77
78
        return $this->render('admin/user.twig',[
79
            'classes' => $classes,
80
            'student_name' => $this->ldapFirst("uid=$id")['displayname'][0]
81
        ]);
82
83
    }
84
85
}
86
87