Completed
Push — master ( 62d9c0...305eb4 )
by Seth
06:56 queued 04:58
created

Toolbox::getCourseSnapshot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace smtech\GradingAnalytics;
4
5
use smtech\LTI\Configuration\Option;
6
use Battis\HierarchicalSimpleCache;
7
8
class Toolbox extends \smtech\StMarksReflexiveCanvasLTI\Toolbox
9
{
10
    protected $courseHistory = [];
11
12
    const DEPT = 0;
13
    const SCHOOL = 1;
14
15
    protected $snapshots = [[], []];
16
17
    const AVERAGE_TURN_AROUND = 0;
18
    const AVERAGE_ASSIGNMENT_COUNT = 1;
19
20
    protected $numbers = [[], []];
21
22
    public function getGenerator()
23
    {
24
        parent::getGenerator();
25
26
        $this->generator->setOptionProperty(
27
            Option::COURSE_NAVIGATION(),
28
            'visibility',
29
            'admins'
30
        );
31
        $this->generator->setOptionProperty(
32
            Option::ACCOUNT_NAVIGATION(),
33
            'visibility',
34
            'admins'
35
        );
36
37
        return $this->generator;
38
    }
39
40
    private $GRAPH_DATA_COUNT = 0;
41
    public function graphWidth($dataCount = false)
42
    {
43
        if ($dataCount) {
44
            $this->GRAPH_DATA_COUNT = $dataCount;
0 ignored issues
show
Documentation Bug introduced by Seth Battis
The property $GRAPH_DATA_COUNT was declared of type integer, but $dataCount is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
45
        }
46
        return max(GRAPH_MIN_WIDTH, $this->GRAPH_DATA_COUNT * GRAPH_BAR_WIDTH);
47
    }
48
49
    public function graphHeight($dataCount = false)
50
    {
51
        if ($dataCount) {
52
            $this->GRAPH_DATA_COUNT = $dataCount;
0 ignored issues
show
Documentation Bug introduced by Seth Battis
The property $GRAPH_DATA_COUNT was declared of type integer, but $dataCount is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
53
        }
54
        return $this->graphWidth() * GRAPH_ASPECT_RATIO;
55
    }
56
57
    public function getMostCurrentCourseTimestamp($courseId)
58
    {
59
        return substr($this->getCourseHistory($courseId)[0]['timestamp'], 0, 10);
60
    }
61
62
    public function getDepartmentId($courseId)
63
    {
64
        return $this->getCourseHistory($courseId)[0]['course[account_id]'];
65
    }
66
67
    public function getCourseHistory($courseId)
68
    {
69
        if (empty($this->courseHistory)) {
70
            $cache = new HierarchicalSimpleCache($this->getMySql(), $this->config(self::TOOL_ID));
71
            $cache->pushKey('course');
72
            $this->courseHistory = $cache->getCache($courseId);
0 ignored issues
show
Documentation Bug introduced by Seth Battis
It seems like $cache->getCache($courseId) of type boolean is incompatible with the declared type array of property $courseHistory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
            if (empty($this->courseHistory)) {
74
                if ($response = $this->mysql_query("
75
                    SELECT * FROM `course_statistics`
76
                        WHERE
77
                            `course[id]` = '$courseId'
78
                        ORDER BY
79
                            `timestamp` DESC
80
                ")) {
81
                    while ($row = $response->fetch_assoc()) {
82
                        $this->courseHistory[] = $row;
83
                    }
84
                    $cache->setCache($courseId, $this->courseHistory);
85
                }
86
            }
87
        }
88
        return $this->courseHistory;
89
    }
90
91
    public function getCourseSnapshot($courseId)
92
    {
93
        $history = $this->getCourseHistory($courseId);
94
        if (!empty($history)) {
95
            return $history[0];
96
        }
97
        return false;
98
    }
99
100
    public function getDepartmentSnapshot($courseId)
101
    {
102
        return $this->getSnapshot($courseId, self::DEPT);
103
    }
104
105
    public function getSchoolSnapshot($courseId)
106
    {
107
        return $this->getSnapshot($courseId, self::SCHOOL);
108
    }
109
110
    public function getSnapshot($courseId, $domain = self::DEPT)
111
    {
112
        if (empty($this->snapshots[$domain])) {
113
            $cache = new HierarchicalSimpleCache($this->getMySql(), $this->config(self::TOOL_ID));
114
            $cache->pushKey('snapshot');
115
            if ($domain === self::DEPT) {
116
                $cache->pushKey('account');
117
            }
118
            $key = ($domain === self::DEPT ? $this->getDepartmentId($courseId) : 'school');
119
            $this->snapshots[$domain] = $cache->getCache($key);
120
            $this->numbers[$domain] = $cache->getCache("$key-numbers");
121
            if (empty($this->snapshots[$domain])) {
122
                if ($response = $this->mysql_query("
123
                    SELECT * FROM `course_statistics`
124
                        WHERE
125
                            " . ($domain === self::DEPT ? "`course[account_id]` = '" . $this->getDepartmentId($courseId) . "' AND" : '') . "
126
                            `timestamp` LIKE '" . $this->getMostCurrentCourseTimestamp($courseId) . "%'
127
                        GROUP BY
128
                            `course[id]`
129
                        ORDER BY
130
                            `timestamp` DESC
131
                ")) {
132
                    $totalTurnAround = 0;
133
                    $divisorTurnAround = 0;
134
                    $totalAssignmentCount = 0;
135
136
                    while ($row = $response->fetch_assoc()) {
137
                        $this->snapshots[$domain][] = $row;
138
139
                        /* average turn-around */
140
                        $totalTurnAround += $row['average_grading_turn_around'] * $row['student_count'] * $row['graded_assignment_count'];
141
                        $divisorTurnAround += $row['student_count'] * $row['graded_assignment_count'];
142
143
                        /* average assignment count */
144
                        $totalAssignmentCount += $row['assignments_due_count'] + $row['dateless_assignment_count'];
145
                    }
146
                    $this->numbers[$domain][self::AVERAGE_TURN_AROUND] = $totalTurnAround / $divisorTurnAround;
147
                    $this->numbers[$domain][self::AVERAGE_ASSIGNMENT_COUNT] = $totalAssignmentCount / $response->num_rows;
148
149
                    $cache->setCache($key, $this->snapshots[$domain]);
150
                    $cache->setCache("$key-numbers", $this->numbers[$domain]);
151
                }
152
            }
153
        }
154
        return $this->snapshots[$domain];
155
    }
156
157 View Code Duplication
    public function averageTurnAround($courseId, $domain = self::DEPT)
0 ignored issues
show
Duplication introduced by Seth Battis
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $this->getSnapshot($courseId, $domain);
160
        if (!empty($this->numbers[$domain][self::AVERAGE_TURN_AROUND])) {
161
            return $this->numbers[$domain][self::AVERAGE_TURN_AROUND];
162
        }
163
        return false;
164
    }
165
166 View Code Duplication
    public function averageAssignmentCount($courseId, $domain = self::DEPT)
0 ignored issues
show
Duplication introduced by Seth Battis
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        $this->getSnapshot($courseId, $domain);
169
        if (!empty($this->numbers[$domain][self::AVERAGE_ASSIGNMENT_COUNT])) {
170
            return $this->numbers[$domain][self::AVERAGE_ASSIGNMENT_COUNT];
171
        }
172
        return false;
173
    }
174
}
175