Completed
Push — master ( 506c79...7daaad )
by Julito
12:09
created

WhispeakMyStudentsQuizTrackingHook::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class WhispeakMyStudentsQuizTrackingHook.
6
 */
7
class WhispeakMyStudentsQuizTrackingHook extends HookObserver implements HookMyStudentsQuizTrackingObserverInterface
8
{
9
    /**
10
     * WhispeakMyStudentsQuizTrackingHook constructor.
11
     */
12
    protected function __construct()
13
    {
14
        parent::__construct(
15
            'plugin/whispeakauth/WhispeakAuthPlugin.php',
16
            'whispeakauth'
17
        );
18
    }
19
20
    /**
21
     * Return an associative array this value and attributes.
22
     * <code>
23
     * [
24
     *     'value' => 'Users online',
25
     *     'attrs' => ['class' => 'text-center'],
26
     * ]
27
     * </code>.
28
     *
29
     * @return array
30
     */
31
    public function trackingHeader(HookMyStudentsQuizTrackingEventInterface $hook)
32
    {
33
        return [
34
            'value' => WhispeakAuthPlugin::create()->get_lang('plugin_title'),
35
            'attrs' => [
36
                'class' => 'text-center',
37
            ],
38
        ];
39
    }
40
41
    /**
42
     * Return an associative array this value and attributes.
43
     * <code>
44
     * [
45
     *     'value' => '5 connected users ',
46
     *     'attrs' => ['class' => 'text-center text-success'],
47
     * ]
48
     * </code>.
49
     *
50
     * @throws \Doctrine\ORM\Query\QueryException
51
     *
52
     * @return array
53
     */
54
    public function trackingContent(HookMyStudentsQuizTrackingEventInterface $hook)
55
    {
56
        $data = $hook->getEventData();
57
58
        $totalCount = WhispeakAuthPlugin::countAllAttemptsInQuiz($data['quiz_id'], $data['student_id']);
59
60
        if (0 === $totalCount) {
61
            return [
62
                'value' => '-',
63
                'attrs' => ['class' => 'text-center'],
64
            ];
65
        }
66
67
        $successCount = WhispeakAuthPlugin::countSuccessAttemptsInQuiz($data['quiz_id'], $data['student_id']);
68
69
        $attrs = ['class' => 'text-center '];
70
        $attrs['class'] .= $successCount <= $totalCount / 2 ? 'text-danger' : 'text-success';
71
72
        return [
73
            'value' => Display::tag('strong', "$successCount / $totalCount"),
74
            'attrs' => $attrs,
75
        ];
76
    }
77
}
78